#include <bits/stdc++.h>
#define ar array
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 1e5 + 5, B = 300, lg = 17;
vector <int> g[N];
int st[lg][N], bit[N], d[N], n, m, q;
int c[N], tin[N], tout[N], tim = 0;
void dfs(int x, int par) {
	tin[x] = ++tim; st[0][x] = par; d[x] = d[par] + 1;
	for (int i = 1; i < lg; i++)
		st[i][x] = st[i - 1][st[i - 1][x]];
	for (int &y : g[x]) {
		if (y != par) {
			dfs(y, x);
		}
	}
	tout[x] = tim;
}
void upd(int i, int qx) {
	while (i <= n) {
		bit[i] += qx;
		i |= (i + 1);
	}
}
int get(int i) {
	int ans = 0;
	while (i >= 0) {
		ans += bit[i];
		i = (i & (i + 1)) - 1;
	}
	return ans;
}
int get(int l, int r) {
	return get(r) - get(l - 1);
}
int lca = -1, cnt[N], sm = 0, ans = 0;
void add(int x) {
	if (lca == -1)
		return ans++, cnt[x]++, sm++, upd(tin[x], 1), lca = x, void();
	if (cnt[x] == 0) {
		int sb = get(tin[x], tout[x]), nsb = sm - sb;
		if (nsb > 0 && sb > 0)
			return;
		if (sb == 0) {
			int b = 16, y = x, res = 0;
			while (b >= 0) {
				int ty = st[b][y];
				if (get(tin[ty], tout[ty]) == 0)
					y = ty, res += (1 << b);
				b--;
			}
			y = st[0][y];
			if (sm == get(tin[y], tout[y]))
				res++, lca = y;
			ans += res + 1;
		} else {
			ans += d[lca] - d[x];
			lca = x;
		}
	}
	cnt[x]++, sm++, upd(tin[x], 1);
}
void rem(int x) {
	cnt[x]--, sm--, upd(tin[x], -1);
	if (cnt[x] == 0) {
		int sb = get(tin[x], tout[x]), nsb = sm - sb;
		if (nsb > 0 && sb > 0)
			return;
		if (sb == 0) {
		}
	}
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
	cin >> n >> m >> q;
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	for (int i = 1; i <= m; i++)
		cin >> c[i];
	dfs(1, 0);
	tout[0] = n;
	while (q--) {
		int l, r;
		cin >> l >> r;
		for (int i = l; i <= r; i++)
			add(c[i]);
		cout << ans << '\n';
		for (int i = l; i <= r; i++)
			cnt[c[i]] = 0, upd(tin[c[i]], -1);
		sm = ans = 0; lca = -1;
	}
	return 0;
	vector <ar <int, 3>> query(q);
	vector <int> res(q);
	for (int i = 0; i < q; i++)
		cin >> query[i][0] >> query[i][1], query[i][2] = i;
	sort(all(query), [] (const ar <int, 3> &a, const ar <int, 3> &b) {
		if (a[0] / B == b[0] / B)
			return a[1] < b[1];
		return a[0] / B < b[0] / B;
	});
}
| # | 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... |