#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2")
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
const int N=1e5+1, S=400, LG=18;
struct Query{
int l, r, i;
Query(int a=0, int b=0, int c=0): l(a), r(b), i(c){}
bool operator<(const Query& x){
return l/S!=x.l/S?l<x.l:r<x.r;
}
} qq[N];
struct FenwickTree{
int n;
vector<int> t;
void init(int _n){
n=_n;
t.assign(n+1, 0);
}
void update(int pos, int val){
for (int i=pos; i<=n; i+=i&(-i)) t[i]+=val;
}
int get(int pos){
int ans=0;
for (int i=pos; i; i-=i&(-i)) ans+=t[i];
return ans;
}
int lower_bound(int val){
int ans=0;
for (int i=LG; i>=0; --i) if (ans+(1<<i)<=n && t[ans+(1<<i)]<val){
ans+=1<<i;
val-=t[ans];
}
return ans+1;
}
};
int n, m, q, a[N], tdfs, tin[N], tout[N], dep[N], ans[N], cnt[N], tour[N];
pair<int, int> st[N*2][LG];
vector<int> g[N];
void dfs(int u, int p){
tin[u]=++tdfs;
tour[tdfs]=u;
dep[u]=dep[p]+1;
st[tdfs][0]={dep[u], u};
for (int v:g[u]) if (v!=p){
dfs(v, u);
++tdfs;
st[tdfs][0]={dep[u], u};
}
tout[u]=tdfs;
}
void build(){
for (int k=1; k<LG; ++k) for (int i=1; i+(1<<k)-1<=tdfs; ++i) st[i][k]=min(st[i][k-1], st[i+(1<<(k-1))][k-1]);
}
int lca(int u, int v){
u=tin[u]; v=tin[v];
if (u>v) swap(u, v);
int lg=__lg(v-u+1);
return min(st[u][lg], st[v-(1<<lg)+1][lg]).second;
}
int distance(int u, int v){
return dep[u]+dep[v]-dep[lca(u, v)]*2;
}
namespace among{
struct SUS{
int cnt[N];
int size;
FenwickTree bit;
int ans;
int get_order(int i){
return bit.get(i);
}
int find_by_order(int i){
return bit.lower_bound(i);
}
int find_first(){
return bit.lower_bound(1);
}
int find_last(){
return bit.lower_bound(size);
}
void insert(int x){
if (cnt[x]==0 && size){
int i=get_order(x);
int next=i==size?find_first():find_by_order(i+1);
int prev=i==0?find_last():find_by_order(i);
ans-=distance(tour[prev], tour[next]);
ans+=distance(tour[prev], tour[x]);
ans+=distance(tour[x], tour[next]);
}
++cnt[x];
bit.update(x, 1);
++size;
}
void erase(int x){
bit.update(x, -1);
--size;
--cnt[x];
if (cnt[x]==0 && size){
int i=get_order(x);
int next=i==size?find_first():find_by_order(i+1);
int prev=i==0?find_last():find_by_order(i);
ans+=distance(tour[prev], tour[next]);
ans-=distance(tour[prev], tour[x]);
ans-=distance(tour[x], tour[next]);
}
}
} us;
}
void solve(){
#ifdef sus
freopen("03-01.txt", "r", stdin);
freopen("cf.out", "w", stdout);
#endif
cin >> n >> m >> q;
for (int i=1; i<n; ++i){
int x, y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
build();
for (int i=1; i<=m; ++i) cin >> a[i];
for (int i=1; i<=q; ++i){
int x, y; cin >> x >> y;
qq[i]={x, y, i};
}
sort(qq+1, qq+q+1);
int cl=1, cr=0;
among::us.bit.init(n*2);
for (int i=1; i<=q; ++i){
int l=qq[i].l, r=qq[i].r;
while (l<cl){
--cl;
among::us.insert(tin[a[cl]]);
}
while (r>cr){
++cr;
among::us.insert(tin[a[cr]]);
}
while (l>cl){
among::us.erase(tin[a[cl]]);
++cl;
}
while (r<cr){
among::us.erase(tin[a[cr]]);
--cr;
}
ans[qq[i].i]=among::us.ans/2+1;
}
for (int i=1; i<=q; ++i) cout << ans[i] << '\n';
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9840 KB |
Output is correct |
4 |
Correct |
3 ms |
10072 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
10072 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 |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
6 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
23 |
Correct |
4 ms |
9820 KB |
Output is correct |
24 |
Correct |
6 ms |
9820 KB |
Output is correct |
25 |
Correct |
4 ms |
9844 KB |
Output is correct |
26 |
Correct |
4 ms |
9816 KB |
Output is correct |
27 |
Correct |
3 ms |
9840 KB |
Output is correct |
28 |
Correct |
3 ms |
9800 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9840 KB |
Output is correct |
4 |
Correct |
3 ms |
10072 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
10072 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 |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
6 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
23 |
Correct |
4 ms |
9820 KB |
Output is correct |
24 |
Correct |
6 ms |
9820 KB |
Output is correct |
25 |
Correct |
4 ms |
9844 KB |
Output is correct |
26 |
Correct |
4 ms |
9816 KB |
Output is correct |
27 |
Correct |
3 ms |
9840 KB |
Output is correct |
28 |
Correct |
3 ms |
9800 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
20 ms |
9820 KB |
Output is correct |
31 |
Correct |
22 ms |
9816 KB |
Output is correct |
32 |
Correct |
25 ms |
9960 KB |
Output is correct |
33 |
Correct |
26 ms |
9816 KB |
Output is correct |
34 |
Correct |
25 ms |
9820 KB |
Output is correct |
35 |
Correct |
5 ms |
9820 KB |
Output is correct |
36 |
Correct |
6 ms |
9820 KB |
Output is correct |
37 |
Correct |
5 ms |
9820 KB |
Output is correct |
38 |
Correct |
24 ms |
9804 KB |
Output is correct |
39 |
Correct |
25 ms |
9820 KB |
Output is correct |
40 |
Correct |
25 ms |
9816 KB |
Output is correct |
41 |
Correct |
5 ms |
10072 KB |
Output is correct |
42 |
Correct |
6 ms |
10072 KB |
Output is correct |
43 |
Correct |
5 ms |
9820 KB |
Output is correct |
44 |
Correct |
25 ms |
10072 KB |
Output is correct |
45 |
Correct |
25 ms |
9820 KB |
Output is correct |
46 |
Correct |
25 ms |
9820 KB |
Output is correct |
47 |
Correct |
4 ms |
9820 KB |
Output is correct |
48 |
Correct |
5 ms |
9816 KB |
Output is correct |
49 |
Correct |
5 ms |
9820 KB |
Output is correct |
50 |
Correct |
24 ms |
9968 KB |
Output is correct |
51 |
Correct |
22 ms |
9956 KB |
Output is correct |
52 |
Correct |
23 ms |
9820 KB |
Output is correct |
53 |
Correct |
24 ms |
9816 KB |
Output is correct |
54 |
Correct |
23 ms |
9820 KB |
Output is correct |
55 |
Correct |
24 ms |
9820 KB |
Output is correct |
56 |
Correct |
4 ms |
9820 KB |
Output is correct |
57 |
Correct |
4 ms |
9816 KB |
Output is correct |
58 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4859 ms |
31560 KB |
Output is correct |
5 |
Correct |
3336 ms |
42928 KB |
Output is correct |
6 |
Execution timed out |
5091 ms |
45788 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Runtime error |
55 ms |
54196 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
4 ms |
9816 KB |
Output is correct |
4 |
Runtime error |
97 ms |
54300 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9840 KB |
Output is correct |
4 |
Correct |
3 ms |
10072 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
10072 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 |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
6 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
23 |
Correct |
4 ms |
9820 KB |
Output is correct |
24 |
Correct |
6 ms |
9820 KB |
Output is correct |
25 |
Correct |
4 ms |
9844 KB |
Output is correct |
26 |
Correct |
4 ms |
9816 KB |
Output is correct |
27 |
Correct |
3 ms |
9840 KB |
Output is correct |
28 |
Correct |
3 ms |
9800 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
20 ms |
9820 KB |
Output is correct |
31 |
Correct |
22 ms |
9816 KB |
Output is correct |
32 |
Correct |
25 ms |
9960 KB |
Output is correct |
33 |
Correct |
26 ms |
9816 KB |
Output is correct |
34 |
Correct |
25 ms |
9820 KB |
Output is correct |
35 |
Correct |
5 ms |
9820 KB |
Output is correct |
36 |
Correct |
6 ms |
9820 KB |
Output is correct |
37 |
Correct |
5 ms |
9820 KB |
Output is correct |
38 |
Correct |
24 ms |
9804 KB |
Output is correct |
39 |
Correct |
25 ms |
9820 KB |
Output is correct |
40 |
Correct |
25 ms |
9816 KB |
Output is correct |
41 |
Correct |
5 ms |
10072 KB |
Output is correct |
42 |
Correct |
6 ms |
10072 KB |
Output is correct |
43 |
Correct |
5 ms |
9820 KB |
Output is correct |
44 |
Correct |
25 ms |
10072 KB |
Output is correct |
45 |
Correct |
25 ms |
9820 KB |
Output is correct |
46 |
Correct |
25 ms |
9820 KB |
Output is correct |
47 |
Correct |
4 ms |
9820 KB |
Output is correct |
48 |
Correct |
5 ms |
9816 KB |
Output is correct |
49 |
Correct |
5 ms |
9820 KB |
Output is correct |
50 |
Correct |
24 ms |
9968 KB |
Output is correct |
51 |
Correct |
22 ms |
9956 KB |
Output is correct |
52 |
Correct |
23 ms |
9820 KB |
Output is correct |
53 |
Correct |
24 ms |
9816 KB |
Output is correct |
54 |
Correct |
23 ms |
9820 KB |
Output is correct |
55 |
Correct |
24 ms |
9820 KB |
Output is correct |
56 |
Correct |
4 ms |
9820 KB |
Output is correct |
57 |
Correct |
4 ms |
9816 KB |
Output is correct |
58 |
Correct |
4 ms |
9820 KB |
Output is correct |
59 |
Correct |
3 ms |
9816 KB |
Output is correct |
60 |
Correct |
3 ms |
9820 KB |
Output is correct |
61 |
Correct |
4 ms |
9820 KB |
Output is correct |
62 |
Correct |
4859 ms |
31560 KB |
Output is correct |
63 |
Correct |
3336 ms |
42928 KB |
Output is correct |
64 |
Execution timed out |
5091 ms |
45788 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |