QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#869534#8616. Fast Tree Queriesucup-team5657#TL 2637ms16500kbC++142.8kb2025-01-25 10:54:572025-01-25 10:54:58

Judging History

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

  • [2025-01-25 10:54:58]
  • 评测
  • 测评结果:TL
  • 用时:2637ms
  • 内存:16500kb
  • [2025-01-25 10:54:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0, f=1; char ch=getchar();
	while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
	while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar();
	return x*f;
}
typedef unsigned u256 __attribute((vector_size(32)));
const int N=100000>>3;
int n,m,q,b[100005],siz[100005],son[100005],top[100005],fa[100005];
int hx[100005],line[100005],dep[100005],head[100005],tot,dfn;
struct Edge{
	int to,nxt;
}edge[200005];
void add(int u,int v){
	tot++;
	edge[tot].to=v; edge[tot].nxt=head[u];
	head[u]=tot;
}
void dfs1(int x,int f,int d){
	dep[x]=d,fa[x]=f,siz[x]=1;
	for(int i=head[x];i;i=edge[i].nxt){
		int tmp=edge[i].to;
		if(tmp==f) continue;
		dfs1(tmp,x,d+1); siz[x]+=siz[tmp];
		if(siz[tmp]>siz[son[x]]) son[x]=tmp; 
	}
}
void dfs2(int x,int link){
	top[x]=link; line[++dfn]=x,hx[x]=dfn;
	if(son[x]==0) return;
	dfs2(son[x],link);
	for(int i=head[x];i;i=edge[i].nxt){
		int tmp=edge[i].to;
		if(tmp==fa[x] || tmp==son[x]) continue;
		dfs2(tmp,tmp);
	}
}
u256 a[N],ans,tmp;
inline void add(int l,int r,int x){
	u256 xx=u256{x,x,x,x,x,x,x,x};
	if((l>>3)==(r>>3)){
		for(int i=l&7;i<=(r&7);i++){
			a[l>>3][i]+=x;
		}
	}else{
		for(int i=l&7;i<=7;i++){
			a[l>>3][i]+=x;
		}
		for(int i=0;i<=(r&7);i++){
			a[r>>3][i]+=x;
		}
		for(int i=(l>>3)+1;i<=(r>>3)-1;i++){
			a[i]+=xx;
		}
	}
}
inline int query(int l,int r){
	u256 ans=u256{0,0,0,0,0,0,0,0};
	int res=0;
	if((l>>3)==(r>>3)){
		for(int i=l&7;i<=(r&7);i++){
			res^=a[l>>3][i];
		}
	}else{
		for(int i=l&7;i<=7;i++){
			res^=a[l>>3][i];
		}
		for(int i=0;i<=(r&7);i++){
			res^=a[r>>3][i];
		}
		for(int i=(l>>3)+1;i<=(r>>3)-1;i++){
			ans^=a[i];
		}
		for(int i=0;i<=7;i++){
			res^=ans[i];
		}
	}
	return res;
}
void Update(int x,int y,int num){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			int t=x; x=y; y=t;
		}
		add(hx[top[x]],hx[x],num);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]){
		int t=x; x=y; y=t;
	}
	add(hx[x],hx[y],num);
}
int Query(int x,int y){
	int ans=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			int t=x; x=y; y=t;
		}
		ans^=query(hx[top[x]],hx[x]);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]){
		int t=x; x=y; y=t;
	}
	ans^=query(hx[x],hx[y]); 
	return ans;
}
int main(){
		#ifdef LOCAL
			assert(freopen("input.txt","r",stdin));
			assert(freopen("output.txt","w",stdout));
		#endif
	n=read(),q=read();
	for(int i=1;i<n;i++){
		int a=read(), b=read();
		add(a, b); add(b, a);
	}
	// cerr<<"ok"<<endl;
	dfs1(1,0,0);
	// cerr<<"ok"<<endl;
	dfs2(1,1);
	// cerr<<"ok"<<endl;
	for(int i=1;i<=n;i++) a[i>>3][i&7]=line[i];
	while(q--){
		char s[5]; scanf("%s", s);
		if(s[0]=='?'){
			int x=read(), y=read();
			printf("%d\n", Query(x, y));
		}else{
			int x=read(), y=read(), v=read();
			Update(x,y,v);
		}
	}
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5972kb

input:

5 6
1 2
1 3
3 4
3 5
? 2 5
+ 1 4 1
? 2 5
+ 4 5 2
? 4 5
? 1 1

output:

5
1
6
2

result:

ok 4 number(s): "5 1 6 2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

100 100
6 74
6 93
7 46
7 78
10 77
11 9
11 19
11 37
11 51
11 65
12 57
13 15
13 81
13 100
14 2
14 31
16 11
16 24
16 43
16 82
18 4
18 8
21 56
24 29
24 96
26 22
27 32
28 59
29 6
29 94
32 33
35 54
35 80
35 88
37 66
37 71
37 84
38 17
39 64
39 92
40 49
43 7
43 13
43 44
43 79
44 35
44 60
44 63
44 73
46 75
4...

output:

106
66
23766
20394
16388
3365
12076
7844
43127
56700
59
34700
24083
1164
24068
18776
3375
17495
21576
63126
11157
108177
63127
99162
105173
10715
110921
18320
19725
30958
120179
81226
107525
15669
21804
31071
63099
8313
191380
232240
84531
89146
173181
5447
160027
228651
98075
32595
109808
221822
11...

result:

ok 51 numbers

Test #3:

score: 0
Accepted
time: 2ms
memory: 6108kb

input:

10000 10000
1 8776
3 1597
3 8333
4 675
4 6993
4 7587
5 3379
5 6733
7 2440
7 6480
7 9506
10 4545
10 6664
12 1476
12 5343
14 1340
14 4167
14 5990
14 9910
15 5118
15 9423
16 8106
17 3856
20 5201
23 1138
24 3431
24 5578
25 251
26 3219
26 4156
26 6668
27 3795
28 6282
28 8196
34 9595
35 3919
36 5893
37 58...

output:

9911
12310
4793
31764
2730
42301
62944
16978
8306
25311
3011
1327
48224
8407
15662
38117
42837
59074
40600
39018
126441
21755
24519
51286
121187
4335
8349
10553
60726
86675
10797
3883
90069
95477
129342
37777
42339
124045
94323
16858
217612
79454
258856
118337
257945
196316
170121
216631
168616
1663...

result:

ok 5012 numbers

Test #4:

score: 0
Accepted
time: 18ms
memory: 7464kb

input:

50000 50000
1 1362
3 3371
3 13803
3 41794
3 47253
5 6227
5 42748
5 47841
6 28509
6 47395
7 8651
8 35909
10 24241
10 31205
10 33574
10 44109
10 48982
12 31361
12 44645
13 42041
15 20480
16 9467
16 11685
16 16024
16 28894
16 30759
19 3485
19 23470
19 24714
22 14239
25 44841
31 20447
35 5378
35 45983
3...

output:

5042
36803
61033
62216
64370
9744
33748
43519
25564
87892
120265
52351
36778
1507
81138
124599
19620
169852
82219
106112
38410
47506
214605
229380
175036
184353
71808
135637
195043
213707
60790
86453
31176
26740
107180
117349
64903
55397
245841
33362
73881
227391
227333
52027
217335
146795
51029
100...

result:

ok 24888 numbers

Test #5:

score: 0
Accepted
time: 44ms
memory: 8948kb

input:

100000 100000
4 6907
4 36895
4 37669
4 43936
4 99352
5 70501
10 29516
11 2844
11 77315
13 67782
13 72511
14 17273
14 52833
16 97869
19 29567
20 150
20 22843
20 40110
20 55549
20 70574
22 19544
25 43083
26 6781
28 16286
31 54186
33 6987
33 30861
33 98931
35 1140
35 21137
35 26681
35 59133
35 68440
35...

output:

81605
93578
30029
52473
57143
63629
77074
53264
71956
49059
16958
120352
93046
80080
67928
99557
182425
198595
36952
180370
156161
98094
56273
28969
34287
146511
31057
171228
128057
13930
81021
69266
100431
118091
249004
63451
147951
223183
255240
173677
30490
137681
135801
8441
205904
32855
66449
2...

result:

ok 50026 numbers

Test #6:

score: 0
Accepted
time: 65ms
memory: 9080kb

input:

100000 100000
1 484
1 23605
4 29115
5 78235
6 41049
7 17427
7 59118
7 73639
8 11499
8 21824
11 1488
11 34365
11 46659
15 1670
15 18862
16 88552
17 6551
17 18453
17 22090
18 90500
19 7369
19 40175
19 88031
19 89418
20 82312
22 36035
22 37511
22 90587
24 26545
25 99359
26 9713
26 19360
26 23939
27 145...

output:

118339
49557
8069
129295
8844
117076
73582
44568
123694
84080
220459
65210
20353
78112
178132
238977
76360
242837
177610
55964
146913
258846
46783
101598
210987
130886
215693
137264
91070
47636
222290
212175
70753
64509
143987
258750
63355
124572
249779
144712
237288
64472
32941
26055
220986
221734
...

result:

ok 50004 numbers

Test #7:

score: 0
Accepted
time: 324ms
memory: 10872kb

input:

100000 100000
2 96325
3 2625
3 19305
3 23547
3 33880
4 34351
4 80895
6 81122
7 8449
8 33132
9 6805
10 66297
12 40279
15 97995
17 28489
17 58943
17 63155
18 16755
19 36111
19 48497
20 73429
21 58028
22 23782
23 23210
25 43059
25 98782
28 96774
29 13371
30 18348
30 33119
31 91098
32 20761
34 19579
34 ...

output:

127926
27191
52198
17494
136
89133
88302
171
53017
111240
104493
80525
30736
39514
30186
242564
127960
116595
77217
181066
78202
117647
65124
5801
23054
231346
224212
101596
208931
56567
153986
225153
98732
39206
196696
201593
49107
59019
227567
23907
240224
47177
110143
93337
12687
16281
91369
7419...

result:

ok 49756 numbers

Test #8:

score: 0
Accepted
time: 476ms
memory: 12160kb

input:

100000 100000
1 18235
1 18934
2 89403
2 90291
3 31647
3 35123
4 14885
5 62625
6 75214
6 78206
6 86462
8 68147
10 73927
12 70742
13 18440
13 52686
14 486
15 38714
17 22417
19 10945
20 65914
22 168
24 72860
24 77513
25 43311
28 65572
31 24246
31 59430
32 26581
33 50532
35 41198
35 50438
38 10180
39 26...

output:

22351
118753
109047
88534
43548
60668
10313
43770
93628
94411
177029
257319
102775
45279
115695
72012
192085
82192
95036
247561
261897
165855
165273
226260
148341
25180
217815
163115
16411
218342
48666
2097
28168
215064
103606
87112
56628
51686
160381
172733
114741
224702
118590
202031
122700
81734
...

result:

ok 50083 numbers

Test #9:

score: 0
Accepted
time: 623ms
memory: 13184kb

input:

100000 100000
1 11525
1 89745
2 67284
3 9976
4 50748
5 77041
6 78293
7 56148
8 96259
9 89843
10 27797
11 16227
12 42015
13 68712
14 79151
15 28737
16 12689
16 46963
17 28758
17 97100
18 9035
18 45786
19 90894
20 6079
21 74811
22 59751
24 46439
25 61997
26 49256
27 47844
27 94562
28 36184
28 66803
29...

output:

27686
112778
112901
88910
1259
100292
96264
120935
67017
16105
254118
72138
79696
90798
240510
79481
58592
122335
35752
50037
4041
228323
26517
261989
101035
109710
124017
214226
78961
147898
227267
88759
232759
3546
176037
13839
58194
199727
72664
208807
235932
95313
72316
175531
185967
46635
25389...

result:

ok 50026 numbers

Test #10:

score: 0
Accepted
time: 912ms
memory: 14336kb

input:

100000 100000
1 8418
2 20507
3 79696
4 480
5 96826
6 39218
7 33218
8 91962
9 61011
10 76027
11 51859
12 47310
13 9311
14 62652
15 54337
16 37358
17 8695
18 30671
19 48794
20 60657
21 52785
22 67049
23 53237
24 89488
25 75316
26 59632
27 67663
28 64116
29 55756
30 9293
31 94163
32 68737
33 19549
34 2...

output:

59881
78759
127664
105428
29216
107850
23544
72603
31268
104150
10678
99895
191639
208183
143507
28071
112078
206140
244014
94502
180431
86547
228779
253881
114121
78644
211819
183217
3147
260855
252995
92807
134492
222747
179363
178012
163544
6300
56553
216082
135851
241124
54901
160545
83866
34670...

result:

ok 50161 numbers

Test #11:

score: 0
Accepted
time: 911ms
memory: 12928kb

input:

100000 100000
1 65542
2 44999
3 44775
4 53755
5 91414
6 88642
7 43743
8 66023
9 81924
10 33144
11 16238
12 69557
13 88380
14 14104
15 1590
16 22493
17 76223
18 2992
19 59612
20 19262
21 44547
22 9268
23 12883
24 8940
25 89232
26 83134
27 39177
28 25894
29 66905
30 75897
31 68348
32 31913
33 37824
34...

output:

85995
93714
48766
33134
15475
33702
26879
252344
91559
31139
32105
17681
214842
50526
179
127372
79021
215907
232859
150665
37612
228872
251465
221167
238269
143930
94966
193052
240279
70502
210689
5253
244204
197389
79884
65172
23165
26916
135637
44849
246604
184554
131027
12149
145820
236776
27224...

result:

ok 49916 numbers

Test #12:

score: 0
Accepted
time: 460ms
memory: 16500kb

input:

100000 100000
1 20244
2 70573
3 10436
4 38605
5 82242
6 7959
7 41794
8 6055
9 31572
10 18325
11 47705
12 92257
13 84847
14 29103
15 56455
16 78786
17 15635
18 46887
19 42925
20 73042
21 13655
22 17805
23 79078
24 2264
25 75041
26 20673
27 67835
28 26420
29 43093
30 18005
31 30599
32 67822
33 74865
3...

output:

57973
49029
120357
260910
16992
270917
140712
61400
151081
193091
45372
245217
372812
730619
84632
443274
274702
287820
414375
965786
802481
1047701
620743
227679
563632
989174
510166
711609
1202310
1778396
696203
940565
166128
1123056
1109958
1219135
89745
975640
1434947
1480887
1051570
1795047
219...

result:

ok 5037 numbers

Test #13:

score: 0
Accepted
time: 1365ms
memory: 13312kb

input:

100000 100000
1 49702
2 2333
3 57831
4 12821
5 62769
6 86649
7 42954
8 30845
9 6944
10 85242
11 29311
12 5403
13 41153
14 2035
15 93153
16 81418
17 11087
18 12973
19 43286
20 9192
21 26723
22 55297
23 69360
24 78933
25 69855
26 33093
27 36813
28 99089
29 46016
30 31662
31 21406
32 91918
33 75022
34 ...

output:

116023
74250
12533
23051
52968
29149
10175
3442
19482
110017
81204
46160
87223
6212
112117
46650
8708
41872
113753
21588
42133
118956
82183
41958
50964
67881
106887
70982
61306
47670
2186
57415
113171
117211
32700
77284
106361
121708
98751
107296
66647
51878
94513
15650
34650
107171
100594
125147
30...

result:

ok 94964 numbers

Test #14:

score: 0
Accepted
time: 2637ms
memory: 13048kb

input:

100000 100000
1 51281
2 98534
3 56890
4 91069
5 13958
6 53488
7 85023
8 36325
9 53644
10 51148
11 40287
12 22074
13 71930
14 42072
15 26952
16 97057
17 38720
18 18348
19 55081
20 46329
21 6145
22 96670
23 17563
24 63818
25 33264
26 89453
27 4561
28 8807
29 90035
30 50901
31 76224
32 63425
33 64586
3...

output:

48966
34949
100005
115783
90664
101369
86978
68458
124455
60125
720
162086
197800
46796
33398
187834
19882
136944
40634
187939
207446
218823
12321
205875
63213
7354
91929
104422
44719
49354
225170
123331
90704
129455
65861
194543
456311
164926
246119
166606
10928
404670
246844
136232
51411
149969
19...

result:

ok 49756 numbers

Test #15:

score: 0
Accepted
time: 1292ms
memory: 14848kb

input:

100000 100000
1 92615
2 88373
3 88745
4 58354
5 24241
6 70117
7 7474
8 47980
9 6849
10 9192
11 66174
12 88815
13 50287
14 60656
15 11112
16 48531
17 69958
18 53839
19 71335
20 10861
21 15521
22 67298
23 61906
24 34513
25 74665
26 31182
27 77910
28 39425
29 13475
30 29380
31 36965
32 71970
33 28150
3...

output:

238801
120555
164488
98713
2599
122503
405454
177726
330455
441985
228138
270151
149692
239460
58748
85861
1527370
1278580
2080749
187336
925428
767180
859833
1016615
780947
21643
1213833
1194783
1960577
1896675
869390
3610735
297981
1420255
2596004
4151506
2371827
2701564
621389
381816
584791
31707...

result:

ok 5005 numbers

Test #16:

score: -100
Time Limit Exceeded

input:

100000 100000
1 34291
2 31226
3 1756
4 67460
5 46349
6 4923
7 97346
8 68889
9 39438
10 52280
11 53924
12 52709
13 89517
14 65425
15 58125
16 90680
17 19878
18 57533
19 86396
20 57326
21 77690
22 15600
23 13138
24 52111
25 21607
26 11387
27 98786
28 67947
29 90338
30 67334
31 9684
32 58446
33 94828
3...

output:

37670
69685
92969
5958
66654
73849
1961
100898
24685
93282
114575
93770
128991
22741
89985
94025
115600
85797
114687
51826
42881
95166
781
74173
34457
79306
69052
115127
7799
64463
54387
73689
126906
105702
23781
11098
103084
59293
85954
79852
1087
29204
128722
87608
23226
40972
66104
4696
56425
125...

result: