QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#80138#5410. 树特卡克林Crysfly40 2547ms33544kbC++174.0kb2023-02-22 16:02:052023-02-22 16:02:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-22 16:02:09]
  • 评测
  • 测评结果:40
  • 用时:2547ms
  • 内存:33544kb
  • [2023-02-22 16:02:05]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f

int n,m;

const int V=(1<<17);
struct bit{
	int tr[1<<17|5],sum;
	void ins(int x){
		++sum;
		++tr[x];
	}
	void del(int x){
		--sum;
		--tr[x]; 
	}
	void out(){
		For(i,0,7)cout<<tr[i]<<" ";puts("");
	}
	int kth(int k){
		int now=0;
		For(i,0,V){
			now+=tr[i];
			if(now>=k)return i;
		}
	}
}T;

#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
int fa[maxn],ch[maxn][2],sz[maxn],si[maxn],mx[maxn],val[maxn];
bool rev[maxn];

struct myset{
	priority_queue<pii>q1,q2;
	void insert(pii o){q1.push(o);}
	void erase(pii o){q2.push(o);}
	pii top(){
		while(q2.size() && q1.top()==q2.top())q1.pop(),q2.pop();
		return q1.top();
	}
	int size(){return q1.size()-q2.size();}
}s[maxn];
// (size,max)

bool nrt(int p){return ls(fa[p])==p||rs(fa[p])==p;}
bool dir(int p){return rs(fa[p])==p;}
void up(int p){
	mx[p]=max(p,max(mx[ls(p)],mx[rs(p)]));
	sz[p]=sz[ls(p)]+sz[rs(p)]+1+si[p];
	val[p]=val[ls(p)]^val[rs(p)]^p;
}
void pr(int x){
	rev[x]^=1;
	swap(ls(x),rs(x));
}
void down(int x){
	if(rev[x]){
		if(ls(x))pr(ls(x));
		if(rs(x))pr(rs(x));
		rev[x]=0;
	}
}
void downall(int p){
	if(nrt(p))downall(fa[p]);
	down(p);
}
void rot(int x){
	int y=fa[x],z=fa[y],k=dir(x),s=ch[x][!k];
	fa[x]=z; if(nrt(y)) ch[z][dir(y)]=x;
	fa[y]=x; ch[x][!k]=y;
	if(s)fa[s]=y; ch[y][k]=s; up(y),up(x);
}
void splay(int x){
	if(!x)return;
	downall(x);
	while(nrt(x)){
		int y=fa[x];
		if(nrt(y))rot(dir(x)==dir(y)?y:x);
		rot(x);
	}
}

int findrt(int x){
	splay(x);
	if(!fa[x]){
		down(x);
		while(ls(x))down(x=ls(x));
		return x;
	}
	return findrt(fa[x]);
}

int st[maxn],tp,k;
void find(int x,int tmp){
//	cout<<"find "<<x<<" "<<k<<" "<<tmp<<"\n";
	down(x);
	if(sz[rs(x)]+tmp>=(1<<k)) find(rs(x),tmp);
	tmp+=sz[rs(x)]+si[x]+1;
//	cout<<"tmp "<<x<<" "<<tmp<<"\n";
	if(tmp>=(1<<k)){
		st[++tp]=x;
		while(tmp>=(1<<k))++k;
	}
	if(sz[ls(x)]+tmp>=(1<<k)) find(ls(x),tmp);
}

void make(int x,int y){
//	cout<<"make "<<x<<" "<<y<<"\n";
	T.del(val[x]);
	if(rs(x)){
		si[x]+=sz[rs(x)];
		s[x].insert(mkp(sz[rs(x)],mx[rs(x)]));
		T.ins(val[rs(x)]);
	}
	splay(y);
	if(rs(x)=y){
		si[x]-=sz[y];
		s[x].erase(mkp(sz[y],mx[y]));
		T.del(val[y]);
	}
	up(x),T.ins(val[x]);
}

void check(int x){
//	cout<<"check "<<x<<"\n";
	splay(x);
	if(s[x].size()){
		pii it=s[x].top();
		if(it>mkp(sz[rs(x)],mx[rs(x)])) make(x,it.se);
	}
}

void acc(int x){
//	cout<<"acc "<<x<<"\n";
	tp=0;
	for(int y=0;x;x=fa[y=x]) splay(x),st[++tp]=x;
	Rep(i,tp,1){
		splay(st[i]);
		make(st[i],st[i-1]);
	}
}

void calcall(int x){
//	cout<<"calcall "<<x<<"\n";
	tp=0,k=0; find(x,0);
//	For(i,1,tp)cout<<st[i]<<" ";puts(" st");
	For(i,1,tp){
		int u=st[i];
		check(u);
	}
}

void mkrt(int x){
	acc(x),splay(x),pr(x);
	calcall(x);
	splay(x);
}

void cut(int x,int y){
	splay(x); int szx=sz[rs(x)]+1+si[x];
	splay(y); int szy=sz[rs(y)]+1+si[y];
	if(szx<szy)swap(x,y),swap(szx,szy);
	acc(x),splay(y);
	si[x]-=sz[y],fa[y]=0,s[x].erase(mkp(sz[y],mx[y])),up(x);
	calcall(x);
}

void link(int x,int y){
	mkrt(y),acc(x);
	fa[y]=x,si[x]+=sz[y],s[x].insert(mkp(sz[y],mx[y])),up(x);
//	puts("linked");
	calcall(x);
}

signed main()
{
	n=read(),m=read();
	For(i,1,n)T.ins(i),up(i);
//	link(1,2);
//	mkrt(2);
//	T.out();
//	For(i,1,3)cout<<fa[i]<<" "<<ls(i)<<" "<<rs(i)<<" "<<val[i]<<" "<<rev[i]<<"\n";
//	exit(0);
	For(_,1,m){
		int op=read(),u=read(),v=read(),k=read();
		if(op==1)link(u,v);
		else cut(u,v);
	//	T.out();
		printf("%d\n",T.kth((k-1)%T.sum+1));
	}
	return 0;
}

詳細信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 1ms
memory: 20096kb

input:

10 50
1 4 5 8
1 2 10 6
1 7 10 1
1 2 5 7
1 1 5 1
1 8 3 1
1 5 3 3
1 1 9 3
0 10 2 8
1 10 1 6
0 1 10 1
0 4 5 9
1 8 4 9
0 3 5 3
0 10 7 6
1 1 4 1
0 4 8 3
1 5 6 2
1 10 7 1
1 2 7 1
0 2 7 8
0 4 1 10
1 7 2 6
1 2 4 2
1 2 3 4
0 5 6 7
1 8 6 1
0 2 7 7
1 10 3 1
0 3 8 5
0 2 4 7
1 4 10 9
0 1 9 8
0 2 3 1
1 10 8 4
1 1...

output:

9
8
1
1
3
4
9
9
4
9
2
6
11
9
15
6
6
2
2
4
2
9
4
6
10
6
4
13
1
4
1
14
14
3
7
4
7
3
9
8
4
7
8
4
3
3
7
7
5
2

result:

ok 50 numbers

Subtask #2:

score: 5
Accepted

Dependency #1:

100%
Accepted

Test #2:

score: 5
Accepted
time: 1ms
memory: 16220kb

input:

100 500
1 38 34 84
1 71 2 8
1 22 1 89
1 70 2 7
1 100 15 69
1 97 86 77
1 84 39 47
1 21 43 73
1 100 1 64
1 69 99 42
1 19 8 25
1 29 70 91
1 38 1 45
1 92 63 5
1 54 83 94
1 5 12 41
1 73 91 90
1 100 32 10
1 43 38 2
1 73 96 65
1 67 94 26
1 21 31 65
1 22 37 42
1 85 37 12
1 3 46 84
1 69 88 28
1 92 82 87
1 54...

output:

85
8
92
7
74
81
51
79
68
47
28
4
52
7
10
49
9
14
4
77
30
77
50
16
9
36
13
14
30
13
4
10
52
7
34
41
63
60
29
14
62
78
74
40
42
27
51
24
6
24
66
33
27
40
48
76
24
13
14
112
40
33
44
41
47
68
80
33
49
112
76
99
45
65
90
22
51
30
33
59
65
22
50
22
50
63
67
51
58
50
45
3
75
41
74
64
1
7
51
10
42
22
76
49...

result:

ok 500 numbers

Subtask #3:

score: 10
Accepted

Dependency #2:

100%
Accepted

Test #3:

score: 10
Accepted
time: 11ms
memory: 17128kb

input:

1000 5000
1 481 556 393
1 197 951 694
1 844 89 877
1 996 276 422
1 936 284 893
1 606 205 37
1 908 355 156
1 580 338 612
1 889 574 90
1 835 998 105
1 795 673 814
1 286 226 514
1 393 557 950
1 548 913 346
1 714 779 585
1 509 140 1000
1 85 564 274
1 576 820 932
1 246 536 8
1 361 356 299
1 430 364 650
1...

output:

393
697
881
425
896
37
157
623
91
106
819
520
962
352
595
16
280
948
8
307
666
567
563
103
151
5
384
680
260
88
449
313
932
965
62
722
663
823
798
704
527
409
926
463
611
290
99
675
749
6
486
721
550
42
268
994
125
243
158
524
586
934
947
716
108
478
300
759
931
754
992
263
261
784
13
631
229
724
14...

result:

ok 5000 numbers

Subtask #4:

score: 10
Accepted

Test #4:

score: 10
Accepted
time: 2547ms
memory: 30308kb

input:

100000 99999
1 1 2 44734
1 2 3 61340
1 1 4 68331
1 2 5 86313
1 4 6 26991
1 4 7 72397
1 3 8 24098
1 5 9 31437
1 5 10 82367
1 5 11 20958
1 11 12 38919
1 12 13 87596
1 2 14 41335
1 1 15 78828
1 14 16 73861
1 9 17 81368
1 1 18 40205
1 9 19 60737
1 4 20 9347
1 5 21 84645
1 18 22 20063
1 15 23 98769
1 15 ...

output:

44735
61342
68333
86315
26994
72400
24102
31442
82372
20963
38925
87603
41342
78835
73869
81377
40214
60746
9356
84654
20073
98780
78096
84936
68247
59462
13246
58069
42163
54594
13293
42645
26853
5004
97006
66947
80024
951
20786
52358
56517
76013
46338
9103
21979
20761
46128
72450
88242
9521
60767
...

result:

ok 99999 numbers

Subtask #5:

score: 10
Accepted

Test #5:

score: 10
Accepted
time: 2507ms
memory: 33544kb

input:

100000 99999
1 2 1 30000
1 3 2 76304
1 4 1 35053
1 5 4 38046
1 6 4 43843
1 7 1 64206
1 8 6 52957
1 9 3 42768
1 10 8 6520
1 11 5 43975
1 12 1 64810
1 13 12 64219
1 14 10 78432
1 15 4 62142
1 16 15 235
1 17 3 77806
1 18 17 58130
1 19 5 47941
1 20 14 19568
1 21 7 99780
1 22 17 39362
1 23 4 10525
1 24 1...

output:

30001
76306
35056
38050
43847
64210
52962
42774
6527
43983
64818
64228
78442
62152
246
77817
58142
47953
19581
99794
39376
10539
60403
89304
59521
30009
47189
78769
8444
80436
45397
27687
37175
16360
59044
53960
89899
8332
14084
21922
75850
25515
25098
68678
86109
75488
75879
10223
80048
70404
21913...

result:

ok 99999 numbers

Subtask #6:

score: 0
Time Limit Exceeded

Dependency #3:

100%
Accepted

Test #6:

score: 0
Time Limit Exceeded

input:

100000 500000
1 80697 84881 19262
1 95888 80521 10177
1 94305 51186 64430
1 98326 42972 48338
1 12913 26290 94592
1 72437 16368 33161
1 90564 19898 47803
1 72637 75741 79240
1 36415 11660 46397
1 96674 79314 69371
1 5150 54843 2612
1 33519 46498 64905
1 83708 94254 81506
1 51872 64932 91946
1 63852 ...

output:

19262
10177
64429
48337
94596
33161
47805
79242
46399
69373
2612
64909
81512
91956
23328
77077
51755
60147
21156
73862
37883
3541
75252
16667
79090
17015
8464
13951
30540
87627
56430
33759
99460
18806
34544
27730
23888
78785
60056
28296
65149
42767
64112
56512
52968
79488
32043
53322
73047
27093
288...

result:


Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Dependency #6:

0%