QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641764 | #21403. 文艺平衡树 | Zhou_JK | AC ✓ | 204ms | 6100kb | C++23 | 3.1kb | 2024-10-14 23:08:36 | 2024-10-14 23:08:36 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<random>
#include<chrono>
using namespace std;
constexpr int N=100005;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
struct FHQ_Treap
{
struct Node
{
int ch[2];
int val;
unsigned int rd;
int siz;
int tag;
Node()
{
ch[0]=ch[1]=0;
val=0;
rd=0;
siz=0,tag=0;
return;
}
void clear()
{
*this=Node();
return;
}
}tree[N];
int rt,tot;
FHQ_Treap():rt(0),tot(0){}
void clear()
{
rt=tot=0;
return;
}
int new_node(int val)
{
int now=++tot;
tree[now].clear();
tree[now].val=val,tree[now].rd=rnd();
tree[now].siz=1;
return now;
}
void reverse(int u)
{
if(!u) return;
tree[u].tag^=1;
swap(tree[u].ch[0],tree[u].ch[1]);
return;
}
void push_down(int u)
{
if(!tree[u].tag) return;
reverse(tree[u].ch[0]);
reverse(tree[u].ch[1]);
tree[u].tag=0;
return;
}
void push_up(int u)
{
tree[u].siz=tree[tree[u].ch[0]].siz+tree[tree[u].ch[1]].siz+1;
return;
}
int merge(int u,int v)
{
if(!u) return v;
if(!v) return u;
push_down(u);
push_down(v);
if(tree[u].rd<tree[v].rd)
{
tree[u].ch[1]=merge(tree[u].ch[1],v);
push_up(u);
return u;
}
else
{
tree[v].ch[0]=merge(u,tree[v].ch[0]);
push_up(v);
return v;
}
}
void split_size(int now,int k,int &x,int &y)
{
if(!now)
{
x=y=0;
return;
}
push_down(now);
if(k>tree[tree[now].ch[0]].siz)
{
x=now;
split_size(tree[now].ch[1],k-tree[tree[now].ch[0]].siz-1,tree[x].ch[1],y);
push_up(x);
}
else
{
y=now;
split_size(tree[now].ch[0],k,x,tree[y].ch[0]);
push_up(y);
}
return;
}
void insert(int val)
{
int x,y;
split_size(rt,val,x,y);
rt=merge(merge(x,new_node(val)),y);
return;
}
void reverse(int l,int r)
{
int x,y,z;
split_size(rt,r,x,z);
split_size(x,l-1,x,y);
reverse(y);
rt=merge(merge(x,y),z);
return;
}
int query(int u)
{
int x,y,z;
split_size(rt,u,x,z);
split_size(x,u-1,x,y);
int res=tree[y].val;
rt=merge(merge(x,y),z);
return res;
}
}T;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
T.insert(i);
for(int i=1;i<=m;i++)
{
int l,r;
cin>>l>>r;
T.reverse(l,r);
}
for(int i=1;i<=n;i++)
cout<<T.query(i)<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5928kb
input:
30 5 7 26 7 18 5 9 4 15 3 15
output:
1 2 4 17 16 15 6 5 18 19 20 21 22 23 3 24 25 26 14 13 12 11 10 9 8 7 27 28 29 30
result:
ok single line: '1 2 4 17 16 15 6 5 18 19 20 21... 13 12 11 10 9 8 7 27 28 29 30 '
Test #2:
score: 0
Accepted
time: 1ms
memory: 5992kb
input:
100 10 9 29 10 68 7 72 15 73 57 79 4 39 11 69 45 61 1 42 15 21
output:
5 4 47 46 45 44 43 42 41 40 39 38 37 36 78 79 31 32 33 34 35 77 76 75 74 24 23 22 21 20 19 18 54 53 52 51 50 49 48 3 2 1 6 72 63 64 65 66 67 68 29 8 7 73 25 26 27 28 69 70 71 62 61 60 59 58 57 56 55 17 16 15 14 13 12 11 10 9 30 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
result:
ok single line: '5 4 47 46 45 44 43 42 41 40 39...91 92 93 94 95 96 97 98 99 100 '
Test #3:
score: 0
Accepted
time: 0ms
memory: 5972kb
input:
200 50 49 166 53 181 17 136 38 105 39 120 17 132 24 148 51 178 35 166 37 128 22 145 33 158 43 150 41 49 59 130 59 188 57 124 25 113 15 136 55 178 47 162 45 175 101 159 51 168 11 90 45 124 61 172 15 134 37 148 3 86 57 130 81 129 59 159 31 128 8 119 1 53 1 128 9 88 35 160 41 200 40 127 47 165 39 112 1...
output:
105 104 72 71 14 13 12 11 162 182 183 184 185 186 97 48 33 118 10 9 8 7 6 17 18 19 20 117 173 172 159 45 89 90 73 74 75 76 77 78 79 187 58 119 120 121 64 65 178 200 199 198 197 196 195 194 193 192 191 190 189 135 134 133 132 131 47 46 88 87 86 85 116 174 175 176 114 144 41 42 43 31 30 29 28 130 96 9...
result:
ok single line: '105 104 72 71 14 13 12 11 162 ...32 166 165 164 163 181 180 179 '
Test #4:
score: 0
Accepted
time: 0ms
memory: 6020kb
input:
200 50 57 184 29 105 1 100 4 76 9 104 19 87 25 118 37 136 39 166 55 170 133 169 23 154 25 102 47 136 17 37 43 122 31 116 12 113 35 146 29 143 53 140 39 160 5 104 157 193 5 57 55 65 11 136 55 122 53 138 15 84 49 95 3 127 56 149 35 104 71 159 23 96 37 120 6 95 28 107 63 181 3 30 57 105 27 96 29 154 12...
output:
34 35 107 108 45 177 41 70 18 87 88 89 92 168 144 158 159 123 124 125 126 127 171 170 64 65 66 67 149 148 147 146 145 154 6 134 105 23 141 122 121 120 119 118 117 116 115 46 47 48 49 50 51 52 53 10 9 8 7 153 152 151 150 97 98 99 100 194 143 157 156 33 167 166 165 164 163 162 161 160 142 79 80 81 82 ...
result:
ok single line: '34 35 107 108 45 177 41 70 18 ... 77 78 195 196 197 198 199 200 '
Test #5:
score: 0
Accepted
time: 2ms
memory: 5996kb
input:
10000 3000 2766 8161 802 5794 623 6126 2879 8058 2011 8169 2017 9893 237 4761 3032 7817 838 4475 400 4961 241 1478 1957 6725 2245 5631 1519 5260 2189 5951 1585 7664 3098 8044 1809 6484 3127 8674 3126 6755 873 5942 105 5541 1685 5556 78 4452 3255 8289 537 5681 3 6305 4404 9793 548 5182 1000 5611 1821...
output:
8694 1445 8229 8230 8231 2838 2837 2836 2835 7220 7221 7222 7223 584 285 2924 515 739 740 741 742 743 9340 6210 6211 6212 8914 8913 8912 1994 5690 5691 5692 5693 5435 4580 4581 4582 4583 4584 4585 4586 4587 2028 4038 4037 4036 4181 9954 9955 3287 6066 6653 6652 6651 5031 5030 5029 5028 6582 6581 585...
result:
ok single line: '8694 1445 8229 8230 8231 2838 ...9995 9996 9997 9998 9999 10000 '
Test #6:
score: 0
Accepted
time: 89ms
memory: 5972kb
input:
50000 50000 5351 22924 23569 31837 10321 34305 24821 31124 7255 36650 9801 30062 27465 36441 6799 39256 12861 36908 19999 49816 38177 49281 4887 27918 13810 33483 8219 31074 1231 32512 12811 30950 31705 38376 12517 36898 9193 41418 6640 28721 3319 35161 14859 35985 8787 32188 81 18433 2859 28749 472...
output:
48014 26930 36546 1261 19039 41093 41094 9017 30509 14540 42177 3620 48562 34139 49910 28964 29701 9016 17610 49590 36985 17667 35860 18397 49199 39712 15509 24957 4982 453 454 455 9061 20463 38246 49225 29795 18827 18828 5060 13133 33917 30100 39575 20799 48246 48245 21475 38732 21765 29762 23049 5...
result:
ok single line: '48014 26930 36546 1261 19039 4...614 8615 15358 2597 2598 50000 '
Test #7:
score: 0
Accepted
time: 131ms
memory: 6016kb
input:
80000 70000 5639 33189 58689 76401 22323 63782 9379 60566 24044 65425 26023 71449 7993 61134 19089 64394 11624 49545 54470 72393 15679 50852 12239 42684 18293 57292 6893 44628 19100 61605 6111 38496 7615 60504 4047 38342 8735 60458 11602 61623 4837 51608 56131 66851 44401 75157 14135 42318 15677 510...
output:
60666 41986 49896 20550 5704 5705 5706 10915 35225 23036 23037 67036 67037 36041 11205 31611 76382 21038 29429 70674 70673 35560 36629 36628 34587 51651 21187 29365 22356 27638 68982 68983 68984 33197 53012 13558 21168 79338 79339 79340 71899 5753 54124 59519 15893 2101 61797 77780 14668 60916 44254...
result:
ok single line: '60666 41986 49896 20550 5704 5... 52749 78001 61574 79999 80000 '
Test #8:
score: 0
Accepted
time: 202ms
memory: 6100kb
input:
100000 100000 26201 76499 2133 49270 16969 54134 20129 81465 30309 95715 16450 75674 31087 95302 25186 63359 28653 85166 33110 72970 2773 55409 10286 68395 53815 86337 304 61183 27663 88134 17939 66827 28395 88990 17457 98881 958 55658 17527 57075 19575 69265 4019 47133 48549 59998 16148 54577 12091...
output:
48382 82046 57881 9235 76677 33462 84995 83053 8060 82151 82152 63534 63535 48529 61112 7695 22575 31032 52215 39360 6022 33089 3260 42656 58062 79509 83713 19526 18576 21796 28655 28654 84107 8663 68055 24698 24697 29016 29017 52336 23602 4974 32300 89077 82078 65138 71972 59397 56986 69328 26439 5...
result:
ok single line: '48382 82046 57881 9235 76677 3...98311 26991 26992 58731 100000 '
Test #9:
score: 0
Accepted
time: 198ms
memory: 6000kb
input:
100000 100000 89003 90209 46083 96417 23507 88692 24354 68519 14791 58309 937 56827 8485 74848 25853 79517 5262 70656 4118 54651 980 52207 25265 70914 31713 90674 545 40408 29188 77333 10967 69263 15841 57641 5223 46419 23905 70366 383 59463 28161 94766 26005 68202 15775 59971 32578 92749 24170 6505...
output:
44538 59025 59600 24875 87550 32120 32121 96880 35553 23105 46839 71441 65830 87344 67098 25193 10833 34147 35461 56271 88328 52956 39722 52742 71452 63088 63089 46633 46632 46631 33087 32623 32622 32621 90948 10909 87520 4356 25063 61766 89391 99065 34630 28669 94972 99221 31212 35311 89976 34897 6...
result:
ok single line: '44538 59025 59600 24875 87550 ...45224 75512 99998 99999 100000 '
Test #10:
score: 0
Accepted
time: 204ms
memory: 5908kb
input:
100000 100000 4087 54523 35889 83701 29237 63069 13324 54160 66297 71875 2063 82484 24510 85797 17796 40115 6656 67026 30611 64488 75085 82245 4003 58692 62333 88273 4706 42110 18680 73012 9242 67838 2497 35928 27556 78883 27622 85762 33271 94733 27115 79171 5601 52585 10950 74087 22029 81150 90337 ...
output:
99318 20129 52948 52947 4068 47045 82589 82588 57020 57021 65919 32841 88592 3956 9346 72561 54657 37672 99925 48134 93660 88805 88806 70239 43192 84763 57016 57015 57014 2249 70962 68269 99523 99522 99521 92142 92143 51961 47914 92784 34096 34097 40316 30416 87491 87492 30115 72921 3506 3507 29634 ...
result:
ok single line: '99318 20129 52948 52947 4068 4...22295 22294 22293 99999 100000 '