Submission #834230

# Submission time Handle Problem Language Result Execution time Memory
834230 2023-08-22T12:05:13 Z LittleCube Tourism (JOI23_tourism) C++17
100 / 100
195 ms 56516 KB
#pragma GCC optimize("Ofast,unroll-loops")
#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';

}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7552 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 4 ms 7544 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 4 ms 7508 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 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 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7552 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 4 ms 7544 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 4 ms 7508 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 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 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 5 ms 8020 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8276 KB Output is correct
34 Correct 7 ms 8260 KB Output is correct
35 Correct 5 ms 8148 KB Output is correct
36 Correct 6 ms 8148 KB Output is correct
37 Correct 6 ms 8148 KB Output is correct
38 Correct 6 ms 8276 KB Output is correct
39 Correct 6 ms 8276 KB Output is correct
40 Correct 5 ms 8276 KB Output is correct
41 Correct 5 ms 8276 KB Output is correct
42 Correct 5 ms 8276 KB Output is correct
43 Correct 6 ms 8308 KB Output is correct
44 Correct 6 ms 8276 KB Output is correct
45 Correct 5 ms 8276 KB Output is correct
46 Correct 5 ms 8276 KB Output is correct
47 Correct 6 ms 8276 KB Output is correct
48 Correct 5 ms 8148 KB Output is correct
49 Correct 6 ms 8276 KB Output is correct
50 Correct 6 ms 8276 KB Output is correct
51 Correct 6 ms 8276 KB Output is correct
52 Correct 6 ms 8212 KB Output is correct
53 Correct 5 ms 8276 KB Output is correct
54 Correct 5 ms 8276 KB Output is correct
55 Correct 6 ms 8276 KB Output is correct
56 Correct 5 ms 7764 KB Output is correct
57 Correct 4 ms 7892 KB Output is correct
58 Correct 6 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 69 ms 40652 KB Output is correct
5 Correct 65 ms 42568 KB Output is correct
6 Correct 77 ms 49868 KB Output is correct
7 Correct 99 ms 56444 KB Output is correct
8 Correct 104 ms 56460 KB Output is correct
9 Correct 101 ms 56496 KB Output is correct
10 Correct 102 ms 56512 KB Output is correct
11 Correct 99 ms 56516 KB Output is correct
12 Correct 82 ms 55868 KB Output is correct
13 Correct 89 ms 55840 KB Output is correct
14 Correct 83 ms 55868 KB Output is correct
15 Correct 56 ms 38596 KB Output is correct
16 Correct 100 ms 56224 KB Output is correct
17 Correct 54 ms 26316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7504 KB Output is correct
2 Correct 70 ms 29132 KB Output is correct
3 Correct 85 ms 36036 KB Output is correct
4 Correct 74 ms 34240 KB Output is correct
5 Correct 118 ms 47152 KB Output is correct
6 Correct 121 ms 47132 KB Output is correct
7 Correct 118 ms 47040 KB Output is correct
8 Correct 124 ms 47116 KB Output is correct
9 Correct 120 ms 47040 KB Output is correct
10 Correct 120 ms 47072 KB Output is correct
11 Correct 120 ms 47164 KB Output is correct
12 Correct 121 ms 47156 KB Output is correct
13 Correct 123 ms 47460 KB Output is correct
14 Correct 133 ms 48188 KB Output is correct
15 Correct 146 ms 50832 KB Output is correct
16 Correct 124 ms 47420 KB Output is correct
17 Correct 123 ms 47432 KB Output is correct
18 Correct 121 ms 47424 KB Output is correct
19 Correct 107 ms 45944 KB Output is correct
20 Correct 98 ms 45908 KB Output is correct
21 Correct 97 ms 45944 KB Output is correct
22 Correct 100 ms 45936 KB Output is correct
23 Correct 92 ms 45920 KB Output is correct
24 Correct 94 ms 45976 KB Output is correct
25 Correct 94 ms 45884 KB Output is correct
26 Correct 94 ms 45948 KB Output is correct
27 Correct 93 ms 46016 KB Output is correct
28 Correct 104 ms 46120 KB Output is correct
29 Correct 96 ms 46012 KB Output is correct
30 Correct 98 ms 46088 KB Output is correct
31 Correct 104 ms 46376 KB Output is correct
32 Correct 110 ms 46656 KB Output is correct
33 Correct 116 ms 47804 KB Output is correct
34 Correct 122 ms 49756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7504 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 136 ms 38128 KB Output is correct
5 Correct 135 ms 38976 KB Output is correct
6 Correct 168 ms 46404 KB Output is correct
7 Correct 173 ms 50060 KB Output is correct
8 Correct 195 ms 50056 KB Output is correct
9 Correct 182 ms 50020 KB Output is correct
10 Correct 170 ms 50008 KB Output is correct
11 Correct 173 ms 50096 KB Output is correct
12 Correct 168 ms 50084 KB Output is correct
13 Correct 180 ms 50112 KB Output is correct
14 Correct 55 ms 26396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7552 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 4 ms 7544 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7508 KB Output is correct
21 Correct 4 ms 7508 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 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 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 5 ms 8020 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8276 KB Output is correct
34 Correct 7 ms 8260 KB Output is correct
35 Correct 5 ms 8148 KB Output is correct
36 Correct 6 ms 8148 KB Output is correct
37 Correct 6 ms 8148 KB Output is correct
38 Correct 6 ms 8276 KB Output is correct
39 Correct 6 ms 8276 KB Output is correct
40 Correct 5 ms 8276 KB Output is correct
41 Correct 5 ms 8276 KB Output is correct
42 Correct 5 ms 8276 KB Output is correct
43 Correct 6 ms 8308 KB Output is correct
44 Correct 6 ms 8276 KB Output is correct
45 Correct 5 ms 8276 KB Output is correct
46 Correct 5 ms 8276 KB Output is correct
47 Correct 6 ms 8276 KB Output is correct
48 Correct 5 ms 8148 KB Output is correct
49 Correct 6 ms 8276 KB Output is correct
50 Correct 6 ms 8276 KB Output is correct
51 Correct 6 ms 8276 KB Output is correct
52 Correct 6 ms 8212 KB Output is correct
53 Correct 5 ms 8276 KB Output is correct
54 Correct 5 ms 8276 KB Output is correct
55 Correct 6 ms 8276 KB Output is correct
56 Correct 5 ms 7764 KB Output is correct
57 Correct 4 ms 7892 KB Output is correct
58 Correct 6 ms 8276 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 4 ms 7380 KB Output is correct
61 Correct 5 ms 7764 KB Output is correct
62 Correct 69 ms 40652 KB Output is correct
63 Correct 65 ms 42568 KB Output is correct
64 Correct 77 ms 49868 KB Output is correct
65 Correct 99 ms 56444 KB Output is correct
66 Correct 104 ms 56460 KB Output is correct
67 Correct 101 ms 56496 KB Output is correct
68 Correct 102 ms 56512 KB Output is correct
69 Correct 99 ms 56516 KB Output is correct
70 Correct 82 ms 55868 KB Output is correct
71 Correct 89 ms 55840 KB Output is correct
72 Correct 83 ms 55868 KB Output is correct
73 Correct 56 ms 38596 KB Output is correct
74 Correct 100 ms 56224 KB Output is correct
75 Correct 54 ms 26316 KB Output is correct
76 Correct 4 ms 7504 KB Output is correct
77 Correct 70 ms 29132 KB Output is correct
78 Correct 85 ms 36036 KB Output is correct
79 Correct 74 ms 34240 KB Output is correct
80 Correct 118 ms 47152 KB Output is correct
81 Correct 121 ms 47132 KB Output is correct
82 Correct 118 ms 47040 KB Output is correct
83 Correct 124 ms 47116 KB Output is correct
84 Correct 120 ms 47040 KB Output is correct
85 Correct 120 ms 47072 KB Output is correct
86 Correct 120 ms 47164 KB Output is correct
87 Correct 121 ms 47156 KB Output is correct
88 Correct 123 ms 47460 KB Output is correct
89 Correct 133 ms 48188 KB Output is correct
90 Correct 146 ms 50832 KB Output is correct
91 Correct 124 ms 47420 KB Output is correct
92 Correct 123 ms 47432 KB Output is correct
93 Correct 121 ms 47424 KB Output is correct
94 Correct 107 ms 45944 KB Output is correct
95 Correct 98 ms 45908 KB Output is correct
96 Correct 97 ms 45944 KB Output is correct
97 Correct 100 ms 45936 KB Output is correct
98 Correct 92 ms 45920 KB Output is correct
99 Correct 94 ms 45976 KB Output is correct
100 Correct 94 ms 45884 KB Output is correct
101 Correct 94 ms 45948 KB Output is correct
102 Correct 93 ms 46016 KB Output is correct
103 Correct 104 ms 46120 KB Output is correct
104 Correct 96 ms 46012 KB Output is correct
105 Correct 98 ms 46088 KB Output is correct
106 Correct 104 ms 46376 KB Output is correct
107 Correct 110 ms 46656 KB Output is correct
108 Correct 116 ms 47804 KB Output is correct
109 Correct 122 ms 49756 KB Output is correct
110 Correct 4 ms 7504 KB Output is correct
111 Correct 4 ms 7380 KB Output is correct
112 Correct 5 ms 7764 KB Output is correct
113 Correct 136 ms 38128 KB Output is correct
114 Correct 135 ms 38976 KB Output is correct
115 Correct 168 ms 46404 KB Output is correct
116 Correct 173 ms 50060 KB Output is correct
117 Correct 195 ms 50056 KB Output is correct
118 Correct 182 ms 50020 KB Output is correct
119 Correct 170 ms 50008 KB Output is correct
120 Correct 173 ms 50096 KB Output is correct
121 Correct 168 ms 50084 KB Output is correct
122 Correct 180 ms 50112 KB Output is correct
123 Correct 55 ms 26396 KB Output is correct
124 Correct 152 ms 47128 KB Output is correct
125 Correct 111 ms 41968 KB Output is correct
126 Correct 164 ms 50056 KB Output is correct
127 Correct 166 ms 50028 KB Output is correct
128 Correct 163 ms 50072 KB Output is correct
129 Correct 170 ms 50108 KB Output is correct
130 Correct 152 ms 50040 KB Output is correct
131 Correct 122 ms 55356 KB Output is correct
132 Correct 114 ms 56324 KB Output is correct
133 Correct 115 ms 52572 KB Output is correct
134 Correct 130 ms 48960 KB Output is correct
135 Correct 130 ms 48888 KB Output is correct
136 Correct 136 ms 48960 KB Output is correct
137 Correct 112 ms 50936 KB Output is correct
138 Correct 119 ms 51012 KB Output is correct
139 Correct 116 ms 51012 KB Output is correct
140 Correct 121 ms 50984 KB Output is correct
141 Correct 117 ms 50952 KB Output is correct
142 Correct 121 ms 50992 KB Output is correct
143 Correct 65 ms 30912 KB Output is correct
144 Correct 156 ms 49856 KB Output is correct