Submission #789197

# Submission time Handle Problem Language Result Execution time Memory
789197 2023-07-21T07:30:56 Z 박영우(#10045) Tourism (JOI23_tourism) C++17
100 / 100
428 ms 93684 KB
#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 1010101
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
struct fenwick {
	vector<int> arr;
	vector<int> tree;
	int N;
	fenwick(int N = 0) :N(N) { arr = tree = vector<int>(N + 1); }
	void upd(int i, int x) { x -= arr[i]; arr[i] += x; while (i <= N) { tree[i] += x, i += i & -i; } }
	int get(int i) { int ans = 0; while (i) { ans += tree[i], i -= i & -i; } return ans; }
};
int N, M, Q;
namespace segtree {
	int tree[MAX * 4];
	int N;
	void init(int _N) { N = _N; fill(tree, tree + MAX * 4 - 1, 1e9); }
	void upd(int s, int e, int i, int x, int loc = 1) {
		if (e < i || i < s) return;
		if (s == e) {
			tree[loc] = x;
			return;
		}
		int m = s + e >> 1;
		upd(s, m, i, x, loc * 2);
		upd(m + 1, e, i, x, loc * 2 + 1);
		tree[loc] = min(tree[loc * 2], tree[loc * 2 + 1]);
	}
	void upd(int i, int x) { upd(1, N, i, x); }
	int query(int s, int e, int l, int r, int loc = 1) {
		if (e < l || r < s) return 1e9;
		if (l <= s && e <= r) return tree[loc];
		int m = s + e >> 1;
		return min(query(s, m, l, r, loc * 2), query(m + 1, e, l, r, loc * 2 + 1));
	}
	int query(int l, int r) { return query(1, N, l, r); }
}
vector<int> adj[MAX];
vector<int> qv[MAX];
int R[MAX];
int C[MAX];
int dep[MAX];
int sp[MAX][MAXS];
pii range[MAX];
int cnt;
int ans[MAX];
int mdep[MAX];
void dfs(int x, int p = 0) {
	cnt++;
	range[x] = { cnt, cnt };
	sp[x][0] = p;
	for (int i = 1; i < MAXS; i++) sp[x][i] = sp[sp[x][i - 1]][i - 1];
	for (auto v : adj[x]) if (v != p) {
		dep[v] = dep[x] + 1;
		dfs(v, x);
		range[x].second = max(range[x].second, range[v].second);
	}
}
inline int lca(int u, int v) {
	if (!u) return v;
	if (!v) return u;
	int i;
	if (dep[u] != dep[v]) {
		if (dep[u] > dep[v]) swap(u, v);
		int d = dep[v] - dep[u];
		for (i = 0; i < MAXS; i++) if (d >> i & 1) v = sp[v][i];
	}
	if (u == v) return u;
	for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
	return sp[u][0];
}
namespace ltree {
	int tree[MAX * 4];
	void init(int s, int e, int loc = 1) {
		if (s == e) {
			tree[loc] = C[e];
			return;
		}
		int m = s + e >> 1;
		init(s, m, loc * 2);
		init(m + 1, e, loc * 2 + 1);
		tree[loc] = lca(tree[loc * 2], tree[loc * 2 + 1]);
	}
	int query(int s, int e, int l, int r, int loc = 1) {
		if (e < l || r < s) return 0;
		if (l <= s && e <= r) return tree[loc];
		int m = s + e >> 1;
		return lca(query(s, m, l, r, loc * 2), query(m + 1, e, l, r, loc * 2 + 1));
	}
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M >> Q;
	int i, j, a, b;
	for (i = 1; i < N; i++) {
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	int l;
	for (i = 1; i <= M; i++) cin >> C[i];
	dep[1] = 1;
	dfs(1);
	for (i = 1; i <= Q; i++) {
		cin >> l >> R[i];
		if (l == R[i]) ans[i] = 1;
		else qv[l].push_back(i);
	}
	fenwick fen(M);
	segtree::init(N);
	segtree::upd(range[C[M]].first, M);
	ltree::init(1, M);
	fen.upd(M, dep[C[M]]);
	for (l = M - 1; l >= 1; l--) {
		int v = C[l];
		while (v) {
			int ml = segtree::query(range[v].first, range[v].second);
			if (ml >= 1e9) {
				v = sp[v][0];
				continue;
			}
			int d = mdep[ml];
			mdep[ml] = dep[v];
			fen.upd(ml, dep[C[ml]] - mdep[ml]);
			int dd = dep[v] - d;
			for (i = 0; i < MAXS; i++) if (dd >> i & 1) v = sp[v][i];
		}
		segtree::upd(range[C[l]].first, l);
		fen.upd(l, dep[C[l]]);
		for (auto q : qv[l]) ans[q] = fen.get(R[q]) - dep[ltree::query(1, M, l, R[q])] + 1;
	}
	for (i = 1; i <= Q; i++) cout << ans[i] << ln;
}

Compilation message

tourism.cpp: In function 'void segtree::upd(int, int, int, int, int)':
tourism.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |   int m = s + e >> 1;
      |           ~~^~~
tourism.cpp: In function 'int segtree::query(int, int, int, int, int)':
tourism.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |   int m = s + e >> 1;
      |           ~~^~~
tourism.cpp: In function 'void ltree::init(int, int, int)':
tourism.cpp:91:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   91 |   int m = s + e >> 1;
      |           ~~^~~
tourism.cpp: In function 'int ltree::query(int, int, int, int, int)':
tourism.cpp:99:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |   int m = s + e >> 1;
      |           ~~^~~
tourism.cpp: In function 'int main()':
tourism.cpp:106:9: warning: unused variable 'j' [-Wunused-variable]
  106 |  int i, j, a, b;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 30 ms 63548 KB Output is correct
2 Correct 29 ms 63580 KB Output is correct
3 Correct 30 ms 63520 KB Output is correct
4 Correct 28 ms 63584 KB Output is correct
5 Correct 29 ms 63564 KB Output is correct
6 Correct 28 ms 63628 KB Output is correct
7 Correct 30 ms 63612 KB Output is correct
8 Correct 29 ms 63572 KB Output is correct
9 Correct 30 ms 63692 KB Output is correct
10 Correct 31 ms 63608 KB Output is correct
11 Correct 30 ms 63640 KB Output is correct
12 Correct 33 ms 63652 KB Output is correct
13 Correct 29 ms 63564 KB Output is correct
14 Correct 31 ms 63700 KB Output is correct
15 Correct 35 ms 63648 KB Output is correct
16 Correct 30 ms 63700 KB Output is correct
17 Correct 33 ms 63688 KB Output is correct
18 Correct 31 ms 63636 KB Output is correct
19 Correct 32 ms 63692 KB Output is correct
20 Correct 29 ms 63668 KB Output is correct
21 Correct 34 ms 63708 KB Output is correct
22 Correct 29 ms 63584 KB Output is correct
23 Correct 29 ms 63700 KB Output is correct
24 Correct 39 ms 63684 KB Output is correct
25 Correct 35 ms 63592 KB Output is correct
26 Correct 31 ms 63588 KB Output is correct
27 Correct 31 ms 63572 KB Output is correct
28 Correct 30 ms 63552 KB Output is correct
29 Correct 31 ms 63588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 63548 KB Output is correct
2 Correct 29 ms 63580 KB Output is correct
3 Correct 30 ms 63520 KB Output is correct
4 Correct 28 ms 63584 KB Output is correct
5 Correct 29 ms 63564 KB Output is correct
6 Correct 28 ms 63628 KB Output is correct
7 Correct 30 ms 63612 KB Output is correct
8 Correct 29 ms 63572 KB Output is correct
9 Correct 30 ms 63692 KB Output is correct
10 Correct 31 ms 63608 KB Output is correct
11 Correct 30 ms 63640 KB Output is correct
12 Correct 33 ms 63652 KB Output is correct
13 Correct 29 ms 63564 KB Output is correct
14 Correct 31 ms 63700 KB Output is correct
15 Correct 35 ms 63648 KB Output is correct
16 Correct 30 ms 63700 KB Output is correct
17 Correct 33 ms 63688 KB Output is correct
18 Correct 31 ms 63636 KB Output is correct
19 Correct 32 ms 63692 KB Output is correct
20 Correct 29 ms 63668 KB Output is correct
21 Correct 34 ms 63708 KB Output is correct
22 Correct 29 ms 63584 KB Output is correct
23 Correct 29 ms 63700 KB Output is correct
24 Correct 39 ms 63684 KB Output is correct
25 Correct 35 ms 63592 KB Output is correct
26 Correct 31 ms 63588 KB Output is correct
27 Correct 31 ms 63572 KB Output is correct
28 Correct 30 ms 63552 KB Output is correct
29 Correct 31 ms 63588 KB Output is correct
30 Correct 32 ms 63912 KB Output is correct
31 Correct 34 ms 63828 KB Output is correct
32 Correct 34 ms 63908 KB Output is correct
33 Correct 33 ms 63920 KB Output is correct
34 Correct 35 ms 64044 KB Output is correct
35 Correct 33 ms 63892 KB Output is correct
36 Correct 37 ms 63956 KB Output is correct
37 Correct 36 ms 63924 KB Output is correct
38 Correct 41 ms 64084 KB Output is correct
39 Correct 34 ms 64136 KB Output is correct
40 Correct 33 ms 64084 KB Output is correct
41 Correct 35 ms 64084 KB Output is correct
42 Correct 31 ms 64108 KB Output is correct
43 Correct 34 ms 64008 KB Output is correct
44 Correct 35 ms 64096 KB Output is correct
45 Correct 33 ms 64048 KB Output is correct
46 Correct 33 ms 64084 KB Output is correct
47 Correct 35 ms 64044 KB Output is correct
48 Correct 34 ms 63948 KB Output is correct
49 Correct 33 ms 64064 KB Output is correct
50 Correct 31 ms 63956 KB Output is correct
51 Correct 33 ms 64032 KB Output is correct
52 Correct 34 ms 64120 KB Output is correct
53 Correct 37 ms 63936 KB Output is correct
54 Correct 33 ms 63912 KB Output is correct
55 Correct 33 ms 63916 KB Output is correct
56 Correct 31 ms 63684 KB Output is correct
57 Correct 32 ms 63876 KB Output is correct
58 Correct 33 ms 63972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 63572 KB Output is correct
2 Correct 29 ms 63556 KB Output is correct
3 Correct 30 ms 63640 KB Output is correct
4 Correct 228 ms 80424 KB Output is correct
5 Correct 197 ms 84432 KB Output is correct
6 Correct 211 ms 87616 KB Output is correct
7 Correct 365 ms 90908 KB Output is correct
8 Correct 304 ms 90928 KB Output is correct
9 Correct 336 ms 90820 KB Output is correct
10 Correct 428 ms 90856 KB Output is correct
11 Correct 330 ms 90828 KB Output is correct
12 Correct 185 ms 89736 KB Output is correct
13 Correct 198 ms 89716 KB Output is correct
14 Correct 186 ms 89760 KB Output is correct
15 Correct 77 ms 86116 KB Output is correct
16 Correct 201 ms 88668 KB Output is correct
17 Correct 97 ms 68892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 63572 KB Output is correct
2 Correct 141 ms 71756 KB Output is correct
3 Correct 199 ms 73340 KB Output is correct
4 Correct 156 ms 74028 KB Output is correct
5 Correct 246 ms 78344 KB Output is correct
6 Correct 240 ms 78456 KB Output is correct
7 Correct 251 ms 78480 KB Output is correct
8 Correct 277 ms 78356 KB Output is correct
9 Correct 243 ms 78364 KB Output is correct
10 Correct 245 ms 78436 KB Output is correct
11 Correct 255 ms 78480 KB Output is correct
12 Correct 245 ms 78596 KB Output is correct
13 Correct 289 ms 78900 KB Output is correct
14 Correct 259 ms 79548 KB Output is correct
15 Correct 273 ms 79372 KB Output is correct
16 Correct 268 ms 78836 KB Output is correct
17 Correct 243 ms 78860 KB Output is correct
18 Correct 245 ms 78824 KB Output is correct
19 Correct 291 ms 78548 KB Output is correct
20 Correct 336 ms 78500 KB Output is correct
21 Correct 259 ms 78488 KB Output is correct
22 Correct 256 ms 78440 KB Output is correct
23 Correct 290 ms 78432 KB Output is correct
24 Correct 264 ms 78396 KB Output is correct
25 Correct 263 ms 78412 KB Output is correct
26 Correct 288 ms 78604 KB Output is correct
27 Correct 286 ms 78560 KB Output is correct
28 Correct 258 ms 78536 KB Output is correct
29 Correct 275 ms 78572 KB Output is correct
30 Correct 292 ms 78760 KB Output is correct
31 Correct 288 ms 78892 KB Output is correct
32 Correct 275 ms 79432 KB Output is correct
33 Correct 325 ms 79928 KB Output is correct
34 Correct 273 ms 79596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 63572 KB Output is correct
2 Correct 29 ms 63540 KB Output is correct
3 Correct 30 ms 63688 KB Output is correct
4 Correct 273 ms 75820 KB Output is correct
5 Correct 313 ms 78208 KB Output is correct
6 Correct 329 ms 82136 KB Output is correct
7 Correct 342 ms 84284 KB Output is correct
8 Correct 319 ms 84300 KB Output is correct
9 Correct 328 ms 84360 KB Output is correct
10 Correct 320 ms 84280 KB Output is correct
11 Correct 347 ms 84388 KB Output is correct
12 Correct 353 ms 84360 KB Output is correct
13 Correct 381 ms 84316 KB Output is correct
14 Correct 90 ms 70336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 63548 KB Output is correct
2 Correct 29 ms 63580 KB Output is correct
3 Correct 30 ms 63520 KB Output is correct
4 Correct 28 ms 63584 KB Output is correct
5 Correct 29 ms 63564 KB Output is correct
6 Correct 28 ms 63628 KB Output is correct
7 Correct 30 ms 63612 KB Output is correct
8 Correct 29 ms 63572 KB Output is correct
9 Correct 30 ms 63692 KB Output is correct
10 Correct 31 ms 63608 KB Output is correct
11 Correct 30 ms 63640 KB Output is correct
12 Correct 33 ms 63652 KB Output is correct
13 Correct 29 ms 63564 KB Output is correct
14 Correct 31 ms 63700 KB Output is correct
15 Correct 35 ms 63648 KB Output is correct
16 Correct 30 ms 63700 KB Output is correct
17 Correct 33 ms 63688 KB Output is correct
18 Correct 31 ms 63636 KB Output is correct
19 Correct 32 ms 63692 KB Output is correct
20 Correct 29 ms 63668 KB Output is correct
21 Correct 34 ms 63708 KB Output is correct
22 Correct 29 ms 63584 KB Output is correct
23 Correct 29 ms 63700 KB Output is correct
24 Correct 39 ms 63684 KB Output is correct
25 Correct 35 ms 63592 KB Output is correct
26 Correct 31 ms 63588 KB Output is correct
27 Correct 31 ms 63572 KB Output is correct
28 Correct 30 ms 63552 KB Output is correct
29 Correct 31 ms 63588 KB Output is correct
30 Correct 32 ms 63912 KB Output is correct
31 Correct 34 ms 63828 KB Output is correct
32 Correct 34 ms 63908 KB Output is correct
33 Correct 33 ms 63920 KB Output is correct
34 Correct 35 ms 64044 KB Output is correct
35 Correct 33 ms 63892 KB Output is correct
36 Correct 37 ms 63956 KB Output is correct
37 Correct 36 ms 63924 KB Output is correct
38 Correct 41 ms 64084 KB Output is correct
39 Correct 34 ms 64136 KB Output is correct
40 Correct 33 ms 64084 KB Output is correct
41 Correct 35 ms 64084 KB Output is correct
42 Correct 31 ms 64108 KB Output is correct
43 Correct 34 ms 64008 KB Output is correct
44 Correct 35 ms 64096 KB Output is correct
45 Correct 33 ms 64048 KB Output is correct
46 Correct 33 ms 64084 KB Output is correct
47 Correct 35 ms 64044 KB Output is correct
48 Correct 34 ms 63948 KB Output is correct
49 Correct 33 ms 64064 KB Output is correct
50 Correct 31 ms 63956 KB Output is correct
51 Correct 33 ms 64032 KB Output is correct
52 Correct 34 ms 64120 KB Output is correct
53 Correct 37 ms 63936 KB Output is correct
54 Correct 33 ms 63912 KB Output is correct
55 Correct 33 ms 63916 KB Output is correct
56 Correct 31 ms 63684 KB Output is correct
57 Correct 32 ms 63876 KB Output is correct
58 Correct 33 ms 63972 KB Output is correct
59 Correct 31 ms 63572 KB Output is correct
60 Correct 29 ms 63556 KB Output is correct
61 Correct 30 ms 63640 KB Output is correct
62 Correct 228 ms 80424 KB Output is correct
63 Correct 197 ms 84432 KB Output is correct
64 Correct 211 ms 87616 KB Output is correct
65 Correct 365 ms 90908 KB Output is correct
66 Correct 304 ms 90928 KB Output is correct
67 Correct 336 ms 90820 KB Output is correct
68 Correct 428 ms 90856 KB Output is correct
69 Correct 330 ms 90828 KB Output is correct
70 Correct 185 ms 89736 KB Output is correct
71 Correct 198 ms 89716 KB Output is correct
72 Correct 186 ms 89760 KB Output is correct
73 Correct 77 ms 86116 KB Output is correct
74 Correct 201 ms 88668 KB Output is correct
75 Correct 97 ms 68892 KB Output is correct
76 Correct 31 ms 63572 KB Output is correct
77 Correct 141 ms 71756 KB Output is correct
78 Correct 199 ms 73340 KB Output is correct
79 Correct 156 ms 74028 KB Output is correct
80 Correct 246 ms 78344 KB Output is correct
81 Correct 240 ms 78456 KB Output is correct
82 Correct 251 ms 78480 KB Output is correct
83 Correct 277 ms 78356 KB Output is correct
84 Correct 243 ms 78364 KB Output is correct
85 Correct 245 ms 78436 KB Output is correct
86 Correct 255 ms 78480 KB Output is correct
87 Correct 245 ms 78596 KB Output is correct
88 Correct 289 ms 78900 KB Output is correct
89 Correct 259 ms 79548 KB Output is correct
90 Correct 273 ms 79372 KB Output is correct
91 Correct 268 ms 78836 KB Output is correct
92 Correct 243 ms 78860 KB Output is correct
93 Correct 245 ms 78824 KB Output is correct
94 Correct 291 ms 78548 KB Output is correct
95 Correct 336 ms 78500 KB Output is correct
96 Correct 259 ms 78488 KB Output is correct
97 Correct 256 ms 78440 KB Output is correct
98 Correct 290 ms 78432 KB Output is correct
99 Correct 264 ms 78396 KB Output is correct
100 Correct 263 ms 78412 KB Output is correct
101 Correct 288 ms 78604 KB Output is correct
102 Correct 286 ms 78560 KB Output is correct
103 Correct 258 ms 78536 KB Output is correct
104 Correct 275 ms 78572 KB Output is correct
105 Correct 292 ms 78760 KB Output is correct
106 Correct 288 ms 78892 KB Output is correct
107 Correct 275 ms 79432 KB Output is correct
108 Correct 325 ms 79928 KB Output is correct
109 Correct 273 ms 79596 KB Output is correct
110 Correct 28 ms 63572 KB Output is correct
111 Correct 29 ms 63540 KB Output is correct
112 Correct 30 ms 63688 KB Output is correct
113 Correct 273 ms 75820 KB Output is correct
114 Correct 313 ms 78208 KB Output is correct
115 Correct 329 ms 82136 KB Output is correct
116 Correct 342 ms 84284 KB Output is correct
117 Correct 319 ms 84300 KB Output is correct
118 Correct 328 ms 84360 KB Output is correct
119 Correct 320 ms 84280 KB Output is correct
120 Correct 347 ms 84388 KB Output is correct
121 Correct 353 ms 84360 KB Output is correct
122 Correct 381 ms 84316 KB Output is correct
123 Correct 90 ms 70336 KB Output is correct
124 Correct 309 ms 84044 KB Output is correct
125 Correct 232 ms 80996 KB Output is correct
126 Correct 366 ms 84456 KB Output is correct
127 Correct 339 ms 84400 KB Output is correct
128 Correct 322 ms 84424 KB Output is correct
129 Correct 338 ms 84428 KB Output is correct
130 Correct 340 ms 84468 KB Output is correct
131 Correct 372 ms 92296 KB Output is correct
132 Correct 307 ms 93684 KB Output is correct
133 Correct 288 ms 89020 KB Output is correct
134 Correct 377 ms 84616 KB Output is correct
135 Correct 405 ms 84564 KB Output is correct
136 Correct 386 ms 84556 KB Output is correct
137 Correct 179 ms 84720 KB Output is correct
138 Correct 186 ms 84720 KB Output is correct
139 Correct 186 ms 84740 KB Output is correct
140 Correct 203 ms 84788 KB Output is correct
141 Correct 185 ms 84680 KB Output is correct
142 Correct 184 ms 84748 KB Output is correct
143 Correct 85 ms 78444 KB Output is correct
144 Correct 268 ms 82224 KB Output is correct