QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19102#1877. Matryoshka DollsrealmikemchRE 0ms0kbC++112.7kb2022-01-28 08:52:392022-05-06 04:11:51

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 04:11:51]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-01-28 08:52:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const long long MAXN=5e5+5,MAXM=5e5+5;

long long n,m;
long long a[MAXN],pos[MAXN]; //pos[i] means the position of number i, a[pos[i]]=i

struct Query{
	long long l,r;
	long long blockID,id;
}q[MAXM];

void fread(long long& vari){
	char ch=getchar();
	while(ch<'0' || ch>'9')
		ch=getchar();
	while(ch>='0' && ch<='9'){
		vari=((vari<<3)+(vari<<1))+(ch^48);
		ch=getchar();
	}
}

void input(){
	long long i,j;
	fread(n); fread(m);
	for(i=1;i<=n;i++)
		fread(a[i]);
	for(i=1;i<=m;i++){
		q[i].l=0; q[i].r=0;
		fread(q[i].l);
		fread(q[i].r);
	}
}

void init(){
	long long i,j;
	for(i=1;i<=n;i++)
		pos[a[i]]=i;
	for(i=1;i<=m;i++)
		q[i].id=i;
}

bool exist[MAXN]={0};

void solveBF(){
	long long i,j;
	for(i=1;i<=m;i++){
		long long ans=0;
		memset(exist,0,sizeof(exist));
		for(j=q[i].l;j<=q[i].r;j++)
			exist[a[j]]=1;
		for(j=1;j<=n-1;j++)
			if(exist[j] && exist[j+1]){
				long long tmp=pos[j]-pos[j+1];
				ans+=(tmp>=0 ? tmp : -tmp);
			}
		printf("%lld\n",ans);
	}
}

bool f(Query a,Query b){
	return a.blockID<b.blockID || (
		a.blockID==b.blockID && (
			((a.blockID)&1) ? a.r<b.r : a.r>b.r
		)
	);
}

long long rans[MAXM];

void solve(){
	long long i,j;
	long long bn=n/sqrt(2*m);
	bn=max(1ll,bn);
	for(i=1;i<=m;i++)
		q[i].blockID=(q[i].l)/bn+1;
	sort(q+1,q+m+1,f);
	long long l=1,r=1,ans=0;
	for(i=1;i<=m;i++){
		while(r<q[i].r+1){
			exist[a[r]]=1;
			ans=ans+exist[a[r]-1]*(
				pos[a[r]]-pos[a[r]-1]>=0 
				? pos[a[r]]-pos[a[r]-1] 
				: pos[a[r]-1]-pos[a[r]]
			)+exist[a[r]+1]*(
				pos[a[r]]-pos[a[r]+1]>=0
				? pos[a[r]]-pos[a[r]+1]
				: pos[a[r]+1]-pos[a[r]]
			);
			r++;
		}
		while(l>q[i].l){
			l--;
			exist[a[l]]=1;
			ans=ans+exist[a[l]-1]*(
				pos[a[l]]-pos[a[l]-1]>=0 
				? pos[a[l]]-pos[a[l]-1] 
				: pos[a[l]-1]-pos[a[l]]
			)+exist[a[l]+1]*(
				pos[a[l]]-pos[a[l]+1]>=0
				? pos[a[l]]-pos[a[l]+1]
				: pos[a[l]+1]-pos[a[l]]
			);
		}
		while(r>q[i].r+1){
			r--;
			exist[a[r]]=0;
			ans=ans-exist[a[r]-1]*(
				pos[a[r]]-pos[a[r]-1]>=0 
				? pos[a[r]]-pos[a[r]-1] 
				: pos[a[r]-1]-pos[a[r]]
			)-exist[a[r]+1]*(
				pos[a[r]]-pos[a[r]+1]>=0
				? pos[a[r]]-pos[a[r]+1]
				: pos[a[r]+1]-pos[a[r]]
			);
		}
		while(l<q[i].l){
			exist[a[l]]=0;
			ans=ans-exist[a[l]-1]*(
				pos[a[l]]-pos[a[l]-1]>=0 
				? pos[a[l]]-pos[a[l]-1] 
				: pos[a[l]-1]-pos[a[l]]
			)-exist[a[l]+1]*(
				pos[a[l]]-pos[a[l]+1]>=0
				? pos[a[l]]-pos[a[l]+1]
				: pos[a[l]+1]-pos[a[l]]
			);
			l++;
		}
		rans[q[i].id]=ans;
	}
	for(i=1;i<=m;i++)
		printf("%lld\n",rans[i]);
}

int main(){
	long long i,j;
	freopen("rrads.in","r",stdin);
	freopen("rrads.out","w",stdout);
	input();
	init();
	if(n<=1000 && m<=1000)
		solveBF();
	else
		solve();
	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: