#include <bits/stdc++.h>
#define FOR(i, x, n) for(int i = x; i < n; i++)
#define F0R(i, n) FOR(i, 0, n)
#define ROF(i, x, n) for(int i = n - 1; i >= x; i--)
#define R0F(i, n) ROF(i, 0, n)
#define WTF cout << "WTF" << endl
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 5e5 + 7;
const int LOG = 22;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
#define lc now << 1
#define rc now << 1 | 1
#define endl '\n'
LL chs[N], vir[N], fact[N], ifact[N];
LL ans;
VI adj[N], order;
LL n, q, bad, cnt;
LL head[N], par[N], s[N], height[N];
LL t[N][2];
struct SumTree {
LL tree[N << 2];
void add(int lq, int rq, LL val, int now = 1, int ls = 0, int rs = n - 1) {
if (rq < lq || rq < ls || rs < lq) return;
if (lq <= ls && rs <= rq) {
tree[now] += val;
return;
}
int mid = (ls + rs) >> 1;
add(lq, rq, val, lc, ls, mid);
add(lq, rq, val, rc, mid + 1, rs);
return;
}
LL get(int id, int now = 1, int ls = 0, int rs = n - 1) {
if (ls == rs) return tree[now];
int mid = (ls + rs) >> 1;
if (id <= mid) return get(id, lc, ls, mid) + tree[now];
return get(id, rc, mid + 1, rs) + tree[now];
}
} val, sz;
struct DescendTree {
bool tree[N << 2];
bool arr[N];
void ch(int id, bool val, int now = 1, int ls = 0, int rs = n - 1) {
if (ls == rs) {
//cout << "tree[" << ls << ", " << rs << "] = " << tree[now] << endl;
tree[now] = val;
return;
}
int mid = (ls + rs) >> 1;
if (id <= mid) ch(id, val, lc, ls, mid);
else ch(id, val, rc, mid + 1, rs);
tree[now] = tree[lc] | tree[rc];
//cout << "tree[" << ls << ", " << rs << "] = " << tree[now] << endl;
return;
}
void change(int id, int val) {
ch(id, val);
arr[id] = val;
}
int g(int x) {
return arr[x];
}
int rightest(int q, int now = 1, int ls = 0, int rs = n - 1) {
if (ls > q || !tree[now]) return n;
if (ls == rs) return ls;
int mid = (ls + rs) >> 1;
int case1 = rightest(q, rc, mid + 1, rs);
return (case1 == n ? rightest(q, lc, ls, mid) : case1);
}
} active;
inline int pw(LL a, int b) {
LL ret = 1;
for(; b; b >>= 1, a = a * a % MOD)
if (b & 1) ret = ret * a % MOD;
return ret;
}
inline int nCr(int nn, int rr) {
if (nn < rr || rr < 0) return 0;
return (fact[nn] * ifact[rr] % MOD) * ifact[nn - rr] % MOD;
}
void init() {
LL v0;
cin >> n >> v0;
val.add(0, 0, v0);
F0R(i, n - 1) {
int u, v;
cin >> u >> v;
adj[--u].PB(--v);
adj[v].PB(u);
}
cin >> q;
ans = 1;
return;
}
void preD(int now, int p, int h = 0) {
par[now] = p;
s[now] = 1;
height[now] = h;
for(int on : adj[now]) if (on != p) {
preD(on, now, h + 1);
s[now] += s[on];
}
return;
}
void precalc() {
fact[0] = 1;
FOR(i, 1, N) fact[i] = fact[i - 1] * i % MOD;
ifact[N - 1] = pw(fact[N - 1], MOD - 2);
R0F(i, N - 1) ifact[i] = ifact[i + 1] * (i + 1) % MOD;
iota(head, head + n, 0);
F0R(i, n) {
sort(ALL(adj[i]), [](const int &a, const int &b) {return s[a] < s[b];});
if (i) adj[i].pop_back();
reverse(ALL(adj[i]));
}
return;
}
void dfs(int now) {
order.PB(now);
t[now][0] = cnt++;
if (!adj[now].empty()) head[ adj[now][0] ] = head[now];
for(int on : adj[now]) dfs(on);
t[now][1] = cnt;
return;
}
void updChoose(int now) {
if (!chs[now]) bad--;
else ans = (ans * pw(chs[now], MOD - 2)) % MOD;
LL val_now = val.get(t[now][0]);
LL sz_now = sz.get(t[now][0]);
chs[now] = nCr(val_now + sz_now - 1, sz_now - 1);
if (!chs[now]) bad++;
else ans = (ans * chs[now]) % MOD;
return;
}
void remChoose(int now) {
if (!chs[now]) bad--;
else ans = (ans * pw(chs[now], MOD - 2)) % MOD;
chs[now] = 1;
return;
}
int getUpperActive(int v) {
while(!active.g(t[v][0])) {
int pl = order[ active.rightest(t[v][0]) ];
if (t[pl][0] >= t[ head[v] ][0]) v = pl;
else v = par[ head[v] ];
}
return v;
}
void addUpTo(int v, int z, LL x1, LL x2) {
while(height[v] >= height[z]) {
int w = (height[ head[v] ] >= height[z] ? head[v] : z);
sz.add(t[w][0], t[v][0], x1);
val.add(t[w][0], t[v][0], x2);
v = par[w];
}
return;
}
void addTag() {
int v, x;
cin >> v >> x;
v--;
val.add(t[v][0], t[v][0], x);
LL sz_v = sz.get(t[v][0]);
LL val_v = val.get(t[v][0]);
int up = getUpperActive(v);
//cout << "up is " << up << endl;
addUpTo(par[v], up, -sz_v, -val_v);
vir[v] = x;
updChoose(up);
updChoose(v);
//cout << "t[" << v << "][0] = " << t[v][0] << endl;
active.change(t[v][0], 1);
return;
}
void remTag() {
int v;
cin >> v;
v--;
LL sz_v = sz.get(t[v][0]);
LL val_v = val.get(t[v][0]);
int up = getUpperActive(par[v]);
addUpTo(par[v], up, sz_v, val_v);
val.add(t[v][0], t[v][0], -vir[v]);
updChoose(up);
remChoose(v);
active.change(t[v][0], 0);
return;
}
int main() {
IOS;
init();
height[n] = -1;
par[0] = n;
preD(0, n);
precalc();
dfs(0);
F0R(i, n) sz.add(t[i][0], t[i][0], s[i]);
active.change(0, 1);
fill(chs, chs + n, 1);
updChoose(0);
cout << ans << endl;
while(q--) {
int tp; cin >> tp;
if (tp == 1) addTag();
else remTag();
cout << (bad ? 0 : ans) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
49040 KB |
Output is correct |
2 |
Correct |
174 ms |
49084 KB |
Output is correct |
3 |
Correct |
199 ms |
49012 KB |
Output is correct |
4 |
Correct |
171 ms |
49012 KB |
Output is correct |
5 |
Correct |
154 ms |
45084 KB |
Output is correct |
6 |
Correct |
17 ms |
20500 KB |
Output is correct |
7 |
Correct |
17 ms |
20284 KB |
Output is correct |
8 |
Correct |
17 ms |
20308 KB |
Output is correct |
9 |
Correct |
187 ms |
41396 KB |
Output is correct |
10 |
Correct |
204 ms |
41388 KB |
Output is correct |
11 |
Correct |
184 ms |
41448 KB |
Output is correct |
12 |
Correct |
186 ms |
40488 KB |
Output is correct |
13 |
Correct |
173 ms |
47232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19924 KB |
Output is correct |
2 |
Correct |
16 ms |
19924 KB |
Output is correct |
3 |
Correct |
16 ms |
19924 KB |
Output is correct |
4 |
Correct |
16 ms |
19924 KB |
Output is correct |
5 |
Correct |
17 ms |
20052 KB |
Output is correct |
6 |
Correct |
20 ms |
20180 KB |
Output is correct |
7 |
Correct |
24 ms |
20256 KB |
Output is correct |
8 |
Correct |
20 ms |
20228 KB |
Output is correct |
9 |
Correct |
23 ms |
20472 KB |
Output is correct |
10 |
Correct |
22 ms |
20460 KB |
Output is correct |
11 |
Correct |
22 ms |
20472 KB |
Output is correct |
12 |
Correct |
20 ms |
20436 KB |
Output is correct |
13 |
Correct |
23 ms |
20388 KB |
Output is correct |
14 |
Correct |
22 ms |
20436 KB |
Output is correct |
15 |
Correct |
23 ms |
20708 KB |
Output is correct |
16 |
Correct |
26 ms |
20432 KB |
Output is correct |
17 |
Correct |
25 ms |
20492 KB |
Output is correct |
18 |
Correct |
20 ms |
20116 KB |
Output is correct |
19 |
Correct |
26 ms |
20448 KB |
Output is correct |
20 |
Correct |
20 ms |
20180 KB |
Output is correct |
21 |
Correct |
23 ms |
20168 KB |
Output is correct |
22 |
Correct |
18 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
54700 KB |
Output is correct |
2 |
Correct |
249 ms |
55244 KB |
Output is correct |
3 |
Correct |
218 ms |
55556 KB |
Output is correct |
4 |
Correct |
318 ms |
56120 KB |
Output is correct |
5 |
Correct |
451 ms |
53572 KB |
Output is correct |
6 |
Correct |
18 ms |
20672 KB |
Output is correct |
7 |
Correct |
18 ms |
20436 KB |
Output is correct |
8 |
Correct |
19 ms |
20480 KB |
Output is correct |
9 |
Correct |
572 ms |
49396 KB |
Output is correct |
10 |
Correct |
460 ms |
49092 KB |
Output is correct |
11 |
Correct |
446 ms |
49224 KB |
Output is correct |
12 |
Correct |
520 ms |
48468 KB |
Output is correct |
13 |
Correct |
403 ms |
69100 KB |
Output is correct |
14 |
Correct |
394 ms |
71612 KB |
Output is correct |
15 |
Correct |
455 ms |
71616 KB |
Output is correct |
16 |
Correct |
404 ms |
71616 KB |
Output is correct |
17 |
Correct |
402 ms |
71616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
849 ms |
50492 KB |
Output is correct |
2 |
Correct |
913 ms |
54868 KB |
Output is correct |
3 |
Correct |
918 ms |
54964 KB |
Output is correct |
4 |
Correct |
846 ms |
55084 KB |
Output is correct |
5 |
Correct |
1006 ms |
53644 KB |
Output is correct |
6 |
Correct |
891 ms |
54788 KB |
Output is correct |
7 |
Correct |
692 ms |
39332 KB |
Output is correct |
8 |
Correct |
651 ms |
39332 KB |
Output is correct |
9 |
Correct |
896 ms |
54984 KB |
Output is correct |
10 |
Correct |
904 ms |
55176 KB |
Output is correct |
11 |
Correct |
914 ms |
55028 KB |
Output is correct |
12 |
Correct |
675 ms |
39312 KB |
Output is correct |
13 |
Correct |
471 ms |
35056 KB |
Output is correct |
14 |
Correct |
605 ms |
37092 KB |
Output is correct |
15 |
Correct |
590 ms |
37696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
49040 KB |
Output is correct |
2 |
Correct |
174 ms |
49084 KB |
Output is correct |
3 |
Correct |
199 ms |
49012 KB |
Output is correct |
4 |
Correct |
171 ms |
49012 KB |
Output is correct |
5 |
Correct |
154 ms |
45084 KB |
Output is correct |
6 |
Correct |
17 ms |
20500 KB |
Output is correct |
7 |
Correct |
17 ms |
20284 KB |
Output is correct |
8 |
Correct |
17 ms |
20308 KB |
Output is correct |
9 |
Correct |
187 ms |
41396 KB |
Output is correct |
10 |
Correct |
204 ms |
41388 KB |
Output is correct |
11 |
Correct |
184 ms |
41448 KB |
Output is correct |
12 |
Correct |
186 ms |
40488 KB |
Output is correct |
13 |
Correct |
173 ms |
47232 KB |
Output is correct |
14 |
Correct |
15 ms |
19924 KB |
Output is correct |
15 |
Correct |
16 ms |
19924 KB |
Output is correct |
16 |
Correct |
16 ms |
19924 KB |
Output is correct |
17 |
Correct |
16 ms |
19924 KB |
Output is correct |
18 |
Correct |
17 ms |
20052 KB |
Output is correct |
19 |
Correct |
20 ms |
20180 KB |
Output is correct |
20 |
Correct |
24 ms |
20256 KB |
Output is correct |
21 |
Correct |
20 ms |
20228 KB |
Output is correct |
22 |
Correct |
23 ms |
20472 KB |
Output is correct |
23 |
Correct |
22 ms |
20460 KB |
Output is correct |
24 |
Correct |
22 ms |
20472 KB |
Output is correct |
25 |
Correct |
20 ms |
20436 KB |
Output is correct |
26 |
Correct |
23 ms |
20388 KB |
Output is correct |
27 |
Correct |
22 ms |
20436 KB |
Output is correct |
28 |
Correct |
23 ms |
20708 KB |
Output is correct |
29 |
Correct |
26 ms |
20432 KB |
Output is correct |
30 |
Correct |
25 ms |
20492 KB |
Output is correct |
31 |
Correct |
20 ms |
20116 KB |
Output is correct |
32 |
Correct |
26 ms |
20448 KB |
Output is correct |
33 |
Correct |
20 ms |
20180 KB |
Output is correct |
34 |
Correct |
23 ms |
20168 KB |
Output is correct |
35 |
Correct |
18 ms |
20052 KB |
Output is correct |
36 |
Correct |
226 ms |
54700 KB |
Output is correct |
37 |
Correct |
249 ms |
55244 KB |
Output is correct |
38 |
Correct |
218 ms |
55556 KB |
Output is correct |
39 |
Correct |
318 ms |
56120 KB |
Output is correct |
40 |
Correct |
451 ms |
53572 KB |
Output is correct |
41 |
Correct |
18 ms |
20672 KB |
Output is correct |
42 |
Correct |
18 ms |
20436 KB |
Output is correct |
43 |
Correct |
19 ms |
20480 KB |
Output is correct |
44 |
Correct |
572 ms |
49396 KB |
Output is correct |
45 |
Correct |
460 ms |
49092 KB |
Output is correct |
46 |
Correct |
446 ms |
49224 KB |
Output is correct |
47 |
Correct |
520 ms |
48468 KB |
Output is correct |
48 |
Correct |
403 ms |
69100 KB |
Output is correct |
49 |
Correct |
394 ms |
71612 KB |
Output is correct |
50 |
Correct |
455 ms |
71616 KB |
Output is correct |
51 |
Correct |
404 ms |
71616 KB |
Output is correct |
52 |
Correct |
402 ms |
71616 KB |
Output is correct |
53 |
Correct |
849 ms |
50492 KB |
Output is correct |
54 |
Correct |
913 ms |
54868 KB |
Output is correct |
55 |
Correct |
918 ms |
54964 KB |
Output is correct |
56 |
Correct |
846 ms |
55084 KB |
Output is correct |
57 |
Correct |
1006 ms |
53644 KB |
Output is correct |
58 |
Correct |
891 ms |
54788 KB |
Output is correct |
59 |
Correct |
692 ms |
39332 KB |
Output is correct |
60 |
Correct |
651 ms |
39332 KB |
Output is correct |
61 |
Correct |
896 ms |
54984 KB |
Output is correct |
62 |
Correct |
904 ms |
55176 KB |
Output is correct |
63 |
Correct |
914 ms |
55028 KB |
Output is correct |
64 |
Correct |
675 ms |
39312 KB |
Output is correct |
65 |
Correct |
471 ms |
35056 KB |
Output is correct |
66 |
Correct |
605 ms |
37092 KB |
Output is correct |
67 |
Correct |
590 ms |
37696 KB |
Output is correct |
68 |
Correct |
15 ms |
20052 KB |
Output is correct |
69 |
Correct |
15 ms |
19892 KB |
Output is correct |
70 |
Correct |
672 ms |
62616 KB |
Output is correct |
71 |
Correct |
684 ms |
62536 KB |
Output is correct |
72 |
Correct |
670 ms |
62600 KB |
Output is correct |
73 |
Correct |
702 ms |
62588 KB |
Output is correct |
74 |
Correct |
703 ms |
62440 KB |
Output is correct |
75 |
Correct |
670 ms |
58872 KB |
Output is correct |
76 |
Correct |
981 ms |
54936 KB |
Output is correct |
77 |
Correct |
952 ms |
54568 KB |
Output is correct |
78 |
Correct |
908 ms |
53656 KB |
Output is correct |
79 |
Correct |
716 ms |
58736 KB |
Output is correct |
80 |
Correct |
726 ms |
57276 KB |
Output is correct |
81 |
Correct |
738 ms |
58168 KB |
Output is correct |
82 |
Correct |
747 ms |
48992 KB |
Output is correct |
83 |
Correct |
435 ms |
55576 KB |
Output is correct |
84 |
Correct |
432 ms |
54928 KB |
Output is correct |
85 |
Correct |
442 ms |
54828 KB |
Output is correct |