QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18817#1877. Matryoshka Dollshouzhiyuan_2025WA 163ms21312kbC++1.6kb2022-01-27 13:57:202022-05-06 02:44: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 02:44:51]
  • 评测
  • 测评结果:WA
  • 用时:163ms
  • 内存:21312kb
  • [2022-01-27 13:57:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+5;
int nex[N],las[N],rk[N],a[N],n,m,id[N],staans,stalas[N],stanex[N];
struct que{int l,r,id;}q[N];
ll now;
int solve(int x,int y,int z){return abs(x-z)-abs(y-z)-abs(x-y);}
void del(int x){
	if(las[x]==0) now-=abs(rk[nex[x]]-rk[x]);
	else if(nex[x]==n+1) now-=abs(rk[las[x]]-rk[x]);
	else now+=solve(rk[las[x]],rk[x],rk[nex[x]]);
	nex[las[x]]=nex[x];
	las[nex[x]]=las[x];
}
ll work(int l,int r){
	ll ans=0;
	for(int i=l;i<=r;i++){
		if(las[a[i]]==0) now-=abs(rk[nex[a[i]]]-rk[a[i]]);
		else if(nex[a[i]]==n+1) now-=abs(rk[las[a[i]]]-rk[a[i]]);
		else now+=solve(rk[las[a[i]]],rk[a[i]],rk[nex[a[i]]]);
		nex[las[a[i]]]=nex[a[i]];
		las[nex[a[i]]]=las[a[i]];
	}
	for(int i=r;i>=l;i--){
		nex[las[a[i]]]=a[i];
		las[nex[a[i]]]=a[i];
	}
	return ans+now;
}
ll ans[N];
int l[N];
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),rk[a[i]]=i;
	int S=sqrt(n);
	for(int i=1;i<=n;i++)id[i]=(i-1)/S+1,l[id[i]+1]=i+1;
	l[1]=1;
	for(int i=1;i<=m;i++)scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	sort(q+1,q+m+1,[](que a,que b){return id[a.l]==id[b.l]?a.l<b.l:a.r>b.r;});
	for(int i=1;i<=n;i++)stalas[i]=i-1,stanex[i]=i+1;
	for(int i=1;i<n;i++)staans+=abs(rk[i+1]-rk[i]);
	int x=1;
	for(int Id=1;Id<=id[n];Id++){
		now=staans;
		memcpy(las,stalas,sizeof(las));
		memcpy(nex,stanex,sizeof(nex));
		for(int j=1;j<l[Id];j++)del(a[j]);
		int R=n;
		for(;id[q[x].l]==Id;x++){
			while(R>q[x].r)del(a[R--]);
			ans[q[x].id]=work(l[Id],q[x].l-1);
		}
	}
	for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 21312kb

input:

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

output:

7
5
3
1
0

result:

ok 5 number(s): "7 5 3 1 0"

Test #2:

score: 0
Accepted
time: 1ms
memory: 20012kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 163ms
memory: 20124kb

input:

100000 1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...

output:

704982704

result:

wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'