Submission #999536

# Submission time Handle Problem Language Result Execution time Memory
999536 2024-06-15T17:43:20 Z aykhn Sumtree (INOI20_sumtree) C++17
100 / 100
1220 ms 154136 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MXN = 1e6 + 5;
const int LOG = 20;
const int mod = 1e9 + 7;

struct FindUp
{
	int sz;
	vector<int> st;
	void init(int n)
	{
		sz = n;
		st.assign((n + 1) << 2, 0);
	}
	void add(int l, int r, int x, int lx, int rx, int val)
	{
		if (l > rx || r < lx) return;
		if (l >= lx && r <= rx)
		{
			st[x] += val;
			return;
		}
		int mid = (l + r) >> 1;
		add(l, mid, 2*x, lx, rx, val);
		add(mid + 1, r, 2*x + 1, lx, rx, val);
	}
	int get(int l, int r, int x, int ind)
	{
		if (l == r) return st[x];
		int mid = (l + r) >> 1;
		if (ind <= mid) return st[x] + get(l, mid, 2*x, ind);
		else return st[x] + get(mid + 1, r, 2*x + 1, ind);
	}
};
struct FindVAL
{
	int sz;
	vector<int> st;
	void init(int n)
	{
		sz = n;
		st.assign((n + 1) << 2, 0);
	}
	void add(int l, int r, int x, int ind, int val)
	{
		if (l == r)
		{
			st[x] += val;
			return;
		}
		int mid = (l + r) >> 1;
		if (ind <= mid) add(l, mid, 2*x, ind, val);
		else add(mid + 1, r, 2*x + 1, ind, val);
		st[x] = st[2*x] + st[2*x + 1];
	}
	int get(int l, int r, int x, int lx, int rx)
	{
		if (l > rx || r < lx) return 0;
		if (l >= lx && r <= rx) return st[x];
		int mid = (l + r) >> 1;
		return get(l, mid, 2*x, lx, rx) + get(mid + 1, r, 2*x + 1, lx, rx);
	}
};

int n, r;
vector<int> adj[MXN];
int f[MXN], tag[MXN];
int p[LOG][MXN], sz[MXN];
int in[MXN], out[MXN], tim;
FindUp stup;
FindVAL stsz, sts;
int res = 1, z = 0;

int pw(int a, int b, int c)
{
	a %= c;
	int res = 1;
	while (b)
	{
		if (b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return res;
}

int nck(int n, int k)
{
	if (min(n, k) < 0 || k > n) return 0;
	return (f[n] * pw((f[n - k] * f[k]) % mod, mod - 2, mod)) % mod;
}

void dfs(int a)
{
	sz[a] = 1;
	in[a] = ++tim;
	for (int &v : adj[a])
	{
		if (v == p[0][a]) continue;
		p[0][v] = a;
		dfs(v);
		sz[a] += sz[v];
 	}
	out[a] = tim;
}

int getco(int u)
{
	// cout << u << ' ' << sz[u] << ' ' << stsz.get(1, n, 1, in[u] + 1, out[u]) << ' ' << tag[u] << ' ' << sts.get(1, n, 1, in[u] + 1, out[u]) << '\n';
	return nck(sz[u] - stsz.get(1, n, 1, in[u] + 1, out[u]) - 1 + tag[u] - sts.get(1, n, 1, in[u] + 1, out[u]), tag[u] - sts.get(1, n, 1, in[u] + 1, out[u]));
}

int invalid(int u)
{
	return (sts.get(1, n, 1, in[u] + 1, out[u]) > tag[u]);
}

int UP(int u)
{
	int x = stup.get(1, n, 1, in[u]);
	for (int i = LOG - 1; i >= 0; i--)
	{
		if (stup.get(1, n, 1, in[p[i][u]]) == x) u = p[i][u];
	}
	return u;
}


signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	f[0] = 1;
	for (int i = 1; i < MXN; i++) 
	{
		f[i] = (f[i - 1] * i) % mod;
		tag[i] = -1;
	}
	cin >> n >> r;
	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	int q;
	cin >> q;
	tag[1] = r;
	p[0][1] = 1;
	dfs(1);
	for (int i = 1; i < LOG; i++) for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
	stup.init(n), stsz.init(n), sts.init(n);
	stup.add(1, n, 1, 1, n, 1);
	stsz.add(1, n, 1, in[1], 1);
	res = getco(1);
	cout << res << '\n';
	while (q--)
	{
		int t;
		cin >> t;
		if (t == 1)
		{
			int u, v;
			cin >> u >> v;
			int P = UP(u);
			int S = stsz.get(1, n, 1, in[u] + 1, out[u]);
			tag[u] = v;
			int ff = invalid(P);
			if (!ff) res = (res * pw(getco(P), mod - 2, mod)) % mod;
			else z--;
			stsz.add(1, n, 1, in[u], -S + sz[u]);
			stsz.add(1, n, 1, in[P], S - sz[u]);
			
			S = sts.get(1, n, 1, in[u] + 1, out[u]);
			sts.add(1, n, 1, in[u], -S + tag[u]);
			sts.add(1, n, 1, in[P], S - tag[u]);
			ff = invalid(P);
			if (!ff) res = (res * getco(P)) % mod;
			else z++;
			ff = invalid(u);
			if (!ff) res = (res * getco(u)) % mod;
			else z++;
			stup.add(1, n, 1, in[u], out[u], 1);
		}
		else
		{
			int u;
			cin >> u;
			stup.add(1, n, 1, in[u], out[u], -1);
			int P = UP(u);
			int S = stsz.get(1, n, 1, in[u] + 1, out[u]);
			int ff = invalid(P);
			if (!ff) res = (res * pw(getco(P), mod - 2, mod)) % mod;
			else z--;
			ff = invalid(u);
			if (!ff) res = (res * pw(getco(u), mod - 2, mod)) % mod;
			else z--;
			stsz.add(1, n, 1, in[u], S - sz[u]);
			stsz.add(1, n, 1, in[P], -S + sz[u]);

			S = sts.get(1, n, 1, in[u] + 1, out[u]);
			sts.add(1, n, 1, in[u], S - tag[u]);
			sts.add(1, n, 1, in[P], -S + tag[u]);
			ff = invalid(P);
			if (!ff) res = (res * getco(P)) % mod;
			else z++;

			tag[u] = -1;
		}
		if (z) cout << 0 << '\n';
		else cout << res << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 95 ms 146768 KB Output is correct
2 Correct 94 ms 147772 KB Output is correct
3 Correct 97 ms 146768 KB Output is correct
4 Correct 101 ms 147772 KB Output is correct
5 Correct 99 ms 143444 KB Output is correct
6 Correct 16 ms 88668 KB Output is correct
7 Correct 16 ms 88500 KB Output is correct
8 Correct 16 ms 88576 KB Output is correct
9 Correct 93 ms 142160 KB Output is correct
10 Correct 102 ms 143116 KB Output is correct
11 Correct 88 ms 142212 KB Output is correct
12 Correct 82 ms 139408 KB Output is correct
13 Correct 81 ms 141908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 85848 KB Output is correct
2 Correct 14 ms 86004 KB Output is correct
3 Correct 14 ms 85976 KB Output is correct
4 Correct 15 ms 85852 KB Output is correct
5 Correct 17 ms 86108 KB Output is correct
6 Correct 22 ms 86364 KB Output is correct
7 Correct 19 ms 86364 KB Output is correct
8 Correct 20 ms 86364 KB Output is correct
9 Correct 25 ms 88412 KB Output is correct
10 Correct 24 ms 88508 KB Output is correct
11 Correct 25 ms 88660 KB Output is correct
12 Correct 16 ms 88412 KB Output is correct
13 Correct 24 ms 88664 KB Output is correct
14 Correct 24 ms 88412 KB Output is correct
15 Correct 24 ms 88664 KB Output is correct
16 Correct 24 ms 88536 KB Output is correct
17 Correct 23 ms 88412 KB Output is correct
18 Correct 21 ms 86108 KB Output is correct
19 Correct 22 ms 88412 KB Output is correct
20 Correct 19 ms 86364 KB Output is correct
21 Correct 19 ms 86360 KB Output is correct
22 Correct 15 ms 85980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 148736 KB Output is correct
2 Correct 239 ms 148976 KB Output is correct
3 Correct 133 ms 149076 KB Output is correct
4 Correct 291 ms 149840 KB Output is correct
5 Correct 557 ms 148052 KB Output is correct
6 Correct 18 ms 88668 KB Output is correct
7 Correct 18 ms 88588 KB Output is correct
8 Correct 19 ms 88412 KB Output is correct
9 Correct 536 ms 142824 KB Output is correct
10 Correct 386 ms 141684 KB Output is correct
11 Correct 340 ms 142168 KB Output is correct
12 Correct 327 ms 141396 KB Output is correct
13 Correct 470 ms 153940 KB Output is correct
14 Correct 539 ms 153936 KB Output is correct
15 Correct 484 ms 153880 KB Output is correct
16 Correct 493 ms 154136 KB Output is correct
17 Correct 489 ms 153184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 711 ms 144716 KB Output is correct
2 Correct 674 ms 147136 KB Output is correct
3 Correct 643 ms 146284 KB Output is correct
4 Correct 631 ms 146256 KB Output is correct
5 Correct 651 ms 143152 KB Output is correct
6 Correct 636 ms 147028 KB Output is correct
7 Correct 570 ms 123200 KB Output is correct
8 Correct 605 ms 124756 KB Output is correct
9 Correct 693 ms 147304 KB Output is correct
10 Correct 606 ms 146420 KB Output is correct
11 Correct 593 ms 147344 KB Output is correct
12 Correct 527 ms 125012 KB Output is correct
13 Correct 307 ms 121776 KB Output is correct
14 Correct 384 ms 121940 KB Output is correct
15 Correct 444 ms 121888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 146768 KB Output is correct
2 Correct 94 ms 147772 KB Output is correct
3 Correct 97 ms 146768 KB Output is correct
4 Correct 101 ms 147772 KB Output is correct
5 Correct 99 ms 143444 KB Output is correct
6 Correct 16 ms 88668 KB Output is correct
7 Correct 16 ms 88500 KB Output is correct
8 Correct 16 ms 88576 KB Output is correct
9 Correct 93 ms 142160 KB Output is correct
10 Correct 102 ms 143116 KB Output is correct
11 Correct 88 ms 142212 KB Output is correct
12 Correct 82 ms 139408 KB Output is correct
13 Correct 81 ms 141908 KB Output is correct
14 Correct 14 ms 85848 KB Output is correct
15 Correct 14 ms 86004 KB Output is correct
16 Correct 14 ms 85976 KB Output is correct
17 Correct 15 ms 85852 KB Output is correct
18 Correct 17 ms 86108 KB Output is correct
19 Correct 22 ms 86364 KB Output is correct
20 Correct 19 ms 86364 KB Output is correct
21 Correct 20 ms 86364 KB Output is correct
22 Correct 25 ms 88412 KB Output is correct
23 Correct 24 ms 88508 KB Output is correct
24 Correct 25 ms 88660 KB Output is correct
25 Correct 16 ms 88412 KB Output is correct
26 Correct 24 ms 88664 KB Output is correct
27 Correct 24 ms 88412 KB Output is correct
28 Correct 24 ms 88664 KB Output is correct
29 Correct 24 ms 88536 KB Output is correct
30 Correct 23 ms 88412 KB Output is correct
31 Correct 21 ms 86108 KB Output is correct
32 Correct 22 ms 88412 KB Output is correct
33 Correct 19 ms 86364 KB Output is correct
34 Correct 19 ms 86360 KB Output is correct
35 Correct 15 ms 85980 KB Output is correct
36 Correct 129 ms 148736 KB Output is correct
37 Correct 239 ms 148976 KB Output is correct
38 Correct 133 ms 149076 KB Output is correct
39 Correct 291 ms 149840 KB Output is correct
40 Correct 557 ms 148052 KB Output is correct
41 Correct 18 ms 88668 KB Output is correct
42 Correct 18 ms 88588 KB Output is correct
43 Correct 19 ms 88412 KB Output is correct
44 Correct 536 ms 142824 KB Output is correct
45 Correct 386 ms 141684 KB Output is correct
46 Correct 340 ms 142168 KB Output is correct
47 Correct 327 ms 141396 KB Output is correct
48 Correct 470 ms 153940 KB Output is correct
49 Correct 539 ms 153936 KB Output is correct
50 Correct 484 ms 153880 KB Output is correct
51 Correct 493 ms 154136 KB Output is correct
52 Correct 489 ms 153184 KB Output is correct
53 Correct 711 ms 144716 KB Output is correct
54 Correct 674 ms 147136 KB Output is correct
55 Correct 643 ms 146284 KB Output is correct
56 Correct 631 ms 146256 KB Output is correct
57 Correct 651 ms 143152 KB Output is correct
58 Correct 636 ms 147028 KB Output is correct
59 Correct 570 ms 123200 KB Output is correct
60 Correct 605 ms 124756 KB Output is correct
61 Correct 693 ms 147304 KB Output is correct
62 Correct 606 ms 146420 KB Output is correct
63 Correct 593 ms 147344 KB Output is correct
64 Correct 527 ms 125012 KB Output is correct
65 Correct 307 ms 121776 KB Output is correct
66 Correct 384 ms 121940 KB Output is correct
67 Correct 444 ms 121888 KB Output is correct
68 Correct 17 ms 85852 KB Output is correct
69 Correct 14 ms 86040 KB Output is correct
70 Correct 892 ms 142388 KB Output is correct
71 Correct 1026 ms 142420 KB Output is correct
72 Correct 1112 ms 142376 KB Output is correct
73 Correct 1031 ms 121920 KB Output is correct
74 Correct 1220 ms 122996 KB Output is correct
75 Correct 1122 ms 117884 KB Output is correct
76 Correct 626 ms 118056 KB Output is correct
77 Correct 748 ms 117072 KB Output is correct
78 Correct 862 ms 117340 KB Output is correct
79 Correct 1093 ms 120672 KB Output is correct
80 Correct 1150 ms 117300 KB Output is correct
81 Correct 1169 ms 119536 KB Output is correct
82 Correct 417 ms 132104 KB Output is correct
83 Correct 597 ms 137748 KB Output is correct
84 Correct 585 ms 143700 KB Output is correct
85 Correct 537 ms 153000 KB Output is correct