UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#200048#2976. 序列变换wosile10015453ms19876kbC++113.3kb2023-12-26 11:08:212023-12-26 12:04:23

answer

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
namespace IO{
	int read(){
		int x=0,c=getchar(),f=1;
		while(c<'0' || c>'9'){
			if(c=='-')f=-1;
			c=getchar();
		}
		while(c>='0' && c<='9'){
			x=(x<<3)+(x<<1)+c-'0';
			c=getchar();
		}
		return f==1?x:-x;
	}
	void write(int x){
		if(x<0){
			putchar('-');
			write(-x);
			return;
		}
		if(x>9)write(x/10);
		putchar(x%10+48);
	}
	void write(int x,char div){
		write(x);
		putchar(div);
	}
	void write(const char *s){
		int pos=0;
		while(s[pos])putchar(s[pos++]);
	}
}
using namespace IO;
int n,Q;
int val[600005];
int mn[600005],dat[600005],tag[600005];
int p[600005];
int ls[600005],rs[600005];
int sz[600005];
int rt=0;
int tot=0;
int newnode(int v){
	++tot;
	val[tot]=mn[tot]=v;
	dat[tot]=rand();
	tag[tot]=0;
	sz[tot]=1;
	return tot;
}
void pushup(int x){
	sz[x]=sz[ls[x]]+sz[rs[x]]+1;
	mn[x]=min(min(mn[ls[x]],mn[rs[x]]),val[x]);
}
void pushdown(int x){
	if(!tag[x])return;
	int tmp=tag[x];
	if(ls[x]){
		tag[ls[x]]+=tmp;
		val[ls[x]]+=tmp;
		mn[ls[x]]+=tmp;
	}
	if(rs[x]){
		tag[rs[x]]+=tmp;
		val[rs[x]]+=tmp;
		mn[rs[x]]+=tmp;
	}
	tag[x]=0;
}
void split(int x,int v,int &tx,int &ty){
	if(x==0){
		tx=ty=0;
		return;
	}
	pushdown(x);
	if(v>=sz[ls[x]]+1){
		tx=x;
		split(rs[x],v-sz[ls[x]]-1,rs[x],ty);
	}
	else{
		ty=x;
		split(ls[x],v,tx,ls[x]);
	}
	pushup(x);
}
int merge(int x,int y){
	if(x==0 || y==0)return x|y;
	if(dat[x]<dat[y]){
		pushdown(x);
		rs[x]=merge(rs[x],y);
		pushup(x);
		return x;
	}
	else{
		pushdown(y);
		ls[y]=merge(x,ls[y]);
		pushup(y);
		return y;
	}
}
int query(int x,int k){
	pushdown(x);
	if(k==sz[ls[x]]+1)return val[x];
	if(k<=sz[ls[x]])return query(ls[x],k);
	return query(rs[x],k-sz[ls[x]]-1);
}
void sswap(int x,int y){
	if(x>y)swap(x,y);
	int t1,t2,t3,t4,t5;
	split(rt,y,t4,t5);
	split(t4,y-1,t3,t4);
	split(t3,x,t2,t3);
	split(t2,x-1,t1,t2);
	rt=merge(merge(merge(merge(t1,t4),t3),t2),t5);
}
void output(int x){
	if(!x)return;
	pushdown(x);
	output(ls[x]);
	write(val[x],32);
	output(rs[x]);
}
int main(){
	val[0]=mn[0]=inf;
	n=read();Q=read();
	for(int i=1;i<=n;i++)p[i]=read();
	for(int i=1;i<=n;i++)p[p[i]+n]=i;
	for(int i=1;i<=n+n;i++)rt=merge(rt,newnode(p[i]));
	// output(rt);
	// printf("\n");
	while(Q--){
		int op=read();
		int x=read();
		// printf("***\n");
		// printf("%d %d\n",op,x);
		if(op==1)write(query(rt,x),10);
		if(op==2){
			int y=read();
			int t1,t2,t3,t4,t5;
			split(rt,y,t1,t2);
			split(t1,x-1,t3,t1);
			int l=mn[t1];
			int r=l+y-x;
			split(t2,r+n-y,t2,t5);
			split(t2,l-1+n-y,t2,t4);
			// printf("t1:\n");
			// output(t1);
			// printf("\nt2:\n");
			// output(t2);
			// printf("\nt3:\n");
			// output(t3);
			// printf("\nt4:\n");
			// output(t4);
			// printf("\nt5:\n");
			// output(t5);
			// printf("\n");
			int d=mn[t4]-mn[t1];
			mn[t1]+=d;
			val[t1]+=d;
			tag[t1]+=d;
			mn[t4]-=d;
			val[t4]-=d;
			tag[t4]-=d;
			rt=merge(merge(merge(merge(t3,t4),t2),t1),t5);
		}
		if(op==3){
			int y=read();
			// printf("SWAP %d %d\n",x,y);
			int tx=query(rt,x),ty=query(rt,y);
			// printf("tx=%d,ty=%d\n",tx,ty);
			sswap(x,y);
			sswap(tx+n,ty+n);
		}
		// output(rt);
		// putchar(10);
	}
	int tl,tr;
	split(rt,n,tl,tr);
	output(tl);
	return 0;
}
// quod erat demonstrandum

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 17
Accepted

Test #1:

score: 17
Accepted
time: 25ms
memory: 1792kb

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: 2137ms
memory: 19872kb

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: 1806ms
memory: 19872kb

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: 1584ms
memory: 19872kb

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: 1848ms
memory: 19876kb

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: 1645ms
memory: 19872kb

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: 1437ms
memory: 19876kb

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: 1596ms
memory: 19872kb

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: 1520ms
memory: 19872kb

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: 1855ms
memory: 19876kb

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