답안 #551763

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551763 2022-04-21T13:08:13 Z nonsensenonsense1 Sprinkler (JOI22_sprinkler) C++17
100 / 100
3121 ms 155708 KB
#pragma GCC optimize("O3,unroll-loops")
#include <cstdio>
#include <vector>

int md;
inline int mul(int a, int b) {
	return (long long)a * b % md;
}

const int N = 200005;
const int D = 40;
const int LG = 19;
const int K = 500;
std::vector<int> g[N], list[N], t[N];
int q, n, in[N], dep[N], pr[N], pos[N], h[N], start[N], out[N], amt, st[LG][N * 2], clz[N * 2], bounds[N][D + 1][2];

void dfs(int v) {
	static int dt = 0;
	in[v] = dt++;
	pos[v] = list[dep[v]].size();
	list[dep[v]].push_back(v);
	start[v] = amt;
	st[0][amt++] = dep[v];
	for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i] != pr[v]) {
		if (i) st[0][amt++] = dep[v];
		pr[g[v][i]] = v;
		dep[g[v][i]] = dep[v] + 1;
		dfs(g[v][i]);
	}
	out[v] = dt;
}

int l;
int dlca(int u, int v) {
	u = start[u];
	v = start[v];
	if (u > v) std::swap(u, v);
	++v;
	l = clz[v - u];
	return std::min(st[l][u], st[l][v - (1 << l)]);
}

int f(int u, int v) {
	return dep[u] + dep[v] - 2 * dlca(u, v);
}

void update(std::vector<int> &t, int l, int r, int x) {
	if (!x) {
		for (l += t.size() >> 1, r += t.size() >> 1; l < r; l >>= 1, r >>= 1) {
			if (l & 1) t[l++] = 0;
			if (r & 1) t[--r] = 0;
		}
		return;
	}
	for (l += t.size() >> 1, r += t.size() >> 1; l < r; l >>= 1, r >>= 1) {
		if (l & 1) {
			t[l] = mul(t[l], x);
			++l;
		}
		if (r & 1) {
			--r;
			t[r] = mul(t[r], x);
		}
	}
}

int get(std::vector<int> &t, int i) {
	int res = 1;
	for (i += t.size() >> 1; i > 0; i >>= 1) res = mul(res, t[i]);
	return res;
}

int sub_left(int v, int dep) {
	int l = 0, r = (int)list[dep].size();
	while (l < r) {
		int m = l + r >> 1;
		if (in[list[dep][m]] >= in[v]) r = m;
		else l = m + 1;
	}
	return r;
}

int sub_right(int v, int dep) {
	int l = 0, r = (int)list[dep].size();
	while (l < r) {
		int m = l + r >> 1;
		if (out[list[dep][m]] > out[v]) r = m;
		else l = m + 1;
	}
	return r;
}

bool par(int u, int v) {
	if (v == -1) return 0;
	return in[u] <= in[v] && out[u] >= out[v];
}

int main() {
	scanf("%d%d", &n, &md);
	clz[1] = 0;
	for (int i = 2; i <= 2 * n; ++i) {
		clz[i] = clz[i - 1];
		while (1 << clz[i] + 1 <= i) ++clz[i];
	}
	for (int i = 1; i < n; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a - 1].push_back(b - 1);
		g[b - 1].push_back(a - 1);
	}
	dfs(0);
	for (int j = 1; j < LG; ++j) for (int i = 0; i + (1 << j) <= 2 * n; ++i) st[j][i] = std::min(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
	for (int i = 0; i < n; ++i) scanf("%d", h + i);
	for (int i = 0; i < n; ++i) t[i].resize(list[i].size() << 1, 1);
	for (int i = 0; i < n; ++i) {
		int left = pos[i] ? list[dep[i]][pos[i] - 1] : -1, right = pos[i] + 1 < list[dep[i]].size() ? list[dep[i]][pos[i] + 1] : -1;
		for (int j = 0, v = i; j <= D; ++j) {
			if (!par(v, left)) bounds[v][j][0] = pos[i];
			if (!par(v, right)) bounds[v][j][1] = pos[i] + 1;
			if (!v) break;
			v = pr[v];
		}
	}
	scanf("%d", &q);
	while (q--) {
		int type, v;
		scanf("%d%d", &type, &v);
		--v;
		if (type == 1) {
			int dist, x;
			scanf("%d%d", &dist, &x);
			for (int i = std::min(n - 1, dep[v] + dist), j = v; i >= std::max(0, dep[v] - dist); --i) {
				while (j && dep[v] + i - 2 * (dep[j] - 1) <= dist) j = pr[j];
				int l = bounds[j][i - dep[j]][0], r = bounds[j][i - dep[j]][1];
				update(t[i], l, r, x);
			}
		} else printf("%d\n", mul(h[v], get(t[dep[v]], pos[v])));
	}
	return 0;
}

Compilation message

sprinkler.cpp: In function 'int sub_left(int, int)':
sprinkler.cpp:76:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   76 |   int m = l + r >> 1;
      |           ~~^~~
sprinkler.cpp: In function 'int sub_right(int, int)':
sprinkler.cpp:86:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int m = l + r >> 1;
      |           ~~^~~
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:103:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  103 |   while (1 << clz[i] + 1 <= i) ++clz[i];
      |               ~~~~~~~^~~
sprinkler.cpp:112:131: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  112 |  for (int j = 1; j < LG; ++j) for (int i = 0; i + (1 << j) <= 2 * n; ++i) st[j][i] = std::min(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
      |                                                                                                                                 ~~^~~
sprinkler.cpp:116:73: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |   int left = pos[i] ? list[dep[i]][pos[i] - 1] : -1, right = pos[i] + 1 < list[dep[i]].size() ? list[dep[i]][pos[i] + 1] : -1;
      |                                                              ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |  scanf("%d%d", &n, &md);
      |  ~~~~~^~~~~~~~~~~~~~~~~
sprinkler.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:113:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |  for (int i = 0; i < n; ++i) scanf("%d", h + i);
      |                              ~~~~~^~~~~~~~~~~~~
sprinkler.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
sprinkler.cpp:127:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |   scanf("%d%d", &type, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
sprinkler.cpp:131:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |    scanf("%d%d", &dist, &x);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14488 KB Output is correct
2 Correct 9 ms 14456 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 10 ms 15060 KB Output is correct
5 Correct 10 ms 14932 KB Output is correct
6 Correct 10 ms 14932 KB Output is correct
7 Correct 10 ms 14892 KB Output is correct
8 Correct 11 ms 15000 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 9 ms 14420 KB Output is correct
14 Correct 9 ms 14420 KB Output is correct
15 Correct 9 ms 14352 KB Output is correct
16 Correct 9 ms 14468 KB Output is correct
17 Correct 9 ms 14360 KB Output is correct
18 Correct 12 ms 14392 KB Output is correct
19 Correct 10 ms 14440 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 9 ms 14420 KB Output is correct
22 Correct 9 ms 14436 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14384 KB Output is correct
26 Correct 9 ms 14420 KB Output is correct
27 Correct 10 ms 14420 KB Output is correct
28 Correct 11 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 605 ms 125668 KB Output is correct
3 Correct 691 ms 122476 KB Output is correct
4 Correct 755 ms 139316 KB Output is correct
5 Correct 618 ms 124132 KB Output is correct
6 Correct 593 ms 123944 KB Output is correct
7 Correct 517 ms 124108 KB Output is correct
8 Correct 378 ms 124768 KB Output is correct
9 Correct 679 ms 150252 KB Output is correct
10 Correct 743 ms 144860 KB Output is correct
11 Correct 523 ms 125748 KB Output is correct
12 Correct 653 ms 122588 KB Output is correct
13 Correct 397 ms 123764 KB Output is correct
14 Correct 414 ms 123052 KB Output is correct
15 Correct 431 ms 123200 KB Output is correct
16 Correct 419 ms 123084 KB Output is correct
17 Correct 428 ms 123616 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 12 ms 14428 KB Output is correct
21 Correct 8 ms 14480 KB Output is correct
22 Correct 9 ms 14472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 605 ms 125668 KB Output is correct
3 Correct 691 ms 122476 KB Output is correct
4 Correct 755 ms 139316 KB Output is correct
5 Correct 618 ms 124132 KB Output is correct
6 Correct 593 ms 123944 KB Output is correct
7 Correct 517 ms 124108 KB Output is correct
8 Correct 378 ms 124768 KB Output is correct
9 Correct 679 ms 150252 KB Output is correct
10 Correct 743 ms 144860 KB Output is correct
11 Correct 523 ms 125748 KB Output is correct
12 Correct 653 ms 122588 KB Output is correct
13 Correct 397 ms 123764 KB Output is correct
14 Correct 414 ms 123052 KB Output is correct
15 Correct 431 ms 123200 KB Output is correct
16 Correct 419 ms 123084 KB Output is correct
17 Correct 428 ms 123616 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 12 ms 14428 KB Output is correct
21 Correct 8 ms 14480 KB Output is correct
22 Correct 9 ms 14472 KB Output is correct
23 Correct 9 ms 14460 KB Output is correct
24 Correct 604 ms 125712 KB Output is correct
25 Correct 645 ms 122624 KB Output is correct
26 Correct 702 ms 147348 KB Output is correct
27 Correct 598 ms 124300 KB Output is correct
28 Correct 489 ms 124172 KB Output is correct
29 Correct 496 ms 124156 KB Output is correct
30 Correct 378 ms 124864 KB Output is correct
31 Correct 626 ms 141772 KB Output is correct
32 Correct 755 ms 144844 KB Output is correct
33 Correct 503 ms 125756 KB Output is correct
34 Correct 775 ms 122480 KB Output is correct
35 Correct 9 ms 14420 KB Output is correct
36 Correct 9 ms 14420 KB Output is correct
37 Correct 9 ms 14420 KB Output is correct
38 Correct 9 ms 14468 KB Output is correct
39 Correct 9 ms 14476 KB Output is correct
40 Correct 8 ms 14420 KB Output is correct
41 Correct 9 ms 14416 KB Output is correct
42 Correct 8 ms 14420 KB Output is correct
43 Correct 9 ms 14420 KB Output is correct
44 Correct 8 ms 14420 KB Output is correct
45 Correct 9 ms 14420 KB Output is correct
46 Correct 9 ms 14420 KB Output is correct
47 Correct 8 ms 14420 KB Output is correct
48 Correct 9 ms 14440 KB Output is correct
49 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14448 KB Output is correct
2 Correct 862 ms 146996 KB Output is correct
3 Correct 2895 ms 139852 KB Output is correct
4 Correct 1281 ms 141404 KB Output is correct
5 Correct 1180 ms 122572 KB Output is correct
6 Correct 737 ms 122488 KB Output is correct
7 Correct 619 ms 122648 KB Output is correct
8 Correct 360 ms 123216 KB Output is correct
9 Correct 871 ms 139172 KB Output is correct
10 Correct 2882 ms 144076 KB Output is correct
11 Correct 781 ms 123044 KB Output is correct
12 Correct 2808 ms 123932 KB Output is correct
13 Correct 2462 ms 131800 KB Output is correct
14 Correct 2303 ms 132872 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 8 ms 14392 KB Output is correct
17 Correct 9 ms 14348 KB Output is correct
18 Correct 8 ms 14388 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 909 ms 140644 KB Output is correct
3 Correct 3067 ms 133704 KB Output is correct
4 Correct 1304 ms 137780 KB Output is correct
5 Correct 1197 ms 124364 KB Output is correct
6 Correct 769 ms 124220 KB Output is correct
7 Correct 674 ms 124052 KB Output is correct
8 Correct 375 ms 124580 KB Output is correct
9 Correct 917 ms 147948 KB Output is correct
10 Correct 3041 ms 145204 KB Output is correct
11 Correct 812 ms 125556 KB Output is correct
12 Correct 2826 ms 124380 KB Output is correct
13 Correct 2629 ms 132112 KB Output is correct
14 Correct 2306 ms 133140 KB Output is correct
15 Correct 9 ms 14376 KB Output is correct
16 Correct 10 ms 14388 KB Output is correct
17 Correct 9 ms 14396 KB Output is correct
18 Correct 9 ms 14472 KB Output is correct
19 Correct 8 ms 14380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14488 KB Output is correct
2 Correct 9 ms 14456 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 10 ms 15060 KB Output is correct
5 Correct 10 ms 14932 KB Output is correct
6 Correct 10 ms 14932 KB Output is correct
7 Correct 10 ms 14892 KB Output is correct
8 Correct 11 ms 15000 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 9 ms 14420 KB Output is correct
14 Correct 9 ms 14420 KB Output is correct
15 Correct 9 ms 14352 KB Output is correct
16 Correct 9 ms 14468 KB Output is correct
17 Correct 9 ms 14360 KB Output is correct
18 Correct 12 ms 14392 KB Output is correct
19 Correct 10 ms 14440 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 9 ms 14420 KB Output is correct
22 Correct 9 ms 14436 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14384 KB Output is correct
26 Correct 9 ms 14420 KB Output is correct
27 Correct 10 ms 14420 KB Output is correct
28 Correct 11 ms 14420 KB Output is correct
29 Correct 9 ms 14420 KB Output is correct
30 Correct 605 ms 125668 KB Output is correct
31 Correct 691 ms 122476 KB Output is correct
32 Correct 755 ms 139316 KB Output is correct
33 Correct 618 ms 124132 KB Output is correct
34 Correct 593 ms 123944 KB Output is correct
35 Correct 517 ms 124108 KB Output is correct
36 Correct 378 ms 124768 KB Output is correct
37 Correct 679 ms 150252 KB Output is correct
38 Correct 743 ms 144860 KB Output is correct
39 Correct 523 ms 125748 KB Output is correct
40 Correct 653 ms 122588 KB Output is correct
41 Correct 397 ms 123764 KB Output is correct
42 Correct 414 ms 123052 KB Output is correct
43 Correct 431 ms 123200 KB Output is correct
44 Correct 419 ms 123084 KB Output is correct
45 Correct 428 ms 123616 KB Output is correct
46 Correct 8 ms 14420 KB Output is correct
47 Correct 9 ms 14420 KB Output is correct
48 Correct 12 ms 14428 KB Output is correct
49 Correct 8 ms 14480 KB Output is correct
50 Correct 9 ms 14472 KB Output is correct
51 Correct 9 ms 14460 KB Output is correct
52 Correct 604 ms 125712 KB Output is correct
53 Correct 645 ms 122624 KB Output is correct
54 Correct 702 ms 147348 KB Output is correct
55 Correct 598 ms 124300 KB Output is correct
56 Correct 489 ms 124172 KB Output is correct
57 Correct 496 ms 124156 KB Output is correct
58 Correct 378 ms 124864 KB Output is correct
59 Correct 626 ms 141772 KB Output is correct
60 Correct 755 ms 144844 KB Output is correct
61 Correct 503 ms 125756 KB Output is correct
62 Correct 775 ms 122480 KB Output is correct
63 Correct 9 ms 14420 KB Output is correct
64 Correct 9 ms 14420 KB Output is correct
65 Correct 9 ms 14420 KB Output is correct
66 Correct 9 ms 14468 KB Output is correct
67 Correct 9 ms 14476 KB Output is correct
68 Correct 8 ms 14420 KB Output is correct
69 Correct 9 ms 14416 KB Output is correct
70 Correct 8 ms 14420 KB Output is correct
71 Correct 9 ms 14420 KB Output is correct
72 Correct 8 ms 14420 KB Output is correct
73 Correct 9 ms 14420 KB Output is correct
74 Correct 9 ms 14420 KB Output is correct
75 Correct 8 ms 14420 KB Output is correct
76 Correct 9 ms 14440 KB Output is correct
77 Correct 8 ms 14420 KB Output is correct
78 Correct 10 ms 14448 KB Output is correct
79 Correct 862 ms 146996 KB Output is correct
80 Correct 2895 ms 139852 KB Output is correct
81 Correct 1281 ms 141404 KB Output is correct
82 Correct 1180 ms 122572 KB Output is correct
83 Correct 737 ms 122488 KB Output is correct
84 Correct 619 ms 122648 KB Output is correct
85 Correct 360 ms 123216 KB Output is correct
86 Correct 871 ms 139172 KB Output is correct
87 Correct 2882 ms 144076 KB Output is correct
88 Correct 781 ms 123044 KB Output is correct
89 Correct 2808 ms 123932 KB Output is correct
90 Correct 2462 ms 131800 KB Output is correct
91 Correct 2303 ms 132872 KB Output is correct
92 Correct 9 ms 14420 KB Output is correct
93 Correct 8 ms 14392 KB Output is correct
94 Correct 9 ms 14348 KB Output is correct
95 Correct 8 ms 14388 KB Output is correct
96 Correct 8 ms 14420 KB Output is correct
97 Correct 9 ms 14336 KB Output is correct
98 Correct 909 ms 140644 KB Output is correct
99 Correct 3067 ms 133704 KB Output is correct
100 Correct 1304 ms 137780 KB Output is correct
101 Correct 1197 ms 124364 KB Output is correct
102 Correct 769 ms 124220 KB Output is correct
103 Correct 674 ms 124052 KB Output is correct
104 Correct 375 ms 124580 KB Output is correct
105 Correct 917 ms 147948 KB Output is correct
106 Correct 3041 ms 145204 KB Output is correct
107 Correct 812 ms 125556 KB Output is correct
108 Correct 2826 ms 124380 KB Output is correct
109 Correct 2629 ms 132112 KB Output is correct
110 Correct 2306 ms 133140 KB Output is correct
111 Correct 9 ms 14376 KB Output is correct
112 Correct 10 ms 14388 KB Output is correct
113 Correct 9 ms 14396 KB Output is correct
114 Correct 9 ms 14472 KB Output is correct
115 Correct 8 ms 14380 KB Output is correct
116 Correct 830 ms 131360 KB Output is correct
117 Correct 2910 ms 131052 KB Output is correct
118 Correct 1371 ms 155708 KB Output is correct
119 Correct 1213 ms 132844 KB Output is correct
120 Correct 877 ms 132544 KB Output is correct
121 Correct 883 ms 132816 KB Output is correct
122 Correct 421 ms 133556 KB Output is correct
123 Correct 912 ms 152468 KB Output is correct
124 Correct 3121 ms 145152 KB Output is correct
125 Correct 836 ms 132268 KB Output is correct
126 Correct 2935 ms 131168 KB Output is correct
127 Correct 3065 ms 131100 KB Output is correct
128 Correct 2642 ms 132072 KB Output is correct
129 Correct 2796 ms 124680 KB Output is correct