답안 #551732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551732 2022-04-21T11:54:00 Z nonsensenonsense1 Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 71996 KB
#include <cstdio>
#include <vector>

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

const int N = 200000;
const int LG = 18;
const int K = 500;
std::vector<int> g[N], list[N], t[N];
int q, n, dep[N], pr[LG][N], in[N], out[N], pos[N], h[N];

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

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

int lca(int u, int v) {
	if (par(u, v)) return u;
	for (int i = LG - 1; i >= 0; --i) if (!par(pr[i][u], v)) u = pr[i][u];
	return pr[0][u];
}

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

void update(std::vector<int> &t, int l, int r, int x) {
	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 main() {
	scanf("%d%d", &n, &md);
	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 < n; ++i) pr[j][i] = pr[j - 1][pr[j - 1][i]];
	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);
	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 = dep[v] + dist; i >= dep[v] - dist; --i) {
				int l = 0, r = list[i].size();
				while (l < r) {
					int m = l + r >> 1;
					if (out[list[i][m]] >= out[v] || f(v, list[i][m]) <= dist) r = m;
					else l = m + 1;
				}
				int fi = r;
				l = 0, r = list[i].size();
				while (l < r) {
					int m = l + r >> 1;
					if (out[list[i][m]] >= out[v] && f(v, list[i][m]) > dist) r = m;
					else l = m + 1;
				}
				update(t[i], fi, r, x);
			}
		} else printf("%d\n", mul(h[v], get(t[dep[v]], pos[v])));
	}
	return 0;
}

Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:84:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |      int m = l + r >> 1;
      |              ~~^~~
sprinkler.cpp:91:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   91 |      int m = l + r >> 1;
      |              ~~^~~
sprinkler.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  scanf("%d%d", &n, &md);
      |  ~~~~~^~~~~~~~~~~~~~~~~
sprinkler.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:71:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |  for (int i = 0; i < n; ++i) scanf("%d", h + i);
      |                              ~~~~~^~~~~~~~~~~~~
sprinkler.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
sprinkler.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   scanf("%d%d", &type, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
sprinkler.cpp:80:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |    scanf("%d%d", &dist, &x);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14484 KB Output is correct
4 Correct 12 ms 14700 KB Output is correct
5 Correct 20 ms 14548 KB Output is correct
6 Correct 14 ms 14564 KB Output is correct
7 Correct 13 ms 14640 KB Output is correct
8 Correct 11 ms 14588 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14444 KB Output is correct
13 Correct 9 ms 14444 KB Output is correct
14 Correct 9 ms 14420 KB Output is correct
15 Correct 8 ms 14420 KB Output is correct
16 Correct 8 ms 14504 KB Output is correct
17 Correct 8 ms 14420 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 8 ms 14392 KB Output is correct
21 Correct 11 ms 14420 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 9 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 718 ms 46136 KB Output is correct
3 Correct 2979 ms 47412 KB Output is correct
4 Correct 908 ms 62356 KB Output is correct
5 Correct 1560 ms 48836 KB Output is correct
6 Correct 1585 ms 48516 KB Output is correct
7 Correct 1477 ms 48756 KB Output is correct
8 Correct 1145 ms 49272 KB Output is correct
9 Correct 595 ms 71996 KB Output is correct
10 Correct 1373 ms 66712 KB Output is correct
11 Correct 726 ms 50352 KB Output is correct
12 Correct 2913 ms 47300 KB Output is correct
13 Correct 765 ms 48164 KB Output is correct
14 Correct 1474 ms 47580 KB Output is correct
15 Correct 1876 ms 43796 KB Output is correct
16 Correct 1996 ms 46940 KB Output is correct
17 Correct 2251 ms 47248 KB Output is correct
18 Correct 8 ms 14524 KB Output is correct
19 Correct 8 ms 14484 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14520 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 718 ms 46136 KB Output is correct
3 Correct 2979 ms 47412 KB Output is correct
4 Correct 908 ms 62356 KB Output is correct
5 Correct 1560 ms 48836 KB Output is correct
6 Correct 1585 ms 48516 KB Output is correct
7 Correct 1477 ms 48756 KB Output is correct
8 Correct 1145 ms 49272 KB Output is correct
9 Correct 595 ms 71996 KB Output is correct
10 Correct 1373 ms 66712 KB Output is correct
11 Correct 726 ms 50352 KB Output is correct
12 Correct 2913 ms 47300 KB Output is correct
13 Correct 765 ms 48164 KB Output is correct
14 Correct 1474 ms 47580 KB Output is correct
15 Correct 1876 ms 43796 KB Output is correct
16 Correct 1996 ms 46940 KB Output is correct
17 Correct 2251 ms 47248 KB Output is correct
18 Correct 8 ms 14524 KB Output is correct
19 Correct 8 ms 14484 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14520 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 9 ms 14388 KB Output is correct
24 Correct 839 ms 50352 KB Output is correct
25 Correct 3903 ms 47224 KB Output is correct
26 Correct 891 ms 69184 KB Output is correct
27 Correct 1757 ms 48748 KB Output is correct
28 Correct 1976 ms 48724 KB Output is correct
29 Correct 1970 ms 48812 KB Output is correct
30 Correct 1207 ms 49436 KB Output is correct
31 Correct 657 ms 64584 KB Output is correct
32 Correct 1673 ms 66836 KB Output is correct
33 Correct 852 ms 50588 KB Output is correct
34 Correct 3971 ms 47308 KB Output is correct
35 Correct 8 ms 14420 KB Output is correct
36 Correct 8 ms 14420 KB Output is correct
37 Correct 8 ms 14520 KB Output is correct
38 Correct 11 ms 14528 KB Output is correct
39 Correct 9 ms 14420 KB Output is correct
40 Correct 9 ms 14420 KB Output is correct
41 Correct 9 ms 14396 KB Output is correct
42 Correct 8 ms 14420 KB Output is correct
43 Correct 8 ms 14420 KB Output is correct
44 Correct 8 ms 14420 KB Output is correct
45 Correct 8 ms 14420 KB Output is correct
46 Correct 8 ms 14420 KB Output is correct
47 Correct 8 ms 14436 KB Output is correct
48 Correct 8 ms 14420 KB Output is correct
49 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 1048 ms 63444 KB Output is correct
3 Execution timed out 4078 ms 57924 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 1423 ms 58100 KB Output is correct
3 Execution timed out 4066 ms 55416 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14484 KB Output is correct
4 Correct 12 ms 14700 KB Output is correct
5 Correct 20 ms 14548 KB Output is correct
6 Correct 14 ms 14564 KB Output is correct
7 Correct 13 ms 14640 KB Output is correct
8 Correct 11 ms 14588 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14444 KB Output is correct
13 Correct 9 ms 14444 KB Output is correct
14 Correct 9 ms 14420 KB Output is correct
15 Correct 8 ms 14420 KB Output is correct
16 Correct 8 ms 14504 KB Output is correct
17 Correct 8 ms 14420 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 8 ms 14392 KB Output is correct
21 Correct 11 ms 14420 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 9 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 718 ms 46136 KB Output is correct
31 Correct 2979 ms 47412 KB Output is correct
32 Correct 908 ms 62356 KB Output is correct
33 Correct 1560 ms 48836 KB Output is correct
34 Correct 1585 ms 48516 KB Output is correct
35 Correct 1477 ms 48756 KB Output is correct
36 Correct 1145 ms 49272 KB Output is correct
37 Correct 595 ms 71996 KB Output is correct
38 Correct 1373 ms 66712 KB Output is correct
39 Correct 726 ms 50352 KB Output is correct
40 Correct 2913 ms 47300 KB Output is correct
41 Correct 765 ms 48164 KB Output is correct
42 Correct 1474 ms 47580 KB Output is correct
43 Correct 1876 ms 43796 KB Output is correct
44 Correct 1996 ms 46940 KB Output is correct
45 Correct 2251 ms 47248 KB Output is correct
46 Correct 8 ms 14524 KB Output is correct
47 Correct 8 ms 14484 KB Output is correct
48 Correct 7 ms 14420 KB Output is correct
49 Correct 8 ms 14520 KB Output is correct
50 Correct 8 ms 14420 KB Output is correct
51 Correct 9 ms 14388 KB Output is correct
52 Correct 839 ms 50352 KB Output is correct
53 Correct 3903 ms 47224 KB Output is correct
54 Correct 891 ms 69184 KB Output is correct
55 Correct 1757 ms 48748 KB Output is correct
56 Correct 1976 ms 48724 KB Output is correct
57 Correct 1970 ms 48812 KB Output is correct
58 Correct 1207 ms 49436 KB Output is correct
59 Correct 657 ms 64584 KB Output is correct
60 Correct 1673 ms 66836 KB Output is correct
61 Correct 852 ms 50588 KB Output is correct
62 Correct 3971 ms 47308 KB Output is correct
63 Correct 8 ms 14420 KB Output is correct
64 Correct 8 ms 14420 KB Output is correct
65 Correct 8 ms 14520 KB Output is correct
66 Correct 11 ms 14528 KB Output is correct
67 Correct 9 ms 14420 KB Output is correct
68 Correct 9 ms 14420 KB Output is correct
69 Correct 9 ms 14396 KB Output is correct
70 Correct 8 ms 14420 KB Output is correct
71 Correct 8 ms 14420 KB Output is correct
72 Correct 8 ms 14420 KB Output is correct
73 Correct 8 ms 14420 KB Output is correct
74 Correct 8 ms 14420 KB Output is correct
75 Correct 8 ms 14436 KB Output is correct
76 Correct 8 ms 14420 KB Output is correct
77 Correct 8 ms 14420 KB Output is correct
78 Correct 8 ms 14420 KB Output is correct
79 Correct 1048 ms 63444 KB Output is correct
80 Execution timed out 4078 ms 57924 KB Time limit exceeded
81 Halted 0 ms 0 KB -