QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19154#1877. Matryoshka DollsPeanut_TangWA 4ms9360kbC++141.6kb2022-01-28 11:53:242022-05-06 04:20:34

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:20:34]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:9360kb
  • [2022-01-28 11:53:24]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<cassert>
#include<ctime>
#include<vector>
#include<random>
std::mt19937 rnd(time(0));
inline int ib(int x){return x>0?x:-x;}
typedef long long lli;
const int N=5e5,B=707;

int n,q,pe[N+5],po[N+5];

lli ans[N+5];

struct QR{
	int w,l,r;
	QR(){}
	QR(int _w,int _l,int _r){w=_w,l=_l,r=_r;}
};
std::vector<QR>qr[B+5];

lli rs;
int pr[N+5],nx[N+5];
bool us[N+5];
inline void init(int l,int r){
	rs=0;
	std::fill(pr,pr+n+2,0),std::fill(nx,nx+n+2,0),std::fill(us,us+n+2,0);
	us[0]=us[n+1]=1;for(int i=l;i<=r;++i)us[pe[i]]=1;
	for(int i=1,u=0;i<=n+1;++i)if(us[i]){
		if(u&&i<=n)rs+=ib(po[i]-po[u]);
		pr[i]=u,nx[u]=i;
		u=i;
	}
}
inline void del(int w){
	int x=pe[w],y=pr[x],z=nx[x],u=po[y],v=po[z];
	if(y)rs-=ib(w-u);
	if(z<=n)rs-=ib(v-w);
	nx[y]=z,pr[z]=y;
	if(y&&z<=n)rs+=ib(v-u);
}
inline void rev(int w){
	int x=pe[w],y=pr[x],z=nx[x],u=po[y],v=po[z];
	if(y&&z<=n)rs-=ib(v-u);
	nx[y]=x,pr[z]=x;
	if(y)rs+=ib(w-u);
	if(z<=n)rs+=ib(v-w);
}

int main(){
//	freopen("rrads.in","r",stdin);
//	freopen("rrads.out","w",stdout);
	
	scanf("%d%d",&n,&q);
	for (int i=1; i<=n; i++) scanf("%d",pr+i);
	for(int i=1,l,r;i<=q;++i){
        scanf("%d%d",&l,&r);
        qr[(l-1)/B+1].push_back(QR(i,l,r));
	}

	for(int i=1,x;(x=(i-1)*B+1)<=n;++i)if(qr[i].size()){
		std::sort(qr[i].begin(),qr[i].end(),[](const QR&a,const QR&b){return a.r>b.r;});
		init(x,n);
		int nl=x,nr=n;
		for(auto nw:qr[i]){
			for(;nr>nw.r;)del(nr),--nr;
			for(;nl<nw.l;)del(nl),++nl;
			ans[nw.w]=rs;
			for(;nl>x;)--nl,rev(nl);
		}
	}
	
	for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 9360kb

input:

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

output:

0
0
0
0
0

result:

wrong answer 1st numbers differ - expected: '7', found: '0'