ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#200042 | #2976. 序列变换 | snow_trace | 100 | 17953ms | 20048kb | C++ | 2.7kb | 2023-12-26 10:28:03 | 2023-12-26 12:03:09 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 800005;
int rnd[N],sz[N],add[N],ls[N],rs[N],mx[N],a[N],p[N],val[N];
int rt,rt1;
int n,m,nw;
int new_(int v){
rnd[++nw] = rand(),sz[nw] = 1,val[nw] = v;return nw;
}void push_up(int k){
sz[k] = sz[ls[k]]+sz[rs[k]]+1;
mx[k] = val[k];
if(ls[k])mx[k] = max(mx[k],mx[ls[k]]);
if(rs[k])mx[k] = max(mx[k],mx[rs[k]]);
}void add_(int k,int x){
add[k]+=x,val[k]+=x,mx[k]+=x;return;
}void push_down(int k){
if(add[k]!=0){
if(ls[k])add_(ls[k],add[k]);
if(rs[k])add_(rs[k],add[k]);
add[k] = 0;
}return;
}void split_(int now,int& x,int& y,int k){
if(!now){
x = y =0;return;
}push_down(now);
if(sz[ls[now]]<k){
x = now;split_(rs[now],rs[now],y,k-sz[ls[now]]-1);
}else{
y = now;split_(ls[now],x,ls[now],k);
}push_up(now);
}int merge(int x,int y){
if(!x or !y)return x|y;
push_down(x);push_down(y);
if(rnd[x]<rnd[y]){
rs[x] = merge(rs[x],y);push_up(x);return x;
}else{
ls[y] = merge(x,ls[y]);push_up(y);return y;
}
}void ins(int pos,int v){
int x,y;split_(rt,x,y,pos);rt = merge(merge(x,new_(v)),y);
}void ins1(int pos,int v){
int x,y;split_(rt1,x,y,pos);rt1 = merge(merge(x,new_(v)),y);
}int query(int pos){
int x,y,z;
split_(rt,x,y,pos-1);split_(y,y,z,1);
int res = val[y];rt = merge(merge(x,y),z);
return res;
}void Reverse(int l,int r){
int x,y,z;
split_(rt,x,y,l-1);split_(y,y,z,r-l+1);
int Mx = mx[y];
int L = Mx-(r-l),R = Mx;
// cout << " " << L << " " << R << endl;
//l->l
int X,Y,Z;
split_(rt1,X,Y,L-1);split_(Y,Y,Z,R-L+1);
add_(Y,L-l),add_(y,l-L);
rt = merge(merge(x,Y),z);
rt1 = merge(merge(X,y),Z);
}void Swap(int a,int b){
int x,y,z,w,u;
split_(rt,x,y,a-1);split_(y,y,z,1);
split_(z,z,w,b-a-1);split_(w,w,u,1);
int A = val[y],B = val[w];
if(A>B)swap(A,B);
int X,Y,Z,W,U;
split_(rt1,X,Y,A-1);split_(Y,Y,Z,1);
split_(Z,Z,W,B-A-1);split_(W,W,U,1);
rt1 = merge(merge(merge(X,merge(W,Z)),Y),U);
rt = merge(merge(merge(x,merge(w,z)),y),u);
}void dfs(int now){
push_down(now);
if(ls[now])dfs(ls[now]);
cout << val[now] << " ";
if(rs[now])dfs(rs[now]);
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i<=n;i++){
cin>> a[i];p[a[i]] = i;
}for(int i = 1;i<=n;i++)ins(i,a[i]);
for(int i = 1;i<=n;i++)ins1(i,p[i]);
//for(int i = 1;i<=n;i++)cout << query(i) << " ";cout << endl;
// dfs(rt);
while(m--){
int op;cin >> op;
if(op == 1){
int pos;cin >> pos;cout << query(pos) << '\n';
}if(op == 2){
int l,r;cin>> l >> r;Reverse(l,r);
}if(op == 3){
int x,y;cin >> x >> y;
if(x>y)swap(x,y);
if(x == y)continue;
Swap(x,y);
}
}dfs(rt);cout << '\n';
return 0;
}/*
6 2
4 5 3 2 1 6
2 1 4
3 5 6
*/
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 17
Accepted
Test #1:
score: 17
Accepted
time: 32ms
memory: 1916kb
input:
9999 9995 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...
output:
5823 7413 3790 5339 6749 967 3801 6442 4766 7744 7613 1234 1954 1038 7148 7159 2748 1377 692 47 1812...
result:
ok 13367 numbers
Subtask #2:
score: 10
Accepted
Test #2:
score: 10
Accepted
time: 2486ms
memory: 17696kb
input:
300000 299995 239661 110904 154800 19216 242069 272551 160498 29726 142264 98649 105160 195285 27530...
output:
80060 11792 44638 4334 264065 141792 184623 210648 206434 42207 216817 230277 268606 164773 7515 122...
result:
ok 449980 numbers
Subtask #3:
score: 10
Accepted
Test #3:
score: 10
Accepted
time: 1857ms
memory: 17704kb
input:
299998 299995 256068 115086 81766 125925 152536 229784 33059 288838 189461 22836 38427 168528 157297...
output:
50908 291728 37452 222642 166256 53319 100727 145545 6402 81505 22798 105155 152880 219952 298124 21...
result:
ok 399784 numbers
Test #4:
score: 0
Accepted
time: 2001ms
memory: 17704kb
input:
299996 299996 197147 105127 131641 68555 83324 89999 53808 296227 155259 285577 211656 264649 164656...
output:
161551 24739 10234 126 163130 46584 57127 271322 152157 196576 256316 31887 248462 143812 175764 240...
result:
ok 412232 numbers
Subtask #4:
score: 63
Accepted
Test #5:
score: 63
Accepted
time: 2287ms
memory: 20048kb
input:
299999 299999 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32...
output:
141391 268402 27138 255801 197326 178206 176402 80008 161045 129728 19386 227281 198227 226878 11900...
result:
ok 399939 numbers
Test #6:
score: 0
Accepted
time: 1907ms
memory: 20044kb
input:
299996 299998 2 5 1 3 8 6 10 11 9 13 7 15 4 12 14 16 20 17 22 19 24 18 23 26 21 25 29 28 32 27 33 30...
output:
170216 234444 88673 246298 287858 101893 143808 31499 36085 12449 154843 179506 279380 244837 294989...
result:
ok 400033 numbers
Test #7:
score: 0
Accepted
time: 1782ms
memory: 19952kb
input:
299995 299995 4 5 2 1 7 8 6 11 12 3 14 9 10 17 18 15 13 20 21 19 24 16 25 22 26 23 30 31 27 28 33 35...
output:
20144 249096 185099 152486 56785 239284 28121 102206 11913 76060 150979 156464 247395 51814 175217 2...
result:
ok 400242 numbers
Test #8:
score: 0
Accepted
time: 1896ms
memory: 17820kb
input:
300000 300000 1 5 3 6 4 8 9 2 7 11 12 10 13 16 14 18 20 15 21 19 24 25 22 23 17 26 27 30 29 31 33 32...
output:
13879 227773 78208 76853 276086 198248 158749 167995 292950 253167 40106 61125 173103 86728 73770 12...
result:
ok 400274 numbers
Test #9:
score: 0
Accepted
time: 1772ms
memory: 17840kb
input:
299997 299996 2 6 1 5 10 11 3 4 13 8 15 9 14 16 19 12 21 17 23 7 25 20 26 28 27 18 29 24 31 33 30 35...
output:
278438 198245 196946 227149 297001 19652 171800 85417 253128 280895 211268 71309 244920 166994 62337...
result:
ok 400607 numbers
Test #10:
score: 0
Accepted
time: 1933ms
memory: 20048kb
input:
299999 299999 1 299998 3 299996 299995 6 7 299992 9 10 11 12 13 299986 15 16 17 18 19 299980 299979 ...
output:
43548 152413 64660 206659 74543 143253 87098 251493 244397 170242 268626 168956 47127 21081 292061 2...
result:
ok 399705 numbers
Extra Test:
score: 0
Extra Test Passed