Submission #470159

# Submission time Handle Problem Language Result Execution time Memory
470159 2021-09-03T07:02:24 Z Sohsoh84 Sumtree (INOI20_sumtree) C++14
50 / 100
721 ms 98664 KB
// orz
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)                      (x).begin(),(x).end()
#define X                           first
#define Y                           second
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;
const ll MOD = 1e9 + 7;
const ll LOG = 20;

struct Fenwick {
	int fen[MAXN];
	Fenwick() {}

	inline void add(int ind, int val) {
		for (++ind; ind < MAXN; ind += ind & -ind)
			fen[ind] += val;
	}

	inline int query(int ind) {
		int ans = 0;
		for (++ind; ind > 0; ind -= ind & -ind)
			ans += fen[ind];
		return ans;
	}

	inline void add(int l, int r, int val) {
		if (r < l) return;
		add(l, val);
		add(r + 1, -val);
	}


	inline int query(int l, int r) {
		if (r < l) return 0;
		return query(r) - query(l - 1);
	}

	inline void update(int ind, int val) {
		val -= query(ind, ind);
		add(ind, val);
	}
};


inline ll poww(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if (b & 1) ans = ans * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}

	return ans;
}

inline ll Inv(ll a) {
	return poww(a, MOD - 2);
}

// R == 0 ?

int n, r, q, sub_g[MAXN], par[MAXN][LOG],
    tin[MAXN], tout[MAXN], T, H[MAXN], R[MAXN];
ll fact[MAXN], inv[MAXN], F[MAXN], ans = 1;
vector<int> adj[MAXN];
Fenwick s_fen, t_fen, p_fen;

void dfs(int v, int p) {
	sub_g[v] = 1;
	par[v][0] = p;
	H[v] = H[p] + 1;
	tin[v] = ++T;

	for (int u : adj[v])
		if (u != p)
			dfs(u, v), sub_g[v] += sub_g[u];
	tout[v] = T;
}

inline ll C(ll k, ll n) {
	if (k < 0 || k > n) return 0;
	return fact[n] * inv[k] % MOD * inv[n - k] % MOD;
}

inline void update(int ind, ll val) {
	ans = ans * val % MOD * Inv(F[ind]) % MOD;
	F[ind] = val;
}

inline int Par(int v) {
	int t = p_fen.query(tin[v]);
	for (int i = LOG - 1; i >= 0; i--)
		if (p_fen.query(tin[par[v][i]]) == t)
			v = par[v][i];
	return par[v][0];
}

inline void add(int v, int r) {
	R[v] = r;
	p_fen.add(tin[v] + 1, tout[v], 1);
	r -= t_fen.query(tin[v] + 1, tout[v]);
	t_fen.add(tin[v], r);
	
	int s = sub_g[v] - s_fen.query(tin[v] + 1, tout[v]);
	s_fen.add(tin[v], s);
	
	update(v, C(s - 1, s + r - 1));
}

inline void recalc(int v) {
	int r = R[v];
	r -= t_fen.query(tin[v] + 1, tout[v]);
	t_fen.update(tin[v], r);
	
	int s = sub_g[v] - s_fen.query(tin[v] + 1, tout[v]);
	s_fen.update(tin[v], s);
	
	update(v, C(s - 1, s + r - 1));
}

inline void remove(int v) {
	p_fen.add(tin[v] + 1, tout[v], -1);
	s_fen.update(tin[v], 0);
	t_fen.update(tin[v], 0);
	R[v] = 1;
	update(v, 1);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	fact[0] = inv[0] = 1;
	for (int i = 1; i < MAXN; i++) 
		fact[i] = fact[i - 1] * i % MOD, inv[i] = Inv(fact[i]);
	fill(F, F + MAXN, 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);	
	}


	dfs(1, 0);
	for (int i = 1; i < LOG; i++)
		for (int v = 1; v <= n; v++)
			par[v][i] = par[par[v][i - 1]][i - 1];
	
	add(1, r);

	cout << ans << endl;
	cin >> q;
	while (q--) {
		int c;
		cin >> c;
		if (c == 1) {
			int v, u, p;
			cin >> v >> u;
			add(v, u);
			p = Par(v);
			recalc(p);
		} else {
			int v, p;
			cin >> v;
			p = Par(v);
		
			remove(v);
			recalc(p);
		}

		cout << ans << endl;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 349 ms 80372 KB Output is correct
2 Correct 372 ms 80332 KB Output is correct
3 Correct 344 ms 80400 KB Output is correct
4 Correct 379 ms 80428 KB Output is correct
5 Correct 329 ms 76336 KB Output is correct
6 Correct 180 ms 47988 KB Output is correct
7 Correct 180 ms 47812 KB Output is correct
8 Correct 176 ms 47828 KB Output is correct
9 Correct 343 ms 72672 KB Output is correct
10 Correct 346 ms 72800 KB Output is correct
11 Correct 362 ms 72664 KB Output is correct
12 Correct 329 ms 71364 KB Output is correct
13 Correct 319 ms 77912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 47332 KB Output is correct
2 Correct 177 ms 47424 KB Output is correct
3 Correct 178 ms 47452 KB Output is correct
4 Correct 174 ms 47428 KB Output is correct
5 Correct 177 ms 47656 KB Output is correct
6 Correct 184 ms 47684 KB Output is correct
7 Correct 181 ms 47692 KB Output is correct
8 Correct 181 ms 47752 KB Output is correct
9 Correct 180 ms 47936 KB Output is correct
10 Correct 188 ms 47964 KB Output is correct
11 Correct 194 ms 47940 KB Output is correct
12 Correct 180 ms 47960 KB Output is correct
13 Correct 191 ms 48068 KB Output is correct
14 Correct 184 ms 47864 KB Output is correct
15 Correct 180 ms 48068 KB Output is correct
16 Correct 183 ms 48080 KB Output is correct
17 Correct 183 ms 47960 KB Output is correct
18 Correct 180 ms 47680 KB Output is correct
19 Correct 183 ms 47896 KB Output is correct
20 Correct 176 ms 47684 KB Output is correct
21 Correct 182 ms 47668 KB Output is correct
22 Correct 182 ms 47444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 369 ms 82696 KB Output is correct
2 Correct 405 ms 85736 KB Output is correct
3 Correct 383 ms 85988 KB Output is correct
4 Correct 438 ms 86688 KB Output is correct
5 Correct 580 ms 84400 KB Output is correct
6 Correct 186 ms 48080 KB Output is correct
7 Correct 180 ms 47876 KB Output is correct
8 Correct 190 ms 47832 KB Output is correct
9 Correct 561 ms 80288 KB Output is correct
10 Correct 501 ms 79588 KB Output is correct
11 Correct 490 ms 79572 KB Output is correct
12 Correct 562 ms 79524 KB Output is correct
13 Correct 582 ms 98608 KB Output is correct
14 Correct 591 ms 98612 KB Output is correct
15 Correct 584 ms 98544 KB Output is correct
16 Correct 579 ms 98664 KB Output is correct
17 Correct 585 ms 98580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 721 ms 79556 KB Output is correct
2 Correct 682 ms 82012 KB Output is correct
3 Correct 666 ms 82204 KB Output is correct
4 Correct 705 ms 82256 KB Output is correct
5 Correct 650 ms 80524 KB Output is correct
6 Correct 676 ms 82052 KB Output is correct
7 Correct 536 ms 66608 KB Output is correct
8 Correct 589 ms 66628 KB Output is correct
9 Correct 714 ms 82132 KB Output is correct
10 Correct 689 ms 82340 KB Output is correct
11 Correct 696 ms 82304 KB Output is correct
12 Correct 551 ms 66620 KB Output is correct
13 Incorrect 478 ms 64348 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 349 ms 80372 KB Output is correct
2 Correct 372 ms 80332 KB Output is correct
3 Correct 344 ms 80400 KB Output is correct
4 Correct 379 ms 80428 KB Output is correct
5 Correct 329 ms 76336 KB Output is correct
6 Correct 180 ms 47988 KB Output is correct
7 Correct 180 ms 47812 KB Output is correct
8 Correct 176 ms 47828 KB Output is correct
9 Correct 343 ms 72672 KB Output is correct
10 Correct 346 ms 72800 KB Output is correct
11 Correct 362 ms 72664 KB Output is correct
12 Correct 329 ms 71364 KB Output is correct
13 Correct 319 ms 77912 KB Output is correct
14 Correct 177 ms 47332 KB Output is correct
15 Correct 177 ms 47424 KB Output is correct
16 Correct 178 ms 47452 KB Output is correct
17 Correct 174 ms 47428 KB Output is correct
18 Correct 177 ms 47656 KB Output is correct
19 Correct 184 ms 47684 KB Output is correct
20 Correct 181 ms 47692 KB Output is correct
21 Correct 181 ms 47752 KB Output is correct
22 Correct 180 ms 47936 KB Output is correct
23 Correct 188 ms 47964 KB Output is correct
24 Correct 194 ms 47940 KB Output is correct
25 Correct 180 ms 47960 KB Output is correct
26 Correct 191 ms 48068 KB Output is correct
27 Correct 184 ms 47864 KB Output is correct
28 Correct 180 ms 48068 KB Output is correct
29 Correct 183 ms 48080 KB Output is correct
30 Correct 183 ms 47960 KB Output is correct
31 Correct 180 ms 47680 KB Output is correct
32 Correct 183 ms 47896 KB Output is correct
33 Correct 176 ms 47684 KB Output is correct
34 Correct 182 ms 47668 KB Output is correct
35 Correct 182 ms 47444 KB Output is correct
36 Correct 369 ms 82696 KB Output is correct
37 Correct 405 ms 85736 KB Output is correct
38 Correct 383 ms 85988 KB Output is correct
39 Correct 438 ms 86688 KB Output is correct
40 Correct 580 ms 84400 KB Output is correct
41 Correct 186 ms 48080 KB Output is correct
42 Correct 180 ms 47876 KB Output is correct
43 Correct 190 ms 47832 KB Output is correct
44 Correct 561 ms 80288 KB Output is correct
45 Correct 501 ms 79588 KB Output is correct
46 Correct 490 ms 79572 KB Output is correct
47 Correct 562 ms 79524 KB Output is correct
48 Correct 582 ms 98608 KB Output is correct
49 Correct 591 ms 98612 KB Output is correct
50 Correct 584 ms 98544 KB Output is correct
51 Correct 579 ms 98664 KB Output is correct
52 Correct 585 ms 98580 KB Output is correct
53 Correct 721 ms 79556 KB Output is correct
54 Correct 682 ms 82012 KB Output is correct
55 Correct 666 ms 82204 KB Output is correct
56 Correct 705 ms 82256 KB Output is correct
57 Correct 650 ms 80524 KB Output is correct
58 Correct 676 ms 82052 KB Output is correct
59 Correct 536 ms 66608 KB Output is correct
60 Correct 589 ms 66628 KB Output is correct
61 Correct 714 ms 82132 KB Output is correct
62 Correct 689 ms 82340 KB Output is correct
63 Correct 696 ms 82304 KB Output is correct
64 Correct 551 ms 66620 KB Output is correct
65 Incorrect 478 ms 64348 KB Output isn't correct
66 Halted 0 ms 0 KB -