Submission #874688

# Submission time Handle Problem Language Result Execution time Memory
874688 2023-11-17T16:07:55 Z MinaRagy06 Tourism (JOI23_tourism) C++17
100 / 100
3537 ms 101868 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
#define md ((l + r) >> 1)

int lazy[1 << 18];
void add(int i, int l, int r, int s, int e, int v) {
	if (s <= l && r <= e) {
		lazy[i] += v;
		return;
	}
	if (r < s || e < l) {
		return;
	}
	add(i << 1, l, md, s, e, v);
	add(i << 1 | 1, md + 1, r, s, e, v);
}
int get(int i, int l, int r, int x) {
	if (l == r) {
		return lazy[i];
	}
	if (x <= md) {
		return get(i << 1, l, md, x) + lazy[i];
	} else {
		return get(i << 1 | 1, md + 1, r, x) + lazy[i];
	}
}
const int N = 100'005;
int anc[18][N], h[N], mp[N], mph[N], mxh[N], height;
vector<int> adj[N];
int nodes = 1;
void dfs(int i, int d, int x, int curd, int p, int realp) {
	h[x] = d;
	mxh[x] = max(mxh[x], curd);
	mp[i] = x;
	mph[i] = curd;
	height = max(height, d);
	if (curd == 0) {
		anc[0][x] = p;
		for (int j = 1; j < 18; j++) {
			anc[j][x] = anc[j - 1][anc[j - 1][x]];
		}
	}
	for (auto nxt : adj[i]) {
		if (nxt == realp) continue;
		if ((int)adj[i].size() > (2 - (i == 1))) {
			dfs(nxt, d + 1, ++nodes, 0, x, i);
		} else {
			dfs(nxt, d, x, curd + 1, p, i);
		}
	}
}
int sparse[18][N];
int getlca(int u, int v) {
	if (h[u] > h[v]) {
		swap(u, v);
	}
	if (v == 0) return u;
	if (h[u] != h[v]) {
		for (int j = 17; j >= 0; j--) {
			if (h[anc[j][v]] > h[u]) {
				v = anc[j][v];
			}
		}
		v = anc[0][v];
	}
	if (u != v) {
		for (int j = 17; j >= 0; j--) {
			if (anc[j][u] != anc[j][v]) {
				u = anc[j][u];
				v = anc[j][v];
			}
		}
		u = anc[0][u];
	}
	return u;
}
int query(int l, int r) {
	int lg = __lg(r - l + 1);
	return getlca(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
}
stack<array<int, 2>> s[N];
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 0, 1, 0, 1, 1);
	int arr[m];
	for (int i = 0; i < m; i++) {
		cin >> arr[i];
	}
	for (int i = 0; i < m; i++) {
		sparse[0][i] = mp[arr[i]];
	}
	for (int j = 1; j < 18; j++) {
		for (int i = 0; i + (1 << (j - 1)) < m; i++) {
			sparse[j][i] = getlca(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
		}
	}
	vector<array<int, 3>> ask[m];
	for (int i = 0; i < q; i++) {
		int l, r;
		cin >> l >> r;
		l--, r--;
		int lca = query(l, r);
		ask[r].push_back({l, i, h[lca]});
	}
	int ans[q]{};
	array<int, 2> a[m];
	for (int i = 0; i < m; i++) {
		a[i][0] = mp[arr[i]];
		a[i][1] = mph[arr[i]];
	}
	for (int j = height; j >= 0; j--) {
		memset(lazy, 0, sizeof lazy);
		for (int i = 0; i <= n; i++) {
			while (s[i].size()) s[i].pop();
			s[i].push({(int)1e9, -1});
		}
		for (int r = 0; r < m; r++) {
			auto &[cura, curh] = a[r];
			if (j > h[cura]) goto askl;
			add(1, 0, m - 1, s[cura].top()[1] + 1, r, curh + 1);
			while (curh >= s[cura].top()[0]) {
				array<int, 2> lst = s[cura].top();
				s[cura].pop();
				add(1, 0, m - 1, s[cura].top()[1] + 1, lst[1], curh - lst[0]);
			}
			s[cura].push({curh, r});
			cura = anc[0][cura];
			curh = mxh[cura];
askl:
			for (auto &[l, idx, mnh] : ask[r]) {
				if (j < mnh) continue;
				int v = get(1, 0, m - 1, l);
				ans[idx] += v;
			}
		}
	}
	for (int i = 0; i < m; i++) {
		a[i][0] = mp[arr[i]];
		a[i][1] = mph[arr[i]];
	}
	for (int j = height; j >= 0; j--) {
		memset(lazy, 0, sizeof lazy);
		for (int i = 0; i <= n; i++) {
			while (s[i].size()) s[i].pop();
			s[i].push({(int)-1e9, -1});
		}
		for (int r = 0; r < m; r++) {
			auto &[cura, curh] = a[r];
			if (j > h[cura]) goto askl2;
			add(1, 0, m - 1, s[cura].top()[1] + 1, r, curh);
			while (curh <= s[cura].top()[0]) {
				array<int, 2> lst = s[cura].top();
				s[cura].pop();
				add(1, 0, m - 1, s[cura].top()[1] + 1, lst[1], curh - lst[0]);
			}
			s[cura].push({curh, r});
			cura = anc[0][cura];
			curh = mxh[cura];
askl2:
			for (auto &[l, idx, mnh] : ask[r]) {
				if (j != mnh) continue;
				int v = get(1, 0, m - 1, l);
				ans[idx] -= v;
			}
		}
	}
	for (auto i : ans) {
		cout << i << '\n';
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 39 ms 82512 KB Output is correct
2 Correct 39 ms 82516 KB Output is correct
3 Correct 36 ms 82524 KB Output is correct
4 Correct 37 ms 84560 KB Output is correct
5 Correct 37 ms 84568 KB Output is correct
6 Correct 38 ms 84572 KB Output is correct
7 Correct 38 ms 84572 KB Output is correct
8 Correct 41 ms 84780 KB Output is correct
9 Correct 39 ms 84772 KB Output is correct
10 Correct 39 ms 84752 KB Output is correct
11 Correct 39 ms 84568 KB Output is correct
12 Correct 39 ms 84572 KB Output is correct
13 Correct 38 ms 84572 KB Output is correct
14 Correct 38 ms 84572 KB Output is correct
15 Correct 37 ms 84564 KB Output is correct
16 Correct 38 ms 84556 KB Output is correct
17 Correct 37 ms 84824 KB Output is correct
18 Correct 37 ms 84560 KB Output is correct
19 Correct 38 ms 84568 KB Output is correct
20 Correct 38 ms 84572 KB Output is correct
21 Correct 37 ms 84572 KB Output is correct
22 Correct 44 ms 84564 KB Output is correct
23 Correct 37 ms 84564 KB Output is correct
24 Correct 37 ms 84768 KB Output is correct
25 Correct 38 ms 84620 KB Output is correct
26 Correct 37 ms 84572 KB Output is correct
27 Correct 37 ms 84560 KB Output is correct
28 Correct 37 ms 80476 KB Output is correct
29 Correct 38 ms 84572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 82512 KB Output is correct
2 Correct 39 ms 82516 KB Output is correct
3 Correct 36 ms 82524 KB Output is correct
4 Correct 37 ms 84560 KB Output is correct
5 Correct 37 ms 84568 KB Output is correct
6 Correct 38 ms 84572 KB Output is correct
7 Correct 38 ms 84572 KB Output is correct
8 Correct 41 ms 84780 KB Output is correct
9 Correct 39 ms 84772 KB Output is correct
10 Correct 39 ms 84752 KB Output is correct
11 Correct 39 ms 84568 KB Output is correct
12 Correct 39 ms 84572 KB Output is correct
13 Correct 38 ms 84572 KB Output is correct
14 Correct 38 ms 84572 KB Output is correct
15 Correct 37 ms 84564 KB Output is correct
16 Correct 38 ms 84556 KB Output is correct
17 Correct 37 ms 84824 KB Output is correct
18 Correct 37 ms 84560 KB Output is correct
19 Correct 38 ms 84568 KB Output is correct
20 Correct 38 ms 84572 KB Output is correct
21 Correct 37 ms 84572 KB Output is correct
22 Correct 44 ms 84564 KB Output is correct
23 Correct 37 ms 84564 KB Output is correct
24 Correct 37 ms 84768 KB Output is correct
25 Correct 38 ms 84620 KB Output is correct
26 Correct 37 ms 84572 KB Output is correct
27 Correct 37 ms 84560 KB Output is correct
28 Correct 37 ms 80476 KB Output is correct
29 Correct 38 ms 84572 KB Output is correct
30 Correct 46 ms 84876 KB Output is correct
31 Correct 49 ms 84824 KB Output is correct
32 Correct 50 ms 84932 KB Output is correct
33 Correct 56 ms 84824 KB Output is correct
34 Correct 53 ms 84824 KB Output is correct
35 Correct 49 ms 84828 KB Output is correct
36 Correct 50 ms 84828 KB Output is correct
37 Correct 48 ms 84816 KB Output is correct
38 Correct 40 ms 84816 KB Output is correct
39 Correct 39 ms 84816 KB Output is correct
40 Correct 40 ms 84816 KB Output is correct
41 Correct 39 ms 84816 KB Output is correct
42 Correct 39 ms 84912 KB Output is correct
43 Correct 41 ms 84784 KB Output is correct
44 Correct 49 ms 84820 KB Output is correct
45 Correct 46 ms 84828 KB Output is correct
46 Correct 47 ms 84820 KB Output is correct
47 Correct 44 ms 84828 KB Output is correct
48 Correct 46 ms 84828 KB Output is correct
49 Correct 47 ms 84828 KB Output is correct
50 Correct 40 ms 84708 KB Output is correct
51 Correct 40 ms 84816 KB Output is correct
52 Correct 40 ms 84908 KB Output is correct
53 Correct 40 ms 84928 KB Output is correct
54 Correct 39 ms 84820 KB Output is correct
55 Correct 39 ms 84828 KB Output is correct
56 Correct 38 ms 84828 KB Output is correct
57 Correct 38 ms 80732 KB Output is correct
58 Correct 56 ms 84816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 82776 KB Output is correct
2 Correct 42 ms 84820 KB Output is correct
3 Correct 48 ms 84820 KB Output is correct
4 Correct 133 ms 94872 KB Output is correct
5 Correct 119 ms 97340 KB Output is correct
6 Correct 132 ms 98348 KB Output is correct
7 Correct 168 ms 100436 KB Output is correct
8 Correct 169 ms 100456 KB Output is correct
9 Correct 176 ms 100512 KB Output is correct
10 Correct 172 ms 100432 KB Output is correct
11 Correct 186 ms 100436 KB Output is correct
12 Correct 145 ms 100432 KB Output is correct
13 Correct 148 ms 100328 KB Output is correct
14 Correct 139 ms 100176 KB Output is correct
15 Correct 77 ms 93128 KB Output is correct
16 Correct 162 ms 100180 KB Output is correct
17 Correct 135 ms 93012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 82524 KB Output is correct
2 Correct 527 ms 90708 KB Output is correct
3 Correct 868 ms 91988 KB Output is correct
4 Correct 713 ms 91300 KB Output is correct
5 Correct 968 ms 93556 KB Output is correct
6 Correct 1214 ms 93548 KB Output is correct
7 Correct 1008 ms 93548 KB Output is correct
8 Correct 1035 ms 93556 KB Output is correct
9 Correct 1014 ms 93552 KB Output is correct
10 Correct 1159 ms 93776 KB Output is correct
11 Correct 1089 ms 93588 KB Output is correct
12 Correct 1299 ms 93524 KB Output is correct
13 Correct 1068 ms 93776 KB Output is correct
14 Correct 1173 ms 94740 KB Output is correct
15 Correct 1669 ms 97080 KB Output is correct
16 Correct 1251 ms 93904 KB Output is correct
17 Correct 1130 ms 94036 KB Output is correct
18 Correct 1163 ms 93904 KB Output is correct
19 Correct 2206 ms 93520 KB Output is correct
20 Correct 1764 ms 93648 KB Output is correct
21 Correct 1656 ms 93444 KB Output is correct
22 Correct 2094 ms 93628 KB Output is correct
23 Correct 1973 ms 93440 KB Output is correct
24 Correct 1980 ms 93264 KB Output is correct
25 Correct 2005 ms 93448 KB Output is correct
26 Correct 2090 ms 93700 KB Output is correct
27 Correct 1999 ms 93668 KB Output is correct
28 Correct 1794 ms 93480 KB Output is correct
29 Correct 1907 ms 93516 KB Output is correct
30 Correct 2017 ms 93620 KB Output is correct
31 Correct 1938 ms 93800 KB Output is correct
32 Correct 1887 ms 94036 KB Output is correct
33 Correct 1816 ms 95204 KB Output is correct
34 Correct 3104 ms 96964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 82524 KB Output is correct
2 Correct 36 ms 84572 KB Output is correct
3 Correct 39 ms 84860 KB Output is correct
4 Correct 1106 ms 94584 KB Output is correct
5 Correct 1115 ms 94688 KB Output is correct
6 Correct 1342 ms 96080 KB Output is correct
7 Correct 1377 ms 96788 KB Output is correct
8 Correct 1393 ms 96524 KB Output is correct
9 Correct 1361 ms 96520 KB Output is correct
10 Correct 1369 ms 96612 KB Output is correct
11 Correct 1376 ms 96392 KB Output is correct
12 Correct 1360 ms 96516 KB Output is correct
13 Correct 1361 ms 96516 KB Output is correct
14 Correct 127 ms 93012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 82512 KB Output is correct
2 Correct 39 ms 82516 KB Output is correct
3 Correct 36 ms 82524 KB Output is correct
4 Correct 37 ms 84560 KB Output is correct
5 Correct 37 ms 84568 KB Output is correct
6 Correct 38 ms 84572 KB Output is correct
7 Correct 38 ms 84572 KB Output is correct
8 Correct 41 ms 84780 KB Output is correct
9 Correct 39 ms 84772 KB Output is correct
10 Correct 39 ms 84752 KB Output is correct
11 Correct 39 ms 84568 KB Output is correct
12 Correct 39 ms 84572 KB Output is correct
13 Correct 38 ms 84572 KB Output is correct
14 Correct 38 ms 84572 KB Output is correct
15 Correct 37 ms 84564 KB Output is correct
16 Correct 38 ms 84556 KB Output is correct
17 Correct 37 ms 84824 KB Output is correct
18 Correct 37 ms 84560 KB Output is correct
19 Correct 38 ms 84568 KB Output is correct
20 Correct 38 ms 84572 KB Output is correct
21 Correct 37 ms 84572 KB Output is correct
22 Correct 44 ms 84564 KB Output is correct
23 Correct 37 ms 84564 KB Output is correct
24 Correct 37 ms 84768 KB Output is correct
25 Correct 38 ms 84620 KB Output is correct
26 Correct 37 ms 84572 KB Output is correct
27 Correct 37 ms 84560 KB Output is correct
28 Correct 37 ms 80476 KB Output is correct
29 Correct 38 ms 84572 KB Output is correct
30 Correct 46 ms 84876 KB Output is correct
31 Correct 49 ms 84824 KB Output is correct
32 Correct 50 ms 84932 KB Output is correct
33 Correct 56 ms 84824 KB Output is correct
34 Correct 53 ms 84824 KB Output is correct
35 Correct 49 ms 84828 KB Output is correct
36 Correct 50 ms 84828 KB Output is correct
37 Correct 48 ms 84816 KB Output is correct
38 Correct 40 ms 84816 KB Output is correct
39 Correct 39 ms 84816 KB Output is correct
40 Correct 40 ms 84816 KB Output is correct
41 Correct 39 ms 84816 KB Output is correct
42 Correct 39 ms 84912 KB Output is correct
43 Correct 41 ms 84784 KB Output is correct
44 Correct 49 ms 84820 KB Output is correct
45 Correct 46 ms 84828 KB Output is correct
46 Correct 47 ms 84820 KB Output is correct
47 Correct 44 ms 84828 KB Output is correct
48 Correct 46 ms 84828 KB Output is correct
49 Correct 47 ms 84828 KB Output is correct
50 Correct 40 ms 84708 KB Output is correct
51 Correct 40 ms 84816 KB Output is correct
52 Correct 40 ms 84908 KB Output is correct
53 Correct 40 ms 84928 KB Output is correct
54 Correct 39 ms 84820 KB Output is correct
55 Correct 39 ms 84828 KB Output is correct
56 Correct 38 ms 84828 KB Output is correct
57 Correct 38 ms 80732 KB Output is correct
58 Correct 56 ms 84816 KB Output is correct
59 Correct 35 ms 82776 KB Output is correct
60 Correct 42 ms 84820 KB Output is correct
61 Correct 48 ms 84820 KB Output is correct
62 Correct 133 ms 94872 KB Output is correct
63 Correct 119 ms 97340 KB Output is correct
64 Correct 132 ms 98348 KB Output is correct
65 Correct 168 ms 100436 KB Output is correct
66 Correct 169 ms 100456 KB Output is correct
67 Correct 176 ms 100512 KB Output is correct
68 Correct 172 ms 100432 KB Output is correct
69 Correct 186 ms 100436 KB Output is correct
70 Correct 145 ms 100432 KB Output is correct
71 Correct 148 ms 100328 KB Output is correct
72 Correct 139 ms 100176 KB Output is correct
73 Correct 77 ms 93128 KB Output is correct
74 Correct 162 ms 100180 KB Output is correct
75 Correct 135 ms 93012 KB Output is correct
76 Correct 37 ms 82524 KB Output is correct
77 Correct 527 ms 90708 KB Output is correct
78 Correct 868 ms 91988 KB Output is correct
79 Correct 713 ms 91300 KB Output is correct
80 Correct 968 ms 93556 KB Output is correct
81 Correct 1214 ms 93548 KB Output is correct
82 Correct 1008 ms 93548 KB Output is correct
83 Correct 1035 ms 93556 KB Output is correct
84 Correct 1014 ms 93552 KB Output is correct
85 Correct 1159 ms 93776 KB Output is correct
86 Correct 1089 ms 93588 KB Output is correct
87 Correct 1299 ms 93524 KB Output is correct
88 Correct 1068 ms 93776 KB Output is correct
89 Correct 1173 ms 94740 KB Output is correct
90 Correct 1669 ms 97080 KB Output is correct
91 Correct 1251 ms 93904 KB Output is correct
92 Correct 1130 ms 94036 KB Output is correct
93 Correct 1163 ms 93904 KB Output is correct
94 Correct 2206 ms 93520 KB Output is correct
95 Correct 1764 ms 93648 KB Output is correct
96 Correct 1656 ms 93444 KB Output is correct
97 Correct 2094 ms 93628 KB Output is correct
98 Correct 1973 ms 93440 KB Output is correct
99 Correct 1980 ms 93264 KB Output is correct
100 Correct 2005 ms 93448 KB Output is correct
101 Correct 2090 ms 93700 KB Output is correct
102 Correct 1999 ms 93668 KB Output is correct
103 Correct 1794 ms 93480 KB Output is correct
104 Correct 1907 ms 93516 KB Output is correct
105 Correct 2017 ms 93620 KB Output is correct
106 Correct 1938 ms 93800 KB Output is correct
107 Correct 1887 ms 94036 KB Output is correct
108 Correct 1816 ms 95204 KB Output is correct
109 Correct 3104 ms 96964 KB Output is correct
110 Correct 36 ms 82524 KB Output is correct
111 Correct 36 ms 84572 KB Output is correct
112 Correct 39 ms 84860 KB Output is correct
113 Correct 1106 ms 94584 KB Output is correct
114 Correct 1115 ms 94688 KB Output is correct
115 Correct 1342 ms 96080 KB Output is correct
116 Correct 1377 ms 96788 KB Output is correct
117 Correct 1393 ms 96524 KB Output is correct
118 Correct 1361 ms 96520 KB Output is correct
119 Correct 1369 ms 96612 KB Output is correct
120 Correct 1376 ms 96392 KB Output is correct
121 Correct 1360 ms 96516 KB Output is correct
122 Correct 1361 ms 96516 KB Output is correct
123 Correct 127 ms 93012 KB Output is correct
124 Correct 1548 ms 95952 KB Output is correct
125 Correct 1189 ms 94120 KB Output is correct
126 Correct 1680 ms 96788 KB Output is correct
127 Correct 1721 ms 96628 KB Output is correct
128 Correct 1894 ms 96632 KB Output is correct
129 Correct 1927 ms 96720 KB Output is correct
130 Correct 1762 ms 96624 KB Output is correct
131 Correct 238 ms 100688 KB Output is correct
132 Correct 238 ms 101868 KB Output is correct
133 Correct 238 ms 97364 KB Output is correct
134 Correct 3170 ms 96676 KB Output is correct
135 Correct 3537 ms 96528 KB Output is correct
136 Correct 2624 ms 96520 KB Output is correct
137 Correct 275 ms 96960 KB Output is correct
138 Correct 274 ms 96972 KB Output is correct
139 Correct 262 ms 96828 KB Output is correct
140 Correct 297 ms 96972 KB Output is correct
141 Correct 282 ms 96976 KB Output is correct
142 Correct 283 ms 96916 KB Output is correct
143 Correct 211 ms 85704 KB Output is correct
144 Correct 1739 ms 96188 KB Output is correct