QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561129 | #1877. Matryoshka Dolls | World_Creater | WA | 13ms | 11760kb | C++17 | 2.2kb | 2024-09-12 20:30:24 | 2024-09-12 20:30:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,q,a[100005];
int bel[100005],bell[100005],belr[100005],p[100005],cnt,B;
typedef long long ll;
int lst[100005],nxt[100005],tl[100005],tr[100005];
ll ans[100005],sum;
int pp[100005],idx;
struct query{
int l,r,id;
bool operator <(const query &b) const
{
return bel[l]!=bel[b.l]?bel[l]<bel[b.l]:r>b.r;
}
}qu[500005];
int aabs(int x)
{
return x>0?x:-x;
}
struct node{
int x,y,mid;
ll dt;
}sta[100005];
int top;
void del(int x,int op)
{
ll dt=0;
int llst=lst[x],rrst=nxt[x];
if(llst&&rrst) dt+=aabs(p[rrst]-p[llst]);
if(llst) dt-=aabs(p[x]-p[llst]),lst[rrst]=llst;
if(rrst) dt-=aabs(p[x]-p[rrst]),nxt[llst]=rrst;
if(op) sta[++top]={llst,rrst,x,dt};
sum+=dt;
}
ll solve(int l,int r)
{
ll sum=0,lst=0;
for(int i=1;i<=idx;i++)
{
if(l<=pp[i]&&pp[i]<=r)
{
if(lst) sum+=aabs(pp[i]-lst);
lst=pp[i];
}
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
B=n/sqrt(q);
if(B==0) B++;
for(int i=1;i<=n;i++)
{
cin>>a[i];
p[a[i]]=i;
bel[i]=(i+B-1)/B;
if(bel[i]!=cnt) cnt++,bell[cnt]=i;
belr[cnt]=i;
}
for(int i=1;i<=q;i++)
{
cin>>qu[i].l>>qu[i].r;
qu[i].id=i;
}
sort(qu+1,qu+1+q);
iota(lst+1,lst+1+n,0);
iota(nxt+1,nxt+1+n,2);
for(int i=2;i<=n;i++)
{
sum+=aabs(p[i]-p[i-1]);
}
nxt[n]=0;
for(int i=1,j=1;i<=cnt;i++)
{
memcpy(tl,lst,sizeof(tl));
memcpy(tr,nxt,sizeof(nxt));
ll summ=sum;
int L=bell[i],R=belr[i];
idx=0;
for(int i=L;i<=R;i++)
{
pp[++idx]=i;
}
sort(pp+1,pp+1+idx,[&](int x,int y){
return a[x]<a[y];
});
int l=bell[i],r=n;
for(;bel[qu[j].l]==i;j++)
{
if(bel[qu[j].r]==i) ans[qu[j].id]=solve(qu[j].l,qu[j].r);
while(r>qu[j].r) del(a[r--],0);
while(l<qu[j].l) del(a[l++],1);
ans[qu[j].id]=sum;
while(top)
{
auto [x,y,mid,dt]=sta[top];
top--;
if(x) nxt[x]=mid;
if(y) lst[y]=mid;
sum-=dt;
}
l=bell[i];
}
sum=summ;
memcpy(lst,tl,sizeof(tl));
memcpy(nxt,tr,sizeof(nxt));
for(int k=bell[i];k<=belr[i];k++) del(a[k],0);
}
for(int i=1;i<=q;i++)
{
cout<<ans[i]<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9776kb
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: 2ms
memory: 9728kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 13ms
memory: 9368kb
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: 7704kb
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: 2ms
memory: 11760kb
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:
7 16 34 6 20 0 8 17 0 3
result:
wrong answer 4th numbers differ - expected: '9', found: '6'