#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
const int N=1e5+10;
int n, m, q, sz[N];
vector<int> g[N];
int id[N], tp[N], par[N], depth[N];
struct node {
int l, r;
int val;
bool operator<(const node& B) const {
return l<B.l;
}
};
int fen[N+10];
void upd(int at, int val) {
at++;
while(at<N) {
fen[at]+=val;
at+=at&(-at);
}
}
int que(int at) {
int res=0;
at++;
while(at) {
res+=fen[at];
at-=at&(-at);
}
return res;
}
set<node> s;
auto split(int x) {
auto it=--s.upper_bound(node({x,0,0}));
if(it->l==x)return it;
if(it->r<x)return s.end();
int l=it->l, r=it->r, val = it->val;
s.erase(it);
s.insert({l, x-1, val});
return s.insert({x, r, val}).first;
}
void update(int l, int r, int val) {
auto it2=split(r+1), it1=split(l);
for(auto it=it1;it!=it2;it++) {
upd(it->val, -(it->r-it->l+1));
}
s.erase(it1, it2);
s.insert({l, r, val});
upd(val, r-l+1);
}
int C=1;
void dfs_sz(int at, int p) {
sz[at]=1;
for(int to:g[at]) {
if(to==p)continue;
depth[to]=depth[at]+1;
dfs_sz(to,at);
sz[at]+=sz[to];
}
}
void dfs(int at, int p, int top) {
id[at]=C++;
tp[at]=top;
par[at]=p;
int hv_child=-1, hv_sz=-1;
for(int to:g[at]) {
if(to==p)continue;
if(sz[to]>hv_sz) {
hv_child=to;
hv_sz=sz[to];
}
}
if(hv_child==-1)return;
dfs(hv_child, at, top);
for(int to:g[at]) {
if(to==p or to==hv_child)continue;
dfs(to, at, to);
}
}
void queryy(int x, int y, int val) {
while(tp[x]!=tp[y]) {
if(depth[tp[x]]<depth[tp[y]])swap(x,y);
update(id[tp[x]], id[x], val);
x=par[tp[x]];
}
if(depth[x]>depth[y])swap(x,y);
update(id[x], id[y], val);
}
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q;
for(int i = 1;i<n;i++) {
int A, B;
cin >> A >> B;
g[A].push_back(B);
g[B].push_back(A);
}
for(int i = 0;i<n+10;i++)s.insert({i,0,0});
upd(0, n+10);
dfs_sz(1,1);
dfs(1,1,1);
int c[m+1];
for(int i = 1;i<=m;i++)cin >> c[i];
vector<pair<int,int>> quee[m+1];
int ress[q];
for(int i = 0;i<q;i++) {
int l, r;
cin >> l >> r;
quee[r].push_back({l, i});
}
for(int i = 1;i<=m;i++) {
if(i>1)queryy(c[i], c[i-1], i);
for(auto j:quee[i]) {
if(j.first!=i) {
ress[j.second]=que(m+10)-que(j.first);
}
else ress[j.second]=1;
}
}
for(int i = 0;i<q;i++)cout << ress[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
5 ms |
2948 KB |
Output is correct |
31 |
Correct |
5 ms |
2900 KB |
Output is correct |
32 |
Correct |
7 ms |
3076 KB |
Output is correct |
33 |
Correct |
8 ms |
3116 KB |
Output is correct |
34 |
Correct |
7 ms |
3028 KB |
Output is correct |
35 |
Correct |
6 ms |
3072 KB |
Output is correct |
36 |
Correct |
6 ms |
3088 KB |
Output is correct |
37 |
Correct |
6 ms |
3028 KB |
Output is correct |
38 |
Correct |
4 ms |
3028 KB |
Output is correct |
39 |
Correct |
4 ms |
3028 KB |
Output is correct |
40 |
Correct |
4 ms |
3028 KB |
Output is correct |
41 |
Correct |
4 ms |
3156 KB |
Output is correct |
42 |
Correct |
4 ms |
3144 KB |
Output is correct |
43 |
Correct |
4 ms |
3076 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
5 ms |
3080 KB |
Output is correct |
46 |
Correct |
4 ms |
3092 KB |
Output is correct |
47 |
Correct |
5 ms |
3028 KB |
Output is correct |
48 |
Correct |
5 ms |
3028 KB |
Output is correct |
49 |
Correct |
4 ms |
3028 KB |
Output is correct |
50 |
Correct |
4 ms |
3076 KB |
Output is correct |
51 |
Correct |
5 ms |
3028 KB |
Output is correct |
52 |
Correct |
4 ms |
3028 KB |
Output is correct |
53 |
Correct |
4 ms |
3028 KB |
Output is correct |
54 |
Correct |
4 ms |
3028 KB |
Output is correct |
55 |
Correct |
4 ms |
3080 KB |
Output is correct |
56 |
Correct |
2 ms |
2772 KB |
Output is correct |
57 |
Correct |
3 ms |
2948 KB |
Output is correct |
58 |
Correct |
6 ms |
3080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2828 KB |
Output is correct |
4 |
Correct |
96 ms |
16784 KB |
Output is correct |
5 |
Correct |
89 ms |
20764 KB |
Output is correct |
6 |
Correct |
116 ms |
22784 KB |
Output is correct |
7 |
Correct |
121 ms |
26080 KB |
Output is correct |
8 |
Correct |
140 ms |
26076 KB |
Output is correct |
9 |
Correct |
126 ms |
26048 KB |
Output is correct |
10 |
Correct |
122 ms |
26124 KB |
Output is correct |
11 |
Correct |
125 ms |
26108 KB |
Output is correct |
12 |
Correct |
147 ms |
25204 KB |
Output is correct |
13 |
Correct |
134 ms |
25224 KB |
Output is correct |
14 |
Correct |
137 ms |
25148 KB |
Output is correct |
15 |
Correct |
56 ms |
22984 KB |
Output is correct |
16 |
Correct |
119 ms |
25848 KB |
Output is correct |
17 |
Correct |
42 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
222 ms |
10620 KB |
Output is correct |
3 |
Correct |
351 ms |
12280 KB |
Output is correct |
4 |
Correct |
296 ms |
13932 KB |
Output is correct |
5 |
Correct |
475 ms |
18928 KB |
Output is correct |
6 |
Correct |
498 ms |
19016 KB |
Output is correct |
7 |
Correct |
489 ms |
18944 KB |
Output is correct |
8 |
Correct |
473 ms |
18968 KB |
Output is correct |
9 |
Correct |
486 ms |
19016 KB |
Output is correct |
10 |
Correct |
472 ms |
18932 KB |
Output is correct |
11 |
Correct |
501 ms |
18960 KB |
Output is correct |
12 |
Correct |
461 ms |
19072 KB |
Output is correct |
13 |
Correct |
484 ms |
19424 KB |
Output is correct |
14 |
Correct |
520 ms |
20352 KB |
Output is correct |
15 |
Correct |
501 ms |
23892 KB |
Output is correct |
16 |
Correct |
474 ms |
19416 KB |
Output is correct |
17 |
Correct |
547 ms |
19400 KB |
Output is correct |
18 |
Correct |
449 ms |
19472 KB |
Output is correct |
19 |
Correct |
304 ms |
18764 KB |
Output is correct |
20 |
Correct |
293 ms |
18816 KB |
Output is correct |
21 |
Correct |
311 ms |
18856 KB |
Output is correct |
22 |
Correct |
308 ms |
18856 KB |
Output is correct |
23 |
Correct |
297 ms |
18764 KB |
Output is correct |
24 |
Correct |
297 ms |
18852 KB |
Output is correct |
25 |
Correct |
306 ms |
18768 KB |
Output is correct |
26 |
Correct |
300 ms |
18796 KB |
Output is correct |
27 |
Correct |
301 ms |
18980 KB |
Output is correct |
28 |
Correct |
324 ms |
18904 KB |
Output is correct |
29 |
Correct |
306 ms |
18892 KB |
Output is correct |
30 |
Correct |
322 ms |
19068 KB |
Output is correct |
31 |
Correct |
301 ms |
19304 KB |
Output is correct |
32 |
Correct |
305 ms |
19844 KB |
Output is correct |
33 |
Correct |
335 ms |
21364 KB |
Output is correct |
34 |
Correct |
310 ms |
23620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2820 KB |
Output is correct |
4 |
Correct |
571 ms |
16788 KB |
Output is correct |
5 |
Correct |
647 ms |
16964 KB |
Output is correct |
6 |
Correct |
725 ms |
20920 KB |
Output is correct |
7 |
Correct |
781 ms |
22920 KB |
Output is correct |
8 |
Correct |
771 ms |
22972 KB |
Output is correct |
9 |
Correct |
783 ms |
22860 KB |
Output is correct |
10 |
Correct |
806 ms |
22888 KB |
Output is correct |
11 |
Correct |
779 ms |
22880 KB |
Output is correct |
12 |
Correct |
792 ms |
22936 KB |
Output is correct |
13 |
Correct |
781 ms |
22948 KB |
Output is correct |
14 |
Correct |
42 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
5 ms |
2948 KB |
Output is correct |
31 |
Correct |
5 ms |
2900 KB |
Output is correct |
32 |
Correct |
7 ms |
3076 KB |
Output is correct |
33 |
Correct |
8 ms |
3116 KB |
Output is correct |
34 |
Correct |
7 ms |
3028 KB |
Output is correct |
35 |
Correct |
6 ms |
3072 KB |
Output is correct |
36 |
Correct |
6 ms |
3088 KB |
Output is correct |
37 |
Correct |
6 ms |
3028 KB |
Output is correct |
38 |
Correct |
4 ms |
3028 KB |
Output is correct |
39 |
Correct |
4 ms |
3028 KB |
Output is correct |
40 |
Correct |
4 ms |
3028 KB |
Output is correct |
41 |
Correct |
4 ms |
3156 KB |
Output is correct |
42 |
Correct |
4 ms |
3144 KB |
Output is correct |
43 |
Correct |
4 ms |
3076 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
5 ms |
3080 KB |
Output is correct |
46 |
Correct |
4 ms |
3092 KB |
Output is correct |
47 |
Correct |
5 ms |
3028 KB |
Output is correct |
48 |
Correct |
5 ms |
3028 KB |
Output is correct |
49 |
Correct |
4 ms |
3028 KB |
Output is correct |
50 |
Correct |
4 ms |
3076 KB |
Output is correct |
51 |
Correct |
5 ms |
3028 KB |
Output is correct |
52 |
Correct |
4 ms |
3028 KB |
Output is correct |
53 |
Correct |
4 ms |
3028 KB |
Output is correct |
54 |
Correct |
4 ms |
3028 KB |
Output is correct |
55 |
Correct |
4 ms |
3080 KB |
Output is correct |
56 |
Correct |
2 ms |
2772 KB |
Output is correct |
57 |
Correct |
3 ms |
2948 KB |
Output is correct |
58 |
Correct |
6 ms |
3080 KB |
Output is correct |
59 |
Correct |
1 ms |
2644 KB |
Output is correct |
60 |
Correct |
1 ms |
2644 KB |
Output is correct |
61 |
Correct |
2 ms |
2828 KB |
Output is correct |
62 |
Correct |
96 ms |
16784 KB |
Output is correct |
63 |
Correct |
89 ms |
20764 KB |
Output is correct |
64 |
Correct |
116 ms |
22784 KB |
Output is correct |
65 |
Correct |
121 ms |
26080 KB |
Output is correct |
66 |
Correct |
140 ms |
26076 KB |
Output is correct |
67 |
Correct |
126 ms |
26048 KB |
Output is correct |
68 |
Correct |
122 ms |
26124 KB |
Output is correct |
69 |
Correct |
125 ms |
26108 KB |
Output is correct |
70 |
Correct |
147 ms |
25204 KB |
Output is correct |
71 |
Correct |
134 ms |
25224 KB |
Output is correct |
72 |
Correct |
137 ms |
25148 KB |
Output is correct |
73 |
Correct |
56 ms |
22984 KB |
Output is correct |
74 |
Correct |
119 ms |
25848 KB |
Output is correct |
75 |
Correct |
42 ms |
9804 KB |
Output is correct |
76 |
Correct |
1 ms |
2644 KB |
Output is correct |
77 |
Correct |
222 ms |
10620 KB |
Output is correct |
78 |
Correct |
351 ms |
12280 KB |
Output is correct |
79 |
Correct |
296 ms |
13932 KB |
Output is correct |
80 |
Correct |
475 ms |
18928 KB |
Output is correct |
81 |
Correct |
498 ms |
19016 KB |
Output is correct |
82 |
Correct |
489 ms |
18944 KB |
Output is correct |
83 |
Correct |
473 ms |
18968 KB |
Output is correct |
84 |
Correct |
486 ms |
19016 KB |
Output is correct |
85 |
Correct |
472 ms |
18932 KB |
Output is correct |
86 |
Correct |
501 ms |
18960 KB |
Output is correct |
87 |
Correct |
461 ms |
19072 KB |
Output is correct |
88 |
Correct |
484 ms |
19424 KB |
Output is correct |
89 |
Correct |
520 ms |
20352 KB |
Output is correct |
90 |
Correct |
501 ms |
23892 KB |
Output is correct |
91 |
Correct |
474 ms |
19416 KB |
Output is correct |
92 |
Correct |
547 ms |
19400 KB |
Output is correct |
93 |
Correct |
449 ms |
19472 KB |
Output is correct |
94 |
Correct |
304 ms |
18764 KB |
Output is correct |
95 |
Correct |
293 ms |
18816 KB |
Output is correct |
96 |
Correct |
311 ms |
18856 KB |
Output is correct |
97 |
Correct |
308 ms |
18856 KB |
Output is correct |
98 |
Correct |
297 ms |
18764 KB |
Output is correct |
99 |
Correct |
297 ms |
18852 KB |
Output is correct |
100 |
Correct |
306 ms |
18768 KB |
Output is correct |
101 |
Correct |
300 ms |
18796 KB |
Output is correct |
102 |
Correct |
301 ms |
18980 KB |
Output is correct |
103 |
Correct |
324 ms |
18904 KB |
Output is correct |
104 |
Correct |
306 ms |
18892 KB |
Output is correct |
105 |
Correct |
322 ms |
19068 KB |
Output is correct |
106 |
Correct |
301 ms |
19304 KB |
Output is correct |
107 |
Correct |
305 ms |
19844 KB |
Output is correct |
108 |
Correct |
335 ms |
21364 KB |
Output is correct |
109 |
Correct |
310 ms |
23620 KB |
Output is correct |
110 |
Correct |
1 ms |
2644 KB |
Output is correct |
111 |
Correct |
1 ms |
2644 KB |
Output is correct |
112 |
Correct |
3 ms |
2820 KB |
Output is correct |
113 |
Correct |
571 ms |
16788 KB |
Output is correct |
114 |
Correct |
647 ms |
16964 KB |
Output is correct |
115 |
Correct |
725 ms |
20920 KB |
Output is correct |
116 |
Correct |
781 ms |
22920 KB |
Output is correct |
117 |
Correct |
771 ms |
22972 KB |
Output is correct |
118 |
Correct |
783 ms |
22860 KB |
Output is correct |
119 |
Correct |
806 ms |
22888 KB |
Output is correct |
120 |
Correct |
779 ms |
22880 KB |
Output is correct |
121 |
Correct |
792 ms |
22936 KB |
Output is correct |
122 |
Correct |
781 ms |
22948 KB |
Output is correct |
123 |
Correct |
42 ms |
9676 KB |
Output is correct |
124 |
Correct |
433 ms |
22216 KB |
Output is correct |
125 |
Correct |
342 ms |
19636 KB |
Output is correct |
126 |
Correct |
563 ms |
22972 KB |
Output is correct |
127 |
Correct |
565 ms |
22968 KB |
Output is correct |
128 |
Correct |
515 ms |
23096 KB |
Output is correct |
129 |
Correct |
528 ms |
23092 KB |
Output is correct |
130 |
Correct |
508 ms |
23048 KB |
Output is correct |
131 |
Correct |
134 ms |
25164 KB |
Output is correct |
132 |
Correct |
129 ms |
26024 KB |
Output is correct |
133 |
Correct |
151 ms |
22988 KB |
Output is correct |
134 |
Correct |
325 ms |
22868 KB |
Output is correct |
135 |
Correct |
339 ms |
22980 KB |
Output is correct |
136 |
Correct |
330 ms |
22984 KB |
Output is correct |
137 |
Correct |
199 ms |
23360 KB |
Output is correct |
138 |
Correct |
210 ms |
23300 KB |
Output is correct |
139 |
Correct |
203 ms |
23292 KB |
Output is correct |
140 |
Correct |
197 ms |
23360 KB |
Output is correct |
141 |
Correct |
195 ms |
23308 KB |
Output is correct |
142 |
Correct |
209 ms |
23312 KB |
Output is correct |
143 |
Correct |
79 ms |
17140 KB |
Output is correct |
144 |
Correct |
542 ms |
22704 KB |
Output is correct |