#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
const ll inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 5e5 + 7;
inline ll bpow (ll x, ll n) { ll t = 1; for (; n; n >>= 1, x = x * x % mod) if (n & 1) t = t * x % mod; return t; }
inline ll Inv (cl &x) { return bpow(x, mod - 2); }
ll f [N], inv [N], ans;
ll tin [N], tout [N], rtin [N], c [N], sz [N], n, r, q, zero, T;
vector <ll> adj [N];
void DFS (cl &v = 1, cl &pr = 0) {
rtin[tin[v] = ++T] = v;
for (auto &u : adj[v]) if (u != pr) DFS(u, v);
tout[v] = T;
}
struct segtree {
ll l, r, mx;
segtree *lt, *rt;
inline segtree (cl &L = 1, cl &R = N - 2) : l(L), r(R), mx(0) {}
void build () {
if (l == r) return;
lt = new segtree (l, mid);
rt = new segtree (mid + 1, r);
lt->build(); rt->build();
}
void upd (cl &pos, cl &x) {
if (l == r) { mx = x; return; }
if (pos <= lt->r) lt->upd(pos, x);
else rt->upd(pos, x);
mx = max(lt->mx, rt->mx);
}
ll ask (cl &qr, cl &x) {
if (qr < l || mx < x) return 0;
if (l == r) return l;
if (r <= qr) {
if (rt->mx >= x) return rt->ask(qr, x);
return lt->ask(qr, x);
}
ll p = rt->ask(qr, x);
if (!p) p = lt->ask(qr, x);
return p;
}
} seg;
struct fenwick {
ll fen [N];
fenwick () { memset(fen, 0, sizeof fen); }
inline void add (ll i, cl &x) { for (; i < N; i += i & -i) fen[i] += x; }
inline ll ask (ll l, ll r) {
ll ret = 0;
for (; r; r -= r & -r) ret += fen[r];
for (l--; l; l -= l & -l) ret -= fen[l];
return ret;
}
} fen, fen2;
inline ll nCr (cl &n, cl &r) {
if (n < r || r < 0) return 0;
return f[n] * inv[r] % mod * inv[n - r] % mod;
}
inline void obd (cl &x) { if (!x) zero++; else (ans *= x) %= mod; } // mul
inline void OBD (cl &x) { if (!x) zero--; else (ans *= x) %= mod; } // div
int main ()
{
cin.tie(0)->sync_with_stdio(false);
f[0] = 1; for (ll i = 1; i < N; i++) f[i] = f[i - 1] * i % mod;
inv[N - 1] = Inv(f[N - 1]); for (ll i = N; --i;) inv[i - 1] = inv[i] * i % mod;
cin>> n >> r;
for (ll v, u, i = 1; i < n; i++) {
cin>> v >> u;
adj[v].push_back(u); adj[u].push_back(v);
}
DFS();
seg.r = n; seg.build();
seg.upd(tin[1], tout[1]);
c[1] = r; sz[1] = n;
fen.add(tin[1], c[1]);
fen2.add(tin[1], sz[1]);
ans = nCr(c[1] + sz[1] - 1, sz[1] - 1);
cout<< ans << '\n';
cin>> q;
for (ll tp, v; q--;) {
cin>> tp >> v;
if (tp == 1) {
cin>> c[v];
c[v] -= fen.ask(tin[v], tout[v]);
ll par = rtin[seg.ask(tin[v], tout[v])];
OBD(Inv(nCr(c[par] + sz[par] - 1, sz[par] - 1)));
sz[v] = tout[v] - tin[v] + 1 - fen2.ask(tin[v], tout[v]);
sz[par] -= sz[v];
fen2.add(tin[par], -sz[v]);
fen2.add(tin[v], sz[v]);
c[par] -= c[v];
fen.add(tin[par], -c[v]);
fen.add(tin[v], c[v]);
obd(nCr(c[par] + sz[par] - 1, sz[par] - 1));
obd(nCr(c[v] + sz[v] - 1, sz[v] - 1));
seg.upd(tin[v], tout[v]);
} else {
seg.upd(tin[v], 0);
ll par = rtin[seg.ask(tin[v], tout[v])];
OBD(Inv(nCr(c[par] + sz[par] - 1, sz[par] - 1)));
OBD(Inv(nCr(c[v] + sz[v] - 1, sz[v] - 1)));
sz[par] += sz[v];
fen2.add(tin[par], sz[v]);
fen2.add(tin[v], -sz[v]);
c[par] += c[v];
fen.add(tin[par], c[v]);
fen.add(tin[v], -c[v]);
obd(nCr(c[par] + sz[par] - 1, sz[par] - 1));
}
cout<< (zero ? 0LL : ans) << '\n';
}
cerr<< "\n" __TIMESTAMP__ " - Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
3 2
1 2
2 3
2
1 2 1
2 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
61044 KB |
Output is correct |
2 |
Correct |
169 ms |
61116 KB |
Output is correct |
3 |
Correct |
160 ms |
61120 KB |
Output is correct |
4 |
Correct |
172 ms |
61072 KB |
Output is correct |
5 |
Correct |
177 ms |
58944 KB |
Output is correct |
6 |
Correct |
21 ms |
28288 KB |
Output is correct |
7 |
Correct |
21 ms |
28120 KB |
Output is correct |
8 |
Correct |
21 ms |
28248 KB |
Output is correct |
9 |
Correct |
181 ms |
59288 KB |
Output is correct |
10 |
Correct |
180 ms |
59120 KB |
Output is correct |
11 |
Correct |
196 ms |
59124 KB |
Output is correct |
12 |
Correct |
165 ms |
57540 KB |
Output is correct |
13 |
Correct |
155 ms |
57992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27756 KB |
Output is correct |
2 |
Correct |
20 ms |
27652 KB |
Output is correct |
3 |
Correct |
20 ms |
27640 KB |
Output is correct |
4 |
Correct |
20 ms |
27724 KB |
Output is correct |
5 |
Correct |
21 ms |
27860 KB |
Output is correct |
6 |
Correct |
24 ms |
28108 KB |
Output is correct |
7 |
Correct |
22 ms |
28108 KB |
Output is correct |
8 |
Correct |
23 ms |
28100 KB |
Output is correct |
9 |
Correct |
24 ms |
28364 KB |
Output is correct |
10 |
Correct |
25 ms |
28244 KB |
Output is correct |
11 |
Correct |
24 ms |
28236 KB |
Output is correct |
12 |
Correct |
21 ms |
28244 KB |
Output is correct |
13 |
Correct |
24 ms |
28240 KB |
Output is correct |
14 |
Correct |
25 ms |
28308 KB |
Output is correct |
15 |
Correct |
25 ms |
28396 KB |
Output is correct |
16 |
Correct |
25 ms |
28260 KB |
Output is correct |
17 |
Correct |
25 ms |
28236 KB |
Output is correct |
18 |
Correct |
24 ms |
27924 KB |
Output is correct |
19 |
Correct |
24 ms |
28332 KB |
Output is correct |
20 |
Correct |
23 ms |
28100 KB |
Output is correct |
21 |
Correct |
22 ms |
28056 KB |
Output is correct |
22 |
Correct |
22 ms |
27752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
64052 KB |
Output is correct |
2 |
Correct |
220 ms |
64312 KB |
Output is correct |
3 |
Correct |
205 ms |
64336 KB |
Output is correct |
4 |
Correct |
281 ms |
64656 KB |
Output is correct |
5 |
Correct |
406 ms |
63072 KB |
Output is correct |
6 |
Correct |
22 ms |
28364 KB |
Output is correct |
7 |
Correct |
22 ms |
28292 KB |
Output is correct |
8 |
Correct |
22 ms |
28316 KB |
Output is correct |
9 |
Correct |
437 ms |
63408 KB |
Output is correct |
10 |
Correct |
339 ms |
62880 KB |
Output is correct |
11 |
Correct |
356 ms |
63276 KB |
Output is correct |
12 |
Correct |
375 ms |
62508 KB |
Output is correct |
13 |
Correct |
325 ms |
67332 KB |
Output is correct |
14 |
Correct |
343 ms |
67456 KB |
Output is correct |
15 |
Correct |
330 ms |
67412 KB |
Output is correct |
16 |
Correct |
333 ms |
67340 KB |
Output is correct |
17 |
Correct |
356 ms |
67456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
573 ms |
64224 KB |
Output is correct |
2 |
Correct |
548 ms |
64092 KB |
Output is correct |
3 |
Correct |
545 ms |
64292 KB |
Output is correct |
4 |
Correct |
580 ms |
64324 KB |
Output is correct |
5 |
Correct |
615 ms |
62368 KB |
Output is correct |
6 |
Correct |
552 ms |
64356 KB |
Output is correct |
7 |
Correct |
405 ms |
47288 KB |
Output is correct |
8 |
Correct |
417 ms |
47172 KB |
Output is correct |
9 |
Correct |
581 ms |
64328 KB |
Output is correct |
10 |
Correct |
513 ms |
64260 KB |
Output is correct |
11 |
Correct |
525 ms |
64284 KB |
Output is correct |
12 |
Correct |
421 ms |
47276 KB |
Output is correct |
13 |
Correct |
304 ms |
44172 KB |
Output is correct |
14 |
Correct |
325 ms |
48056 KB |
Output is correct |
15 |
Correct |
378 ms |
48396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
61044 KB |
Output is correct |
2 |
Correct |
169 ms |
61116 KB |
Output is correct |
3 |
Correct |
160 ms |
61120 KB |
Output is correct |
4 |
Correct |
172 ms |
61072 KB |
Output is correct |
5 |
Correct |
177 ms |
58944 KB |
Output is correct |
6 |
Correct |
21 ms |
28288 KB |
Output is correct |
7 |
Correct |
21 ms |
28120 KB |
Output is correct |
8 |
Correct |
21 ms |
28248 KB |
Output is correct |
9 |
Correct |
181 ms |
59288 KB |
Output is correct |
10 |
Correct |
180 ms |
59120 KB |
Output is correct |
11 |
Correct |
196 ms |
59124 KB |
Output is correct |
12 |
Correct |
165 ms |
57540 KB |
Output is correct |
13 |
Correct |
155 ms |
57992 KB |
Output is correct |
14 |
Correct |
19 ms |
27756 KB |
Output is correct |
15 |
Correct |
20 ms |
27652 KB |
Output is correct |
16 |
Correct |
20 ms |
27640 KB |
Output is correct |
17 |
Correct |
20 ms |
27724 KB |
Output is correct |
18 |
Correct |
21 ms |
27860 KB |
Output is correct |
19 |
Correct |
24 ms |
28108 KB |
Output is correct |
20 |
Correct |
22 ms |
28108 KB |
Output is correct |
21 |
Correct |
23 ms |
28100 KB |
Output is correct |
22 |
Correct |
24 ms |
28364 KB |
Output is correct |
23 |
Correct |
25 ms |
28244 KB |
Output is correct |
24 |
Correct |
24 ms |
28236 KB |
Output is correct |
25 |
Correct |
21 ms |
28244 KB |
Output is correct |
26 |
Correct |
24 ms |
28240 KB |
Output is correct |
27 |
Correct |
25 ms |
28308 KB |
Output is correct |
28 |
Correct |
25 ms |
28396 KB |
Output is correct |
29 |
Correct |
25 ms |
28260 KB |
Output is correct |
30 |
Correct |
25 ms |
28236 KB |
Output is correct |
31 |
Correct |
24 ms |
27924 KB |
Output is correct |
32 |
Correct |
24 ms |
28332 KB |
Output is correct |
33 |
Correct |
23 ms |
28100 KB |
Output is correct |
34 |
Correct |
22 ms |
28056 KB |
Output is correct |
35 |
Correct |
22 ms |
27752 KB |
Output is correct |
36 |
Correct |
182 ms |
64052 KB |
Output is correct |
37 |
Correct |
220 ms |
64312 KB |
Output is correct |
38 |
Correct |
205 ms |
64336 KB |
Output is correct |
39 |
Correct |
281 ms |
64656 KB |
Output is correct |
40 |
Correct |
406 ms |
63072 KB |
Output is correct |
41 |
Correct |
22 ms |
28364 KB |
Output is correct |
42 |
Correct |
22 ms |
28292 KB |
Output is correct |
43 |
Correct |
22 ms |
28316 KB |
Output is correct |
44 |
Correct |
437 ms |
63408 KB |
Output is correct |
45 |
Correct |
339 ms |
62880 KB |
Output is correct |
46 |
Correct |
356 ms |
63276 KB |
Output is correct |
47 |
Correct |
375 ms |
62508 KB |
Output is correct |
48 |
Correct |
325 ms |
67332 KB |
Output is correct |
49 |
Correct |
343 ms |
67456 KB |
Output is correct |
50 |
Correct |
330 ms |
67412 KB |
Output is correct |
51 |
Correct |
333 ms |
67340 KB |
Output is correct |
52 |
Correct |
356 ms |
67456 KB |
Output is correct |
53 |
Correct |
573 ms |
64224 KB |
Output is correct |
54 |
Correct |
548 ms |
64092 KB |
Output is correct |
55 |
Correct |
545 ms |
64292 KB |
Output is correct |
56 |
Correct |
580 ms |
64324 KB |
Output is correct |
57 |
Correct |
615 ms |
62368 KB |
Output is correct |
58 |
Correct |
552 ms |
64356 KB |
Output is correct |
59 |
Correct |
405 ms |
47288 KB |
Output is correct |
60 |
Correct |
417 ms |
47172 KB |
Output is correct |
61 |
Correct |
581 ms |
64328 KB |
Output is correct |
62 |
Correct |
513 ms |
64260 KB |
Output is correct |
63 |
Correct |
525 ms |
64284 KB |
Output is correct |
64 |
Correct |
421 ms |
47276 KB |
Output is correct |
65 |
Correct |
304 ms |
44172 KB |
Output is correct |
66 |
Correct |
325 ms |
48056 KB |
Output is correct |
67 |
Correct |
378 ms |
48396 KB |
Output is correct |
68 |
Correct |
20 ms |
27752 KB |
Output is correct |
69 |
Correct |
23 ms |
27648 KB |
Output is correct |
70 |
Correct |
515 ms |
70424 KB |
Output is correct |
71 |
Correct |
534 ms |
70320 KB |
Output is correct |
72 |
Correct |
545 ms |
70516 KB |
Output is correct |
73 |
Correct |
532 ms |
70464 KB |
Output is correct |
74 |
Correct |
643 ms |
70264 KB |
Output is correct |
75 |
Correct |
545 ms |
68984 KB |
Output is correct |
76 |
Correct |
514 ms |
68284 KB |
Output is correct |
77 |
Correct |
626 ms |
68088 KB |
Output is correct |
78 |
Correct |
651 ms |
67044 KB |
Output is correct |
79 |
Correct |
553 ms |
69380 KB |
Output is correct |
80 |
Correct |
616 ms |
67364 KB |
Output is correct |
81 |
Correct |
612 ms |
68704 KB |
Output is correct |
82 |
Correct |
417 ms |
64484 KB |
Output is correct |
83 |
Correct |
401 ms |
66872 KB |
Output is correct |
84 |
Correct |
390 ms |
65964 KB |
Output is correct |
85 |
Correct |
371 ms |
65792 KB |
Output is correct |