Submission #733035

# Submission time Handle Problem Language Result Execution time Memory
733035 2023-04-30T03:20:08 Z LittleCube Tourism (JOI23_tourism) C++14
100 / 100
330 ms 56888 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int N, M, Q, K, t, ans[100005], pre[100005], C[100005], dep[100005], head[100005], sz[100005], tableE[200005][20], bit[200005];
pii io[100005], tableD[100005][20];
vector<int> E[100005], euler = {-1};
vector<pii> qu[100005];
vector<pii> chain[100005];

void modify(int pos, int val)
{
	if(pos == 0)
		return;
	for (int i = pos; i <= M; i += (i & -i))
		bit[i] += val;
}

int query(int pos)
{
	int val = 0;
	for (int i = pos; i > 0; i -= (i & -i))
		val += bit[i];
	return val;
}


void dfs(int u)
{
	euler.emplace_back(u);
	io[u].F = ++t;
	for (auto v : E[u])
		if(v != pre[u])
		{
			pre[v] = u;
			dep[v] = dep[u] + 1;
			dfs(v);
			sz[u] += sz[v];
			++t;
			euler.emplace_back(u);
		}
	sz[u]++;
	io[u].S = t;
}

void hld(int u, int r)
{
	head[u] = r;
	int mxz = 0, mi = 0;
	for (auto v : E[u])
		if(v != pre[u])
			if(sz[v] > mxz)
				mi = v, mxz = sz[v];
	for (auto v : E[u])
		if(v != pre[u])
			hld(v, (v == mi ? r : v));
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> N >> M >> Q;
	E[0].emplace_back(1);
	for (int i = 1; i < N; i++)
	{
		int u, v;
		cin >> u >> v;
		E[u].emplace_back(v);
		E[v].emplace_back(u);
	}
	for (int i = 1; i <= M; i++)
		cin >> C[i];
	pre[0] = -1;
	dfs(0);
	hld(0, 0);
	K = euler.size() - 1;
	for (int i = 1; i <= K; i++)
		tableE[i][0] = dep[euler[i]];
	for (int p = 0; p + 1 < 20; p++)
		for (int i = 1; i + (1 << p) <= K; i++)
			tableE[i][p + 1] = min(tableE[i][p], tableE[i + (1 << p)][p]);
	for (int i = 1; i <= M; i++)
		tableD[i][0] = io[C[i]];
	for (int p = 0; p + 1 < 20; p++)
		for (int i = 1; i + (1 << p) <= M; i++)
			tableD[i][p + 1].F = min(tableD[i][p].F, tableD[i + (1 << p)][p].F),
			tableD[i][p + 1].S = max(tableD[i][p].S, tableD[i + (1 << p)][p].S);
	for (int i = 1; i <= Q; i++)
	{
		int L, R;
		cin >> L >> R;
		qu[R].emplace_back(pii(L, i));
	}
	for (int r = 1; r <= M; r++)
	{
		int u = C[r], h;
		while (u != -1)
		{
			h = head[u];
			while (!chain[h].empty() && dep[chain[h].back().F] <= dep[u])
			{
				auto [v, j] = chain[h].back();
				int val = dep[v] - dep[h] + 1;
				modify(j, -val);
				chain[h].pop_back();
				if (!chain[h].empty())
				{
					j = chain[h].back().S;
					modify(j, val);
				}			
			}
			int val = dep[u] - dep[h] + 1;
			modify(r, val);
			if (!chain[h].empty())
			{
				int j = chain[h].back().S;
				modify(j, -val);
			}
			chain[h].emplace_back(pii(u, r));
			u = pre[h];
		}
		for (auto [l, idx] : qu[r])
		{
			int lg = __lg(r - l + 1);
			pii iot = pii(min(tableD[l][lg].F, tableD[r - (1 << lg) + 1][lg].F),
						  max(tableD[l][lg].S, tableD[r - (1 << lg) + 1][lg].S));
			lg = __lg(iot.S - iot.F + 1);
			int lca = min(tableE[iot.F][lg], tableE[iot.S - (1 << lg) + 1][lg]);
			ans[idx] = query(r) - query(l - 1) - lca;
		}
	}
	for (int i = 1; i <= Q; i++)
		cout << ans[i] << '\n';

}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:107:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  107 |     auto [v, j] = chain[h].back();
      |          ^
tourism.cpp:127:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  127 |   for (auto [l, idx] : qu[r])
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7376 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7388 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 6 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 6 ms 7456 KB Output is correct
16 Correct 5 ms 7572 KB Output is correct
17 Correct 6 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7576 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7512 KB Output is correct
23 Correct 6 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7512 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7512 KB Output is correct
29 Correct 5 ms 7512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7376 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7388 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 6 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 6 ms 7456 KB Output is correct
16 Correct 5 ms 7572 KB Output is correct
17 Correct 6 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7576 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7512 KB Output is correct
23 Correct 6 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7512 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7512 KB Output is correct
29 Correct 5 ms 7512 KB Output is correct
30 Correct 7 ms 8024 KB Output is correct
31 Correct 6 ms 7964 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8296 KB Output is correct
34 Correct 7 ms 8276 KB Output is correct
35 Correct 10 ms 8148 KB Output is correct
36 Correct 9 ms 8148 KB Output is correct
37 Correct 6 ms 8224 KB Output is correct
38 Correct 9 ms 8276 KB Output is correct
39 Correct 6 ms 8276 KB Output is correct
40 Correct 6 ms 8276 KB Output is correct
41 Correct 6 ms 8276 KB Output is correct
42 Correct 6 ms 8276 KB Output is correct
43 Correct 6 ms 8276 KB Output is correct
44 Correct 6 ms 8276 KB Output is correct
45 Correct 8 ms 8276 KB Output is correct
46 Correct 7 ms 8276 KB Output is correct
47 Correct 8 ms 8276 KB Output is correct
48 Correct 6 ms 8184 KB Output is correct
49 Correct 7 ms 8276 KB Output is correct
50 Correct 7 ms 8276 KB Output is correct
51 Correct 7 ms 8280 KB Output is correct
52 Correct 6 ms 8276 KB Output is correct
53 Correct 6 ms 8276 KB Output is correct
54 Correct 6 ms 8276 KB Output is correct
55 Correct 6 ms 8284 KB Output is correct
56 Correct 5 ms 7764 KB Output is correct
57 Correct 6 ms 7840 KB Output is correct
58 Correct 7 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 7 ms 7684 KB Output is correct
4 Correct 106 ms 40792 KB Output is correct
5 Correct 107 ms 42644 KB Output is correct
6 Correct 134 ms 49984 KB Output is correct
7 Correct 157 ms 56640 KB Output is correct
8 Correct 166 ms 56564 KB Output is correct
9 Correct 153 ms 56448 KB Output is correct
10 Correct 210 ms 56712 KB Output is correct
11 Correct 180 ms 56888 KB Output is correct
12 Correct 137 ms 56000 KB Output is correct
13 Correct 136 ms 55996 KB Output is correct
14 Correct 138 ms 56060 KB Output is correct
15 Correct 90 ms 38672 KB Output is correct
16 Correct 163 ms 56220 KB Output is correct
17 Correct 86 ms 26316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 85 ms 29300 KB Output is correct
3 Correct 135 ms 36100 KB Output is correct
4 Correct 113 ms 34232 KB Output is correct
5 Correct 207 ms 47248 KB Output is correct
6 Correct 187 ms 47168 KB Output is correct
7 Correct 193 ms 47080 KB Output is correct
8 Correct 178 ms 47204 KB Output is correct
9 Correct 200 ms 47144 KB Output is correct
10 Correct 181 ms 47176 KB Output is correct
11 Correct 184 ms 47196 KB Output is correct
12 Correct 208 ms 47368 KB Output is correct
13 Correct 230 ms 47548 KB Output is correct
14 Correct 222 ms 48476 KB Output is correct
15 Correct 258 ms 50860 KB Output is correct
16 Correct 214 ms 47528 KB Output is correct
17 Correct 230 ms 47472 KB Output is correct
18 Correct 204 ms 47424 KB Output is correct
19 Correct 167 ms 46100 KB Output is correct
20 Correct 144 ms 46008 KB Output is correct
21 Correct 154 ms 45992 KB Output is correct
22 Correct 142 ms 46112 KB Output is correct
23 Correct 139 ms 46136 KB Output is correct
24 Correct 169 ms 45932 KB Output is correct
25 Correct 148 ms 45900 KB Output is correct
26 Correct 147 ms 46036 KB Output is correct
27 Correct 154 ms 45992 KB Output is correct
28 Correct 143 ms 46084 KB Output is correct
29 Correct 152 ms 46228 KB Output is correct
30 Correct 187 ms 46300 KB Output is correct
31 Correct 176 ms 46340 KB Output is correct
32 Correct 173 ms 46792 KB Output is correct
33 Correct 179 ms 47884 KB Output is correct
34 Correct 181 ms 49740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 181 ms 38204 KB Output is correct
5 Correct 188 ms 38964 KB Output is correct
6 Correct 235 ms 46404 KB Output is correct
7 Correct 243 ms 50028 KB Output is correct
8 Correct 330 ms 50028 KB Output is correct
9 Correct 254 ms 50112 KB Output is correct
10 Correct 279 ms 50048 KB Output is correct
11 Correct 261 ms 50236 KB Output is correct
12 Correct 257 ms 50112 KB Output is correct
13 Correct 278 ms 50356 KB Output is correct
14 Correct 93 ms 26472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7376 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7388 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7516 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 6 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 6 ms 7456 KB Output is correct
16 Correct 5 ms 7572 KB Output is correct
17 Correct 6 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7576 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7512 KB Output is correct
23 Correct 6 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7512 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7512 KB Output is correct
29 Correct 5 ms 7512 KB Output is correct
30 Correct 7 ms 8024 KB Output is correct
31 Correct 6 ms 7964 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8296 KB Output is correct
34 Correct 7 ms 8276 KB Output is correct
35 Correct 10 ms 8148 KB Output is correct
36 Correct 9 ms 8148 KB Output is correct
37 Correct 6 ms 8224 KB Output is correct
38 Correct 9 ms 8276 KB Output is correct
39 Correct 6 ms 8276 KB Output is correct
40 Correct 6 ms 8276 KB Output is correct
41 Correct 6 ms 8276 KB Output is correct
42 Correct 6 ms 8276 KB Output is correct
43 Correct 6 ms 8276 KB Output is correct
44 Correct 6 ms 8276 KB Output is correct
45 Correct 8 ms 8276 KB Output is correct
46 Correct 7 ms 8276 KB Output is correct
47 Correct 8 ms 8276 KB Output is correct
48 Correct 6 ms 8184 KB Output is correct
49 Correct 7 ms 8276 KB Output is correct
50 Correct 7 ms 8276 KB Output is correct
51 Correct 7 ms 8280 KB Output is correct
52 Correct 6 ms 8276 KB Output is correct
53 Correct 6 ms 8276 KB Output is correct
54 Correct 6 ms 8276 KB Output is correct
55 Correct 6 ms 8284 KB Output is correct
56 Correct 5 ms 7764 KB Output is correct
57 Correct 6 ms 7840 KB Output is correct
58 Correct 7 ms 8276 KB Output is correct
59 Correct 5 ms 7380 KB Output is correct
60 Correct 5 ms 7380 KB Output is correct
61 Correct 7 ms 7684 KB Output is correct
62 Correct 106 ms 40792 KB Output is correct
63 Correct 107 ms 42644 KB Output is correct
64 Correct 134 ms 49984 KB Output is correct
65 Correct 157 ms 56640 KB Output is correct
66 Correct 166 ms 56564 KB Output is correct
67 Correct 153 ms 56448 KB Output is correct
68 Correct 210 ms 56712 KB Output is correct
69 Correct 180 ms 56888 KB Output is correct
70 Correct 137 ms 56000 KB Output is correct
71 Correct 136 ms 55996 KB Output is correct
72 Correct 138 ms 56060 KB Output is correct
73 Correct 90 ms 38672 KB Output is correct
74 Correct 163 ms 56220 KB Output is correct
75 Correct 86 ms 26316 KB Output is correct
76 Correct 5 ms 7380 KB Output is correct
77 Correct 85 ms 29300 KB Output is correct
78 Correct 135 ms 36100 KB Output is correct
79 Correct 113 ms 34232 KB Output is correct
80 Correct 207 ms 47248 KB Output is correct
81 Correct 187 ms 47168 KB Output is correct
82 Correct 193 ms 47080 KB Output is correct
83 Correct 178 ms 47204 KB Output is correct
84 Correct 200 ms 47144 KB Output is correct
85 Correct 181 ms 47176 KB Output is correct
86 Correct 184 ms 47196 KB Output is correct
87 Correct 208 ms 47368 KB Output is correct
88 Correct 230 ms 47548 KB Output is correct
89 Correct 222 ms 48476 KB Output is correct
90 Correct 258 ms 50860 KB Output is correct
91 Correct 214 ms 47528 KB Output is correct
92 Correct 230 ms 47472 KB Output is correct
93 Correct 204 ms 47424 KB Output is correct
94 Correct 167 ms 46100 KB Output is correct
95 Correct 144 ms 46008 KB Output is correct
96 Correct 154 ms 45992 KB Output is correct
97 Correct 142 ms 46112 KB Output is correct
98 Correct 139 ms 46136 KB Output is correct
99 Correct 169 ms 45932 KB Output is correct
100 Correct 148 ms 45900 KB Output is correct
101 Correct 147 ms 46036 KB Output is correct
102 Correct 154 ms 45992 KB Output is correct
103 Correct 143 ms 46084 KB Output is correct
104 Correct 152 ms 46228 KB Output is correct
105 Correct 187 ms 46300 KB Output is correct
106 Correct 176 ms 46340 KB Output is correct
107 Correct 173 ms 46792 KB Output is correct
108 Correct 179 ms 47884 KB Output is correct
109 Correct 181 ms 49740 KB Output is correct
110 Correct 4 ms 7380 KB Output is correct
111 Correct 5 ms 7380 KB Output is correct
112 Correct 5 ms 7764 KB Output is correct
113 Correct 181 ms 38204 KB Output is correct
114 Correct 188 ms 38964 KB Output is correct
115 Correct 235 ms 46404 KB Output is correct
116 Correct 243 ms 50028 KB Output is correct
117 Correct 330 ms 50028 KB Output is correct
118 Correct 254 ms 50112 KB Output is correct
119 Correct 279 ms 50048 KB Output is correct
120 Correct 261 ms 50236 KB Output is correct
121 Correct 257 ms 50112 KB Output is correct
122 Correct 278 ms 50356 KB Output is correct
123 Correct 93 ms 26472 KB Output is correct
124 Correct 273 ms 47376 KB Output is correct
125 Correct 227 ms 42120 KB Output is correct
126 Correct 283 ms 50264 KB Output is correct
127 Correct 314 ms 50200 KB Output is correct
128 Correct 301 ms 50276 KB Output is correct
129 Correct 319 ms 50196 KB Output is correct
130 Correct 329 ms 50312 KB Output is correct
131 Correct 201 ms 55532 KB Output is correct
132 Correct 225 ms 56592 KB Output is correct
133 Correct 239 ms 52764 KB Output is correct
134 Correct 239 ms 49184 KB Output is correct
135 Correct 238 ms 49176 KB Output is correct
136 Correct 232 ms 49088 KB Output is correct
137 Correct 204 ms 51264 KB Output is correct
138 Correct 218 ms 51216 KB Output is correct
139 Correct 237 ms 51364 KB Output is correct
140 Correct 212 ms 51228 KB Output is correct
141 Correct 231 ms 51152 KB Output is correct
142 Correct 214 ms 51156 KB Output is correct
143 Correct 112 ms 30936 KB Output is correct
144 Correct 292 ms 49900 KB Output is correct