#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) (x).size();
#define F first
#define S second
#define int ll
int Pow(int a, int b, int md, int ret = 1) {
for (; b; b >>= 1, a = 1ll * a * a % md)
if (b & 1) ret = 1ll * ret * a % md;
return ret;
}
const int N = 5e5 + 10, MOD = 1e9 + 7;
int F[N], St[N], Ft[N], I[N], hv[N], hd[N], S[N], P[N], val[N], H[N], n, q, R, timer;
vector<int> adj[N]; set<pii> st[N]; ll ret;
struct node {
ll d, lz; node *lc, *rc;
node() : d(0), lz(0), lc(nullptr), rc(nullptr) {}
void bld(int l = 0, int r = n) {
if (r - l < 2) return;
lc = new node(); rc = new node();
int md = (l + r) >> 1;
lc->bld(l, md), rc->bld(md, r);
}
void shift() {
if (!lz) return;
d += lz;
if (lc) lc->lz += lz;
if (rc) rc->lz += lz;
lz = 0;
}
void upd(int ql, int qr, ll x, int l = 0, int r = n) {
shift();
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr) { lz += x; return shift(); }
int md = (l + r) >> 1;
lc->upd(ql, qr, x, l, md), rc->upd(ql, qr, x, md, r);
d = min(lc->d, rc->d);
}
ll get(int p, int l = 0, int r = n) {
shift();
if (r - l < 2) return d;
int md = (l + r) >> 1;
return p < md ? lc->get(p, l, md) : rc->get(p, md, r);
}
};
node *A, *B;
void preDFS(int v, int p = -1) {
S[v] = 1; P[v] = p;
for (int u : adj[v])
if (u != p) { H[u] = H[v] + 1, preDFS(u, v), S[v] += S[u];
if (S[hv[v]] < S[u]) hv[v] = u; }
}
void hldDFS(int v, int p = -1) {
if (!~p) hd[v] = v;
St[v] = timer++;
if (hv[v])
hd[hv[v]] = hd[v], hldDFS(hv[v], v);
for (int u : adj[v])
if (u != p && u != hv[v])
hldDFS(hd[u] = u, v);
Ft[v] = timer;
}
int C(int x, int y) {
if (x < y || y < 0) return 0;
return F[x] * 1ll * I[y] % MOD * I[x - y] % MOD;
}
int32_t main() {
scanf("%lld%lld", &n, &R);
F[0] = 1; st[1].insert(pii(0, 1)); val[1] = R;
for (int i = 1; i < N; i++)
F[i] = 1ll * F[i - 1] * i % MOD;
I[N - 1] = Pow(F[N - 1], MOD - 2, MOD);
for (int i = N - 1; i; i--)
I[i - 1] = 1ll * I[i] * i % MOD;
for (int i = 1; i < n; i++) {
int u, v; scanf("%lld%lld", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
A = new node(), B = new node(); A->bld(), B->bld(); B->upd(0, 1, R);
preDFS(1); hldDFS(1);
printf("%lld\n", C(R + n - 1, n - 1)); ret = C(R + n - 1, n - 1);
for (int i = 1; i <= n; i++)
A->upd(St[i], St[i] + 1, S[i]);
for (scanf("%lld", &q); q; q--) {
int t, v, x; scanf("%lld%lld", &t, &v);
if (t < 2) {
scanf("%lld", &x); ll sub = A->get(St[v]); B->upd(St[v], St[v] + 1, x);
ll y = B->get(St[v]); val[v] = x;
ret = ret * C(y + sub - 1, sub - 1) % MOD;
int u = P[v];
while (st[hd[u]].empty() || st[hd[u]].begin()->F > St[u]) {
A->upd(St[hd[u]], St[u] + 1, -sub);
B->upd(St[hd[u]], St[u] + 1, -y);
u = P[hd[u]];
}
ll pr = prev(st[hd[u]].lower_bound(pii(St[u], n + 1)))->S;
st[hd[v]].insert(pii(St[v], v));
ll cpr = A->get(St[pr]); ll z = B->get(St[pr]);
ret = ret * Pow(C(cpr - 1 + z, z), MOD - 2, MOD) % MOD;
A->upd(St[pr], St[u] + 1, -sub); B->upd(St[pr], St[u] + 1, -y);
z = B->get(St[pr]); cpr = A->get(St[pr]);
ret = ret * C(cpr - 1 + z, z) % MOD;
} else {
ll sub = A->get(St[v]), y = B->get(St[v]);
ret = ret * Pow(C(y + sub - 1, sub - 1), MOD - 2, MOD) % MOD;
int u = P[v]; st[hd[v]].erase({St[v], v});
while (st[hd[u]].empty() || st[hd[u]].begin()->F > St[u]) {
A->upd(St[hd[u]], St[u] + 1, sub);
B->upd(St[hd[u]], St[u] + 1, y);
u = P[hd[u]];
}
ll pr = prev(st[hd[u]].lower_bound(pii(St[u], n + 1)))->S;
ll cpr = A->get(St[pr]); ll z = B->get(St[pr]);
ret = ret * Pow(C(cpr - 1 + z, z), MOD - 2, MOD) % MOD;
A->upd(St[pr], St[u] + 1, sub); B->upd(St[pr], St[u] + 1, y);
z = B->get(St[pr]); cpr = A->get(St[pr]);
ret = ret * C(cpr - 1 + z, z) % MOD; B->upd(St[v], St[v] + 1, -val[v]);
}
printf("%lld\n", ret);
}
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%lld%lld", &n, &R);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:92:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | int u, v; scanf("%lld%lld", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:101:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | for (scanf("%lld", &q); q; q--) {
| ~~~~~^~~~~~~~~~~~
Main.cpp:102:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | int t, v, x; scanf("%lld%lld", &t, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:104:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%lld", &x); ll sub = A->get(St[v]); B->upd(St[v], St[v] + 1, x);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
105412 KB |
Output is correct |
2 |
Correct |
290 ms |
105592 KB |
Output is correct |
3 |
Correct |
311 ms |
105496 KB |
Output is correct |
4 |
Correct |
304 ms |
105532 KB |
Output is correct |
5 |
Correct |
251 ms |
101248 KB |
Output is correct |
6 |
Correct |
34 ms |
44340 KB |
Output is correct |
7 |
Correct |
33 ms |
44236 KB |
Output is correct |
8 |
Correct |
33 ms |
44244 KB |
Output is correct |
9 |
Correct |
361 ms |
99520 KB |
Output is correct |
10 |
Correct |
356 ms |
99204 KB |
Output is correct |
11 |
Correct |
369 ms |
99544 KB |
Output is correct |
12 |
Correct |
322 ms |
96524 KB |
Output is correct |
13 |
Correct |
278 ms |
99780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
43320 KB |
Output is correct |
2 |
Correct |
30 ms |
43340 KB |
Output is correct |
3 |
Correct |
33 ms |
43340 KB |
Output is correct |
4 |
Correct |
31 ms |
43340 KB |
Output is correct |
5 |
Correct |
33 ms |
43548 KB |
Output is correct |
6 |
Correct |
38 ms |
44028 KB |
Output is correct |
7 |
Correct |
41 ms |
43960 KB |
Output is correct |
8 |
Correct |
36 ms |
44012 KB |
Output is correct |
9 |
Correct |
39 ms |
44312 KB |
Output is correct |
10 |
Correct |
39 ms |
44348 KB |
Output is correct |
11 |
Correct |
37 ms |
44384 KB |
Output is correct |
12 |
Correct |
33 ms |
44240 KB |
Output is correct |
13 |
Correct |
39 ms |
44228 KB |
Output is correct |
14 |
Correct |
40 ms |
44268 KB |
Output is correct |
15 |
Correct |
40 ms |
44384 KB |
Output is correct |
16 |
Correct |
39 ms |
44236 KB |
Output is correct |
17 |
Correct |
39 ms |
44220 KB |
Output is correct |
18 |
Correct |
36 ms |
43712 KB |
Output is correct |
19 |
Correct |
41 ms |
44272 KB |
Output is correct |
20 |
Correct |
37 ms |
44024 KB |
Output is correct |
21 |
Correct |
36 ms |
43980 KB |
Output is correct |
22 |
Correct |
32 ms |
43544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
357 ms |
106908 KB |
Output is correct |
2 |
Correct |
438 ms |
107716 KB |
Output is correct |
3 |
Correct |
342 ms |
107460 KB |
Output is correct |
4 |
Correct |
505 ms |
109324 KB |
Output is correct |
5 |
Correct |
687 ms |
108700 KB |
Output is correct |
6 |
Correct |
36 ms |
44492 KB |
Output is correct |
7 |
Correct |
35 ms |
44236 KB |
Output is correct |
8 |
Correct |
43 ms |
44340 KB |
Output is correct |
9 |
Correct |
899 ms |
106776 KB |
Output is correct |
10 |
Correct |
738 ms |
105148 KB |
Output is correct |
11 |
Correct |
789 ms |
104984 KB |
Output is correct |
12 |
Correct |
820 ms |
105284 KB |
Output is correct |
13 |
Correct |
648 ms |
123620 KB |
Output is correct |
14 |
Correct |
653 ms |
123708 KB |
Output is correct |
15 |
Correct |
656 ms |
123568 KB |
Output is correct |
16 |
Correct |
635 ms |
123628 KB |
Output is correct |
17 |
Correct |
636 ms |
123584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1440 ms |
102944 KB |
Output is correct |
2 |
Correct |
1493 ms |
102712 KB |
Output is correct |
3 |
Correct |
1420 ms |
102876 KB |
Output is correct |
4 |
Correct |
1426 ms |
102988 KB |
Output is correct |
5 |
Correct |
1425 ms |
99764 KB |
Output is correct |
6 |
Correct |
1473 ms |
102984 KB |
Output is correct |
7 |
Correct |
1052 ms |
74252 KB |
Output is correct |
8 |
Correct |
1065 ms |
74188 KB |
Output is correct |
9 |
Correct |
1427 ms |
103144 KB |
Output is correct |
10 |
Correct |
1374 ms |
102936 KB |
Output is correct |
11 |
Correct |
1312 ms |
102892 KB |
Output is correct |
12 |
Correct |
1010 ms |
74148 KB |
Output is correct |
13 |
Incorrect |
693 ms |
71896 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
105412 KB |
Output is correct |
2 |
Correct |
290 ms |
105592 KB |
Output is correct |
3 |
Correct |
311 ms |
105496 KB |
Output is correct |
4 |
Correct |
304 ms |
105532 KB |
Output is correct |
5 |
Correct |
251 ms |
101248 KB |
Output is correct |
6 |
Correct |
34 ms |
44340 KB |
Output is correct |
7 |
Correct |
33 ms |
44236 KB |
Output is correct |
8 |
Correct |
33 ms |
44244 KB |
Output is correct |
9 |
Correct |
361 ms |
99520 KB |
Output is correct |
10 |
Correct |
356 ms |
99204 KB |
Output is correct |
11 |
Correct |
369 ms |
99544 KB |
Output is correct |
12 |
Correct |
322 ms |
96524 KB |
Output is correct |
13 |
Correct |
278 ms |
99780 KB |
Output is correct |
14 |
Correct |
30 ms |
43320 KB |
Output is correct |
15 |
Correct |
30 ms |
43340 KB |
Output is correct |
16 |
Correct |
33 ms |
43340 KB |
Output is correct |
17 |
Correct |
31 ms |
43340 KB |
Output is correct |
18 |
Correct |
33 ms |
43548 KB |
Output is correct |
19 |
Correct |
38 ms |
44028 KB |
Output is correct |
20 |
Correct |
41 ms |
43960 KB |
Output is correct |
21 |
Correct |
36 ms |
44012 KB |
Output is correct |
22 |
Correct |
39 ms |
44312 KB |
Output is correct |
23 |
Correct |
39 ms |
44348 KB |
Output is correct |
24 |
Correct |
37 ms |
44384 KB |
Output is correct |
25 |
Correct |
33 ms |
44240 KB |
Output is correct |
26 |
Correct |
39 ms |
44228 KB |
Output is correct |
27 |
Correct |
40 ms |
44268 KB |
Output is correct |
28 |
Correct |
40 ms |
44384 KB |
Output is correct |
29 |
Correct |
39 ms |
44236 KB |
Output is correct |
30 |
Correct |
39 ms |
44220 KB |
Output is correct |
31 |
Correct |
36 ms |
43712 KB |
Output is correct |
32 |
Correct |
41 ms |
44272 KB |
Output is correct |
33 |
Correct |
37 ms |
44024 KB |
Output is correct |
34 |
Correct |
36 ms |
43980 KB |
Output is correct |
35 |
Correct |
32 ms |
43544 KB |
Output is correct |
36 |
Correct |
357 ms |
106908 KB |
Output is correct |
37 |
Correct |
438 ms |
107716 KB |
Output is correct |
38 |
Correct |
342 ms |
107460 KB |
Output is correct |
39 |
Correct |
505 ms |
109324 KB |
Output is correct |
40 |
Correct |
687 ms |
108700 KB |
Output is correct |
41 |
Correct |
36 ms |
44492 KB |
Output is correct |
42 |
Correct |
35 ms |
44236 KB |
Output is correct |
43 |
Correct |
43 ms |
44340 KB |
Output is correct |
44 |
Correct |
899 ms |
106776 KB |
Output is correct |
45 |
Correct |
738 ms |
105148 KB |
Output is correct |
46 |
Correct |
789 ms |
104984 KB |
Output is correct |
47 |
Correct |
820 ms |
105284 KB |
Output is correct |
48 |
Correct |
648 ms |
123620 KB |
Output is correct |
49 |
Correct |
653 ms |
123708 KB |
Output is correct |
50 |
Correct |
656 ms |
123568 KB |
Output is correct |
51 |
Correct |
635 ms |
123628 KB |
Output is correct |
52 |
Correct |
636 ms |
123584 KB |
Output is correct |
53 |
Correct |
1440 ms |
102944 KB |
Output is correct |
54 |
Correct |
1493 ms |
102712 KB |
Output is correct |
55 |
Correct |
1420 ms |
102876 KB |
Output is correct |
56 |
Correct |
1426 ms |
102988 KB |
Output is correct |
57 |
Correct |
1425 ms |
99764 KB |
Output is correct |
58 |
Correct |
1473 ms |
102984 KB |
Output is correct |
59 |
Correct |
1052 ms |
74252 KB |
Output is correct |
60 |
Correct |
1065 ms |
74188 KB |
Output is correct |
61 |
Correct |
1427 ms |
103144 KB |
Output is correct |
62 |
Correct |
1374 ms |
102936 KB |
Output is correct |
63 |
Correct |
1312 ms |
102892 KB |
Output is correct |
64 |
Correct |
1010 ms |
74148 KB |
Output is correct |
65 |
Incorrect |
693 ms |
71896 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |