/*ㅤ∧_∧
( ・∀・)
( つ┳⊃
ε (_)へ⌒ヽフ
( ( ・ω・)
◎―◎ ⊃ ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
/・ ) \
/ノへ ノ /|
ノ \\ |/_/_/*/
#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 1e5 + 5;
const int lg = 18;
int n, m, test, tin[maxn], timer, c[maxn], h[maxn], par[maxn][lg + 2], tour[maxn];
vector <int> g[maxn];
void dfs(int u, int p) {
tin[u] = ++timer;
tour[timer] = u;
for (int x : g[u]) {
if (x == p) continue;
h[x] = h[u] + 1;
par[x][0] = u;
dfs(x, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u,v);
for (int i = lg; i >= 0; i--) {
if ((h[u] - h[v]) >= MASK(i)) u = par[u][i];
}
if (u == v) return u;
for (int i = lg; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
void prelca() {
dfs(1, -1);
for (int j = 1; j <= lg; j++) {
for (int i = 1; i <= n; i++) {
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
int dist(int l, int r) {
return h[l] + h[r] - 2 * h[lca(l, r)];
}
int S;
struct pt{
int l, r, id;
};
bool ss(pt a, pt b) {
if (a.l/S != b.l/S)
return a.l < b.l;
if(a.l/S&1)
return a.r < b.r;
else
return a.r>b.r;
}
bool vs[maxn];
int sum, res[maxn], cnt[maxn];
multiset <int> st;
void add(int x) {
cnt[x]++;
if (cnt[x] > 1) return;
st.insert(tin[x]);
auto it = st.find(tin[x]);
if (next(it) != st.end() && it != st.begin()) {
int t1 = tour[*prev(it)], t2 = tour[*next(it)];
sum += dist(t1, x) + dist(t2, x) - dist(t1, t2);
// cout <<"+ " << t1 << " " << x << " " << t2 << endl;
}
else if (next(it) != st.end()) {
int y = tour[*next(it)];
sum += dist(x, y);
// cout <<"? " << x << " " << y << endl;
}
else if (it != st.begin()) {
int y = tour[*prev(it)];
sum += dist(x, y);
//cout <<"+ "<< y << " " << x << endl;
}
}
void del(int x) {
cnt[x]--;
if (cnt[x] > 0) return;
auto it = st.find(tin[x]);
if (next(it) != st.end() && it != st.begin()) {
int t1 = tour[*prev(it)], t2 = tour[*next(it)];
sum -= dist(t1, x);
sum -= dist(t2, x);
sum += dist(t1, t2);
}
else if (next(it) != st.end()) {
int y = tour[*next(it)];
sum -= dist(x, y);
}
else if (it != st.begin()) {
int y = tour[*prev(it)];
sum -= dist(x, y);
}
// cout << sum << endl;
st.erase(it);
}
main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> m >> test;
for (int i = 1; i < n; i++) {
int l, r;
cin >> l >> r;
g[l].pb(r); g[r].pb(l);
}
prelca();
for (int i = 1; i <= m; i++) cin >> c[i];
vector <pt> query;
S = sqrt(test);
for (int i = 1; i <= test; i++) {
int l, r;
cin >> l >> r;
query.pb({l, r, i});
}
// sort(query.begin(), query.end(), ss);
int L = 1, R = 0;
for (auto q : query) {
// cout << q.l << " " << q.r << endl;
while(R < q.r) {
R++;
add(c[R]);
}
while(L > q.l){
L--;
add(c[L]);
}
while(R > q.r) {
del(c[R]);
R--;
}
while(L < q.l) {
del(c[L]);
L++;
}
//cout << sum << " " << q.l << " " << q.r << endl;
res[q.id] = (sum + dist(tour[*st.begin()], tour[*st.rbegin()])) / 2 + 1;
//return 0;
}
for (int i = 1; i <= test; i++) cout << res[i] << endl;
}
Compilation message (stderr)
tourism.cpp:144:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
144 | main() {
| ^~~~
tourism.cpp: In function 'int main()':
tourism.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | 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... |