QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18956#1877. Matryoshka DollschenyikaiCompile Error//C2.3kb2022-01-27 17:33:252022-05-18 04:05:27

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:27]
  • 评测
  • [2022-01-27 17:33:25]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef set<int>::iterator si;
const int N=500013;
int n,m,a[N],q1[N],q2[N],b[N],c[N],p[N],ord[N],ans[N],sq;
bool cmp(const int&A,const int&B){return p[A]<p[B]||p[A]==p[B]&&q2[A]<q2[B];}
set<int> s;
signed main(){
	//freopen("rrads.in","r",stdin);
	//freopen("rrads.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",a+i),b[a[i]]=i;
	for(int i=1;i<=m;i++)scanf("%d%d",q1+i,q2+i);
	if(n>2){
		for(int i=2;i<=n;i++)c[i]=c[i-1]+abs(b[i]-b[i-1]);
		for(int i=1;i<=m;i++){
			int res=c[q2[i]+10]-c[q1[i]-10],k;
			for(int j=q1[i]-10;j<min(q1[i]+10,q2[i]-10);j=k){
				k=j+1;
				while(b[k]<q1[i]||b[k]>q2[i])k++;
				if(j==q1[i]-10&&(b[j]<q1[i]||b[j]>q2[i]))res-=c[k]-c[j];
				else res-=c[k]-c[j]-abs(b[k]-b[j]);
			}
			int K=k;
			for(int j=q2[i]+10;j>K&&j>=q2[i]-10;j=k){
				k=j-1;
				while(b[k]<q1[i]||b[k]>q2[i])k--;
				if(j==q2[i]+10&&(b[j]<q1[i]||b[j]>q2[i]))res-=c[j]-c[k];
				else res-=c[j]-c[k]-abs(b[k]-b[j]);
			}
			printf("%d\n",res);
		}
		return 0;
	}
	sq=floor(sqrt(n));
	for(int i=1;i<=m;i++)p[i]=q1[i]/sq,ord[i]=i;
	sort(ord+1,ord+m+1,cmp);
	int l=1,r=1,res=0;
	s.insert(a[1]);
	for(int i=1;i<=m;i++){
		while(r<q2[ord[i]]){
			r++;
			s.insert(a[r]);
			si it=s.find(a[r]);
			si it1=it,it2=it;
			it1++,it2--;
			if(it!=s.begin())res+=abs(b[*it2]-r);
			if(it1!=s.end())res+=abs(b[*it1]-r);
			if(it!=s.begin()&&it1!=s.end())res-=abs(b[*it1]-b[*it2]);
		}
		while(l>q1[ord[i]]){
			l--;
			s.insert(a[l]);
			si it=s.find(a[l]);
			si it1=it,it2=it;
			it1++,it2--;
			if(it!=s.begin())res+=abs(b[*it2]-l);
			if(it1!=s.end())res+=abs(b[*it1]-l);
			if(it!=s.begin()&&it1!=s.end())res-=abs(b[*it1]-b[*it2]);
		}
		while(r>q2[ord[i]]){
			si it=s.find(a[r]);
			si it1=it,it2=it;
			it1++,it2--;
			if(it!=s.begin())res-=abs(b[*it2]-r);
			if(it1!=s.end())res-=abs(b[*it1]-r);
			if(it!=s.begin()&&it1!=s.end())res+=abs(b[*it1]-b[*it2]);
			r--;
			s.erase(it);
		}
		while(l<q1[ord[i]]){
			si it=s.find(a[l]);
			si it1=it,it2=it;
			it1++,it2--;
			if(it!=s.begin())res-=abs(b[*it2]-l);
			if(it1!=s.end())res-=abs(b[*it1]-l);
			if(it!=s.begin()&&it1!=s.end())res+=abs(b[*it1]-b[*it2]);
			l++;
			s.erase(it);
		}
		ans[ord[i]]=res;
	}
	for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
	return 0;
}

Details

answer.code:1:9: fatal error: bits/stdc++.h: No such file or directory
 #include<bits/stdc++.h>
         ^~~~~~~~~~~~~~~
compilation terminated.