#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 2022; ///loonf mod sai
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;
struct Data {
int L, R, val;
bool operator < (const Data &other) const {
return L < other.L || (L == other.L && R < other.R) || (L == other.L && R == other.R && val < other.val);
}
};
int n, m, Q;
int C[N];
vector<int> ke[N];
vector<pii> qr[N];
set<Data> S[N];
int high[N], pa[N], szC[N];
int Head[N], ein[N];
int ptrL = 1, ptrR = 0;
struct Fenwick_Tree {
int m;
int fen[N];
void init (int n) {
m = n;
}
void update (int pos, int val) {
for (; pos <= m; pos += pos & -pos) fen[pos] += val;
}
int get (int pos) {
int res = 0;
for (; pos > 0; pos -= pos & -pos) res += fen[pos];
return res;
}
}BIT;
void pdfs (int u, int p) {
high[u] = high[p] + 1;
szC[u] = 1;
pa[u] = p;
iter (&v, ke[u]) {
if (v != p) {
pdfs(v, u);
szC[u] += szC[v];
}
}
}
void hld (int u, int p) {
// cout << u<<" "<<p <<"\n";
static int time_dfs = 0;
Head[u] = p;
ein[u] = ++time_dfs;
int mxV = -1;
iter (&v, ke[u]) if (v != pa[u] && (mxV == -1 || szC[v] > szC[mxV])) mxV = v;
if (mxV != -1) hld(mxV, p);
iter (&v, ke[u]) {
if (v == pa[u] || v == mxV) continue;
hld(v, v);
}
}
void update (int u, int v, int val) {
int uu = u, vv = v;
for (; Head[u] != Head[v]; v = pa[Head[v]]) {
if (Head[u] > Head[v]) swap(u, v);
set<Data> &cur = S[Head[v]];
while (1) {
auto it = cur.begin();
if (it == cur.end() || it->R > high[v]) break;
BIT.update (it->val, -(it->R - it->L + 1));
cur.erase(it);
}
auto it = cur.begin();
if (it != cur.end()) {
Data curV = {high[v] + 1, it->R, it->val};
BIT.update(curV.val, it->L - curV.L);
cur.erase(it);
cur.insert(curV);
}
cur.insert({high[Head[v]], high[v], val});
BIT.update(val, high[v] - high[Head[v]] + 1);
}
if (high[u] > high[v]) swap(u, v);
int L = high[u], R = high[v];
set<Data> &cur = S[Head[u]];
auto itL = cur.upper_bound({L, INF, INF});
if (itL != cur.begin()) {
--itL;
if (itL->L < L && itL->R >= L) {
Data curV = {itL->L, L - 1, itL->val}, curVR = {-1, -1, -1};
BIT.update(itL->val, L - 1 - itL->R);
if (itL->R > R) {
curVR = {R + 1, itL->R, itL->val};
}
cur.erase(itL);
cur.insert(curV);
if (curVR.L != -1) {
BIT.update(curVR.val, curVR.R - curVR.L + 1);
cur.insert(curVR);
}
}
}
auto itR = cur.upper_bound({R, INF, INF});
if (itR != cur.begin()) {
// if (uu == 5 && vv == 7) {
// cout <<R<<":"<<itR->L<<","<<itR->R<<" hih\n";
// cout << curV.L<<","<<curV.R<<","<<curV.val<<" jhih\n";
// }
--itR;
if (itR->R > R && itR->L <= R) {
Data curV = {R + 1, itR->R, itR->val};
BIT.update(itR->val, itR->L - (R + 1));
cur.erase(itR);
cur.insert(curV);
}
}
while (1) {
auto it = cur.lower_bound({L, -1, -1});
if (it == cur.end() || it->L> R) break;
BIT.update(it->val, -(it->R - it->L + 1));
cur.erase(it);
}
BIT.update(val, R - L + 1);
cur.insert({L, R, val});
// cout << uu<<"->"<<vv<<"\n";
// for (int id : {1, 3, 5, 7}) {
// cout <<id<<": ";
// iter (&id2, S[id]) cout <<id2.L<<","<<id2.R<<","<<id2.val<<" ";
// cout<<"\n";
// }
}
void solution() {
cin >> n >> m >> Q;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
rep (i, 1, m) cin >> C[i];
pdfs(1, 0);
hld(1, 1);
rep (i, 1, Q) {
int L, R;
cin >> L >> R;
qr[R].pb({L, i});
}
BIT.init(m);
vector<int> Ans(Q + 1, 0);
rep (R, 1, m) {
if (R > 1) update (C[R - 1], C[R], R - 1);
iter (&id, qr[R]) {
if (id.fs == R) Ans[id.se] = 1;
else Ans[id.se] = BIT.get(m) - BIT.get(id.fs - 1);
}
}
rep (i, 1, Q) cout << Ans[i] <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug +8
chu y break hay return se lam hong logic
xet transition cua i va i + 1
construct ket qua
chu y truong hop : KHONG CO GI
ko làm được
hướng 1: đổi hướng làm
hướng 2: đưa ra nhận xét
tim mo hinh bai toan sau khi doc de
trung ten bien trong ham gan nhat co the dan den sai
10 7 3
6 5
3 6
9 3
8 3
7 8
7 1
2 5
7 10
8 4
9 4 10 1 10 7 6
4 4
1 3
6 7
*/
Compilation message
tourism.cpp: In function 'void update(int, int, int)':
tourism.cpp:91:9: warning: unused variable 'uu' [-Wunused-variable]
91 | int uu = u, vv = v;
| ^~
tourism.cpp:91:17: warning: unused variable 'vv' [-Wunused-variable]
91 | int uu = u, vv = v;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9860 KB |
Output is correct |
4 |
Correct |
5 ms |
9748 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
4 ms |
9732 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
7 ms |
9920 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9816 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9824 KB |
Output is correct |
18 |
Correct |
4 ms |
9744 KB |
Output is correct |
19 |
Correct |
4 ms |
9828 KB |
Output is correct |
20 |
Correct |
5 ms |
9916 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9824 KB |
Output is correct |
23 |
Correct |
5 ms |
9828 KB |
Output is correct |
24 |
Correct |
6 ms |
9924 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
9820 KB |
Output is correct |
27 |
Correct |
5 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9860 KB |
Output is correct |
4 |
Correct |
5 ms |
9748 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
4 ms |
9732 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
7 ms |
9920 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9816 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9824 KB |
Output is correct |
18 |
Correct |
4 ms |
9744 KB |
Output is correct |
19 |
Correct |
4 ms |
9828 KB |
Output is correct |
20 |
Correct |
5 ms |
9916 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9824 KB |
Output is correct |
23 |
Correct |
5 ms |
9828 KB |
Output is correct |
24 |
Correct |
6 ms |
9924 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
9820 KB |
Output is correct |
27 |
Correct |
5 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
6 ms |
10076 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
7 ms |
10076 KB |
Output is correct |
33 |
Correct |
9 ms |
10148 KB |
Output is correct |
34 |
Correct |
7 ms |
10124 KB |
Output is correct |
35 |
Correct |
6 ms |
10060 KB |
Output is correct |
36 |
Correct |
6 ms |
9964 KB |
Output is correct |
37 |
Correct |
6 ms |
10076 KB |
Output is correct |
38 |
Correct |
6 ms |
9940 KB |
Output is correct |
39 |
Correct |
6 ms |
10076 KB |
Output is correct |
40 |
Correct |
6 ms |
10080 KB |
Output is correct |
41 |
Correct |
5 ms |
10076 KB |
Output is correct |
42 |
Correct |
6 ms |
10076 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
6 ms |
10076 KB |
Output is correct |
45 |
Incorrect |
5 ms |
10076 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9880 KB |
Output is correct |
4 |
Correct |
69 ms |
21132 KB |
Output is correct |
5 |
Correct |
58 ms |
23120 KB |
Output is correct |
6 |
Correct |
66 ms |
24660 KB |
Output is correct |
7 |
Correct |
95 ms |
27732 KB |
Output is correct |
8 |
Correct |
91 ms |
27732 KB |
Output is correct |
9 |
Correct |
86 ms |
27644 KB |
Output is correct |
10 |
Correct |
87 ms |
27852 KB |
Output is correct |
11 |
Correct |
86 ms |
27732 KB |
Output is correct |
12 |
Correct |
95 ms |
27216 KB |
Output is correct |
13 |
Correct |
89 ms |
27220 KB |
Output is correct |
14 |
Correct |
91 ms |
27216 KB |
Output is correct |
15 |
Correct |
36 ms |
24008 KB |
Output is correct |
16 |
Correct |
86 ms |
27476 KB |
Output is correct |
17 |
Correct |
42 ms |
14332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
94 ms |
15740 KB |
Output is correct |
3 |
Correct |
141 ms |
16976 KB |
Output is correct |
4 |
Correct |
122 ms |
17212 KB |
Output is correct |
5 |
Correct |
176 ms |
20792 KB |
Output is correct |
6 |
Correct |
179 ms |
20816 KB |
Output is correct |
7 |
Correct |
178 ms |
20876 KB |
Output is correct |
8 |
Correct |
174 ms |
20824 KB |
Output is correct |
9 |
Correct |
193 ms |
21008 KB |
Output is correct |
10 |
Correct |
192 ms |
20820 KB |
Output is correct |
11 |
Correct |
185 ms |
20820 KB |
Output is correct |
12 |
Correct |
185 ms |
20820 KB |
Output is correct |
13 |
Incorrect |
177 ms |
21360 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9864 KB |
Output is correct |
3 |
Correct |
6 ms |
9820 KB |
Output is correct |
4 |
Correct |
214 ms |
20676 KB |
Output is correct |
5 |
Correct |
216 ms |
20816 KB |
Output is correct |
6 |
Correct |
257 ms |
23632 KB |
Output is correct |
7 |
Correct |
280 ms |
25300 KB |
Output is correct |
8 |
Correct |
256 ms |
25172 KB |
Output is correct |
9 |
Correct |
277 ms |
25300 KB |
Output is correct |
10 |
Correct |
260 ms |
25288 KB |
Output is correct |
11 |
Correct |
266 ms |
25172 KB |
Output is correct |
12 |
Correct |
268 ms |
25376 KB |
Output is correct |
13 |
Correct |
259 ms |
25172 KB |
Output is correct |
14 |
Correct |
36 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9860 KB |
Output is correct |
4 |
Correct |
5 ms |
9748 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
4 ms |
9732 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
7 ms |
9920 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9816 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9824 KB |
Output is correct |
18 |
Correct |
4 ms |
9744 KB |
Output is correct |
19 |
Correct |
4 ms |
9828 KB |
Output is correct |
20 |
Correct |
5 ms |
9916 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9824 KB |
Output is correct |
23 |
Correct |
5 ms |
9828 KB |
Output is correct |
24 |
Correct |
6 ms |
9924 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
9820 KB |
Output is correct |
27 |
Correct |
5 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
6 ms |
10076 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
7 ms |
10076 KB |
Output is correct |
33 |
Correct |
9 ms |
10148 KB |
Output is correct |
34 |
Correct |
7 ms |
10124 KB |
Output is correct |
35 |
Correct |
6 ms |
10060 KB |
Output is correct |
36 |
Correct |
6 ms |
9964 KB |
Output is correct |
37 |
Correct |
6 ms |
10076 KB |
Output is correct |
38 |
Correct |
6 ms |
9940 KB |
Output is correct |
39 |
Correct |
6 ms |
10076 KB |
Output is correct |
40 |
Correct |
6 ms |
10080 KB |
Output is correct |
41 |
Correct |
5 ms |
10076 KB |
Output is correct |
42 |
Correct |
6 ms |
10076 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
6 ms |
10076 KB |
Output is correct |
45 |
Incorrect |
5 ms |
10076 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |