// Mtaylor
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define endl '\n';
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
typedef long long ll;
const int N = 3e5 + 5;
template <typename T>
class FenwickTree {
public:
vector<T> tree;
int n;
void init(int n) {
tree.assign(n + 2, 0);
this->n = n;
}
T mrg(T &x, T &y) { return x + y; }
void upd(int x, T v) {
for (; x <= n; x += (x) & (-x)) {
tree[x] = mrg(tree[x], v);
}
}
T getprefix(int x) {
if (x <= 0) return 0;
T rs = 0;
for (; x; x -= (x) & (-x)) {
rs = mrg(rs, tree[x]);
}
return rs;
}
T getrange(int l, int r) { return getprefix(r) - getprefix(l - 1); }
};
FenwickTree<ll> ft;
int nxt[N];
int val[N];
set<int> intervals;
void updateAns(int l, int r, int x) {
set<int>::iterator it;
it = intervals.lower_bound(l);
if (it != intervals.begin()) it--;
vector<int> to_rem;
vector<int> to_add;
while (it != intervals.end()) {
int a = *it;
int b = nxt[a];
if (a > r) break;
if (a < l && b < l) {
it++;
continue;
}
if (a < l) {
ft.upd(val[a], -(b - a + 1));
nxt[a] = l - 1;
ft.upd(val[a], (nxt[a] - a + 1));
} else {
ft.upd(val[a], -(b - a + 1));
to_rem.pb(a);
}
if (b > r) {
to_add.pb(r + 1);
val[r+1]=val[a];
ft.upd(val[a], b - r);
nxt[r + 1] = b;
}
it++;
}
nxt[l] = r;
val[l] = x;
ft.upd(x, r - l + 1);
for (auto &u : to_rem) {
assert(intervals.count(u));
intervals.erase(u);
}
for (auto &u : to_add) {
intervals.insert(u);
}
intervals.insert(l);
}
const int E = 1e6 + 5;
#define neig(u, v, e, g) for (int e = g.head[u], v; ~e && ((v = g.to[e]), 1); e = g.nxt[e])
class ADJ {
public:
int head[E], to[E], nxt[E], n, edgcnt = 0;
ll cst[E];
void addEdge(int a, int b, int c = 0) {
nxt[edgcnt] = head[a];
head[a] = edgcnt;
to[edgcnt] = b;
cst[edgcnt] = c;
edgcnt++;
}
void addBiEdge(int a, int b, int c = 0) {
addEdge(a, b, c);
addEdge(b, a, c);
}
void init(int _n) {
n = _n;
memset(head, -1, n * sizeof(head[0]));
edgcnt = 0;
}
} adj;
class HLD {
public:
vector<int> par, sze, dpth, ndToIdx, chHead, heavyChld, idxToNd;
int n, curPos;
HLD() {}
void run(ADJ &adj) {
n = adj.n;
curPos = 0;
par.assign(n, -1);
sze.assign(n, 0);
dpth.assign(n, 0);
ndToIdx.assign(n, 0);
chHead.assign(n, 0);
heavyChld.assign(n, 0);
idxToNd.assign(n, 0);
calcsz(0, adj);
HLD_util(0, adj);
}
void calcsz(int u, ADJ &adj) {
heavyChld[u] = -1;
sze[u] = 1;
int mx = 0, mxv;
neig(u, v, e, adj) {
if (par[u] == v) continue;
par[v] = u;
dpth[v] = dpth[u] + 1;
calcsz(v, adj);
if (sze[v] > mx) {
mx = sze[v];
mxv = v;
}
sze[u] += sze[v];
}
if (mx * 2 > sze[u]) {
heavyChld[u] = mxv;
}
}
void HLD_util(int u, ADJ &adj, int c = 0) {
if (u == -1) return;
idxToNd[curPos] = u;
ndToIdx[u] = curPos++;
chHead[u] = c;
HLD_util(heavyChld[u], adj, c);
neig(u, v, e, adj) {
if (v == par[u]) continue;
if (v == heavyChld[u]) continue;
HLD_util(v, adj, v);
}
}
int lca(int u, int v) {
while (chHead[u] != chHead[v]) {
if (dpth[chHead[v]] > dpth[chHead[u]]) swap(u, v);
u = par[chHead[u]];
}
if (dpth[u] > dpth[v]) swap(u, v);
return u;
}
int getDist(int u, int v) {
int l = lca(u, v);
return dpth[u] + dpth[v] - 2 * dpth[l];
}
void update(int u, int v, int x) {
while (chHead[u] != chHead[v]) {
if (dpth[chHead[v]] > dpth[chHead[u]]) {
swap(u, v);
}
updateAns(ndToIdx[chHead[u]], ndToIdx[u], x);
u = par[chHead[u]];
}
if (dpth[u] > dpth[v]) {
swap(u, v);
}
updateAns(ndToIdx[u], ndToIdx[v], x);
}
} hld;
int n, m, q;
int c[N];
vector<int> qu[N];
int l[N], r[N];
ll ans[N];
void test_case() {
cin >> n >> m >> q;
adj.init(n);
ft.init(m + 2);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj.addBiEdge(u, v);
}
hld.run(adj);
for (int i = 1; i <= m; i++) {
cin >> c[i];
--c[i];
}
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i];
qu[r[i]].pb(i);
}
for (int i = 1; i <= m; i++) {
if (i >= 2) {
hld.update(c[i - 1], c[i], i - 1);
}
hld.update(c[i], c[i], i);
for (auto &id : qu[i]) {
ans[id] = ft.getrange(l[id], n + 1);
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
}
int main() {
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
while (T--) {
test_case();
}
return 0;
}
Compilation message
tourism.cpp: In member function 'void HLD::calcsz(int, ADJ&)':
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp: In function 'void test_case()':
tourism.cpp:158:26: warning: 'mxv' may be used uninitialized in this function [-Wmaybe-uninitialized]
158 | heavyChld[u] = mxv;
tourism.cpp:145:21: note: 'mxv' was declared here
145 | int mx = 0, mxv;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20824 KB |
Output is correct |
3 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20824 KB |
Output is correct |
3 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20824 KB |
Output is correct |
2 |
Incorrect |
324 ms |
26872 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20824 KB |
Output is correct |
2 |
Incorrect |
3 ms |
16732 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20824 KB |
Output is correct |
3 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |