QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18961#1877. Matryoshka Dollsmomo_liRE 0ms0kbC++4.5kb2022-01-27 17:34:412022-05-06 03:24:52

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-06 03:24:52]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-01-27 17:34:41]
  • 提交

answer

#include<bits/stdc++.h>
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
// #define int long long
#define mod (998244353)
#ifndef ONLINE_JUDGE
#pragma GCC optimize(2)
#endif
using namespace std;
namespace IO{
	inline int read(){
		int n=0;
		int s=1;
		char x;
		while((x=getchar())<'0'||x>'9')
			if(x=='-')
				s=-1;
		while(x>='0'&&x<='9'){
			n=(n<<1)+(n<<3)+(x^48),
			x=getchar();
		}
		return n*s;
	}
	void write(char x){
		putchar(x);
	}
	void write(const char *x){
		for(;*x;++x)
			putchar(*x);
	}
	void write(char *x){
		for(;*x;++x)
			putchar(*x);
	}
	void write(signed x){
		if(x<0)putchar('-'),x=-x;
		if(x>9)write(x/10);
		putchar('0'+x-x/10*10);
	}
	void write(long long x){
		if(x<0)putchar('-'),x=-x;
		if(x>9)write(x/10);
		putchar('0'+x-x/10*10);
	}
	void write(double x){
		printf("%lf",x);
	}
	template<typename type1,typename type2,typename ...typen>
	void write(type1 a1,type2 a2,typen ...an){
		write(a1);
		write(a2,an...);
	}
}// namespace IO
inline long long max(long long a,long long b){return a>b?a:b;}
inline long long min(long long a,long long b){return a>b?b:a;}
// stop
struct List{
	int a,l,r;
}notes[500001];
unsigned int tot;
unsigned int L[500001],R[500001];
unsigned int changed[500001],cnt;
bool qwq[500001];
unsigned int col[500001],cl[500001],cr[500001],len;
void init(int n){
	int m=n/len;
	for(int i=1;i<=n;i++)col[i]=(i-1)/len+1;
	for(int i=1;i<=m;i++)cl[i]=len*(i-1)+1,cr[i]=len*i;
	if(m*len<n)m++,cl[m]=len*(m-1)+1,cr[m]=n;
}
unsigned int pos[500001];
struct Node{
	int l,r,id;
	bool operator<(Node x){
		if(col[l]==col[x.l])return r>x.r;
		return col[l]<col[x.l];
	}
}ques[500001];
unsigned long long ans[500001];
unsigned long long nowans;
unsigned int x[500001];
unsigned int p[500001];
inline int abs(int a){return a>=0?a:-a;}
void init2(int y,int n){
	tot=0;
	memset(notes,0,sizeof(notes));
	memset(x,0,sizeof(x));
	nowans=0;
	for(int i=1;i<=n;i++)
		if(pos[i]>=y){
			// cout<<pos[i]<<" ";
			tot++;
			x[pos[i]]=tot;
			notes[tot].l=tot-1,notes[tot].a=pos[i],L[tot]=tot-1;
			if(tot-1)notes[tot-1].r=tot,nowans+=abs(notes[tot].a-notes[tot-1].a),R[tot-1]=tot;
		}
	// cout<<"\n";
}
signed main(){
	using namespace IO;
	// no define int long long
	freopen("rrads.in","r",stdin);
	freopen("rrads.out","w",stdout);
	int n=read(),m=read();
	len=n/sqrt(m)*2;
	init(n);
	for(int i=1;i<=n;i++)p[i]=read(),pos[p[i]]=i;
	for(int i=1;i<=m;i++)ques[i].l=read(),ques[i].r=read(),ques[i].id=i;
	sort(ques+1,ques+m+1);
	int l,r;
	for(int i=1;i<=m;i++){
		if(i==1||col[ques[i].l]!=col[ques[i-1].l])
			init2(cl[col[ques[i].l]],n),l=cl[col[ques[i].l]],r=n;
		// if(col[ques[i].l]==col[ques[i].r]){
		// 	continue;
		// }
		while(r>ques[i].r){
			int po=x[r];
			int lson=notes[po].l,rson=notes[po].r;
			if(lson){
				R[lson]=rson;
				nowans-=abs(notes[po].a-notes[lson].a);
				notes[lson].r=rson;
			}
			if(rson){
				L[rson]=lson;
				nowans-=abs(notes[po].a-notes[rson].a);
				notes[rson].l=lson;
			}
			if(lson&&rson)nowans+=abs(notes[lson].a-notes[rson].a);
			r--;
			// cout<<nowans<<"\n";
		}
		// cout<<'\n';
		long long poans=nowans;
		while(l<ques[i].l){
			int po=x[l];
			int lson=notes[po].l,rson=notes[po].r;
			// cout<<po<<"\n";
			if(lson){
				if(!qwq[lson])qwq[lson]=1,changed[++cnt]=lson;
				poans-=abs(notes[po].a-notes[lson].a);
				notes[lson].r=rson;
			}
			if(rson){
				if(!qwq[rson])qwq[rson]=1,changed[++cnt]=rson;
				poans-=abs(notes[po].a-notes[rson].a);
				notes[rson].l=lson;
			}
			if(lson&&rson)poans+=abs(notes[lson].a-notes[rson].a);
			l++;
		}
		ans[ques[i].id]=poans;
		l=cl[col[ques[i].l]];
		for(int j=1;j<=cnt;j++)
			notes[changed[j]].l=L[changed[j]],notes[changed[j]].r=R[changed[j]],qwq[changed[j]]=0;
		cnt=0;
	}
	for(int i=1;i<=m;i++)write((long long)ans[i],'\n');
	return 0;
}
/*
  ----  ----
   ----  ----
    ----  ----
    ----  ----
                       ----
               ----
         ----
    ----
                        ----
                 ----
          ----
   ----
                        ----
                 ----
            ----
      ----
                          ----
                ----
          ----
    ----
                          ----
                  ----
           ----
     ----

                     ----   ----

                     ----   ----

    ----   ----

    ----   ----

----------------------------------------
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:


result: