#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ii pair<int,int>
#define ff first
#define ss second
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int maxn = 1e5 + 5, bl = sqrt(maxn);
int n, m, q, cnt, cur;
int in[maxn], out[maxn], h[maxn], par[18][maxn], id[maxn], c[maxn], ans[maxn], in_set[maxn];
vector<int> adj[maxn], euler = {0};
int lgs[3*maxn];
int rev_in[maxn];
set<int> s;
struct Q{
int l, r, id;
bool operator < (Q b) const
{
if (l / bl != b.l / bl) return l < b.l;
return (l / bl & 1) ? (r < b.r) : (r > b.r);
}
} qr[maxn];
void dfs(int x = 1, int p = 1)
{
h[x] = h[p] + 1;
in[x] = ++cnt;
id[x] = euler.size();
euler.push_back(x);
for (int i : adj[x]) if (i != p)
dfs(i, x),
euler.push_back(x);
out[x] = cnt;
}
//inline bool sub(int u, int v) /// u in subtree of v
//{
// return in[v] <= in[u] && out[u] <= out[v];
//}
//
//int lca(int u, int v)
//{
// if (h[u] > h[v]) swap(u, v);
// if (sub(v, u)) return u;
// for (int i = 17; i >= 0; i--)
// if (sub(u, par[i][v]) == 0)
// v = par[i][v];
// return par[0][v];
//}
vector<vector<ii>> st;
inline void build()
{
int sz = euler.size();
int LG = __lg(sz);
st = vector<vector<ii>>(LG + 1, vector<ii>(sz, {sz, 0}));
for (int i = 1; i < sz; i++) st[0][i] = {h[euler[i]], euler[i]};
for (int j = 1; j <= LG; j++)
for (int i = 1; i + (1 << j) - 1 < sz; i++)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
inline int lca(int l, int r)
{
l = id[l];
r = id[r];
if (l > r) swap(l, r);
// assert(r-l+1<=n);
int k = lgs[(r - l + 1)];
return min(st[k][l], st[k][r - (1 << k) + 1]).ss;
}
inline void add(int x)
{
x = c[x];
in_set[x]++;
if (in_set[x] == 1)
{
cur += h[x];
if (s.size() == 0) return s.insert(in[x]), void();
auto it = s.lower_bound(in[x]);
if (it == s.begin())
cur -= h[lca(x, rev_in[*it])];
else if (it == s.end())
cur -= h[lca(x, rev_in[*prev(it)])];
else
{
auto u = rev_in[*it], v = rev_in[*prev(it)];
cur += h[lca(u, v)];
cur -= h[lca(u, x)];
cur -= h[lca(v, x)];
}
}
s.insert(in[x]);
}
inline void del(int x)
{
x = c[x];
in_set[x]--;
if (in_set[x] == 0)
{
cur -= h[x];
if (s.size() == 1) return s.clear(), void();
auto it = s.lower_bound(in[x]);
if (it == s.begin())
cur += h[lca(x, rev_in[*next(it)])];
else if (next(it) == s.end())
cur += h[lca(x, rev_in[*prev(it)])];
else
{
auto u = rev_in[*prev(it)], v = rev_in[*next(it)];
cur -= h[lca(u, v)];
cur += h[lca(u, x)];
cur += h[lca(v, x)];
}
s.erase(it);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); //cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
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);
for (int i = 1; i <= m; i++)
cin >> c[i];
dfs();
for (int i = 1; i <= 3*n; i++) lgs[i] = __lg(i);
build();
for (int i = 1; i <= n; i++) rev_in[in[i]] = i;
for (int i = 1; i <= q; i++)
cin >> qr[i].l >> qr[i].r, qr[i].id = i;
sort(qr + 1, qr + q + 1);
for (int i = 1, l = 1, r = 0; i <= q; i++)
{
auto [lx, rx, id] = qr[i];
while (r < rx) add(++r);
while (l > lx) add(--l);
while (r > rx) del(r--);
while (l < lx) del(l++);
ans[id] = cur - h[lca(rev_in[*s.begin()], rev_in[*s.rbegin()])] + 1;
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:150:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
150 | auto [lx, rx, id] = qr[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6488 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6720 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6488 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6720 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
12 ms |
7004 KB |
Output is correct |
31 |
Correct |
14 ms |
7004 KB |
Output is correct |
32 |
Correct |
18 ms |
7196 KB |
Output is correct |
33 |
Correct |
19 ms |
7000 KB |
Output is correct |
34 |
Correct |
17 ms |
7000 KB |
Output is correct |
35 |
Correct |
3 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7004 KB |
Output is correct |
37 |
Correct |
3 ms |
7004 KB |
Output is correct |
38 |
Correct |
17 ms |
7280 KB |
Output is correct |
39 |
Correct |
16 ms |
7256 KB |
Output is correct |
40 |
Correct |
16 ms |
7260 KB |
Output is correct |
41 |
Correct |
3 ms |
7260 KB |
Output is correct |
42 |
Correct |
4 ms |
7312 KB |
Output is correct |
43 |
Correct |
3 ms |
7256 KB |
Output is correct |
44 |
Correct |
18 ms |
7004 KB |
Output is correct |
45 |
Correct |
17 ms |
7000 KB |
Output is correct |
46 |
Correct |
17 ms |
7256 KB |
Output is correct |
47 |
Correct |
3 ms |
7004 KB |
Output is correct |
48 |
Correct |
3 ms |
7004 KB |
Output is correct |
49 |
Correct |
3 ms |
7004 KB |
Output is correct |
50 |
Correct |
16 ms |
7004 KB |
Output is correct |
51 |
Correct |
15 ms |
7004 KB |
Output is correct |
52 |
Correct |
15 ms |
7212 KB |
Output is correct |
53 |
Correct |
15 ms |
7004 KB |
Output is correct |
54 |
Correct |
16 ms |
7004 KB |
Output is correct |
55 |
Correct |
20 ms |
7216 KB |
Output is correct |
56 |
Correct |
2 ms |
6488 KB |
Output is correct |
57 |
Correct |
2 ms |
7004 KB |
Output is correct |
58 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3415 ms |
32232 KB |
Output is correct |
5 |
Correct |
2228 ms |
42980 KB |
Output is correct |
6 |
Correct |
3294 ms |
47964 KB |
Output is correct |
7 |
Correct |
4985 ms |
51856 KB |
Output is correct |
8 |
Correct |
4874 ms |
51860 KB |
Output is correct |
9 |
Correct |
4936 ms |
51876 KB |
Output is correct |
10 |
Correct |
4873 ms |
51836 KB |
Output is correct |
11 |
Correct |
4949 ms |
54748 KB |
Output is correct |
12 |
Correct |
345 ms |
54436 KB |
Output is correct |
13 |
Correct |
330 ms |
54444 KB |
Output is correct |
14 |
Correct |
352 ms |
54440 KB |
Output is correct |
15 |
Correct |
52 ms |
51144 KB |
Output is correct |
16 |
Correct |
114 ms |
51652 KB |
Output is correct |
17 |
Correct |
91 ms |
8128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
52 ms |
26948 KB |
Output is correct |
3 |
Correct |
85 ms |
28624 KB |
Output is correct |
4 |
Correct |
69 ms |
32456 KB |
Output is correct |
5 |
Correct |
75 ms |
44968 KB |
Output is correct |
6 |
Correct |
90 ms |
44200 KB |
Output is correct |
7 |
Correct |
96 ms |
43672 KB |
Output is correct |
8 |
Correct |
90 ms |
43720 KB |
Output is correct |
9 |
Correct |
85 ms |
43720 KB |
Output is correct |
10 |
Correct |
76 ms |
43716 KB |
Output is correct |
11 |
Correct |
114 ms |
43716 KB |
Output is correct |
12 |
Correct |
102 ms |
43716 KB |
Output is correct |
13 |
Correct |
102 ms |
43716 KB |
Output is correct |
14 |
Correct |
101 ms |
43724 KB |
Output is correct |
15 |
Correct |
105 ms |
43720 KB |
Output is correct |
16 |
Correct |
106 ms |
43716 KB |
Output is correct |
17 |
Correct |
110 ms |
43900 KB |
Output is correct |
18 |
Correct |
103 ms |
43720 KB |
Output is correct |
19 |
Correct |
78 ms |
44880 KB |
Output is correct |
20 |
Correct |
96 ms |
44948 KB |
Output is correct |
21 |
Correct |
89 ms |
43720 KB |
Output is correct |
22 |
Correct |
88 ms |
43720 KB |
Output is correct |
23 |
Correct |
89 ms |
43716 KB |
Output is correct |
24 |
Correct |
88 ms |
43684 KB |
Output is correct |
25 |
Correct |
82 ms |
43720 KB |
Output is correct |
26 |
Correct |
78 ms |
43720 KB |
Output is correct |
27 |
Correct |
88 ms |
43612 KB |
Output is correct |
28 |
Correct |
97 ms |
43744 KB |
Output is correct |
29 |
Correct |
110 ms |
43720 KB |
Output is correct |
30 |
Correct |
102 ms |
43628 KB |
Output is correct |
31 |
Correct |
109 ms |
43716 KB |
Output is correct |
32 |
Correct |
97 ms |
43720 KB |
Output is correct |
33 |
Correct |
101 ms |
43728 KB |
Output is correct |
34 |
Correct |
112 ms |
43716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3859 ms |
29284 KB |
Output is correct |
5 |
Correct |
4081 ms |
30012 KB |
Output is correct |
6 |
Execution timed out |
5023 ms |
39868 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6488 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6720 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
12 ms |
7004 KB |
Output is correct |
31 |
Correct |
14 ms |
7004 KB |
Output is correct |
32 |
Correct |
18 ms |
7196 KB |
Output is correct |
33 |
Correct |
19 ms |
7000 KB |
Output is correct |
34 |
Correct |
17 ms |
7000 KB |
Output is correct |
35 |
Correct |
3 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7004 KB |
Output is correct |
37 |
Correct |
3 ms |
7004 KB |
Output is correct |
38 |
Correct |
17 ms |
7280 KB |
Output is correct |
39 |
Correct |
16 ms |
7256 KB |
Output is correct |
40 |
Correct |
16 ms |
7260 KB |
Output is correct |
41 |
Correct |
3 ms |
7260 KB |
Output is correct |
42 |
Correct |
4 ms |
7312 KB |
Output is correct |
43 |
Correct |
3 ms |
7256 KB |
Output is correct |
44 |
Correct |
18 ms |
7004 KB |
Output is correct |
45 |
Correct |
17 ms |
7000 KB |
Output is correct |
46 |
Correct |
17 ms |
7256 KB |
Output is correct |
47 |
Correct |
3 ms |
7004 KB |
Output is correct |
48 |
Correct |
3 ms |
7004 KB |
Output is correct |
49 |
Correct |
3 ms |
7004 KB |
Output is correct |
50 |
Correct |
16 ms |
7004 KB |
Output is correct |
51 |
Correct |
15 ms |
7004 KB |
Output is correct |
52 |
Correct |
15 ms |
7212 KB |
Output is correct |
53 |
Correct |
15 ms |
7004 KB |
Output is correct |
54 |
Correct |
16 ms |
7004 KB |
Output is correct |
55 |
Correct |
20 ms |
7216 KB |
Output is correct |
56 |
Correct |
2 ms |
6488 KB |
Output is correct |
57 |
Correct |
2 ms |
7004 KB |
Output is correct |
58 |
Correct |
4 ms |
7004 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
2 ms |
6492 KB |
Output is correct |
62 |
Correct |
3415 ms |
32232 KB |
Output is correct |
63 |
Correct |
2228 ms |
42980 KB |
Output is correct |
64 |
Correct |
3294 ms |
47964 KB |
Output is correct |
65 |
Correct |
4985 ms |
51856 KB |
Output is correct |
66 |
Correct |
4874 ms |
51860 KB |
Output is correct |
67 |
Correct |
4936 ms |
51876 KB |
Output is correct |
68 |
Correct |
4873 ms |
51836 KB |
Output is correct |
69 |
Correct |
4949 ms |
54748 KB |
Output is correct |
70 |
Correct |
345 ms |
54436 KB |
Output is correct |
71 |
Correct |
330 ms |
54444 KB |
Output is correct |
72 |
Correct |
352 ms |
54440 KB |
Output is correct |
73 |
Correct |
52 ms |
51144 KB |
Output is correct |
74 |
Correct |
114 ms |
51652 KB |
Output is correct |
75 |
Correct |
91 ms |
8128 KB |
Output is correct |
76 |
Correct |
1 ms |
6492 KB |
Output is correct |
77 |
Correct |
52 ms |
26948 KB |
Output is correct |
78 |
Correct |
85 ms |
28624 KB |
Output is correct |
79 |
Correct |
69 ms |
32456 KB |
Output is correct |
80 |
Correct |
75 ms |
44968 KB |
Output is correct |
81 |
Correct |
90 ms |
44200 KB |
Output is correct |
82 |
Correct |
96 ms |
43672 KB |
Output is correct |
83 |
Correct |
90 ms |
43720 KB |
Output is correct |
84 |
Correct |
85 ms |
43720 KB |
Output is correct |
85 |
Correct |
76 ms |
43716 KB |
Output is correct |
86 |
Correct |
114 ms |
43716 KB |
Output is correct |
87 |
Correct |
102 ms |
43716 KB |
Output is correct |
88 |
Correct |
102 ms |
43716 KB |
Output is correct |
89 |
Correct |
101 ms |
43724 KB |
Output is correct |
90 |
Correct |
105 ms |
43720 KB |
Output is correct |
91 |
Correct |
106 ms |
43716 KB |
Output is correct |
92 |
Correct |
110 ms |
43900 KB |
Output is correct |
93 |
Correct |
103 ms |
43720 KB |
Output is correct |
94 |
Correct |
78 ms |
44880 KB |
Output is correct |
95 |
Correct |
96 ms |
44948 KB |
Output is correct |
96 |
Correct |
89 ms |
43720 KB |
Output is correct |
97 |
Correct |
88 ms |
43720 KB |
Output is correct |
98 |
Correct |
89 ms |
43716 KB |
Output is correct |
99 |
Correct |
88 ms |
43684 KB |
Output is correct |
100 |
Correct |
82 ms |
43720 KB |
Output is correct |
101 |
Correct |
78 ms |
43720 KB |
Output is correct |
102 |
Correct |
88 ms |
43612 KB |
Output is correct |
103 |
Correct |
97 ms |
43744 KB |
Output is correct |
104 |
Correct |
110 ms |
43720 KB |
Output is correct |
105 |
Correct |
102 ms |
43628 KB |
Output is correct |
106 |
Correct |
109 ms |
43716 KB |
Output is correct |
107 |
Correct |
97 ms |
43720 KB |
Output is correct |
108 |
Correct |
101 ms |
43728 KB |
Output is correct |
109 |
Correct |
112 ms |
43716 KB |
Output is correct |
110 |
Correct |
1 ms |
6492 KB |
Output is correct |
111 |
Correct |
1 ms |
6492 KB |
Output is correct |
112 |
Correct |
2 ms |
6492 KB |
Output is correct |
113 |
Correct |
3859 ms |
29284 KB |
Output is correct |
114 |
Correct |
4081 ms |
30012 KB |
Output is correct |
115 |
Execution timed out |
5023 ms |
39868 KB |
Time limit exceeded |
116 |
Halted |
0 ms |
0 KB |
- |