답안 #417374

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
417374 2021-06-03T15:45:59 Z parsabahrami Sumtree (INOI20_sumtree) C++17
50 / 100
1468 ms 120236 KB
#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

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;
}

int main() {
	scanf("%d%d", &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("%d%d", &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("%d\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("%d", &q); q; q--) {
		int t, v, x; scanf("%d%d", &t, &v);
		if (t < 2) {
			scanf("%d", &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 'int main()':
Main.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%d%d", &n, &R);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:91:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |   int u, v; scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:100:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |  for (scanf("%d", &q); q; q--) {
      |       ~~~~~^~~~~~~~~~
Main.cpp:101:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   int t, v, x; scanf("%d%d", &t, &v);
      |                ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:103:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |    scanf("%d", &x); ll sub = A->get(St[v]); B->upd(St[v], St[v] + 1, x);
      |    ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 98372 KB Output is correct
2 Correct 286 ms 98380 KB Output is correct
3 Correct 284 ms 98540 KB Output is correct
4 Correct 293 ms 98476 KB Output is correct
5 Correct 244 ms 93624 KB Output is correct
6 Correct 34 ms 40480 KB Output is correct
7 Correct 33 ms 40132 KB Output is correct
8 Correct 33 ms 40268 KB Output is correct
9 Correct 346 ms 89208 KB Output is correct
10 Correct 331 ms 89156 KB Output is correct
11 Correct 338 ms 89296 KB Output is correct
12 Correct 330 ms 86724 KB Output is correct
13 Correct 243 ms 93528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39460 KB Output is correct
2 Correct 30 ms 39460 KB Output is correct
3 Correct 30 ms 39476 KB Output is correct
4 Correct 30 ms 39492 KB Output is correct
5 Correct 33 ms 39636 KB Output is correct
6 Correct 36 ms 40012 KB Output is correct
7 Correct 36 ms 40012 KB Output is correct
8 Correct 35 ms 39956 KB Output is correct
9 Correct 38 ms 40236 KB Output is correct
10 Correct 40 ms 40408 KB Output is correct
11 Correct 39 ms 40300 KB Output is correct
12 Correct 33 ms 40312 KB Output is correct
13 Correct 39 ms 40332 KB Output is correct
14 Correct 38 ms 40368 KB Output is correct
15 Correct 40 ms 40500 KB Output is correct
16 Correct 41 ms 40220 KB Output is correct
17 Correct 38 ms 40340 KB Output is correct
18 Correct 36 ms 39888 KB Output is correct
19 Correct 38 ms 40320 KB Output is correct
20 Correct 36 ms 39936 KB Output is correct
21 Correct 36 ms 40016 KB Output is correct
22 Correct 33 ms 39632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 328 ms 98620 KB Output is correct
2 Correct 429 ms 99964 KB Output is correct
3 Correct 317 ms 99644 KB Output is correct
4 Correct 480 ms 101788 KB Output is correct
5 Correct 672 ms 100544 KB Output is correct
6 Correct 35 ms 40516 KB Output is correct
7 Correct 35 ms 40180 KB Output is correct
8 Correct 35 ms 40308 KB Output is correct
9 Correct 819 ms 95936 KB Output is correct
10 Correct 738 ms 94620 KB Output is correct
11 Correct 666 ms 94280 KB Output is correct
12 Correct 816 ms 94612 KB Output is correct
13 Correct 651 ms 120172 KB Output is correct
14 Correct 643 ms 120180 KB Output is correct
15 Correct 636 ms 120192 KB Output is correct
16 Correct 638 ms 120204 KB Output is correct
17 Correct 649 ms 120236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1372 ms 92364 KB Output is correct
2 Correct 1411 ms 92064 KB Output is correct
3 Correct 1468 ms 92316 KB Output is correct
4 Correct 1334 ms 92324 KB Output is correct
5 Correct 1381 ms 89496 KB Output is correct
6 Correct 1383 ms 92200 KB Output is correct
7 Correct 1054 ms 67068 KB Output is correct
8 Correct 994 ms 67276 KB Output is correct
9 Correct 1411 ms 92284 KB Output is correct
10 Correct 1303 ms 92288 KB Output is correct
11 Correct 1260 ms 92344 KB Output is correct
12 Correct 1000 ms 67124 KB Output is correct
13 Incorrect 685 ms 65164 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 98372 KB Output is correct
2 Correct 286 ms 98380 KB Output is correct
3 Correct 284 ms 98540 KB Output is correct
4 Correct 293 ms 98476 KB Output is correct
5 Correct 244 ms 93624 KB Output is correct
6 Correct 34 ms 40480 KB Output is correct
7 Correct 33 ms 40132 KB Output is correct
8 Correct 33 ms 40268 KB Output is correct
9 Correct 346 ms 89208 KB Output is correct
10 Correct 331 ms 89156 KB Output is correct
11 Correct 338 ms 89296 KB Output is correct
12 Correct 330 ms 86724 KB Output is correct
13 Correct 243 ms 93528 KB Output is correct
14 Correct 29 ms 39460 KB Output is correct
15 Correct 30 ms 39460 KB Output is correct
16 Correct 30 ms 39476 KB Output is correct
17 Correct 30 ms 39492 KB Output is correct
18 Correct 33 ms 39636 KB Output is correct
19 Correct 36 ms 40012 KB Output is correct
20 Correct 36 ms 40012 KB Output is correct
21 Correct 35 ms 39956 KB Output is correct
22 Correct 38 ms 40236 KB Output is correct
23 Correct 40 ms 40408 KB Output is correct
24 Correct 39 ms 40300 KB Output is correct
25 Correct 33 ms 40312 KB Output is correct
26 Correct 39 ms 40332 KB Output is correct
27 Correct 38 ms 40368 KB Output is correct
28 Correct 40 ms 40500 KB Output is correct
29 Correct 41 ms 40220 KB Output is correct
30 Correct 38 ms 40340 KB Output is correct
31 Correct 36 ms 39888 KB Output is correct
32 Correct 38 ms 40320 KB Output is correct
33 Correct 36 ms 39936 KB Output is correct
34 Correct 36 ms 40016 KB Output is correct
35 Correct 33 ms 39632 KB Output is correct
36 Correct 328 ms 98620 KB Output is correct
37 Correct 429 ms 99964 KB Output is correct
38 Correct 317 ms 99644 KB Output is correct
39 Correct 480 ms 101788 KB Output is correct
40 Correct 672 ms 100544 KB Output is correct
41 Correct 35 ms 40516 KB Output is correct
42 Correct 35 ms 40180 KB Output is correct
43 Correct 35 ms 40308 KB Output is correct
44 Correct 819 ms 95936 KB Output is correct
45 Correct 738 ms 94620 KB Output is correct
46 Correct 666 ms 94280 KB Output is correct
47 Correct 816 ms 94612 KB Output is correct
48 Correct 651 ms 120172 KB Output is correct
49 Correct 643 ms 120180 KB Output is correct
50 Correct 636 ms 120192 KB Output is correct
51 Correct 638 ms 120204 KB Output is correct
52 Correct 649 ms 120236 KB Output is correct
53 Correct 1372 ms 92364 KB Output is correct
54 Correct 1411 ms 92064 KB Output is correct
55 Correct 1468 ms 92316 KB Output is correct
56 Correct 1334 ms 92324 KB Output is correct
57 Correct 1381 ms 89496 KB Output is correct
58 Correct 1383 ms 92200 KB Output is correct
59 Correct 1054 ms 67068 KB Output is correct
60 Correct 994 ms 67276 KB Output is correct
61 Correct 1411 ms 92284 KB Output is correct
62 Correct 1303 ms 92288 KB Output is correct
63 Correct 1260 ms 92344 KB Output is correct
64 Correct 1000 ms 67124 KB Output is correct
65 Incorrect 685 ms 65164 KB Output isn't correct
66 Halted 0 ms 0 KB -