QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#870473#3275. 小明的树lycc100 ✓1701ms61096kbC++203.9kb2025-01-25 16:30:472025-01-25 16:30:55

Judging History

This is the latest submission verdict.

  • [2025-01-25 16:30:55]
  • Judged
  • Verdict: 100
  • Time: 1701ms
  • Memory: 61096kb
  • [2025-01-25 16:30:47]
  • Submitted

answer

// Author: lycc
// 
// Problem: P8990 [北大集训 2021] 小明的树
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P8990
// Memory Limit: 512 MB
// Time Limit: 3000 ms

#include <bits/stdc++.h>
#define int long long
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define pb emplace_back
#define For(i, x, y) for (int i = (x); i <= (y); i ++)
#define rep(i, x, y) for (int i = (x); i >= (y); i --)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
#define ull unsigned long long
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mp make_pair
#define i128 __int128
#define db long double
#define vi vector< int >
#define mem(v, x) memset(v, x, sizeof(v))
#define A3 array< int, 3 >
#define A4 array< int, 4 >
#define vpii vector< pair< int, int > >
using namespace std;
mt19937_64 rnd(time(0));
template< typename T > void cmin(T &x, T y) { return x = min(x, y), void(); }
template< typename T > void cmax(T &x, T y) { return x = max(x, y), void(); }
int ksm(int x, int y, int p) {
    int v = 1; x %= p;
    while (y) v = 1ll * v * ((y & 1) ? x : 1) % p, x = 1ll * x * x % p, y >>= 1;
    return (v % p + p) % p;
}
void file() {
	freopen("1.in", "r", stdin);
	freopen("1.out", "w", stdout);
	return;
}
bool MemoryST;
const int N = 5e5 + 5;
const int mod = 998244353;
const long long INF = 1e18;
const int base = 13131;
using ll = long long;
int n, m, a[N];
struct node {
	int minn, sum, cnt;
} t[N * 4];
node operator + (node i, node j) {
	node res; res.minn = min(i.minn, j.minn); 
	res.sum = res.cnt = 0;
	if (res.minn == i.minn) res.sum = i.sum, res.cnt = i.cnt;
	if (res.minn == j.minn) res.sum += j.sum, res.cnt += j.cnt;
	return res;
}
struct Tag {
	int tag1, tag2;
} tag[N * 4];
node operator + (node i, Tag j) {
	node res; res.cnt = i.cnt;
	res.minn = i.minn + j.tag1;
	res.sum = i.sum + res.cnt * j.tag2;
	return res;
}
Tag operator + (Tag i, Tag j) {
	Tag res; res.tag1 = i.tag1 + j.tag1;
	res.tag2 = i.tag2 + j.tag2;
	return res;
}
void pushdown(int p) {
	t[ls] = t[ls] + tag[p];
	t[rs] = t[rs] + tag[p];
	tag[ls] = tag[ls] + tag[p];
	tag[rs] = tag[rs] + tag[p];
	tag[p] = Tag{0, 0};
	return;
}
void build(int l, int r, int p) {
	if (l == r) return t[p].minn = n - l, t[p].cnt = 1, void();
	int mid = (l + r) >> 1;
	build(l, mid, ls); build(mid + 1, r, rs);
	t[p] = t[ls] + t[rs];
	return;
}
void modify(int l, int r, int x, int y, int p, Tag v) {
	if (x > y) return;
	if (l >= x && r <= y) return t[p] = t[p] + v, tag[p] = tag[p] + v, void();
	int mid = (l + r) >> 1; pushdown(p);
	if (x <= mid) modify(l, mid, x, y, ls, v);
	if (y > mid) modify(mid + 1, r, x, y, rs, v);
	t[p] = t[ls] + t[rs];
	return;
}
void add(int x, int y) {
	modify(1, n - 1, min(a[x], a[y]), max(a[x], a[y]) - 1, 1, Tag{0, 1});
	modify(1, n - 1, 1, min(a[x], a[y]) - 1, 1, Tag{-1, 0});
	return;
}
void del(int x, int y) {
	modify(1, n - 1, min(a[x], a[y]), max(a[x], a[y]) - 1, 1, Tag{0, -1});
	modify(1, n - 1, 1, min(a[x], a[y]) - 1, 1, Tag{1, 0});	
	return;
}
void Main() {
	cin >> n >> m; vpii e; a[1] = n;
	For (i, 1, n - 1) {
		int x, y; cin >> x >> y;
		e.pb(x, y);
	}
	For (i, 1, n - 1) {
		int x; cin >> x;
		a[x] = i;
	}
	build(1, n - 1, 1);
	for (auto [x, y] : e) add(x, y);
	if (t[1].minn == 1) cout << t[1].sum << '\n';
	else cout << "0\n";
	while (m --) {
		int x, y; cin >> x >> y;
		del(x, y);
		cin >> x >> y;
		add(x, y);
		if (t[1].minn == 1) cout << t[1].sum << '\n';
		else cout << "0\n";
	}
    return;
}
bool MemoryED;
signed main() {
	// file();
    ios :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cerr << fixed << setprecision(6) << (&MemoryST - &MemoryED) / 1048576.0 << "MB\n";
    int TESTCNT = 1;
    // cin >> TESTCNT;
    while (TESTCNT --) Main();
    cerr << endl << 1e3 * clock() / CLOCKS_PER_SEC << "ms"; 
    return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 547ms
memory: 59324kb

input:

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

output:

237

result:

ok 1 number(s): "237"

Subtask #2:

score: 20
Accepted

Test #2:

score: 20
Accepted
time: 14ms
memory: 8600kb

input:

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

output:

16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
...

result:

ok 8001 numbers

Test #3:

score: 20
Accepted
time: 13ms
memory: 8808kb

input:

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

output:

12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
...

result:

ok 8001 numbers

Subtask #3:

score: 70
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #4:

score: 70
Accepted
time: 1685ms
memory: 60184kb

input:

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

output:

262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
262
...

result:

ok 500001 numbers

Test #5:

score: 70
Accepted
time: 1689ms
memory: 60156kb

input:

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

output:

415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
415
...

result:

ok 500001 numbers

Test #6:

score: 70
Accepted
time: 1669ms
memory: 60316kb

input:

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

output:

57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
57
...

result:

ok 500001 numbers

Test #7:

score: 70
Accepted
time: 1646ms
memory: 59256kb

input:

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

output:

198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
...

result:

ok 500001 numbers

Test #8:

score: 70
Accepted
time: 1654ms
memory: 59352kb

input:

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

output:

290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
290
...

result:

ok 500001 numbers

Test #9:

score: 70
Accepted
time: 1701ms
memory: 60696kb

input:

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

output:

40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
...

result:

ok 500001 numbers

Test #10:

score: 70
Accepted
time: 1677ms
memory: 61096kb

input:

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

output:

20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
...

result:

ok 500001 numbers