#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
int lazy[18][1 << 18];
void inc(int lvl, int i, int l, int r, int s, int e) {
if (s <= l && r <= e) {
lazy[lvl][i]++;
return;
}
if (r < s || e < l) {
return;
}
inc(lvl, i << 1, l, md, s, e);
inc(lvl, i << 1 | 1, md + 1, r, s, e);
}
int get(int lvl, int i, int l, int r, int x) {
if (l == r) {
return lazy[lvl][i];
}
if (x <= md) {
return get(lvl, i << 1, l, md, x) + lazy[lvl][i];
} else {
return get(lvl, i << 1 | 1, md + 1, r, x) + lazy[lvl][i];
}
}
const int N = 100'005;
int par[N], h[N], height;
vector<int> adj[N];
void dfs(int i, int d, int p) {
par[i] = p;
h[i] = d;
height = max(height, d);
for (auto nxt : adj[i]) {
if (nxt == p) continue;
dfs(nxt, d + 1, i);
}
}
int lst[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, 0);
int a[m];
for (int i = 0; i < m; i++) {
cin >> a[i];
}
vector<array<int, 2>> ask[m];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
ask[r].push_back({l, i});
}
memset(lst, -1, sizeof lst);
int ans[q]{};
for (int r = 0; r < m; r++) {
for (int j = h[a[r]]; j >= 0; j--) {
inc(j, 1, 0, n - 1, lst[a[r]] + 1, r);
lst[a[r]] = r;
a[r] = par[a[r]];
}
for (auto [l, idx] : ask[r]) {
for (int j = 0; j <= height; j++) {
ans[idx] += get(j, 1, 0, n - 1, l);
}
}
}
for (auto i : ans) {
cout << i << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6608 KB |
Output is correct |
2 |
Runtime error |
38 ms |
50260 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6608 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Incorrect |
346 ms |
30324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |