// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "D:/debug.h"
#define debug(...) cerr << "\033[35m" << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 1e5 + 7;
int n, m, q, head[N], in[N], par[N], chain, chain_id[N], sz[N], timer;
int ans[N], c[N];
vector<pair<int, int>> ev[N];
vector<int> g[N];
void pre_dfs(int u, int p) {
sz[u] = 1;
for(int v : g[u]) {
if(v == p) continue;
par[v] = u;
pre_dfs(v, u);
sz[u] += sz[v];
}
}
void hld(int u, int p) {
chain_id[u] = chain;
if(!head[chain]) head[chain] = u;
in[u] = ++timer;
int nxt = 0;
for(int v : g[u]) {
if(v == p) continue;
if(sz[v] > sz[nxt]) nxt = v;
}
if(nxt) hld(nxt, u);
for(int v : g[u]) {
if(v == p or v == nxt) continue;
chain++;
hld(v, u);
}
}
int st[N * 4], lazy[N * 4], bit[N];
void update(int u, int x) {
for(; u <= m; u += (u & -u)) bit[u] += x;
}
int get(int u) {
int ans = 0;
for(; u > 0; u -= (u & -u)) ans += bit[u];
return ans;
}
void push(int i) {
if(lazy[i]) {
for(int j = i * 2; j <= i * 2 + 1; j++) {
st[j] = lazy[j] = lazy[i];
}
lazy[i] = 0;
}
}
void update(int i, int l, int r, int u, int v, int x) {
if(l > v or r < u) return ;
if(u <= l and r <= v and (st[i] != -1 or l == r)) {
if(st[i] != -1) update(st[i], -(r - l + 1));
update(x, r - l + 1);
st[i] = x;
lazy[i] = x;
return;
}
push(i);
int m = (l + r) >> 1;
update(i * 2, l, m, u, v, x);
update(i * 2 + 1, m + 1, r, u, v, x);
if(st[i * 2] == st[i * 2 + 1]) st[i] = st[i * 2];
else st[i] = -1;
}
void update_path(int u, int v, int col) {
while(chain_id[u] != chain_id[v]) {
if(chain_id[u] > chain_id[v]) {
int x = head[chain_id[u]];
update(1, 1, n, in[x], in[u], col);
u = par[x];
} else {
int x = head[chain_id[v]];
update(1, 1, n, in[x], in[v], col);
v = par[x];
}
}
if(in[u] > in[v]) swap(u, v);
update(1, 1, n, in[u], in[v], col);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> m >> q;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= m; i++) cin >> c[i];
for(int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
ev[r].pb({l, i});
}
pre_dfs(1, 0);
hld(1, 0);
memset(st, -1, sizeof st);
for(int i = 1; i <= m; i++) {
int u = c[i];
if(i > 1) update_path(c[i - 1], u, i - 1);
update(1, 1, n, in[u], in[u], i);
for(auto [l, id] : ev[i]) {
ans[id] = get(m) - get(l - 1);
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
tourism.cpp: In function 'int main()':
tourism.cpp:115:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:116:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |