QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19030 | #1877. Matryoshka Dolls | HSY666 | WA | 35ms | 32484kb | C++11 | 4.0kb | 2022-01-27 19:21:13 | 2022-05-06 03:37:18 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int N=5e5+5;
int n,m,a[N],id[N],ql,qr;
int block,cnt,nd=1;
int tl[N],tr[N],nl[N],nr[N];
int rt,tot,rnd[N],tre[N],son[N][2],siz[N];
int sta[N<<1],cl[N<<1],cr[N<<1];
long long ans,ret[N],res;
struct A
{
int Id,Ql,Qr;
bool operator < (const A &x) const
{
return (Qr/block==x.Qr/block) ? Ql<x.Ql : Qr<x.Qr;
}
}q[N];
int Abs (int x)
{
return x>=0 ? x : -x;
}
void upd (int x)
{
siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
}
int newd (int x)
{
++tot;
siz[tot]=1;
tre[tot]=x;
rnd[tot]=rand();
return tot;
}
int merge (int x,int y)
{
if (!x||!y) return x^y;
if (rnd[x]>=rnd[y])
{
son[x][1]=merge(son[x][1],y);
upd(x);
return x;
}
else
{
son[y][0]=merge(x,son[y][0]);
upd(y);
return y;
}
}
void split (int x,int qv,int &p1,int &p2)
{
if (!x)
{
p1=0;
p2=0;
return ;
}
if (tre[x]<=qv)
{
p1=x;
split(son[x][1],qv,son[p1][1],p2);
}
else
{
p2=x;
split(son[x][0],qv,p1,son[p2][0]);
}
upd(x);
}
int kth (int x,int y)
{
if (!x) return 0;
if (siz[son[x][0]]+1==y) return tre[x];
else if (siz[son[x][0]]>=y) return kth(son[x][0],y);
else return kth(son[x][1],y-siz[son[x][0]]-1);
}
void sol (int x)
{
int p1,p2,y,z;
for (int i=nd;i<=x;++i)
{
split(rt,a[i],p1,p2);
y=kth(p1,siz[p1]);
z=kth(p2,1);
if (!y&&!z)
{
rt=merge(merge(p1,newd(a[i])),p2);
continue;
}
if (!y)
{
ans+=Abs(i-id[z]);
tl[id[z]]=i;
tr[i]=id[z];
}
else if (!z)
{
ans+=Abs(i-id[y]);
tr[id[y]]=i;
tl[i]=id[y];
}
else
{
ans-=Abs(id[z]-id[y]);
ans+=Abs(id[z]-i)+Abs(id[y]-i);
tl[id[z]]=i;
tr[i]=id[z];
tr[id[y]]=i;
tl[i]=id[y];
}
rt=merge(merge(p1,newd(a[i])),p2);
// cout <<ans<<endl;
}
nd=x+1;
}
void dell (int x)
{
int y=nl[x],z=nr[x];
if (!y)
{
nl[z]=0;
res-=Abs(x-z);
}
else if (!z)
{
nr[y]=0;
res-=Abs(x-y);
}
else
{
nl[z]=y;
nr[y]=z;
res-=Abs(x-z)+Abs(x-y);
res+=Abs(y-z);
}
}
void delr (int x)
{
int y=nl[x],z=nr[x];
if (!y)
{
++cnt;
sta[cnt]=z;
cl[cnt]=nl[z];
cr[cnt]=nr[z];
nl[z]=0;
res-=Abs(x-z);
}
else if (!z)
{
++cnt;
sta[cnt]=y;
cl[cnt]=nl[y];
cr[cnt]=nr[y];
nr[y]=0;
res-=Abs(x-y);
}
else
{
++cnt;
sta[cnt]=y;
cl[cnt]=nl[y];
cr[cnt]=nr[y];
++cnt;
sta[cnt]=z;
cl[cnt]=nl[z];
cr[cnt]=nr[z];
nl[z]=y;
nr[y]=z;
res-=Abs(x-z)+Abs(x-y);
res+=Abs(y-z);
}
}
void solve1 ()
{
for (int i=1;i<=n;++i)
{
scanf ("%d",&a[i]);
id[a[i]]=i;
}
while (m--)
{
scanf ("%d%d",&ql,&qr);
int x=0;
ans=0;
for (int j=n;j>=1;--j)
{
if (id[j]>=ql&&id[j]<=qr)
{
if (x) ans+=Abs(x-id[j]);
x=id[j];
}
}
printf ("%lld\n",ans);
}
}
void solve2 ()
{
block=(int)sqrt(n);
for (int i=1;i<=n;++i)
{
scanf ("%d",&a[i]);
id[a[i]]=i;
}
for (int i=1;i<=m;++i)
{
scanf ("%d%d",&q[i].Ql,&q[i].Qr);
q[i].Id=i;
}
sort (q+1,q+m+1);
// for (int i=1;i<=m;++i)
// {
// cout <<q[i].Ql<<' '<<q[i].Qr<<endl;
// }
int L=1,R=block-1,X=block-1;
for (int i=1;i<=m;++i)
{
ql=q[i].Ql,qr=q[i].Qr;
// cout <<qr<<' '<<X<<endl;
if (qr/block!=X/block)
{
X=min(qr/block*block+block-1,n);
L=1;
R=X;
sol(R);
// cout <<ans<<endl;
for (int i=1;i<=R;++i)
{
nl[i]=tl[i];
nr[i]=tr[i];
}
res=ans;
}
while (L<ql) dell(L++);
long long x=res;
while (R>qr) delr(R--);
ret[q[i].Id]=res;
while (cnt)
{
int y=sta[cnt];
nl[y]=cl[cnt];
nr[y]=cr[cnt];
--cnt;
}
R=X;
res=x;
}
for (int i=1;i<=m;++i)
{
printf ("%lld\n",ret[i]);
}
}
int main ()
{
// freopen ("rrads.in","r",stdin);
// freopen ("rrads.out","w",stdout);
srand(time(0));
scanf ("%d%d",&n,&m);
// if (1LL*n*m<=300000000LL) solve1();
// else solve2();
solve2();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 30408kb
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: 20148kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 35ms
memory: 26220kb
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: 2ms
memory: 30364kb
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: 0
Accepted
time: 2ms
memory: 32440kb
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 9 20 3 8 22 3 0
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 3ms
memory: 32460kb
input:
20 50 7 3 13 8 9 18 1 15 12 10 20 11 17 16 2 19 5 4 6 14 3 6 5 15 11 20 10 18 9 20 3 17 13 20 16 17 3 20 4 17 15 18 5 19 3 14 8 20 2 20 1 4 15 19 16 20 5 13 14 17 4 10 6 16 8 16 1 12 4 9 11 15 4 20 8 11 3 16 7 7 3 11 3 7 4 4 5 12 6 20 3 14 6 19 6 19 2 14 2 12 5 6 4 6 8 15 10 19 4 14 1 16 1 20 2 13 3...
output:
6 48 36 24 46 74 17 1 104 64 5 68 44 58 130 5 9 8 30 7 13 48 26 38 11 8 92 5 70 0 28 9 0 20 80 44 58 58 48 36 1 2 20 28 34 76 136 46 1 28
result:
ok 50 numbers
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 32484kb
input:
20 100 4 13 20 8 1 18 19 9 17 5 12 7 11 16 6 3 15 10 14 2 4 19 1 6 6 19 4 6 2 17 1 5 11 13 1 15 9 17 9 15 7 20 3 19 10 19 1 10 11 17 10 17 2 18 17 20 12 19 1 3 5 17 7 13 6 18 10 20 1 6 2 17 5 9 4 13 11 13 2 20 7 13 12 17 5 19 17 20 11 19 3 15 3 5 5 11 1 17 10 15 16 20 1 6 2 19 4 12 5 18 9 17 3 6 12 ...
output:
76 16 57 3 84 10 3 74 31 19 59 80 40 44 16 26 94 5 26 0 54 17 53 44 16 84 8 32 3 106 17 12 68 5 30 48 2 16 102 14 9 16 98 28 64 31 6 1 54 20 26 31 74 5 26 3 66 32 36 59 1 26 6 33 35 5 57 1 1 57 24 6 10 68 36 41 34 0 12 8 11 2 62 12 41 10 5 25 0 60 0 44 25 12 8 2 16 36 8 1
result:
wrong answer 20th numbers differ - expected: '2', found: '0'