QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#880757#9648. 数据结构cqbzdj0 1609ms248144kbC++145.2kb2025-02-03 19:39:232025-02-03 19:39:23

Judging History

This is the latest submission verdict.

  • [2025-02-03 19:39:23]
  • Judged
  • Verdict: 0
  • Time: 1609ms
  • Memory: 248144kb
  • [2025-02-03 19:39:23]
  • Submitted

answer

#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 200005
#define mk 3
#define inf 200001
#define pr pair<int, int>
#define vp vector<pr>
#define bmid int mid = (l[i] + r[i]) >> 1
#define lc i << 1
#define rc (i << 1) | 1
#define len(i) (r[i] - l[i] + 1)
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
vector<int> g[Maxn];
int fa[Maxn], dep[Maxn], siz[Maxn], son[Maxn];
void dfs1(int i){
	siz[i] = 1;
	for(int j: g[i]){
		g[j].erase(find(g[j].begin(), g[j].end(), i));
		fa[j] = i;
		dep[j] = dep[i] + 1;
		dfs1(j);
		siz[i] += siz[j];
		if(siz[j] > siz[son[i]]) son[i] = j;
	}
}
int dfn[Maxn], cnt, top[Maxn];
void mark(int i, int k){
	if(!k){
		if(!dfn[i]) dfn[i] = ++cnt;
		return;
	}
	for(int j: g[i]) mark(j, k - 1);
}
void dfs2(int i){
	vector<int> l;
	for(int j = i; j; j = son[j]){
		top[j] = i;
		l.push_back(j);
	}
	for(int k = 0; k <= mk; k++){
		for(int j: l){
			mark(j, k);
		}
	}
	for(int j: l){
		for(int k: g[j]){
			if(k != son[j]) dfs2(k);
		}
	}
}
vp sub[Maxn], ksub[mk + 1][Maxn], kb[mk + 1][Maxn], kup[mk + 1][Maxn], fd[mk + 1][Maxn];
void up(vp &a){
	sort(a.begin(), a.end());
	vp b;
	for(pr i: a){
		if(b.size() && i.first <= b.back().second + 1) b.back().second = max(b.back().second, i.second);
		else b.push_back(i);
	}
	swap(a, b);
}
void link(vp &a, vp b){
	for(pr i: b) a.push_back(i);
	up(a);
}
vp del(vp a, vp b){
	b.push_back({inf, inf});
	vp c;
	for(int i = 0, j = 0; i < a.size();){
		if(b[j].second < a[i].first) j++;
		else if(b[j].first <= a[i].first){
			if(a[i].second <= b[j].second) i++;
			else{
				a[i].first = b[j].second + 1;
				j++;
			}
		}else if(a[i].second < b[j].first){
			c.push_back({a[i].first, a[i].second});
			i++;
		}else{
			c.push_back({a[i].first, b[j].first - 1});
			if(a[i].second <= b[j].second) i++;
			else{
				a[i].first = b[j].second + 1;
				j++;
			}
		}
	}
	return c;
}
void dfs3(int i){
	sub[i].push_back({dfn[i], dfn[i]});
	ksub[0][i].push_back({dfn[i], dfn[i]});
	for(int j: g[i]){
		dfs3(j);
		link(sub[i], sub[j]);
		for(int k = 0; k <= mk; k++) kb[k][j] = ksub[k][i];
		for(int k = 1; k <= mk; k++) link(ksub[k][i], ksub[k - 1][j]);
	}
	vp suf[mk + 1];
	int x;
	if(g[i].size() > 1){
		for(int j = g[i].size() - 1; j >= 0; j--){
			x = g[i][j];
			for(int k = 1; k <= mk; k++) link(kb[k][x], suf[k]);
			for(int k = 1; k <= mk; k++) link(suf[k], ksub[k - 1][x]);
		}
	}
}
void dfs4(int i){
	for(int k = 0; k <= mk; k++) link(kup[k][i], ksub[k][i]);
	if(son[i]){
		for(int k = 0; k <= mk; k++) kup[k][son[i]] = kup[k][i];
	}
	int x;
	for(int k = 0; k <= mk; k++){
		for(int j = 0; j <= k; j++){
			link(fd[k][i], ksub[j][i]);
		}
		x = i;
		for(int j = k - 1; j >= 0 && fa[x]; j--){
			for(int o = 0; o <= j; o++) link(fd[k][i], kb[o][x]);
			x = fa[x];
		}
	}
	for(int j: g[i]) dfs4(j);
}
struct segtree{
	int l[4 * Maxn], r[4 * Maxn];
	LL s[4 * Maxn], x[4 * Maxn], lz[4 * Maxn];
	void build(int i, int L, int R){
		l[i] = L, r[i] = R;
		if(L == R) return;
		bmid;
		build(lc, L, mid);
		build(rc, mid + 1, R);
	}
	void push_up(int i){
		s[i] = s[lc] + s[rc];
		x[i] = max(x[lc], x[rc]);
	}
	void add0(int i, int k){
		s[i] += len(i) * k;
		x[i] += k;
		lz[i] += k;
	}
	void push_down(int i){
		if(lz[i]){
			add0(lc, lz[i]);
			add0(rc, lz[i]);
			lz[i] = 0;
		}
	}
	void add(int i, int L, int R, int k){
		if(R < l[i] || r[i] < L) return;
		if(L <= l[i] && r[i] <= R){
			add0(i, k);
			return;
		}
		push_down(i);
		add(lc, L, R, k);
		add(rc, L, R, k);
		push_up(i);
	}
	LL tot(int i, int L, int R){
		if(R < l[i] || r[i] < L) return 0;
		if(L <= l[i] && r[i] <= R) return s[i];
		push_down(i);
		return tot(lc, L, R) + tot(rc, L, R);
	}
	LL solve(int i, int L, int R){
		if(R < l[i] || r[i] < L) return -1e18;
		if(L <= l[i] && r[i] <= R) return x[i];
		push_down(i);
		return max(solve(lc, L, R), solve(rc, L, R));
	}
} t;
vp get(int u, int v, int k){
	vp a;
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]]) swap(u, v);
		link(a, kup[k][u]);
		u = fa[top[u]];
	}
	if(dep[u] < dep[v]) swap(u, v);
	link(a, del(kup[k][u], kup[k][v]));
	link(a, fd[k][v]);
	return a;
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif
	int n = read(), m = read(), tp, u, v, k, x;
	for(int i = 1; i < n; i++){
		u = read(), v = read();
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs1(1);
	dfs2(1);
	dfs3(1);
	dfs4(1);
	vp r;
	LL ans;
	t.build(1, 1, n);
	for(int i = 1; i <= m; i++){
		tp = read();
		if(tp % 2){
			u = read(), v = read(), k = read();
			r = get(u, v, k);
		}else{
			u = read();
			r = sub[u];
		}
		if(tp <= 2) x = read();
		else if(tp <= 4) ans = 0;
		else ans = -1e18;
		for(pr j: r){
			if(tp <= 2) t.add(1, j.first, j.second, x);
			else if(tp <= 4) ans += t.tot(1, j.first, j.second);
			else ans = max(ans, t.solve(1, j.first, j.second));
		}
		if(tp > 2) printf("%lld\n", ans);
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 1573ms
memory: 209092kb

input:

199999 200000
1 2
2 3
3 4
2 5
4 6
5 7
3 8
7 9
5 10
4 11
7 12
4 13
1 14
7 15
8 16
3 17
10 18
15 19
19 20
12 21
9 22
4 23
18 24
23 25
5 26
13 27
11 28
11 29
27 30
17 31
18 32
6 33
7 34
17 35
1 36
6 37
18 38
35 39
10 40
1 41
32 42
10 43
13 44
22 45
15 46
20 47
12 48
48 49
34 50
16 51
18 52
6 53
10 54
2...

output:

0
62518
0
245795
0
88508
0
90022
0
0
362082
0
166057
0
-263428
27453
0
0
0
0
-149830
0
0
0
-82026
-342853
0
-325561
0
0
0
0
-372644
0
-174422
-309086
-558744
0
0
0
0
0
0
0
294312
-779200
0
-57599
-266309
0
-266309
-668682
0
0
0
0
-420482
0
-437910
0
-145971
0
1361331
0
0
0
89268
0
0
0
-342838
-11323...

result:

ok 99962 numbers

Test #2:

score: 0
Wrong Answer
time: 977ms
memory: 233816kb

input:

200000 200000
1 2
2 3
3 4
2 5
2 6
5 7
5 8
6 9
9 10
10 11
7 12
10 13
10 14
12 15
11 16
13 17
15 18
16 19
15 20
20 21
18 22
21 23
20 24
20 25
22 26
25 27
23 28
27 29
29 30
27 31
29 32
32 33
32 34
33 35
32 36
34 37
34 38
34 39
39 40
38 41
41 42
39 43
39 44
41 45
44 46
42 47
47 48
46 49
49 50
46 51
49 5...

output:

-693174644
-407448312
0
-218765316
-2646366679
-2706177783
-118075560
-688361945
-5326629756
-1034523241
248255
49651
397583533
-675507964
536236
-35250078
0
84875564
3666050542
1641758589
19873128850
278324
6513679386
278324
49896394094
3479173830
1135068121
4643542916
1714703106
348608
207206572
3...

result:

wrong answer 142nd numbers differ - expected: '35901630026', found: '31606662730'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 10
Accepted
time: 1568ms
memory: 209184kb

input:

199999 200000
1 2
1 3
1 4
4 5
1 6
3 7
7 8
2 9
6 10
1 11
2 12
4 13
10 14
14 15
9 16
8 17
15 18
6 19
18 20
9 21
3 22
11 23
10 24
24 25
23 26
8 27
7 28
8 29
1 30
12 31
27 32
2 33
28 34
24 35
33 36
7 37
32 38
2 39
31 40
13 41
41 42
7 43
26 44
39 45
36 46
10 47
2 48
6 49
26 50
29 51
7 52
15 53
45 54
34 5...

output:

0
0
0
0
0
0
0
-104272
107152
1440
0
-154968
53576
0
-558956
-105074
0
0
1693
0
0
-351850
0
0
0
0
-148344
0
0
0
0
0
-336768
0
-242877
-239540
-177269
0
0
0
-713232
0
0
0
0
-361599
13257
0
13257
0
0
-34188
0
53229
-434587
0
0
0
0
81294
0
0
0
0
0
97819
66486
0
53229
-294743
-355594
0
0
0
-10356
66486
4...

result:

ok 133269 numbers

Test #4:

score: 0
Wrong Answer
time: 960ms
memory: 233888kb

input:

200000 200000
1 2
2 3
3 4
3 5
4 6
4 7
5 8
6 9
7 10
6 11
9 12
12 13
11 14
10 15
12 16
15 17
17 18
15 19
16 20
19 21
20 22
19 23
23 24
24 25
23 26
25 27
26 28
26 29
28 30
28 31
27 32
29 33
29 34
33 35
31 36
36 37
35 38
36 39
38 40
40 41
39 42
41 43
42 44
41 45
44 46
43 47
44 48
46 49
49 50
49 51
49 52...

output:

0
0
0
0
0
-1710891717
0
35204
0
91771
0
0
0
91771
-441533729
0
0
0
-1655012194
0
0
-1222376926
-644402193
-211228869
0
0
-1354117733
0
-1373876471
0
0
-1155789170
61132
0
-464974584
91771
0
0
12505
145145
0
8782729811
168854
1238148306
124865
1098321605
137370
7233827198
1894745981
137370
1064541891...

result:

wrong answer 128th numbers differ - expected: '68606402935', found: '55721501047'

Subtask #3:

score: 0
Wrong Answer

Test #5:

score: 10
Accepted
time: 1110ms
memory: 210508kb

input:

199999 200000
1 2
1 3
1 4
1 5
2 6
1 7
6 8
6 9
6 10
6 11
1 12
4 13
10 14
8 15
4 16
1 17
15 18
6 19
7 20
6 21
8 22
22 23
10 24
11 25
24 26
6 27
16 28
4 29
26 30
28 31
6 32
13 33
32 34
14 35
34 36
10 37
3 38
23 39
1 40
2 41
7 42
34 43
1 44
36 45
36 46
36 47
35 48
18 49
34 50
5 51
18 52
14 53
7 54
7 55
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
89044
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
535446
0
0
0
0
0...

result:

ok 99913 numbers

Test #6:

score: 0
Wrong Answer
time: 854ms
memory: 234880kb

input:

199999 200000
1 2
2 3
2 4
3 5
4 6
4 7
3 8
8 9
6 10
9 11
11 12
11 13
10 14
10 15
14 16
15 17
16 18
17 19
19 20
18 21
17 22
19 23
19 24
21 25
22 26
24 27
23 28
24 29
26 30
30 31
28 32
31 33
33 34
30 35
35 36
34 37
33 38
35 39
37 40
39 41
37 42
39 43
40 44
41 45
45 46
43 47
45 48
48 49
45 50
47 51
48 5...

output:

0
63908
207701
15977
522576
15977
143793
152018
15977
0
36756
78843
36756
0
315372
10052804992
20779
78843
0
36756
36756
7665373505
62337
23361399106
3187120
62337
3742676532
546438
147024
103895
0
7880424170
253252
272541
449350
4081653
453517
314628
2267585
361886
18098891585
1438437
633130
449350...

result:

wrong answer 692nd numbers differ - expected: '395133727381', found: '390838760085'

Subtask #4:

score: 0
Wrong Answer

Test #7:

score: 10
Accepted
time: 1122ms
memory: 213136kb

input:

200000 199999
1 2
1 3
1 4
4 5
2 6
6 7
6 8
5 9
4 10
10 11
4 12
3 13
1 14
14 15
13 16
6 17
6 18
7 19
2 20
9 21
20 22
7 23
3 24
12 25
1 26
15 27
10 28
16 29
26 30
22 31
25 32
6 33
26 34
23 35
12 36
1 37
1 38
17 39
18 40
10 41
29 42
13 43
14 44
20 45
45 46
30 47
34 48
33 49
2 50
15 51
44 52
4 53
53 54
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 133520 numbers

Test #8:

score: 0
Wrong Answer
time: 835ms
memory: 239060kb

input:

199999 199999
1 2
1 3
2 4
2 5
2 6
6 7
4 8
4 9
5 10
8 11
9 12
11 13
13 14
11 15
15 16
14 17
14 18
15 19
19 20
19 21
21 22
21 23
23 24
22 25
23 26
26 27
27 28
24 29
25 30
27 31
27 32
31 33
31 34
33 35
32 36
32 37
37 38
38 39
38 40
36 41
38 42
38 43
42 44
41 45
42 46
44 47
43 48
47 49
49 50
50 51
50 52...

output:

0
81278
0
63687
63687
63687
63687
63687
63687
63687
63687
63687
63687
636870
63687
63687
144965
11254124900
636870
63687
144965
144965
63687
0
63687
63687
63687
4947438975
509496
318435
114304
63687
0
63687
105584
63687
0
16280833342
1009002621
105584
246008
105584
260692
105584
316752
311309
117234...

result:

wrong answer 505th numbers differ - expected: '186385406494', found: '182090439198'

Subtask #5:

score: 0
Wrong Answer

Test #9:

score: 10
Accepted
time: 1609ms
memory: 216600kb

input:

199999 200000
1 2
1 3
2 4
1 5
3 6
4 7
6 8
2 9
6 10
3 11
10 12
10 13
2 14
2 15
12 16
2 17
5 18
14 19
18 20
6 21
12 22
11 23
23 24
14 25
19 26
21 27
2 28
25 29
12 30
1 31
4 32
12 33
20 34
25 35
13 36
28 37
4 38
5 39
11 40
20 41
15 42
12 43
7 44
37 45
24 46
24 47
45 48
12 49
46 50
49 51
34 52
2 53
7 54...

output:

1857150
-106797
0
0
64612
0
1808864
0
1319630
0
13621832
1227363
15607376
2151610
4909784
2647098
1470564
0
-2570850
0
14130724
0
7145118
0
31967983
0
-639181
0
27143144
0
12122816
166580
0
0
-2801375
0
11387471
22813486
0
6006168
0
0
0
23241781
12394024
0
13950003
8070072
19017220
0
21145621
0
1431...

result:

ok 99967 numbers

Test #10:

score: 0
Wrong Answer
time: 985ms
memory: 239060kb

input:

200000 200000
1 2
2 3
3 4
1 5
2 6
6 7
7 8
4 9
5 10
9 11
7 12
10 13
9 14
13 15
13 16
12 17
15 18
15 19
19 20
18 21
19 22
22 23
20 24
23 25
25 26
25 27
23 28
25 29
28 30
30 31
30 32
28 33
31 34
32 35
35 36
32 37
34 38
37 39
35 40
37 41
39 42
41 43
40 44
44 45
42 46
42 47
47 48
44 49
46 50
50 51
50 52
...

output:

0
0
5843630
20392960
0
0
-903453097
-179420161
0
0
-1330737
0
-1154278269
-37153603
0
0
-2537810064
-5318685886
-1097999168
401558225
2883316
-3765521921
1544718973
124390
4918819215
5870207005
809430858
392406
12414884357
124390
112222
9272513996
0
62195
81431
124390
995995782
112222
985671590
1104...

result:

wrong answer 48th numbers differ - expected: '16961001844', found: '8371067252'

Subtask #6:

score: 0
Wrong Answer

Test #13:

score: 10
Accepted
time: 1593ms
memory: 214548kb

input:

199999 200000
1 2
1 3
3 4
4 5
5 6
5 7
6 8
7 9
9 10
5 11
4 12
6 13
6 14
14 15
5 16
9 17
9 18
3 19
6 20
9 21
5 22
4 23
6 24
22 25
20 26
8 27
27 28
1 29
22 30
28 31
13 32
8 33
20 34
9 35
6 36
18 37
9 38
28 39
34 40
35 41
4 42
18 43
33 44
39 45
6 46
36 47
8 48
20 49
24 50
38 51
22 52
8 53
47 54
6 55
34 ...

output:

0
0
0
0
0
0
0
0
0
-597754
0
0
0
113310
258283
0
0
6434569
0
258283
0
0
7749214
8481184
258283
197065
5494216
0
13981043
0
0
0
0
0
0
0
0
3224810
0
6758948
0
8832084
0
282018
14877927
0
418806
0
0
0
379295
0
-448225
0
340187
0
0
340187
0
340187
0
9742771
10496602
17197688
0
234958
0
234958
340187
1280...

result:

ok 132677 numbers

Test #14:

score: 0
Wrong Answer
time: 989ms
memory: 248144kb

input:

199999 200000
1 2
1 3
1 4
4 5
3 6
4 7
7 8
8 9
8 10
10 11
9 12
11 13
11 14
14 15
13 16
15 17
16 18
16 19
17 20
19 21
21 22
20 23
22 24
23 25
24 26
25 27
27 28
27 29
27 30
28 31
29 32
31 33
31 34
34 35
33 36
35 37
35 38
38 39
38 40
39 41
39 42
42 43
41 44
42 45
45 46
46 47
45 48
47 49
49 50
49 51
51 5...

output:

0
74311326
147063
1062566318
25407
680607564
49021
49021
-2696287941
-30708
147063
13552
-4727124378
98042
-1568058708
-4778331532
85148
-687534891
-7431
-7722399495
-2730026187
49021
85148
85148
49021
-116193
-4290439791
98042
-3726928035
106905
33565
113952
92528
160327
92528
11812375788
92528
157...

result:

wrong answer 9th numbers differ - expected: '1598679355', found: '-2696287941'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #2:

0%