#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ii pair<int,int>
#define ff first
#define ss second
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};
struct cmp
{
bool operator() (int x, int y) const
{
return in[x] < in[y];
}
};
set<int, cmp> 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.emplace_back(x);
for (int i : adj[x]) if (i != p)
dfs(i, x),
euler.emplace_back(x);
out[x] = cnt;
}
vector<vector<ii>> st;
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))]);
}
int lca(int l, int r)
{
l = id[l];
r = id[r];
if (l > r) swap(l, r);
int k = __lg(r - l + 1);
return min(st[k][l], st[k][r - (1 << k) + 1]).ss;
}
void add(int x)
{
x = c[x];
in_set[x]++;
if (in_set[x] == 1)
{
cur += h[x];
if (s.size() == 0) return s.emplace(x), void();
auto it = s.lower_bound(x);
if (it == s.begin())
cur -= h[lca(x, *it)];
else if (it == s.end())
cur -= h[lca(x, *prev(it))];
else
{
auto u = *it, v = *prev(it);
cur += h[lca(u, v)];
cur -= h[lca(u, x)];
cur -= h[lca(v, x)];
}
}
s.emplace(x);
}
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(x);
if (it == s.begin())
cur += h[lca(x, *next(it))];
else if (next(it) == s.end())
cur += h[lca(x, *prev(it))];
else
{
auto u = *prev(it), v = *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(0); 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].emplace_back(v),
adj[v].emplace_back(u);
for (int i = 1; i <= m; i++)
cin >> c[i];
dfs();
build();
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(*s.begin(), *s.rbegin())] + 1;
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6724 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6492 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6488 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
4 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 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 |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6724 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6492 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6488 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
4 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 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 |
14 ms |
7004 KB |
Output is correct |
31 |
Correct |
17 ms |
7056 KB |
Output is correct |
32 |
Correct |
24 ms |
7200 KB |
Output is correct |
33 |
Correct |
27 ms |
7192 KB |
Output is correct |
34 |
Correct |
21 ms |
7004 KB |
Output is correct |
35 |
Correct |
3 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7016 KB |
Output is correct |
37 |
Correct |
3 ms |
7092 KB |
Output is correct |
38 |
Correct |
21 ms |
7284 KB |
Output is correct |
39 |
Correct |
21 ms |
7260 KB |
Output is correct |
40 |
Correct |
20 ms |
7256 KB |
Output is correct |
41 |
Correct |
3 ms |
7260 KB |
Output is correct |
42 |
Correct |
3 ms |
7260 KB |
Output is correct |
43 |
Correct |
4 ms |
7256 KB |
Output is correct |
44 |
Correct |
22 ms |
7248 KB |
Output is correct |
45 |
Correct |
21 ms |
7000 KB |
Output is correct |
46 |
Correct |
21 ms |
7236 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 |
22 ms |
7208 KB |
Output is correct |
51 |
Correct |
19 ms |
7000 KB |
Output is correct |
52 |
Correct |
19 ms |
7000 KB |
Output is correct |
53 |
Correct |
19 ms |
7004 KB |
Output is correct |
54 |
Correct |
21 ms |
7004 KB |
Output is correct |
55 |
Correct |
20 ms |
7212 KB |
Output is correct |
56 |
Correct |
4 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
7004 KB |
Output is correct |
58 |
Correct |
3 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 |
4 ms |
6664 KB |
Output is correct |
4 |
Correct |
4210 ms |
30192 KB |
Output is correct |
5 |
Correct |
2634 ms |
40928 KB |
Output is correct |
6 |
Correct |
4062 ms |
45856 KB |
Output is correct |
7 |
Execution timed out |
5050 ms |
49280 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
57 ms |
25044 KB |
Output is correct |
3 |
Correct |
85 ms |
26572 KB |
Output is correct |
4 |
Correct |
75 ms |
30332 KB |
Output is correct |
5 |
Correct |
102 ms |
42904 KB |
Output is correct |
6 |
Correct |
109 ms |
42072 KB |
Output is correct |
7 |
Correct |
122 ms |
41528 KB |
Output is correct |
8 |
Correct |
110 ms |
41556 KB |
Output is correct |
9 |
Correct |
103 ms |
41716 KB |
Output is correct |
10 |
Correct |
99 ms |
41716 KB |
Output is correct |
11 |
Correct |
131 ms |
41696 KB |
Output is correct |
12 |
Correct |
116 ms |
41628 KB |
Output is correct |
13 |
Correct |
105 ms |
41664 KB |
Output is correct |
14 |
Correct |
111 ms |
41724 KB |
Output is correct |
15 |
Correct |
115 ms |
41672 KB |
Output is correct |
16 |
Correct |
127 ms |
41860 KB |
Output is correct |
17 |
Correct |
114 ms |
41844 KB |
Output is correct |
18 |
Correct |
124 ms |
41904 KB |
Output is correct |
19 |
Correct |
76 ms |
42884 KB |
Output is correct |
20 |
Correct |
92 ms |
42868 KB |
Output is correct |
21 |
Correct |
98 ms |
41684 KB |
Output is correct |
22 |
Correct |
120 ms |
41608 KB |
Output is correct |
23 |
Correct |
96 ms |
41680 KB |
Output is correct |
24 |
Correct |
86 ms |
41680 KB |
Output is correct |
25 |
Correct |
108 ms |
41608 KB |
Output is correct |
26 |
Correct |
83 ms |
41672 KB |
Output is correct |
27 |
Correct |
119 ms |
41524 KB |
Output is correct |
28 |
Correct |
96 ms |
41620 KB |
Output is correct |
29 |
Correct |
125 ms |
41672 KB |
Output is correct |
30 |
Correct |
107 ms |
41672 KB |
Output is correct |
31 |
Correct |
133 ms |
41556 KB |
Output is correct |
32 |
Correct |
109 ms |
41668 KB |
Output is correct |
33 |
Correct |
131 ms |
41460 KB |
Output is correct |
34 |
Correct |
121 ms |
41664 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 |
4 ms |
6488 KB |
Output is correct |
4 |
Execution timed out |
5023 ms |
27180 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 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 |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6724 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6492 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6488 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
4 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 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 |
14 ms |
7004 KB |
Output is correct |
31 |
Correct |
17 ms |
7056 KB |
Output is correct |
32 |
Correct |
24 ms |
7200 KB |
Output is correct |
33 |
Correct |
27 ms |
7192 KB |
Output is correct |
34 |
Correct |
21 ms |
7004 KB |
Output is correct |
35 |
Correct |
3 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7016 KB |
Output is correct |
37 |
Correct |
3 ms |
7092 KB |
Output is correct |
38 |
Correct |
21 ms |
7284 KB |
Output is correct |
39 |
Correct |
21 ms |
7260 KB |
Output is correct |
40 |
Correct |
20 ms |
7256 KB |
Output is correct |
41 |
Correct |
3 ms |
7260 KB |
Output is correct |
42 |
Correct |
3 ms |
7260 KB |
Output is correct |
43 |
Correct |
4 ms |
7256 KB |
Output is correct |
44 |
Correct |
22 ms |
7248 KB |
Output is correct |
45 |
Correct |
21 ms |
7000 KB |
Output is correct |
46 |
Correct |
21 ms |
7236 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 |
22 ms |
7208 KB |
Output is correct |
51 |
Correct |
19 ms |
7000 KB |
Output is correct |
52 |
Correct |
19 ms |
7000 KB |
Output is correct |
53 |
Correct |
19 ms |
7004 KB |
Output is correct |
54 |
Correct |
21 ms |
7004 KB |
Output is correct |
55 |
Correct |
20 ms |
7212 KB |
Output is correct |
56 |
Correct |
4 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
7004 KB |
Output is correct |
58 |
Correct |
3 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 |
4 ms |
6664 KB |
Output is correct |
62 |
Correct |
4210 ms |
30192 KB |
Output is correct |
63 |
Correct |
2634 ms |
40928 KB |
Output is correct |
64 |
Correct |
4062 ms |
45856 KB |
Output is correct |
65 |
Execution timed out |
5050 ms |
49280 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |