#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
int lazy[36][1 << 18];
void add(int lvl, int i, int l, int r, int s, int e, int v) {
if (s <= l && r <= e) {
lazy[lvl][i] += v;
return;
}
if (r < s || e < l) {
return;
}
add(lvl, i << 1, l, md, s, e, v);
add(lvl, i << 1 | 1, md + 1, r, s, e, v);
}
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 anc[18][N], h[N], mp[N], mph[N], mxh[N], height;
vector<int> adj[N];
int nodes = 1;
void dfs(int i, int d, int x, int curd, int p, int realp) {
h[x] = d;
mxh[x] = max(mxh[x], curd);
mp[i] = x;
mph[i] = curd;
height = max(height, d);
if (curd == 0) {
anc[0][x] = p;
for (int j = 1; j < 18; j++) {
anc[j][x] = anc[j - 1][anc[j - 1][x]];
}
}
for (auto nxt : adj[i]) {
if (nxt == realp) continue;
if ((int)adj[i].size() > (2 - (i == 1))) {
dfs(nxt, d + 1, ++nodes, 0, x, i);
} else {
dfs(nxt, d, x, curd + 1, p, i);
}
}
}
int sparse[18][N];
int getlca(int u, int v) {
if (h[u] > h[v]) {
swap(u, v);
}
if (v == 0) return u;
if (h[u] != h[v]) {
for (int j = 17; j >= 0; j--) {
if (h[anc[j][v]] > h[u]) {
v = anc[j][v];
}
}
v = anc[0][v];
}
if (u != v) {
for (int j = 17; j >= 0; j--) {
if (anc[j][u] != anc[j][v]) {
u = anc[j][u];
v = anc[j][v];
}
}
u = anc[0][u];
}
return u;
}
int query(int l, int r) {
int lg = __lg(r - l + 1);
return getlca(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
}
stack<array<int, 2>> s[36][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, 1, 0, 1, 1);
int a[m];
for (int i = 0; i < m; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
sparse[0][i] = mp[a[i]];
}
for (int j = 1; j < 18; j++) {
for (int i = 0; i + (1 << (j - 1)) < m; i++) {
sparse[j][i] = getlca(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
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});
}
for (int j = 0; j < 18; j++) {
for (int i = 0; i <= n; i++) {
s[j][i].push({(int)1e9, -1});
s[18 + j][i].push({(int)-1e9, -1});
}
}
int ans[q]{};
for (int r = 0; r < m; r++) {
int curh = mph[a[r]];
a[r] = mp[a[r]];
// cout << '\n' << r << ": \n";
for (int j = h[a[r]]; j >= 0; j--) {
// j -> mx[l, i], 18 + j -> mn[l, i]
// {val, pos}
add(j, 1, 0, m - 1, s[j][a[r]].top()[1] + 1, r, curh + 1);
// cout << s[j][a[r]].top()[1] + 1 << ' ' << r << ' ' << curh + 1 << '\n';
while (curh >= s[j][a[r]].top()[0]) {
array<int, 2> lst = s[j][a[r]].top();
s[j][a[r]].pop();
add(j, 1, 0, m - 1, s[j][a[r]].top()[1] + 1, lst[1], curh - lst[0]);
}
s[j][a[r]].push({curh, r});
add(18 + j, 1, 0, m - 1, s[18 + j][a[r]].top()[1] + 1, r, curh);
while (curh <= s[18 + j][a[r]].top()[0]) {
array<int, 2> lst = s[18 + j][a[r]].top();
s[18 + j][a[r]].pop();
add(18 + j, 1, 0, m - 1, s[18 + j][a[r]].top()[1] + 1, lst[1], curh - lst[0]);
}
s[18 + j][a[r]].push({curh, r});
a[r] = anc[0][a[r]];
curh = mxh[a[r]];
}
for (auto [l, idx] : ask[r]) {
int lca = query(l, r);
int mnh = h[lca];
// cout << lca << '\n';
// cout << mnh << "|\n";
for (int j = height; j >= mnh; j--) {
int v = get(j, 1, 0, m - 1, l);
ans[idx] += v;
// cout << v << '\n';
if (j == mnh) {
int v2 = get(18 + j, 1, 0, m - 1, l);
// cout << '.' << v2 << '\n';
ans[idx] -= v2;
}
}
}
}
for (auto i : ans) {
cout << i << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
570 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
570 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
402 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
446 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
570 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |