UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#197139#2833. 双端队列wosile100300ms5888kbC++111.7kb2023-11-07 10:27:532023-11-07 12:10:10

answer

#include<bits/stdc++.h>
using namespace std;
int tr[500005][2];
int rt[200005],pri[500005],val[500005],rev[500005];
int n,q;
int tot;
int newnode(int v){
	tot++;
	tr[tot][0]=tr[tot][1]=0;
	pri[tot]=rand();
	val[tot]=v;
	rev[tot]=0;
	return tot;
}
void pushdown(int x){
	if(!rev[x])return;
	if(tr[x][0]){
		rev[tr[x][0]]^=1;
		swap(tr[tr[x][0]][0],tr[tr[x][0]][1]);
	}
	if(tr[x][1]){
		rev[tr[x][1]]^=1;
		swap(tr[tr[x][1]][0],tr[tr[x][1]][1]);
	}
	rev[x]=0;
}
int merge(int x,int y){
	if(x==0 || y==0)return x+y;
	if(pri[x]<pri[y]){
		pushdown(x);
		tr[x][1]=merge(tr[x][1],y);
		return x;
	}
	else{
		pushdown(y);
		tr[y][0]=merge(x,tr[y][0]);
		return y;
	}
}
void output(int x){
	if(!x)return;
	pushdown(x);
	output(tr[x][0]);
	printf("%d ",val[x]);
	output(tr[x][1]);
}
int main(){
	srand(0114507537);
	while(~scanf("%d%d",&n,&q)){
		for(int i=1;i<=n;i++)rt[i]=0;
		tot=0;
		while(q--){
			int op,u,v,w;
			scanf("%d",&op);
			if(op==1){
				scanf("%d%d%d",&u,&w,&v);
				if(w==0)rt[u]=merge(newnode(v),rt[u]);
				else rt[u]=merge(rt[u],newnode(v));
			}
			if(op==2){
				scanf("%d%d",&u,&w);
				if(rt[u]==0){
					printf("-1\n");
					continue;
				}
				int tmp=rt[u],lst=0;
				pushdown(tmp);
				while(tr[tmp][w]){
					lst=tmp;
					tmp=tr[tmp][w];
					pushdown(tmp);
				}
				if(lst)tr[lst][w]=merge(tr[tmp][0],tr[tmp][1]);
				else rt[u]=merge(tr[tmp][0],tr[tmp][1]);
				printf("%d\n",val[tmp]);
			}
			if(op==3){
				scanf("%d%d%d",&u,&v,&w);
				rev[rt[v]]^=w;
				if(w)swap(tr[rt[v]][0],tr[rt[v]][1]);
				rt[u]=merge(rt[u],rt[v]);
				rt[v]=0;
			}
//			for(int i=1;i<=n;i++){
//				printf("%d:\n",i);
//				output(rt[i]);putchar(10);
//			}
		}
	}
}

详细

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

Test #1:

score: 20
Accepted
time: 0ms
memory: 1208kb

input:

6 88
2 3 0
1 5 0 9938
1 4 0 7831
1 6 1 2211
2 1 1
2 6 0
1 6 0 1275
1 3 0 3440
1 2 0 6933
1 3 1 6974
...

output:

-1
-1
2211
4759
7831
2256
8270
8860
1653
6338
2009
6709
1611
4623
4874
4824
1144
994
1807
8813
-1
-1...

result:

ok 66 lines

Test #2:

score: 20
Accepted
time: 1ms
memory: 1208kb

input:

1 74
1 1 1 3656
1 1 0 5257
1 1 0 4189
2 1 0
2 1 0
2 1 1
1 1 1 5934
2 1 0
1 1 0 4607
1 1 1 2343
1 1 1...

output:

4189
5257
3656
5934
4721
2343
4891
390
1006
431
2160
4348
6216
2256
2781
634
667
5733
8796
5864
1265...

result:

ok 372 lines

Test #3:

score: 20
Accepted
time: 28ms
memory: 1216kb

input:

2 1000
2 1 1
1 1 0 9170
1 2 0 6963
1 2 1 3282
2 1 1
1 1 1 5437
3 2 1 0
2 1 0
1 2 0 5448
1 2 0 1870
1...

output:

-1
9170
-1
7036
1841
4627
2307
4222
901
7959
6363
4489
-1
3738
2761
8527
2143
2280
3832
-1
6859
9644...

result:

ok 9871 lines

Test #4:

score: 20
Accepted
time: 116ms
memory: 4276kb

input:

131072 393215
1 1 1 61956
1 2 1 79013
1 3 0 45517
1 4 0 40463
1 5 1 15281
1 6 0 69586
1 7 0 88636
1 ...

output:

58357
32704
73566
58460
87074
35682
19867
8566
87986
86418
49535
6367
35710
96359
94827
4825
24853
5...

result:

ok 131072 lines

Test #5:

score: 20
Accepted
time: 155ms
memory: 5888kb

input:

200000 499999
1 1 0 61956
1 2 0 71170
3 2 1 0
1 3 1 15281
3 3 2 1
1 4 1 97098
3 4 3 0
1 5 1 76573
3 ...

output:

97352
49358
14111
3348
11121
17888
38928
3459
81254
9027
9892
63567
15089
21002
67031
77265
93323
87...

result:

ok 100000 lines

Extra Test:

score: 0
Extra Test Passed