QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18825 | #1877. Matryoshka Dolls | houzhiyuan_2025 | WA | 135ms | 9580kb | C++ | 1.6kb | 2022-01-27 13:59:12 | 2022-05-06 02:45:58 |
Judging History
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],stalas[N],stanex[N];
struct que{int l,r,id;}q[N];
ll now,staans;
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]);
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7724kb
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: 0ms
memory: 7700kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 135ms
memory: 9580kb
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:
4999950000
result:
ok 1 number(s): "4999950000"
Test #4:
score: 0
Accepted
time: 1ms
memory: 7712kb
input:
20 1 12 8 13 10 18 14 1 19 5 16 15 9 17 20 6 2 11 4 3 7 9 18
output:
36
result:
ok 1 number(s): "36"
Test #5:
score: -100
Wrong Answer
time: 5ms
memory: 7728kb
input:
20 10 5 16 11 7 19 8 12 13 17 18 6 1 14 3 4 2 15 20 10 9 7 11 7 13 7 17 11 15 1 7 4 6 1 5 6 14 3 5 9 9
output:
0 0 0 -10 0 0 0 0 0 0
result:
wrong answer 1st numbers differ - expected: '7', found: '0'