Submission #544596

# Submission time Handle Problem Language Result Execution time Memory
544596 2022-04-02T13:17:15 Z pavement Sprinkler (JOI22_sprinkler) C++17
100 / 100
3387 ms 324500 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;


#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
void read(int &v) {
	v = 0;
	char ch = getchar_unlocked();
	for (; ch < '0' || ch > '9'; ch = getchar_unlocked());
	for (; '0' <= ch && ch <= '9'; ch = getchar_unlocked())
		v = (v << 3) + (v << 1) + (ch & 15);
}

int N, L, Q, cent, cur_cent, cur_sz, anc[18][200005], dep[200005], H[200005], chd[200005], idx[200005], par[200005], sz[200005], dist_pre[200005][18];
bool proc[200005];
vector<int> adj[200005], adj2[200005];
vector<vector<int> > ft[200005], ft2[200005];

void init(int n, int e = -1) {
	anc[0][n] = e;
	for (int i = 1; i <= 17; i++)
		if (anc[i - 1][n] != -1) anc[i][n] = anc[i - 1][anc[i - 1][n]];
	for (auto u : adj[n]) if (u != e) {
		dep[u] = dep[n] + 1;
		init(u, n);
	}
}

int lca(int u, int v) {
	if (dep[u] > dep[v]) swap(u, v);
	for (int i = 17; i >= 0; i--)
		if (dep[v] - (1 << i) >= dep[u]) v = anc[i][v];
	if (u == v) return u;
	for (int i = 17; i >= 0; i--)
		if (anc[i][u] != anc[i][v]) {
			u = anc[i][u];
			v = anc[i][v];
		}
	return anc[0][u];
}

int dist(int u, int v) {
	int tmp = dep[u] + dep[v] - 2 * dep[lca(u, v)];
	return tmp;
}

int get_sz(int n, int e = -1) {
	par[n] = cur_cent;
	sz[n] = 1;
	for (auto u : adj[n]) if (u != e && !proc[u])
		sz[n] += get_sz(u, n);
	return sz[n];
}

void get_cent(int n, int e = -1) {
	int m = 0;
	for (auto u : adj[n]) if (u != e && !proc[u]) {
		m = max(m, sz[u]);
		get_cent(u, n);
	}
	m = max(m, cur_sz - sz[n]);
	if (m <= cur_sz / 2) cent = n;
}

void decomp(int n, int e = -1) {
	cur_cent = e;
	get_sz(n);
	cur_sz = sz[n];
	get_cent(n);
	int tmp_cent = cent;
	proc[cent] = 1;
	for (auto u : adj[tmp_cent])
		if (!proc[u]) decomp(u, tmp_cent);
}

int ls(int x) { return x & -x; }  

void pupd(int p, int a, int d, int v) {
	if (d < 0) return;
	for (int cpy_d = d; a <= chd[p]; a += ls(a))
		for (d = cpy_d + 1; d <= 41; d += ls(d))
			ft[p][a][d] = (ll)ft[p][a][d] * (ll)v % L;
}

void supd(int p, int a, int d, int v) {
	if (d < 0) return;
	int cpy_d = d + 1;
	for (a = chd[p] - a + 1; a <= chd[p]; a += ls(a))
		for (d = cpy_d; d <= 41; d += ls(d))
			ft2[p][a][d] = (ll)ft2[p][a][d] * (ll)v % L;
}

void upd(int p, int a, int d, int v) {
	if (d < 0) return;
	d = 40 - d;
	pupd(p, a, d, v);
	supd(p, a, d, v);
}

int pqry(int p, int a, int d) {
	if (d < 0) return 1;
	int r = 1;
	for (int cpy_d = d + 1; a; a -= ls(a))
		for (d = cpy_d; d; d -= ls(d))
			r = (ll)r * (ll)ft[p][a][d] % L;
	return r;
}

int sqry(int p, int a, int d) {
	if (d < 0) return 1;
	int r = 1, cpy_d = d + 1;
	for (a = chd[p] - a + 1; a; a -= ls(a))
		for (d = cpy_d; d; d -= ls(d)) r = (ll)r * (ll)ft2[p][a][d] % L;
	return r;
}

int qry(int p, int a, int d) {
	if (d > 40) return 1;
	d = 40 - d;
	if (a == -1) return pqry(p, chd[p], d);
	return (ll)pqry(p, a - 1, d) * (ll)sqry(p, a + 1, d) % L;
}

main() {
	memset(anc, -1, sizeof anc);
	read(N);
	read(L);
	for (int i = 1, u, v; i < N; i++) {
		read(u);
		read(v);
		adj[u].pb(v);
		adj[v].pb(u);
	}
	init(1);
	decomp(1);
	for (int i = 1; i <= N; i++)
		if (par[i] != -1) adj2[par[i]].pb(i);
	for (int i = 1; i <= N; i++) {
		chd[i] = adj2[i].size() + 1;
		for (int j = 0; j < adj2[i].size(); j++)
			idx[adj2[i][j]] = j + 2;
		ft[i].resize(chd[i] + 1);
		for (auto &j : ft[i]) j.resize(42, 1);
		ft2[i].resize(chd[i] + 1);
		for (auto &j : ft2[i]) j.resize(42, 1);
	}
	for (int i = 1; i <= N; i++)
		for (int k = 0, curr = i; curr != -1; curr = par[curr], k++)
			dist_pre[i][k] = dist(i, curr);
	for (int i = 1; i <= N; i++) read(H[i]);
	read(Q);
	for (int T, X, D, W; Q--; ) {
		read(T);
		if (T == 1) {
			read(X);
			read(D);
			read(W);
			for (int OX = X, prv = 1, k = 0; X != -1; prv = idx[X], X = par[X], k++)
				upd(X, prv, D - dist_pre[OX][k], W);
		} else {
			read(X);
			int ans = 1, OX = X;
			for (int prv = -1, k = 0; X != -1; prv = idx[X], X = par[X], k++)
				ans = (ll)ans * (ll)qry(X, prv, dist_pre[OX][k]) % L;
			printf("%lld\n", (ll)ans * (ll)H[OX] % L);
		}
	}
}

Compilation message

sprinkler.cpp:153:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  153 | main() {
      | ^~~~
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:169:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |   for (int j = 0; j < adj2[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 33108 KB Output is correct
2 Correct 20 ms 33184 KB Output is correct
3 Correct 17 ms 33140 KB Output is correct
4 Correct 19 ms 34516 KB Output is correct
5 Correct 22 ms 34516 KB Output is correct
6 Correct 19 ms 34516 KB Output is correct
7 Correct 38 ms 34596 KB Output is correct
8 Correct 23 ms 34516 KB Output is correct
9 Correct 26 ms 33128 KB Output is correct
10 Correct 19 ms 33240 KB Output is correct
11 Correct 16 ms 33236 KB Output is correct
12 Correct 18 ms 33248 KB Output is correct
13 Correct 20 ms 33180 KB Output is correct
14 Correct 18 ms 33128 KB Output is correct
15 Correct 17 ms 33236 KB Output is correct
16 Correct 17 ms 33108 KB Output is correct
17 Correct 21 ms 33232 KB Output is correct
18 Correct 20 ms 33132 KB Output is correct
19 Correct 17 ms 33176 KB Output is correct
20 Correct 17 ms 33196 KB Output is correct
21 Correct 17 ms 33232 KB Output is correct
22 Correct 18 ms 33236 KB Output is correct
23 Correct 16 ms 33128 KB Output is correct
24 Correct 16 ms 33236 KB Output is correct
25 Correct 21 ms 33140 KB Output is correct
26 Correct 16 ms 33144 KB Output is correct
27 Correct 17 ms 33148 KB Output is correct
28 Correct 17 ms 33208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 33164 KB Output is correct
2 Correct 2655 ms 305344 KB Output is correct
3 Correct 2134 ms 302104 KB Output is correct
4 Correct 3387 ms 316580 KB Output is correct
5 Correct 2426 ms 303868 KB Output is correct
6 Correct 1622 ms 304612 KB Output is correct
7 Correct 1547 ms 303788 KB Output is correct
8 Correct 884 ms 303024 KB Output is correct
9 Correct 3077 ms 324500 KB Output is correct
10 Correct 2937 ms 319672 KB Output is correct
11 Correct 2525 ms 305364 KB Output is correct
12 Correct 2273 ms 302124 KB Output is correct
13 Correct 674 ms 301636 KB Output is correct
14 Correct 793 ms 301644 KB Output is correct
15 Correct 760 ms 301892 KB Output is correct
16 Correct 842 ms 301972 KB Output is correct
17 Correct 906 ms 302588 KB Output is correct
18 Correct 17 ms 33184 KB Output is correct
19 Correct 18 ms 33236 KB Output is correct
20 Correct 24 ms 33236 KB Output is correct
21 Correct 16 ms 33236 KB Output is correct
22 Correct 16 ms 33160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 33164 KB Output is correct
2 Correct 2655 ms 305344 KB Output is correct
3 Correct 2134 ms 302104 KB Output is correct
4 Correct 3387 ms 316580 KB Output is correct
5 Correct 2426 ms 303868 KB Output is correct
6 Correct 1622 ms 304612 KB Output is correct
7 Correct 1547 ms 303788 KB Output is correct
8 Correct 884 ms 303024 KB Output is correct
9 Correct 3077 ms 324500 KB Output is correct
10 Correct 2937 ms 319672 KB Output is correct
11 Correct 2525 ms 305364 KB Output is correct
12 Correct 2273 ms 302124 KB Output is correct
13 Correct 674 ms 301636 KB Output is correct
14 Correct 793 ms 301644 KB Output is correct
15 Correct 760 ms 301892 KB Output is correct
16 Correct 842 ms 301972 KB Output is correct
17 Correct 906 ms 302588 KB Output is correct
18 Correct 17 ms 33184 KB Output is correct
19 Correct 18 ms 33236 KB Output is correct
20 Correct 24 ms 33236 KB Output is correct
21 Correct 16 ms 33236 KB Output is correct
22 Correct 16 ms 33160 KB Output is correct
23 Correct 16 ms 33160 KB Output is correct
24 Correct 2405 ms 305344 KB Output is correct
25 Correct 2322 ms 302248 KB Output is correct
26 Correct 2980 ms 321944 KB Output is correct
27 Correct 1997 ms 303776 KB Output is correct
28 Correct 1458 ms 303704 KB Output is correct
29 Correct 1210 ms 303688 KB Output is correct
30 Correct 756 ms 303008 KB Output is correct
31 Correct 2389 ms 318880 KB Output is correct
32 Correct 2336 ms 319768 KB Output is correct
33 Correct 1919 ms 305516 KB Output is correct
34 Correct 1891 ms 302096 KB Output is correct
35 Correct 16 ms 33236 KB Output is correct
36 Correct 16 ms 33108 KB Output is correct
37 Correct 17 ms 33168 KB Output is correct
38 Correct 19 ms 33248 KB Output is correct
39 Correct 19 ms 33228 KB Output is correct
40 Correct 17 ms 33196 KB Output is correct
41 Correct 17 ms 33136 KB Output is correct
42 Correct 17 ms 33212 KB Output is correct
43 Correct 17 ms 33180 KB Output is correct
44 Correct 17 ms 33196 KB Output is correct
45 Correct 18 ms 33148 KB Output is correct
46 Correct 17 ms 33152 KB Output is correct
47 Correct 17 ms 33236 KB Output is correct
48 Correct 17 ms 33140 KB Output is correct
49 Correct 18 ms 33232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 33108 KB Output is correct
2 Correct 2454 ms 321412 KB Output is correct
3 Correct 2997 ms 316408 KB Output is correct
4 Correct 2794 ms 317512 KB Output is correct
5 Correct 2201 ms 302160 KB Output is correct
6 Correct 1665 ms 302108 KB Output is correct
7 Correct 1514 ms 302332 KB Output is correct
8 Correct 881 ms 301444 KB Output is correct
9 Correct 2511 ms 316172 KB Output is correct
10 Correct 3011 ms 319412 KB Output is correct
11 Correct 2037 ms 302640 KB Output is correct
12 Correct 2687 ms 301968 KB Output is correct
13 Correct 2075 ms 301564 KB Output is correct
14 Correct 2309 ms 302492 KB Output is correct
15 Correct 17 ms 33108 KB Output is correct
16 Correct 18 ms 33108 KB Output is correct
17 Correct 17 ms 33236 KB Output is correct
18 Correct 17 ms 33180 KB Output is correct
19 Correct 20 ms 33236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 33128 KB Output is correct
2 Correct 2573 ms 318144 KB Output is correct
3 Correct 3053 ms 312736 KB Output is correct
4 Correct 2696 ms 315696 KB Output is correct
5 Correct 2259 ms 303824 KB Output is correct
6 Correct 1740 ms 303736 KB Output is correct
7 Correct 1544 ms 303448 KB Output is correct
8 Correct 892 ms 302784 KB Output is correct
9 Correct 2488 ms 323048 KB Output is correct
10 Correct 3076 ms 319988 KB Output is correct
11 Correct 2035 ms 305216 KB Output is correct
12 Correct 2604 ms 302160 KB Output is correct
13 Correct 2267 ms 301704 KB Output is correct
14 Correct 2261 ms 302716 KB Output is correct
15 Correct 16 ms 33108 KB Output is correct
16 Correct 16 ms 33236 KB Output is correct
17 Correct 18 ms 33140 KB Output is correct
18 Correct 20 ms 33236 KB Output is correct
19 Correct 17 ms 33244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 33108 KB Output is correct
2 Correct 20 ms 33184 KB Output is correct
3 Correct 17 ms 33140 KB Output is correct
4 Correct 19 ms 34516 KB Output is correct
5 Correct 22 ms 34516 KB Output is correct
6 Correct 19 ms 34516 KB Output is correct
7 Correct 38 ms 34596 KB Output is correct
8 Correct 23 ms 34516 KB Output is correct
9 Correct 26 ms 33128 KB Output is correct
10 Correct 19 ms 33240 KB Output is correct
11 Correct 16 ms 33236 KB Output is correct
12 Correct 18 ms 33248 KB Output is correct
13 Correct 20 ms 33180 KB Output is correct
14 Correct 18 ms 33128 KB Output is correct
15 Correct 17 ms 33236 KB Output is correct
16 Correct 17 ms 33108 KB Output is correct
17 Correct 21 ms 33232 KB Output is correct
18 Correct 20 ms 33132 KB Output is correct
19 Correct 17 ms 33176 KB Output is correct
20 Correct 17 ms 33196 KB Output is correct
21 Correct 17 ms 33232 KB Output is correct
22 Correct 18 ms 33236 KB Output is correct
23 Correct 16 ms 33128 KB Output is correct
24 Correct 16 ms 33236 KB Output is correct
25 Correct 21 ms 33140 KB Output is correct
26 Correct 16 ms 33144 KB Output is correct
27 Correct 17 ms 33148 KB Output is correct
28 Correct 17 ms 33208 KB Output is correct
29 Correct 19 ms 33164 KB Output is correct
30 Correct 2655 ms 305344 KB Output is correct
31 Correct 2134 ms 302104 KB Output is correct
32 Correct 3387 ms 316580 KB Output is correct
33 Correct 2426 ms 303868 KB Output is correct
34 Correct 1622 ms 304612 KB Output is correct
35 Correct 1547 ms 303788 KB Output is correct
36 Correct 884 ms 303024 KB Output is correct
37 Correct 3077 ms 324500 KB Output is correct
38 Correct 2937 ms 319672 KB Output is correct
39 Correct 2525 ms 305364 KB Output is correct
40 Correct 2273 ms 302124 KB Output is correct
41 Correct 674 ms 301636 KB Output is correct
42 Correct 793 ms 301644 KB Output is correct
43 Correct 760 ms 301892 KB Output is correct
44 Correct 842 ms 301972 KB Output is correct
45 Correct 906 ms 302588 KB Output is correct
46 Correct 17 ms 33184 KB Output is correct
47 Correct 18 ms 33236 KB Output is correct
48 Correct 24 ms 33236 KB Output is correct
49 Correct 16 ms 33236 KB Output is correct
50 Correct 16 ms 33160 KB Output is correct
51 Correct 16 ms 33160 KB Output is correct
52 Correct 2405 ms 305344 KB Output is correct
53 Correct 2322 ms 302248 KB Output is correct
54 Correct 2980 ms 321944 KB Output is correct
55 Correct 1997 ms 303776 KB Output is correct
56 Correct 1458 ms 303704 KB Output is correct
57 Correct 1210 ms 303688 KB Output is correct
58 Correct 756 ms 303008 KB Output is correct
59 Correct 2389 ms 318880 KB Output is correct
60 Correct 2336 ms 319768 KB Output is correct
61 Correct 1919 ms 305516 KB Output is correct
62 Correct 1891 ms 302096 KB Output is correct
63 Correct 16 ms 33236 KB Output is correct
64 Correct 16 ms 33108 KB Output is correct
65 Correct 17 ms 33168 KB Output is correct
66 Correct 19 ms 33248 KB Output is correct
67 Correct 19 ms 33228 KB Output is correct
68 Correct 17 ms 33196 KB Output is correct
69 Correct 17 ms 33136 KB Output is correct
70 Correct 17 ms 33212 KB Output is correct
71 Correct 17 ms 33180 KB Output is correct
72 Correct 17 ms 33196 KB Output is correct
73 Correct 18 ms 33148 KB Output is correct
74 Correct 17 ms 33152 KB Output is correct
75 Correct 17 ms 33236 KB Output is correct
76 Correct 17 ms 33140 KB Output is correct
77 Correct 18 ms 33232 KB Output is correct
78 Correct 16 ms 33108 KB Output is correct
79 Correct 2454 ms 321412 KB Output is correct
80 Correct 2997 ms 316408 KB Output is correct
81 Correct 2794 ms 317512 KB Output is correct
82 Correct 2201 ms 302160 KB Output is correct
83 Correct 1665 ms 302108 KB Output is correct
84 Correct 1514 ms 302332 KB Output is correct
85 Correct 881 ms 301444 KB Output is correct
86 Correct 2511 ms 316172 KB Output is correct
87 Correct 3011 ms 319412 KB Output is correct
88 Correct 2037 ms 302640 KB Output is correct
89 Correct 2687 ms 301968 KB Output is correct
90 Correct 2075 ms 301564 KB Output is correct
91 Correct 2309 ms 302492 KB Output is correct
92 Correct 17 ms 33108 KB Output is correct
93 Correct 18 ms 33108 KB Output is correct
94 Correct 17 ms 33236 KB Output is correct
95 Correct 17 ms 33180 KB Output is correct
96 Correct 20 ms 33236 KB Output is correct
97 Correct 19 ms 33128 KB Output is correct
98 Correct 2573 ms 318144 KB Output is correct
99 Correct 3053 ms 312736 KB Output is correct
100 Correct 2696 ms 315696 KB Output is correct
101 Correct 2259 ms 303824 KB Output is correct
102 Correct 1740 ms 303736 KB Output is correct
103 Correct 1544 ms 303448 KB Output is correct
104 Correct 892 ms 302784 KB Output is correct
105 Correct 2488 ms 323048 KB Output is correct
106 Correct 3076 ms 319988 KB Output is correct
107 Correct 2035 ms 305216 KB Output is correct
108 Correct 2604 ms 302160 KB Output is correct
109 Correct 2267 ms 301704 KB Output is correct
110 Correct 2261 ms 302716 KB Output is correct
111 Correct 16 ms 33108 KB Output is correct
112 Correct 16 ms 33236 KB Output is correct
113 Correct 18 ms 33140 KB Output is correct
114 Correct 20 ms 33236 KB Output is correct
115 Correct 17 ms 33244 KB Output is correct
116 Correct 2162 ms 303368 KB Output is correct
117 Correct 2751 ms 301908 KB Output is correct
118 Correct 2641 ms 322024 KB Output is correct
119 Correct 2328 ms 303804 KB Output is correct
120 Correct 1758 ms 303528 KB Output is correct
121 Correct 1616 ms 303744 KB Output is correct
122 Correct 944 ms 303108 KB Output is correct
123 Correct 2475 ms 320660 KB Output is correct
124 Correct 3136 ms 314196 KB Output is correct
125 Correct 2036 ms 304688 KB Output is correct
126 Correct 2698 ms 302308 KB Output is correct
127 Correct 2777 ms 302072 KB Output is correct
128 Correct 2332 ms 301916 KB Output is correct
129 Correct 2402 ms 302560 KB Output is correct