QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18795#1877. Matryoshka DollsYunQianCompile Error//C++141.8kb2022-01-27 13:46:352022-05-18 04:05:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-18 04:05:14]
  • 评测
  • [2022-01-27 13:46:35]
  • 提交

answer

#pragma GCC optimize(2)

#include<bits/stdc++.h>

#define int long long

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

const int MN=5e5+5;
int n,m,bl;
int pos[MN],a[MN],res[MN],wz[MN];

struct Node{
	int l,r,id;
}q[MN];

bool cmp(Node x,Node y){
	if(pos[x.l]^pos[y.l])return (pos[x.l]<pos[y.l]);
	else return (pos[x.l)&1)?(pos[x.r]<pos[y.r]):(pos[x.r]>pos[y.r]);
}

int now=0;
set<int>S;

#define Sit set<int>::iterator

void add(int x){
	if(S.empty()){S.insert(x);return ;}
	Sit pr=S.lower_bound(x);
	if(pr==S.end()){pr--;now+=abs(wz[x]-wz[*pr]);S.insert(x);return ;}
	if(pr==S.begin()){now+=abs(wz[x]-wz[*pr]);S.insert(x);return ;}
	Sit su=pr;pr--;
	now-=abs(wz[*su]-wz[*pr]),now+=abs(wz[x]-wz[*su])+abs(wz[x]-wz[*pr]);
	S.insert(x);
}

void del(int x){
	assert(!S.empty());
	Sit it=S.find(x);assert(it!=S.end());
	Sit itl=it,itr=it;itr++;
	if(it!=S.begin())itl--,now-=abs(wz[x]-wz[*itl]);
	if(itr!=S.end())now-=abs(wz[x]-wz[*itr]);
	if(it!=S.begin()&&itr!=S.end())now+=abs(wz[*itl]-wz[*itr]);
	S.erase(it);
}

signed main(void){

	n=read(),m=read();bl=150;
	for(int i=1;i<=n;i++)a[i]=read(),wz[a[i]]=i;
	for(int i=1;i<=n;i++)pos[i]=(int)((i-1)/bl+1);
	for(int i=1;i<=m;i++)q[i].l=read(),q[i].r=read(),q[i].id=i;
	sort(q+1,q+m+1,cmp);
	int l=1,r=0;
	for(int i=1;i<=m;i++){
		int ql=q[i].l,qr=q[i].r;
		if(r<ql||l>qr){
			for(int i=l;i<=r;i++)del(a[i]);l=ql,r=qr;
			for(int i=l;i<=r;i++)add(a[i]);
		}
		else{
			while(l<ql)del(a[l++]);while(l>ql)add(a[--l]);
			while(r<qr)add(a[++r]);while(r>qr)del(a[r--]);
		}
		res[q[i].id]=now;
	}
	for(int i=1;i<=m;i++)cout<<res[i],putchar('\n');

	return 0;
}

Details

answer.code: In function ‘bool cmp(Node, Node)’:
answer.code:26:29: error: expected ‘]’ before ‘)’ token
   26 |         else return (pos[x.l)&1)?(pos[x.r]<pos[y.r]):(pos[x.r]>pos[y.r]);
      |                             ^
      |                             ]
answer.code:26:32: error: expected ‘;’ before ‘)’ token
   26 |         else return (pos[x.l)&1)?(pos[x.r]<pos[y.r]):(pos[x.r]>pos[y.r]);
      |                                ^
      |                                ;
answer.code:26:32: error: expected primary-expression before ‘)’ token