QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#740037#5217. 线段树I_be_wanna100 ✓346ms56812kbC++203.6kb2024-11-13 00:33:502024-11-13 00:33:50

Judging History

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

  • [2024-11-13 00:33:50]
  • 评测
  • 测评结果:100
  • 用时:346ms
  • 内存:56812kb
  • [2024-11-13 00:33:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 5;
const int MAXLOG = 20;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, m, tot, lc[MAXN], rc[MAXN], home[MAXN];
int root, depth[MAXN], father[MAXN][MAXLOG];
int build(int l, int r) {
	int pos = ++tot;
	if (l == r) {
		home[l] = pos;
		return pos;
	}
	int mid; read(mid);
	lc[pos] = build(l, mid);
	father[lc[pos]][0] = pos;
	rc[pos] = build(mid + 1, r);
	father[rc[pos]][0] = pos;
	return pos;
}
int lca(int x, int y) {
	if (depth[x] < depth[y]) swap(x, y);
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (depth[father[x][i]] >= depth[y]) x = father[x][i];
	if (x == y) return x;
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (father[x][i] != father[y][i]) {
			x = father[x][i];
			y = father[y][i];
		}
	return father[x][0];
}
int climb(int x, int y) {
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (depth[father[x][i]] > depth[y]) x = father[x][i];
	return x;
}
void dfs(int pos, int fa) {
	depth[pos] = depth[fa] + 1;
	for (int i = 1; i < MAXLOG; i++)
		father[pos][i] = father[father[pos][i - 1]][i - 1];
	if (lc[pos]) dfs(lc[pos], pos);
	if (rc[pos]) dfs(rc[pos], pos);
}
pair <int, ll> lsum[MAXN], rsum[MAXN];
void work(int pos, int fa) {
	if (lc[pos]) {
		lsum[lc[pos]] = lsum[pos];
		rsum[lc[pos]] = rsum[pos];
		rsum[lc[pos]].first += 1;
		rsum[lc[pos]].second += depth[rc[pos]];
		work(lc[pos], pos);
	}
	if (rc[pos]) {
		lsum[rc[pos]] = lsum[pos];
		rsum[rc[pos]] = rsum[pos];
		lsum[rc[pos]].first += 1;
		lsum[rc[pos]].second += depth[lc[pos]];
		work(rc[pos], pos);
	}
}
int main() {
	read(n), build(1, n);
	home[0] = ++tot;
	lc[tot + 1] = tot;
	father[tot][0] = tot + 1;
	rc[tot + 1] = 1;
	father[1][0] = ++tot;
	
	home[n + 1] = ++tot;
	lc[tot + 1] = tot - 1;
	father[tot - 1][0] = tot + 1;
	rc[tot + 1] = tot;
	father[tot][0] = tot + 1;
	
	dfs(root = ++tot, 0);
	work(root, 0);
	
	read(m);
	for (int i = 1; i <= m; i++) {
		int u, l, r; read(u), read(l), read(r);
		l = home[l - 1], r = home[r + 1];
		int x = lca(l, r), tl = climb(l, x), tr = climb(r, x);
		ll ans = rsum[l].second - rsum[tl].second + lsum[r].second - lsum[tr].second;
		int cnt = rsum[l].first - rsum[tl].first + lsum[r].first - lsum[tr].first;
		ans += 1ll * cnt * depth[u];
		if (u == x || lca(u, x) != x) {
			int y = lca(u, x);
			ans -= 2ll * cnt * depth[y];
		} else if (lca(u, l) != x) {
			int y = lca(u, l);
			ans -= 2ll * (lsum[r].first - lsum[tr].first) * depth[x];
			ans -= 2ll * (rsum[l].first - rsum[y].first) * depth[y];
			ans -= 2ll * (rsum[y].second - rsum[tl].second - (rsum[y].first - rsum[tl].first));
			if (y != u && climb(u, y) == rc[y]) ans -= 2;
		} else {
			int y = lca(u, r);
			ans -= 2ll * (rsum[l].first - rsum[tl].first) * depth[x];
			ans -= 2ll * (lsum[r].first - lsum[y].first) * depth[y];
			ans -= 2ll * (lsum[y].second - lsum[tr].second - (lsum[y].first - lsum[tr].first));
			if (y != u && climb(u, y) == lc[y]) ans -= 2;
		}
		writeln(ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 2ms
memory: 11720kb

input:

100
74 11 9 4 3 1 2 8 5 6 7 10 35 19 12 16 14 13 15 18 17 28 22 21 20 25 24 23 26 27 34 29 31 30 32 33 71 70 40 36 38 37 39 52 48 46 44 42 41 43 45 47 49 50 51 63 54 53 62 56 55 61 59 57 58 60 66 64 65 67 69 68 73 72 86 77 75 76 81 78 80 79 82 85 83 84 97 92 87 89 88 91 90 93 96 95 94 99 98
100
37 2...

output:

63
37
80
46
38
38
49
60
58
65
75
68
67
37
46
79
19
71
73
59
38
78
69
91
32
76
40
30
44
23
133
67
48
101
47
31
38
37
49
52
74
53
16
120
80
15
33
65
51
59
66
48
10
55
22
58
58
81
77
52
29
32
51
43
67
55
89
36
61
68
27
11
98
35
66
59
72
53
40
62
74
55
130
43
92
37
114
55
70
38
83
120
103
51
29
37
66
63...

result:

ok 100 numbers

Test #2:

score: 10
Accepted
time: 23ms
memory: 56812kb

input:

200000
89936 20001 11314 1840 115 108 54 9 5 1 2 4 3 7 6 8 41 10 34 13 12 11 16 15 14 20 17 19 18 21 25 22 23 24 26 31 28 27 29 30 32 33 35 39 38 36 37 40 49 47 43 42 46 44 45 48 51 50 53 52 70 63 56 55 61 60 59 57 58 62 65 64 67 66 68 69 85 73 72 71 83 82 78 74 75 77 76 81 80 79 84 86 106 95 87 90 ...

output:

85651163
126181135
1103314683
242012377
465
95946000
2095846369
271946
192937668
2011780750
314454406
1465851504
460749
551259935
479838150
62513
378643084
1505097375
3020992849
37499550

result:

ok 20 numbers

Test #3:

score: 10
Accepted
time: 233ms
memory: 56756kb

input:

200000
88741 20001 10233 3717 53 25 16 14 10 8 5 4 2 1 3 6 7 9 13 12 11 15 24 21 20 18 17 19 23 22 51 38 26 34 29 27 28 33 32 31 30 36 35 37 48 47 46 42 40 39 41 44 43 45 49 50 52 2777 513 72 60 56 54 55 59 57 58 66 64 61 62 63 65 70 67 68 69 71 82 77 73 74 75 76 80 78 79 81 177 158 135 117 91 86 83...

output:

267
9668
215471
363
240905
298
6035
368201
300
329
2489
131605
443883
22331
130527
170126
296
132
110544
532441
359827
342951
389359
118731
266
212
244
475628
287429
272822
488328
181036
303533
171466
13677
603360
277036
39326
536
119361
638607
216695
335895
134847
620291
206012
25503
396838
22638
4...

result:

ok 200000 numbers

Test #4:

score: 10
Accepted
time: 235ms
memory: 56752kb

input:

200000
102528 20001 9131 6755 539 492 272 22 20 9 4 1 3 2 8 5 6 7 14 10 13 12 11 18 16 15 17 19 21 115 38 23 32 31 26 25 24 30 27 29 28 36 35 34 33 37 79 77 44 43 42 41 39 40 45 48 46 47 63 49 51 50 57 53 52 54 56 55 59 58 61 60 62 66 65 64 69 68 67 74 72 71 70 73 75 76 78 89 87 85 84 81 80 83 82 86...

output:

306291
809613
220413
99
48962
312200
425054
430718
473766
420
644896
97917
194724
41807
64486
140
5451
157911
39574
177586
155378
334269
195071
681502
494439
373364
275648
57647
165
356149
445
265810
200
406008
24894
143415
139
188637
101905
237
219305
634258
203
293
298409
639258
249640
340771
2627...

result:

ok 200000 numbers

Test #5:

score: 10
Accepted
time: 179ms
memory: 56768kb

input:

200000
122888 20001 6269 650 40 16 13 6 5 1 4 3 2 11 8 7 9 10 12 14 15 31 26 21 20 17 19 18 22 24 23 25 28 27 29 30 33 32 39 37 36 34 35 38 532 345 134 113 102 43 41 42 58 57 56 55 52 49 44 45 48 47 46 50 51 53 54 84 72 64 61 59 60 63 62 66 65 68 67 71 70 69 77 76 75 73 74 78 83 82 80 79 81 97 91 89...

output:

240
182
797621839
66467
301731905
91
142441973
136208
903146365
352942786
24297
46883
31740
188
159
7365
39940
2362135
27740
49075
35261
98
71419
865217762
246
2552829
30330517
40381
275
68818
452
243
15921
215
321
35203
389861003
16549
97696
145202
44804
47275
33356
46024
99830
261
53256483
2424513...

result:

ok 200000 numbers

Test #6:

score: 10
Accepted
time: 173ms
memory: 56680kb

input:

200000
118034 20001 17185 15081 3389 1602 173 45 37 12 3 1 2 11 5 4 9 6 8 7 10 34 24 23 13 18 15 14 16 17 19 22 21 20 30 27 26 25 29 28 31 33 32 35 36 39 38 40 41 44 43 42 97 84 55 54 51 47 46 49 48 50 53 52 73 59 57 56 58 61 60 62 63 67 64 66 65 70 68 69 72 71 82 74 78 77 75 76 80 79 81 83 93 86 85...

output:

185782
42159241
20235
27955
91384
6226
762822410
581251658
1267
124
193
5114
329
551402364
4630
132388
41899
502651124
7819081
47333
23184742
150
38681
43734
78372
28836
28355
464804883
5874
33841
62637
12953
512176182
12683300
22967421
102714
257974405
711267314
83255
19615759
54141
262
620312286
1...

result:

ok 200000 numbers

Test #7:

score: 10
Accepted
time: 339ms
memory: 56756kb

input:

200000
86911 20001 6428 106 7 3 2 1 4 6 5 30 20 12 9 8 11 10 16 14 13 15 17 19 18 29 21 25 24 22 23 27 26 28 38 33 31 32 37 35 34 36 48 46 42 40 39 41 44 43 45 47 76 58 53 52 51 50 49 57 56 55 54 70 66 64 60 59 63 62 61 65 68 67 69 72 71 74 73 75 98 87 79 77 78 86 82 80 81 84 83 85 92 90 88 89 91 94...

output:

96069
135821
405392
2973245
44626
538553569
356346956
151726135
217405
454032
172123798
620
206878018
783
640056983
743051278
652374
53251
349441408
631938231
505345104
582642
381789
383177
412769
782050529
71221
174912042
98641
1170694817
926850
13125675
45235
47828
316525503
36375
1437015809
26881...

result:

ok 200000 numbers

Test #8:

score: 10
Accepted
time: 346ms
memory: 56748kb

input:

200000
114377 20001 10135 3288 2907 1165 173 107 80 5 4 1 2 3 57 42 35 33 32 17 12 9 6 8 7 11 10 15 13 14 16 29 26 18 25 19 23 21 20 22 24 28 27 31 30 34 39 38 36 37 40 41 55 52 49 47 44 43 45 46 48 51 50 54 53 56 69 64 59 58 60 63 61 62 67 66 65 68 70 77 75 72 71 73 74 76 79 78 83 81 82 105 86 85 8...

output:

417246
159294
1049199
102287
556428219
660603510
1171316467
282066
722262
191520
6827
2434895747
428896
92042
740532528
104610
771414
75685
117776
47160
342
693617513
353762
48442
545146
150284
74119
621905346
295320559
1157650977
455470
131917
595419
244161
455737415
292385
75807
17196
238689436
12...

result:

ok 200000 numbers

Test #9:

score: 10
Accepted
time: 331ms
memory: 56636kb

input:

200000
126392 20001 7953 6193 3232 1884 83 37 6 3 2 1 4 5 11 9 8 7 10 18 16 13 12 15 14 17 20 19 34 24 22 21 23 32 30 25 27 26 28 29 31 33 35 36 77 58 42 40 39 38 41 48 47 44 43 45 46 53 50 49 51 52 57 54 56 55 62 61 60 59 74 73 64 63 71 68 67 65 66 70 69 72 75 76 79 78 82 81 80 1255 156 144 104 96 ...

output:

1775916341
135101
297009695
716801
1675703274
978483683
669685
413053
158316
679458
2214071072
1266087092
343370
144346
116031
44111
254004
239117
91871
439278
1139
650271
333019
530996
758
674413
492019
59490
457722
296428
109291
275512029
215878
431148165
548106
797781645
466
41423
230004
568230
3...

result:

ok 200000 numbers

Test #10:

score: 10
Accepted
time: 317ms
memory: 56688kb

input:

200000
129838 20001 1711 222 129 124 54 39 21 3 2 1 7 4 5 6 15 8 12 11 10 9 13 14 17 16 18 20 19 37 30 25 24 23 22 28 26 27 29 35 34 32 31 33 36 38 52 44 40 42 41 43 48 45 46 47 51 50 49 53 109 83 74 57 56 55 69 65 61 60 59 58 63 62 64 66 68 67 72 71 70 73 77 76 75 80 78 79 81 82 91 88 87 86 84 85 8...

output:

9925
59559
12406046
402672
24524
175692
402888
280823
308220
734117723
557729811
230875
501335
404797
406835
456
479696
202624
315590
288108015
206325
423276
1406153848
533840
166684
794058
213860
171401
1176035172
246083
213761
326054
2495226024
1229509715
174561
316109
568188350
490497
474574
8969...

result:

ok 200000 numbers

Extra Test:

score: 0
Extra Test Passed