Submission #624199

# Submission time Handle Problem Language Result Execution time Memory
624199 2022-08-07T12:30:29 Z Arinoor Sumtree (INOI20_sumtree) C++17
100 / 100
617 ms 65628 KB
#include <bits/stdc++.h>
using namespace std;

#define pb				push_back
#define mp				make_pair
#define fi				first
#define se				second
#define all(x)			x.begin(), x.end()
#define bug(x)			cout << #x << " : " << x << '\n'
#define ios				ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 2e5 + 10;
const int maxlg = 19;
const int maxf = 1e6 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;

int n, fact[maxf], invfact[maxf];
int val[maxn], tag[maxn];
int par[maxn][maxlg], sz[maxn], st[maxn], ed[maxn], tme = 1;
ll fenC[maxn], fenS[maxn], fenP[maxn];
vector<int> G[maxn];

int add(int a, int b){
	int c = a + b;
	if(c >= mod) c -= mod;
	if(c < 0) c += mod;
	return c;
}

int mul(int a, int b){
	return 1ll * a * b % mod;
}

int pwr(int a, int b){
	int res = 1;
	for(; b; b >>= 1, a = mul(a, a)) if(b & 1) res = mul(res, a);
	return res;
}

int inv(int a){
	return pwr(a, mod - 2);
}

int C(int a, ll b){
	if(a > b)
		return 0;
	return mul(fact[b], mul(invfact[a], invfact[b - a]));
}

void update_fact(){
	fact[0] = 1;
	for(int i = 1; i < maxf; i ++)
		fact[i] = mul(fact[i - 1], i);
	invfact[maxf - 1] = inv(fact[maxf - 1]);
	for(int i = maxf - 2; ~i; i --)
		invfact[i] = mul(invfact[i + 1], i + 1);
}

void add(ll *fen, int i, ll x){
	for(; i <= n; i += i & -i)
		fen[i] += x;
}

ll get(ll *fen, int i){
	ll res = 0;
	for(; i; i -= i & -i)
		res += fen[i];
	return res;
}

ll get(ll *fen, int l, int r){
	return get(fen, r) - get(fen, l);
}

void dfs(int v, int p = 0){
	par[v][0] = p;
	for(int i = 1; i < maxlg and par[v][i - 1]; i ++)
		par[v][i] = par[par[v][i - 1]][i - 1];
	st[v] = tme;
	sz[v] = 1;
	for(int u : G[v]){
		if(u ^ p){
			tme ++;
			dfs(u, v);
			sz[v] += sz[u];
		}
	}
	ed[v] = tme;
}

int get_val(int v){
	int cnt = sz[v] - get(fenC, st[v], ed[v]);
	ll sum = tag[v] - get(fenS, st[v], ed[v]);
	return C(cnt - 1, cnt + sum - 1);
}

int find(int v){
	int x = get(fenP, st[v]);
	for(int i = maxlg - 1; ~i; i --){
		int u = par[v][i];
		if(!u) continue;
		if(get(fenP, st[u]) == x)
			v = u;
	}
	return par[v][0];
}

int main(){
	ios;
	update_fact();
	int r;
	cin >> n >> r;
	for(int i = 0; i < n - 1; i ++){
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs(1);
	memset(tag, -1, sizeof tag);
	tag[1] = r;
	add(fenP, st[1] + 1, 1); add(fenP, ed[1] + 1, -1);
	fill_n(val, maxn, 1);
	int ans = val[1] = get_val(1);
	cout << ans << '\n';
	int q; cin >> q;
	int Cnt = 0;
	while(q--){
		int t, v;
		cin >> t >> v;
		int p = find(v);
		if(t == 1){
			cin >> tag[v];
			int cnt = sz[v] - get(fenC, st[v], ed[v]);
			ll sum = tag[v] - get(fenS, st[v], ed[v]);
			add(fenC, st[v], cnt);
			add(fenS, st[v], sum);
			add(fenC, st[p], -cnt);
			add(fenS, st[p], -sum);
			add(fenP, st[v] + 1, 1); add(fenP, ed[v] + 1, -1);
			val[v] = get_val(v);
			if(val[v])
				ans = mul(ans, val[v]);
			else
				Cnt ++;
			if(inv(val[p]))
				ans = mul(ans, inv(val[p]));
			else
				Cnt --;
			val[p] = get_val(p);
			if(val[p])
				ans = mul(ans, val[p]);
			else
				Cnt ++;
		}
		else{
			int cnt = sz[v] - get(fenC, st[v], ed[v]);
			ll sum = tag[v] - get(fenS, st[v], ed[v]);
			add(fenC, st[v], -cnt);
			add(fenS, st[v], -sum);
			add(fenC, st[p], cnt);
			add(fenS, st[p], sum);
			add(fenP, st[v] + 1, -1); add(fenP, ed[v] + 1, 1);
			if(inv(val[v]))
				ans = mul(ans, inv(val[v]));
			else
				Cnt --;
			val[v] = 1;
			if(inv(val[p]))
				ans = mul(ans, inv(val[p]));
			else
				Cnt --;
			val[p] = get_val(p);
			if(val[p])
				ans = mul(ans, val[p]);
			else
				Cnt ++;
			tag[v] = -1;
		}
		if(Cnt)
			cout << "0\n";
		else
			cout << ans << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 152 ms 46028 KB Output is correct
2 Correct 149 ms 46032 KB Output is correct
3 Correct 176 ms 46028 KB Output is correct
4 Correct 196 ms 45944 KB Output is correct
5 Correct 139 ms 42084 KB Output is correct
6 Correct 21 ms 15052 KB Output is correct
7 Correct 20 ms 14708 KB Output is correct
8 Correct 20 ms 14792 KB Output is correct
9 Correct 149 ms 38392 KB Output is correct
10 Correct 148 ms 38236 KB Output is correct
11 Correct 143 ms 38392 KB Output is correct
12 Correct 122 ms 37220 KB Output is correct
13 Correct 134 ms 43628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 14408 KB Output is correct
2 Correct 18 ms 14420 KB Output is correct
3 Correct 21 ms 14420 KB Output is correct
4 Correct 24 ms 14420 KB Output is correct
5 Correct 23 ms 14504 KB Output is correct
6 Correct 24 ms 14676 KB Output is correct
7 Correct 21 ms 14788 KB Output is correct
8 Correct 21 ms 14740 KB Output is correct
9 Correct 22 ms 14892 KB Output is correct
10 Correct 23 ms 14936 KB Output is correct
11 Correct 23 ms 14944 KB Output is correct
12 Correct 20 ms 14924 KB Output is correct
13 Correct 24 ms 14920 KB Output is correct
14 Correct 23 ms 14972 KB Output is correct
15 Correct 22 ms 15080 KB Output is correct
16 Correct 23 ms 14940 KB Output is correct
17 Correct 23 ms 14904 KB Output is correct
18 Correct 22 ms 14668 KB Output is correct
19 Correct 22 ms 14908 KB Output is correct
20 Correct 22 ms 14676 KB Output is correct
21 Correct 25 ms 14768 KB Output is correct
22 Correct 21 ms 14448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 50000 KB Output is correct
2 Correct 199 ms 52704 KB Output is correct
3 Correct 162 ms 52920 KB Output is correct
4 Correct 246 ms 53780 KB Output is correct
5 Correct 360 ms 51404 KB Output is correct
6 Correct 21 ms 15060 KB Output is correct
7 Correct 21 ms 14928 KB Output is correct
8 Correct 22 ms 14968 KB Output is correct
9 Correct 318 ms 47220 KB Output is correct
10 Correct 276 ms 46684 KB Output is correct
11 Correct 251 ms 46620 KB Output is correct
12 Correct 252 ms 46616 KB Output is correct
13 Correct 272 ms 65564 KB Output is correct
14 Correct 308 ms 65604 KB Output is correct
15 Correct 282 ms 65604 KB Output is correct
16 Correct 336 ms 65628 KB Output is correct
17 Correct 283 ms 65612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 45292 KB Output is correct
2 Correct 411 ms 49132 KB Output is correct
3 Correct 387 ms 49152 KB Output is correct
4 Correct 401 ms 49136 KB Output is correct
5 Correct 467 ms 47564 KB Output is correct
6 Correct 351 ms 49104 KB Output is correct
7 Correct 289 ms 33416 KB Output is correct
8 Correct 295 ms 33668 KB Output is correct
9 Correct 397 ms 49208 KB Output is correct
10 Correct 364 ms 49256 KB Output is correct
11 Correct 405 ms 49332 KB Output is correct
12 Correct 282 ms 33672 KB Output is correct
13 Correct 221 ms 30548 KB Output is correct
14 Correct 242 ms 32164 KB Output is correct
15 Correct 294 ms 32316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 46028 KB Output is correct
2 Correct 149 ms 46032 KB Output is correct
3 Correct 176 ms 46028 KB Output is correct
4 Correct 196 ms 45944 KB Output is correct
5 Correct 139 ms 42084 KB Output is correct
6 Correct 21 ms 15052 KB Output is correct
7 Correct 20 ms 14708 KB Output is correct
8 Correct 20 ms 14792 KB Output is correct
9 Correct 149 ms 38392 KB Output is correct
10 Correct 148 ms 38236 KB Output is correct
11 Correct 143 ms 38392 KB Output is correct
12 Correct 122 ms 37220 KB Output is correct
13 Correct 134 ms 43628 KB Output is correct
14 Correct 19 ms 14408 KB Output is correct
15 Correct 18 ms 14420 KB Output is correct
16 Correct 21 ms 14420 KB Output is correct
17 Correct 24 ms 14420 KB Output is correct
18 Correct 23 ms 14504 KB Output is correct
19 Correct 24 ms 14676 KB Output is correct
20 Correct 21 ms 14788 KB Output is correct
21 Correct 21 ms 14740 KB Output is correct
22 Correct 22 ms 14892 KB Output is correct
23 Correct 23 ms 14936 KB Output is correct
24 Correct 23 ms 14944 KB Output is correct
25 Correct 20 ms 14924 KB Output is correct
26 Correct 24 ms 14920 KB Output is correct
27 Correct 23 ms 14972 KB Output is correct
28 Correct 22 ms 15080 KB Output is correct
29 Correct 23 ms 14940 KB Output is correct
30 Correct 23 ms 14904 KB Output is correct
31 Correct 22 ms 14668 KB Output is correct
32 Correct 22 ms 14908 KB Output is correct
33 Correct 22 ms 14676 KB Output is correct
34 Correct 25 ms 14768 KB Output is correct
35 Correct 21 ms 14448 KB Output is correct
36 Correct 170 ms 50000 KB Output is correct
37 Correct 199 ms 52704 KB Output is correct
38 Correct 162 ms 52920 KB Output is correct
39 Correct 246 ms 53780 KB Output is correct
40 Correct 360 ms 51404 KB Output is correct
41 Correct 21 ms 15060 KB Output is correct
42 Correct 21 ms 14928 KB Output is correct
43 Correct 22 ms 14968 KB Output is correct
44 Correct 318 ms 47220 KB Output is correct
45 Correct 276 ms 46684 KB Output is correct
46 Correct 251 ms 46620 KB Output is correct
47 Correct 252 ms 46616 KB Output is correct
48 Correct 272 ms 65564 KB Output is correct
49 Correct 308 ms 65604 KB Output is correct
50 Correct 282 ms 65604 KB Output is correct
51 Correct 336 ms 65628 KB Output is correct
52 Correct 283 ms 65612 KB Output is correct
53 Correct 403 ms 45292 KB Output is correct
54 Correct 411 ms 49132 KB Output is correct
55 Correct 387 ms 49152 KB Output is correct
56 Correct 401 ms 49136 KB Output is correct
57 Correct 467 ms 47564 KB Output is correct
58 Correct 351 ms 49104 KB Output is correct
59 Correct 289 ms 33416 KB Output is correct
60 Correct 295 ms 33668 KB Output is correct
61 Correct 397 ms 49208 KB Output is correct
62 Correct 364 ms 49256 KB Output is correct
63 Correct 405 ms 49332 KB Output is correct
64 Correct 282 ms 33672 KB Output is correct
65 Correct 221 ms 30548 KB Output is correct
66 Correct 242 ms 32164 KB Output is correct
67 Correct 294 ms 32316 KB Output is correct
68 Correct 19 ms 14420 KB Output is correct
69 Correct 19 ms 14392 KB Output is correct
70 Correct 542 ms 57148 KB Output is correct
71 Correct 528 ms 57108 KB Output is correct
72 Correct 592 ms 57072 KB Output is correct
73 Correct 567 ms 57104 KB Output is correct
74 Correct 617 ms 57096 KB Output is correct
75 Correct 582 ms 53268 KB Output is correct
76 Correct 391 ms 49228 KB Output is correct
77 Correct 383 ms 49156 KB Output is correct
78 Correct 427 ms 48076 KB Output is correct
79 Correct 541 ms 53104 KB Output is correct
80 Correct 497 ms 51436 KB Output is correct
81 Correct 543 ms 52732 KB Output is correct
82 Correct 286 ms 45436 KB Output is correct
83 Correct 336 ms 52284 KB Output is correct
84 Correct 425 ms 51576 KB Output is correct
85 Correct 391 ms 51664 KB Output is correct