QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18857#1877. Matryoshka DollsgshRE 0ms0kbC++202.6kb2022-01-27 14:11:202022-05-06 02:56:17

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 02:56:17]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-01-27 14:11:20]
  • 提交

answer

#include<algorithm>
#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define ll long long
#define For(i,l,r) for(int i=l;i<=r;i++)
#define FOR(i,l,r) for(int i=l;i>=r;i--)
#define MAXN 500001
int N,M,B,p[MAXN],q[MAXN],d[MAXN];ll ans[MAXN];struct que{int l,r,id;}a[MAXN];
int get(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x*f;}
int solve1()
{
	For(i,2,N)d[i]=abs(q[i]-q[i-1]);For(i,2,N)d[i]+=d[i-1];
	For(i,1,M)
	{
		int l=get(),r=get();
		if(r-l<=60){int pre=-1,sum=0;For(i,max(l-20,1),min(r+20,N))if(q[i]>=l&&q[i]<=r){if(pre!=-1)sum+=abs(q[i]-pre);pre=q[i];}cout<<sum<<'\n';}
		else
		{
			int sum=0,pre=-1;
			For(i,max(l-20,1),l+20)if(q[i]>=l&&q[i]<=r){if(pre!=-1)sum+=abs(q[i]-pre);pre=q[i];}
			pre=-1;
			For(i,r-20,min(r+20,N))if(q[i]>=l&&q[i]<=r){if(pre!=-1)sum+=abs(q[i]-pre);pre=q[i];}
			cout<<sum+d[r-20]-d[l+20]<<'\n';
		}
	}
	return 0;
}
int main()
{
	freopen("rrads.in","r",stdin);freopen("rrads.out","w",stdout);
	N=get(),M=get();For(i,1,N)q[p[i]=get()]=i;bool flag=1;For(i,1,N)flag&=abs(i-p[i])<=10;if(flag)return solve1();
	For(i,1,M)a[i].l=get(),a[i].r=get(),a[i].id=i;
	B=N/sqrt(M);sort(a+1,a+M+1,[&](const que&a,const que&b){return a.l/B<b.l/B||a.l/B==b.l/B&&a.r<b.r;});
	int l=1,r=0;ll sum=0;set<int>s;
	For(i,1,M)
	{
		while(r<a[i].r)
		{
			r++;auto tmp=s.lower_bound(p[r]);
			if(tmp!=s.end()&&tmp!=s.begin()){int x=*tmp,y=*--tmp;sum-=abs(q[x]-q[y]);sum+=abs(q[x]-r)+abs(q[y]-r);}
			else if(tmp!=s.end())sum+=abs(q[*tmp]-r);else if(tmp!=s.begin())sum+=abs(q[*--tmp]-r);s.insert(p[r]);
		}
		while(l>a[i].l)
		{
			l--;auto tmp=s.lower_bound(p[l]);
			if(tmp!=s.end()&&tmp!=s.begin()){int x=*tmp,y=*--tmp;sum-=abs(q[x]-q[y]);sum+=abs(q[x]-l)+abs(q[y]-l);}
			else if(tmp!=s.end())sum+=abs(q[*tmp]-l);else if(tmp!=s.begin())sum+=abs(q[*--tmp]-l);s.insert(p[l]);
		}
		while(r>a[i].r)
		{
			s.erase(p[r]);auto tmp=s.lower_bound(p[r]);
			if(tmp!=s.end()&&tmp!=s.begin()){int x=*tmp,y=*--tmp;sum+=abs(q[x]-q[y]);sum-=abs(q[x]-r)+abs(q[y]-r);}
			else if(tmp!=s.end())sum-=abs(q[*tmp]-r);else if(tmp!=s.begin())sum-=abs(q[*--tmp]-r);r--;
		}
		while(l<a[i].l)
		{
			s.erase(p[l]);auto tmp=s.lower_bound(p[l]);
			if(tmp!=s.end()&&tmp!=s.begin()){int x=*tmp,y=*--tmp;sum+=abs(q[x]-q[y]);sum-=abs(q[x]-l)+abs(q[y]-l);}
			else if(tmp!=s.end())sum-=abs(q[*tmp]-l);else if(tmp!=s.begin())sum-=abs(q[*--tmp]-l);l++;
		}
		ans[a[i].id]=sum;
	}
	For(i,1,M)cout<<ans[i]<<'\n';return 0;
}

詳細信息

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: