QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#562355 | #8188. Halve & Merge | Afterlife# | TL | 2ms | 5792kb | C++20 | 2.3kb | 2024-09-13 17:01:41 | 2024-09-13 17:01:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1e3+7;
int n,m;
int tr[N][2],mag[N],sz[N],val[N];
int rt,tot,mx[N];
void clear()
{
rt=tot=0;
}
int newnode(int v)
{
int x=++tot;
sz[x]=1;
tr[x][0]=tr[x][1]=0;
val[x]=v;
mx[x]=val[x];
mag[x]=rand();
return x;
}
void update(int x)
{
sz[x]=sz[tr[x][0]]+sz[tr[x][1]]+1;
mx[x]=max({mx[tr[x][0]],mx[tr[x][1]],val[x]});
}
int merge(int x,int y)
{
if(!x||!y)
return x|y;
if(mag[x]>mag[y])
{
tr[x][1]=merge(tr[x][1],y);
update(x);
return x;
}
else
{
tr[y][0]=merge(x,tr[y][0]);
update(y);
return y;
}
}
void split(int x,int K,int &l,int &r)
{
if(!x)
{
l=r=0;
return;
}
if(K<=sz[tr[x][0]])
{
split(tr[x][0],K,l,r);
tr[x][0]=r;
update(r=x);
}
else
{
split(tr[x][1],K-sz[tr[x][0]]-1,l,r);
tr[x][1]=l,update(l=x);
}
}
int qry(int x,int v)
{
if(!x)
return 0;
if(mx[tr[x][0]]>v)
return qry(tr[x][0],v);
else if(val[x]>v)
return sz[tr[x][0]];
else
return sz[tr[x][0]]+1+qry(tr[x][1],v);
}
int getv(int x,int k)
{
if(k<=sz[tr[x][0]])
return getv(tr[x][0],k);
else if(k==sz[tr[x][0]]+1)
return val[x];
else
return getv(tr[x][1],k-sz[tr[x][0]]-1);
}
int getl(int x)
{
if(!tr[x][0])
return val[x];
return getl(tr[x][0]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
int w;
cin>>w;
int x=newnode(w);
rt=merge(rt,x);
}
while(m--)
{
int op,x;
cin>>op>>x;
if(op==1)
cout<<getv(rt,x)<<"\n";
else
{
int L,R;
split(rt,x,L,R);
while(R)
{
int w=getl(R);
int c=qry(R,w);
int t;
split(R,c,t,R);
c=qry(L,w);
int z;
split(L,c,L,z);
L=merge(L,t);
L=merge(L,z);
}
rt=L;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5696kb
input:
4 3 4 3 2 1 2 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5636kb
input:
5 7 4 3 5 2 1 2 4 2 1 1 3 1 1 2 4 1 4 1 5
output:
3 1 3 5
result:
ok 4 number(s): "3 1 3 5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
2 2 2 1 2 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5700kb
input:
25 25 21 20 19 12 11 17 22 16 24 1 13 7 14 9 2 5 8 3 4 23 15 25 18 10 6 2 4 2 11 1 10 1 22 1 8 2 1 1 11 2 10 2 1 1 6 1 11 2 2 1 22 1 21 1 4 1 1 1 12 2 23 2 19 2 4 2 11 1 6 1 19 1 7 1 16
output:
17 25 3 21 5 21 25 13 9 7 20 9 16 2 19
result:
ok 15 numbers
Test #5:
score: 0
Accepted
time: 1ms
memory: 5644kb
input:
50 50 40 46 9 20 17 44 43 11 14 15 25 38 48 36 19 35 23 6 16 30 5 42 47 8 3 2 27 41 31 24 50 4 49 34 37 33 26 32 39 1 29 12 18 10 45 7 13 21 28 22 1 10 2 5 1 18 1 50 1 32 2 29 2 33 1 11 1 31 2 23 1 35 1 3 1 47 2 9 2 13 1 15 2 16 1 46 1 40 2 16 1 22 1 35 2 8 1 16 2 25 1 19 1 12 1 8 2 49 1 17 2 25 1 1...
output:
15 6 22 4 18 48 23 28 31 12 41 42 25 23 40 43 37 33 40 39 37 1 47 11 36 34 17
result:
ok 27 numbers
Test #6:
score: 0
Accepted
time: 1ms
memory: 5732kb
input:
75 75 7 29 18 60 15 31 34 53 43 63 38 54 9 5 50 28 74 68 55 21 67 35 19 8 26 40 47 75 10 56 44 25 52 39 58 3 30 62 64 16 42 1 73 49 45 4 13 66 69 37 32 61 20 57 11 41 65 51 22 24 27 36 12 17 70 48 6 72 14 46 33 59 71 2 23 1 48 1 71 2 43 1 31 1 11 1 4 2 44 2 71 2 21 1 27 1 15 2 6 1 69 1 48 2 57 1 17 ...
output:
66 33 51 34 49 37 60 52 72 13 71 33 12 50 43 14 69 71 47 57 17 6 33 15 3 46 63 9 66 7 54 70 41 53 28 27 49 5 40 19 27 14 42 7 24 15 54
result:
ok 47 numbers
Test #7:
score: 0
Accepted
time: 1ms
memory: 5636kb
input:
100 100 34 41 2 16 70 22 13 43 95 74 97 88 3 29 78 77 83 50 79 58 93 17 30 19 21 45 44 99 64 69 75 46 27 42 91 37 84 71 92 8 11 24 15 23 66 81 18 7 53 59 12 20 5 32 54 76 39 52 73 89 25 4 49 26 56 36 38 35 87 31 72 96 1 10 68 94 86 90 65 60 40 33 6 82 47 55 62 28 100 98 61 80 51 67 85 63 14 57 48 9 ...
output:
31 42 87 5 34 74 58 32 50 59 28 43 1 46 23 18 4 48 90 35 4 2 98 20 99 32 91 43 22 23 81 66 46 30 22 31 22 21 72 45 95 11 26 6 4 42 53 11 35 45 85 64 2 70 43
result:
ok 55 numbers
Test #8:
score: 0
Accepted
time: 2ms
memory: 5720kb
input:
5000 5000 4273 4131 1283 3216 1735 1507 4834 576 1708 3554 2183 3309 2559 2685 937 4262 3644 459 2106 4310 2101 3240 1764 2671 2532 1241 2443 4283 1578 314 2828 3513 4542 3656 4399 673 1317 4916 2367 110 4406 3410 3661 453 1221 4473 2191 503 2649 3838 4904 1239 120 2027 4443 2546 1012 4360 49 1267 5...
output:
479 3346 4797 3082 3200 1792 2157 383 1090 3223 1162 264 81 154 4643 3418 3526 3445 3142 1105 28 1981 1008 1829 1036 2218 4022 748 1683 1866 1304 1998 4303 4286 3368 3215 1408 3510 1783 3617 2063 4227 3641 4511 465 2018 4018 375 1591 3507 3019 1270 1051 2354 2727 3659 3550 1660 2677 4095 1881 1908 1...
result:
ok 5000 numbers
Test #9:
score: 0
Accepted
time: 2ms
memory: 5748kb
input:
5000 5000 2529 3862 4114 4780 4184 2787 3081 1575 4740 2944 4813 2626 901 4649 518 4782 1180 2166 3693 4678 4795 4194 2308 1442 3087 4467 4016 283 3664 3017 4756 218 3796 888 3715 4251 4698 329 4371 2533 755 1549 4658 3154 3670 1228 2337 1598 1406 3468 2302 2352 4204 1471 3133 2072 4484 767 2152 419...
output:
251 28 4495 4896 1141 4312 1376 4247 3934 1980 1218 423 3810 1573 3229 4363 3820 99 3706 2898 1179 1907 1016 3341 970 3254 3945 782 1557 1350 4618 4651 4503 2924 2366 1646 343 839 1336 3035 1653 3478 681 1140 3403 3182 2598 2953 4960 1717 4244 1824 1578 3194 2413 159 4437 3983 1747 1477 3584 2692 19...
result:
ok 5000 numbers
Test #10:
score: 0
Accepted
time: 2ms
memory: 5792kb
input:
5000 5000 1849 4447 3474 1988 2793 3570 3480 3416 4492 2781 3060 2257 1324 3186 3438 2923 4160 3193 3960 642 2555 4712 3421 4019 4597 4759 1480 2855 601 4782 1993 1253 4471 3737 1936 109 1838 4436 3760 2547 3204 596 87 3370 4704 780 480 1580 3018 3783 2016 1513 1609 2101 4264 919 3386 1843 1486 4024...
output:
3547 2510 3389 1761 2170 536 4664 3173 3428 4495 2321 3913 4849 771 3521 3717 1921 90 4377 3792 3982 1984 1331 895 398 958 744 4336 1571 2905 283 1247 1217 397 2718 3323 2169 3927 2097 2632 3116 3612 2794 1950 4761 4919 3750 1213 125 2343 2993 829 1090 1713 2612 4439 3825 1147 102 1490 1826 1084 144...
result:
ok 5000 numbers
Test #11:
score: -100
Time Limit Exceeded
input:
200000 200000 179452 184566 70496 25045 45839 40048 152780 4407 43919 144186 96868 104482 96217 61207 142227 165786 26564 9980 22512 90283 87997 159485 151301 130070 192792 194726 169304 28217 69023 192199 108110 53151 69453 55703 102598 39287 85763 193001 104872 74888 53143 159465 199469 145888 374...
output:
93207 9021 71900 82003 153245 113222 25525 32537 148606 165234 125924 45534 88410 188086 54063 29701 49503 66055 140254 130909 183032 64616 98512 114336 197868 33906 187784 150293 61702 100105 19633 108843 194306 43885 187790 188925 199564 195593 2753 73806 100166 59078 36608 92116 25731 161812 6620...