#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 3e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll n,m,q,timer = 0;
vector<ll>adj[N];
ll tin[N], heavy[N], sz[N], p[N], head[N], res[N], a[N];
vector<pll>t[N];
set<array<ll,3>>s;
struct fenwick_tree{
ll n;
vector<ll>ft;
void init(ll _n){
n = _n;
ft.resize(n + 10);
}
void update(ll idx, ll val){
while(idx <= n) ft[idx] += val, idx += (idx & (-idx));
}
ll get(ll idx){
ll res = 0;
while(idx > 0) res += ft[idx], idx -= (idx & (-idx));
return res;
}
} ft;
void dfs(ll u, ll par){
sz[u] = 1;
for(auto v : adj[u]){
if(v == par) continue;
dfs(v, u);
p[v] = u;
sz[u] += sz[v];
if(sz[v] > sz[heavy[u]]) heavy[u] = v;
}
}
void HLD(ll u, ll h){
head[u] = h; tin[u] = ++timer;
if(heavy[u]) HLD(heavy[u], h);
for(auto v : adj[u]){
if(v != p[u] && v != heavy[u]) HLD(v, v);
}
}
void change(ll l, ll r, ll val){
auto it = s.lower_bound({l, l, 0});
if(it != s.begin()) --it;
vector<array<ll,3>>del, add;
add.pb({l, r, val});
while(it != s.end() && (*it)[0] <= r){
ll l1 = (*it)[0], r1 = (*it)[1], v1 = (*it)[2];
if(r1 < l){
++it;
continue;
}
del.pb(*it);
if(l1 < l) add.pb({l1, l - 1, v1});
if(r1 > r) add.pb({r + 1, r1, v1});
++it;
}
for(auto x : del){
ft.update(x[2], -(x[1] - x[0] + 1));
s.erase(x);
}
for(auto x : add){
ft.update(x[2], x[1] - x[0] + 1);
s.insert(x);
}
}
void upd(ll a, ll b, ll val){
for(; head[a] != head[b]; b = p[head[b]]){
if(tin[a] > tin[b]) swap(a, b);
change(tin[head[b]], tin[b], val);
}
if(tin[a] > tin[b]) swap(a, b);
change(tin[a], tin[b], val);
}
void to_thic_cau(){
cin >> n >> m >> q;
for(int i = 1; i <= n - 1;i++){
ll u,v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, 0);
HLD(1, 1);
for(int i = 1; i <= m;i++) cin >> a[i];
for(int i = 1; i <= q;i++){
ll l,r; cin >> l >> r;
t[r].pb({l, i});
}
s.insert({1, n, 1});
ft.init(m);
ft.update(1, m);
for(int i = 1; i <= m;i++){
if(i > 1) upd(a[i-1], a[i], i);
for(auto x : t[i]){
ll l = x.F, idx = x.S;
if(l == i){
res[idx] = 1;
continue;
}
res[idx] = ft.get(i) - ft.get(l);
}
}
for(int i = 1; i <= q;i++) cout << res[i] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14604 KB |
Output is correct |
3 |
Correct |
6 ms |
14608 KB |
Output is correct |
4 |
Correct |
9 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14572 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14644 KB |
Output is correct |
9 |
Correct |
10 ms |
14428 KB |
Output is correct |
10 |
Correct |
10 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
11 ms |
14668 KB |
Output is correct |
13 |
Correct |
9 ms |
14592 KB |
Output is correct |
14 |
Correct |
8 ms |
14424 KB |
Output is correct |
15 |
Correct |
6 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14448 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
8 ms |
14428 KB |
Output is correct |
19 |
Correct |
10 ms |
14664 KB |
Output is correct |
20 |
Correct |
8 ms |
14428 KB |
Output is correct |
21 |
Correct |
7 ms |
14428 KB |
Output is correct |
22 |
Correct |
10 ms |
14428 KB |
Output is correct |
23 |
Correct |
12 ms |
14428 KB |
Output is correct |
24 |
Correct |
7 ms |
14428 KB |
Output is correct |
25 |
Correct |
7 ms |
14428 KB |
Output is correct |
26 |
Correct |
10 ms |
14428 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
8 ms |
14428 KB |
Output is correct |
29 |
Correct |
9 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14604 KB |
Output is correct |
3 |
Correct |
6 ms |
14608 KB |
Output is correct |
4 |
Correct |
9 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14572 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14644 KB |
Output is correct |
9 |
Correct |
10 ms |
14428 KB |
Output is correct |
10 |
Correct |
10 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
11 ms |
14668 KB |
Output is correct |
13 |
Correct |
9 ms |
14592 KB |
Output is correct |
14 |
Correct |
8 ms |
14424 KB |
Output is correct |
15 |
Correct |
6 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14448 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
8 ms |
14428 KB |
Output is correct |
19 |
Correct |
10 ms |
14664 KB |
Output is correct |
20 |
Correct |
8 ms |
14428 KB |
Output is correct |
21 |
Correct |
7 ms |
14428 KB |
Output is correct |
22 |
Correct |
10 ms |
14428 KB |
Output is correct |
23 |
Correct |
12 ms |
14428 KB |
Output is correct |
24 |
Correct |
7 ms |
14428 KB |
Output is correct |
25 |
Correct |
7 ms |
14428 KB |
Output is correct |
26 |
Correct |
10 ms |
14428 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
8 ms |
14428 KB |
Output is correct |
29 |
Correct |
9 ms |
14428 KB |
Output is correct |
30 |
Correct |
15 ms |
14680 KB |
Output is correct |
31 |
Correct |
13 ms |
14820 KB |
Output is correct |
32 |
Correct |
19 ms |
14940 KB |
Output is correct |
33 |
Correct |
13 ms |
14940 KB |
Output is correct |
34 |
Correct |
12 ms |
14940 KB |
Output is correct |
35 |
Correct |
13 ms |
14940 KB |
Output is correct |
36 |
Correct |
12 ms |
14940 KB |
Output is correct |
37 |
Correct |
13 ms |
14940 KB |
Output is correct |
38 |
Correct |
12 ms |
14824 KB |
Output is correct |
39 |
Correct |
8 ms |
14940 KB |
Output is correct |
40 |
Correct |
7 ms |
14988 KB |
Output is correct |
41 |
Correct |
7 ms |
14940 KB |
Output is correct |
42 |
Correct |
7 ms |
14940 KB |
Output is correct |
43 |
Correct |
7 ms |
14940 KB |
Output is correct |
44 |
Correct |
9 ms |
14820 KB |
Output is correct |
45 |
Correct |
15 ms |
14940 KB |
Output is correct |
46 |
Correct |
11 ms |
14936 KB |
Output is correct |
47 |
Correct |
11 ms |
14936 KB |
Output is correct |
48 |
Correct |
11 ms |
14940 KB |
Output is correct |
49 |
Correct |
15 ms |
14936 KB |
Output is correct |
50 |
Correct |
10 ms |
14936 KB |
Output is correct |
51 |
Correct |
13 ms |
14816 KB |
Output is correct |
52 |
Correct |
15 ms |
14896 KB |
Output is correct |
53 |
Correct |
11 ms |
14940 KB |
Output is correct |
54 |
Correct |
16 ms |
14816 KB |
Output is correct |
55 |
Correct |
9 ms |
14940 KB |
Output is correct |
56 |
Correct |
9 ms |
14936 KB |
Output is correct |
57 |
Correct |
10 ms |
14768 KB |
Output is correct |
58 |
Correct |
13 ms |
14948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14516 KB |
Output is correct |
2 |
Correct |
9 ms |
14424 KB |
Output is correct |
3 |
Correct |
10 ms |
14684 KB |
Output is correct |
4 |
Correct |
96 ms |
29064 KB |
Output is correct |
5 |
Correct |
79 ms |
32084 KB |
Output is correct |
6 |
Correct |
104 ms |
33876 KB |
Output is correct |
7 |
Correct |
175 ms |
37716 KB |
Output is correct |
8 |
Correct |
130 ms |
37716 KB |
Output is correct |
9 |
Correct |
155 ms |
37760 KB |
Output is correct |
10 |
Correct |
121 ms |
37752 KB |
Output is correct |
11 |
Correct |
122 ms |
37764 KB |
Output is correct |
12 |
Correct |
109 ms |
37748 KB |
Output is correct |
13 |
Correct |
115 ms |
37712 KB |
Output is correct |
14 |
Correct |
110 ms |
37700 KB |
Output is correct |
15 |
Correct |
40 ms |
33472 KB |
Output is correct |
16 |
Correct |
109 ms |
37468 KB |
Output is correct |
17 |
Correct |
74 ms |
20952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
259 ms |
22748 KB |
Output is correct |
3 |
Correct |
422 ms |
24320 KB |
Output is correct |
4 |
Correct |
367 ms |
24704 KB |
Output is correct |
5 |
Correct |
634 ms |
29640 KB |
Output is correct |
6 |
Correct |
620 ms |
29524 KB |
Output is correct |
7 |
Correct |
602 ms |
29520 KB |
Output is correct |
8 |
Correct |
620 ms |
29600 KB |
Output is correct |
9 |
Correct |
656 ms |
29668 KB |
Output is correct |
10 |
Correct |
610 ms |
29588 KB |
Output is correct |
11 |
Correct |
594 ms |
29760 KB |
Output is correct |
12 |
Correct |
591 ms |
29924 KB |
Output is correct |
13 |
Correct |
545 ms |
30204 KB |
Output is correct |
14 |
Correct |
578 ms |
31180 KB |
Output is correct |
15 |
Correct |
627 ms |
34804 KB |
Output is correct |
16 |
Correct |
664 ms |
30156 KB |
Output is correct |
17 |
Correct |
601 ms |
30280 KB |
Output is correct |
18 |
Correct |
582 ms |
30296 KB |
Output is correct |
19 |
Correct |
324 ms |
25388 KB |
Output is correct |
20 |
Correct |
318 ms |
25284 KB |
Output is correct |
21 |
Correct |
320 ms |
25164 KB |
Output is correct |
22 |
Correct |
316 ms |
25236 KB |
Output is correct |
23 |
Correct |
324 ms |
25172 KB |
Output is correct |
24 |
Correct |
324 ms |
25200 KB |
Output is correct |
25 |
Correct |
322 ms |
25224 KB |
Output is correct |
26 |
Correct |
340 ms |
25172 KB |
Output is correct |
27 |
Correct |
333 ms |
25172 KB |
Output is correct |
28 |
Correct |
325 ms |
25308 KB |
Output is correct |
29 |
Correct |
344 ms |
25428 KB |
Output is correct |
30 |
Correct |
377 ms |
25864 KB |
Output is correct |
31 |
Correct |
334 ms |
25684 KB |
Output is correct |
32 |
Correct |
331 ms |
26256 KB |
Output is correct |
33 |
Correct |
343 ms |
28004 KB |
Output is correct |
34 |
Correct |
354 ms |
30520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14560 KB |
Output is correct |
3 |
Correct |
10 ms |
14684 KB |
Output is correct |
4 |
Correct |
733 ms |
28764 KB |
Output is correct |
5 |
Correct |
792 ms |
29016 KB |
Output is correct |
6 |
Correct |
893 ms |
32996 KB |
Output is correct |
7 |
Correct |
927 ms |
35120 KB |
Output is correct |
8 |
Correct |
914 ms |
35156 KB |
Output is correct |
9 |
Correct |
923 ms |
35108 KB |
Output is correct |
10 |
Correct |
892 ms |
35152 KB |
Output is correct |
11 |
Correct |
975 ms |
35064 KB |
Output is correct |
12 |
Correct |
995 ms |
35036 KB |
Output is correct |
13 |
Correct |
976 ms |
35064 KB |
Output is correct |
14 |
Correct |
56 ms |
20904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14604 KB |
Output is correct |
3 |
Correct |
6 ms |
14608 KB |
Output is correct |
4 |
Correct |
9 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14572 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14644 KB |
Output is correct |
9 |
Correct |
10 ms |
14428 KB |
Output is correct |
10 |
Correct |
10 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
11 ms |
14668 KB |
Output is correct |
13 |
Correct |
9 ms |
14592 KB |
Output is correct |
14 |
Correct |
8 ms |
14424 KB |
Output is correct |
15 |
Correct |
6 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14448 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
8 ms |
14428 KB |
Output is correct |
19 |
Correct |
10 ms |
14664 KB |
Output is correct |
20 |
Correct |
8 ms |
14428 KB |
Output is correct |
21 |
Correct |
7 ms |
14428 KB |
Output is correct |
22 |
Correct |
10 ms |
14428 KB |
Output is correct |
23 |
Correct |
12 ms |
14428 KB |
Output is correct |
24 |
Correct |
7 ms |
14428 KB |
Output is correct |
25 |
Correct |
7 ms |
14428 KB |
Output is correct |
26 |
Correct |
10 ms |
14428 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
8 ms |
14428 KB |
Output is correct |
29 |
Correct |
9 ms |
14428 KB |
Output is correct |
30 |
Correct |
15 ms |
14680 KB |
Output is correct |
31 |
Correct |
13 ms |
14820 KB |
Output is correct |
32 |
Correct |
19 ms |
14940 KB |
Output is correct |
33 |
Correct |
13 ms |
14940 KB |
Output is correct |
34 |
Correct |
12 ms |
14940 KB |
Output is correct |
35 |
Correct |
13 ms |
14940 KB |
Output is correct |
36 |
Correct |
12 ms |
14940 KB |
Output is correct |
37 |
Correct |
13 ms |
14940 KB |
Output is correct |
38 |
Correct |
12 ms |
14824 KB |
Output is correct |
39 |
Correct |
8 ms |
14940 KB |
Output is correct |
40 |
Correct |
7 ms |
14988 KB |
Output is correct |
41 |
Correct |
7 ms |
14940 KB |
Output is correct |
42 |
Correct |
7 ms |
14940 KB |
Output is correct |
43 |
Correct |
7 ms |
14940 KB |
Output is correct |
44 |
Correct |
9 ms |
14820 KB |
Output is correct |
45 |
Correct |
15 ms |
14940 KB |
Output is correct |
46 |
Correct |
11 ms |
14936 KB |
Output is correct |
47 |
Correct |
11 ms |
14936 KB |
Output is correct |
48 |
Correct |
11 ms |
14940 KB |
Output is correct |
49 |
Correct |
15 ms |
14936 KB |
Output is correct |
50 |
Correct |
10 ms |
14936 KB |
Output is correct |
51 |
Correct |
13 ms |
14816 KB |
Output is correct |
52 |
Correct |
15 ms |
14896 KB |
Output is correct |
53 |
Correct |
11 ms |
14940 KB |
Output is correct |
54 |
Correct |
16 ms |
14816 KB |
Output is correct |
55 |
Correct |
9 ms |
14940 KB |
Output is correct |
56 |
Correct |
9 ms |
14936 KB |
Output is correct |
57 |
Correct |
10 ms |
14768 KB |
Output is correct |
58 |
Correct |
13 ms |
14948 KB |
Output is correct |
59 |
Correct |
9 ms |
14516 KB |
Output is correct |
60 |
Correct |
9 ms |
14424 KB |
Output is correct |
61 |
Correct |
10 ms |
14684 KB |
Output is correct |
62 |
Correct |
96 ms |
29064 KB |
Output is correct |
63 |
Correct |
79 ms |
32084 KB |
Output is correct |
64 |
Correct |
104 ms |
33876 KB |
Output is correct |
65 |
Correct |
175 ms |
37716 KB |
Output is correct |
66 |
Correct |
130 ms |
37716 KB |
Output is correct |
67 |
Correct |
155 ms |
37760 KB |
Output is correct |
68 |
Correct |
121 ms |
37752 KB |
Output is correct |
69 |
Correct |
122 ms |
37764 KB |
Output is correct |
70 |
Correct |
109 ms |
37748 KB |
Output is correct |
71 |
Correct |
115 ms |
37712 KB |
Output is correct |
72 |
Correct |
110 ms |
37700 KB |
Output is correct |
73 |
Correct |
40 ms |
33472 KB |
Output is correct |
74 |
Correct |
109 ms |
37468 KB |
Output is correct |
75 |
Correct |
74 ms |
20952 KB |
Output is correct |
76 |
Correct |
6 ms |
14424 KB |
Output is correct |
77 |
Correct |
259 ms |
22748 KB |
Output is correct |
78 |
Correct |
422 ms |
24320 KB |
Output is correct |
79 |
Correct |
367 ms |
24704 KB |
Output is correct |
80 |
Correct |
634 ms |
29640 KB |
Output is correct |
81 |
Correct |
620 ms |
29524 KB |
Output is correct |
82 |
Correct |
602 ms |
29520 KB |
Output is correct |
83 |
Correct |
620 ms |
29600 KB |
Output is correct |
84 |
Correct |
656 ms |
29668 KB |
Output is correct |
85 |
Correct |
610 ms |
29588 KB |
Output is correct |
86 |
Correct |
594 ms |
29760 KB |
Output is correct |
87 |
Correct |
591 ms |
29924 KB |
Output is correct |
88 |
Correct |
545 ms |
30204 KB |
Output is correct |
89 |
Correct |
578 ms |
31180 KB |
Output is correct |
90 |
Correct |
627 ms |
34804 KB |
Output is correct |
91 |
Correct |
664 ms |
30156 KB |
Output is correct |
92 |
Correct |
601 ms |
30280 KB |
Output is correct |
93 |
Correct |
582 ms |
30296 KB |
Output is correct |
94 |
Correct |
324 ms |
25388 KB |
Output is correct |
95 |
Correct |
318 ms |
25284 KB |
Output is correct |
96 |
Correct |
320 ms |
25164 KB |
Output is correct |
97 |
Correct |
316 ms |
25236 KB |
Output is correct |
98 |
Correct |
324 ms |
25172 KB |
Output is correct |
99 |
Correct |
324 ms |
25200 KB |
Output is correct |
100 |
Correct |
322 ms |
25224 KB |
Output is correct |
101 |
Correct |
340 ms |
25172 KB |
Output is correct |
102 |
Correct |
333 ms |
25172 KB |
Output is correct |
103 |
Correct |
325 ms |
25308 KB |
Output is correct |
104 |
Correct |
344 ms |
25428 KB |
Output is correct |
105 |
Correct |
377 ms |
25864 KB |
Output is correct |
106 |
Correct |
334 ms |
25684 KB |
Output is correct |
107 |
Correct |
331 ms |
26256 KB |
Output is correct |
108 |
Correct |
343 ms |
28004 KB |
Output is correct |
109 |
Correct |
354 ms |
30520 KB |
Output is correct |
110 |
Correct |
5 ms |
14428 KB |
Output is correct |
111 |
Correct |
5 ms |
14560 KB |
Output is correct |
112 |
Correct |
10 ms |
14684 KB |
Output is correct |
113 |
Correct |
733 ms |
28764 KB |
Output is correct |
114 |
Correct |
792 ms |
29016 KB |
Output is correct |
115 |
Correct |
893 ms |
32996 KB |
Output is correct |
116 |
Correct |
927 ms |
35120 KB |
Output is correct |
117 |
Correct |
914 ms |
35156 KB |
Output is correct |
118 |
Correct |
923 ms |
35108 KB |
Output is correct |
119 |
Correct |
892 ms |
35152 KB |
Output is correct |
120 |
Correct |
975 ms |
35064 KB |
Output is correct |
121 |
Correct |
995 ms |
35036 KB |
Output is correct |
122 |
Correct |
976 ms |
35064 KB |
Output is correct |
123 |
Correct |
56 ms |
20904 KB |
Output is correct |
124 |
Correct |
538 ms |
34224 KB |
Output is correct |
125 |
Correct |
415 ms |
31124 KB |
Output is correct |
126 |
Correct |
644 ms |
34896 KB |
Output is correct |
127 |
Correct |
598 ms |
34684 KB |
Output is correct |
128 |
Correct |
606 ms |
34896 KB |
Output is correct |
129 |
Correct |
570 ms |
34760 KB |
Output is correct |
130 |
Correct |
625 ms |
34684 KB |
Output is correct |
131 |
Correct |
129 ms |
36688 KB |
Output is correct |
132 |
Correct |
143 ms |
37712 KB |
Output is correct |
133 |
Correct |
143 ms |
33932 KB |
Output is correct |
134 |
Correct |
360 ms |
30548 KB |
Output is correct |
135 |
Correct |
387 ms |
30548 KB |
Output is correct |
136 |
Correct |
355 ms |
30456 KB |
Output is correct |
137 |
Correct |
235 ms |
35496 KB |
Output is correct |
138 |
Correct |
261 ms |
35356 KB |
Output is correct |
139 |
Correct |
289 ms |
35508 KB |
Output is correct |
140 |
Correct |
280 ms |
36032 KB |
Output is correct |
141 |
Correct |
301 ms |
36156 KB |
Output is correct |
142 |
Correct |
267 ms |
36008 KB |
Output is correct |
143 |
Correct |
53 ms |
26308 KB |
Output is correct |
144 |
Correct |
634 ms |
34388 KB |
Output is correct |