#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define endl '\n'
const int MAXN=1e5+10;
const int BUCK=450;
struct Query {
int l, r;
int ind;
bool friend operator < (Query a, Query b) {
return a.r>b.r;
}
};
struct Sight {
int x;
int ind;
bool friend operator < (Sight a, Sight b) {
return a.x<b.x;
}
};
Sight c[MAXN];
Query que[MAXN];
vector <int> v[MAXN];
vector <int> arr, isl;
int fe[MAXN], le[MAXN], in[MAXN], dep[MAXN];
int sp[2*MAXN][20], stepen[2*MAXN];
stack <pair <int*,int> > st;
int pos[MAXN], lef[MAXN], ri[MAXN], res;
vector <int> sq[MAXN/BUCK];
int ans[MAXN];
int n, m, q, cnt;
bool cmpi(int a, int b) {
return in[a]<in[b];
}
bool cmpc(Sight a, Sight b) {
return in[a.x]<in[b.x];
}
void dfs(int x, int p) {
in[x]=cnt;
cnt++;
fe[x]=arr.size();
for (auto i:v[x]) {
if (i==p) continue;
arr.push_back(x);
dep[i]=dep[x]+1;
dfs(i,x);
}
le[x]=arr.size();
arr.push_back(x);
}
void sparce_table() {
int kn=arr.size();
for (int i=0;i<kn;i++) sp[i][0]=arr[i];
for (int i=1;(1<<i)<=kn;i++) {
for (int j=0;j+(1<<i)-1<kn;j++) {
int i1, i2;
i1=sp[j][i-1];
i2=sp[j + (1 << (i-1))][i-1];
if (dep[i1]<dep[i2]) sp[j][i]=i1;
else sp[j][i]=i2;
}
}
int st=0;
for (int i=1;i<=kn;i++) {
if ((1 << (st+1))<i) st++;
stepen[i]=st;
}
}
int lca(int l, int r) {
int st=stepen[r-l+1];
int i1, i2;
i1=sp[l][st];
i2=sp[r - (1<<st)+1][st];
if (dep[i1]<dep[i2]) return i1;
return i2;
}
int getdist(int a, int b) {
if (in[a]>in[b]) swap(a,b);
return dep[a]+dep[b]-2*dep[lca(fe[a],fe[b])];
}
void undo() {
for (int times=0;times<3;times++) {
(*st.top().first)=st.top().second;
st.pop();
}
}
void remove(int i) {
i=pos[i];
st.push({&res,res});
st.push({ri+lef[i],ri[lef[i]]});
st.push({lef+ri[i],lef[ri[i]]});
res-=getdist(c[lef[i]].x,c[i].x);
res-=getdist(c[i].x,c[ri[i]].x);
res+=getdist(c[lef[i]].x,c[ri[i]].x);
ri[lef[i]]=ri[i];
lef[ri[i]]=lef[i];
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
cin >> n >> m >> q;
for (int i=0;i<n-1;i++) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cnt=1;
dfs(1,-1);
sparce_table();
for (int i=0;i<m;i++) {
cin >> c[i].x;
c[i].ind=i;
}
sort(c,c+m,cmpc);
for (int i=0;i<m;i++) {
pos[c[i].ind]=i;
lef[i]=i-1;
ri[i]=i+1;
res+=getdist(c[i].x,c[(i+1)%m].x);
}
lef[0]=m-1;
ri[m-1]=0;
for (int i=0;i<q;i++) {
cin >> que[i].l >> que[i].r;
que[i].l--; que[i].r--;
que[i].ind=i;
}
sort(que,que+q);
for (int i=0;i<q;i++) {
if (que[i].l==que[i].r) ans[que[i].ind]=1;
else sq[que[i].l/BUCK].push_back(i);
}
int curl, curr;
for (int i=0;i*BUCK<m;i++) {
curr=m-1;
for (auto j:sq[i]) {
while (curr>que[j].r) {
remove(curr);
curr--;
}
curl=i*BUCK;
while (curl<que[j].l) {
remove(curl);
curl++;
}
ans[que[j].ind]=res/2+1;
while (curl>i*BUCK) {
undo();
curl--;
}
}
while (curr<m-1) {
curr++;
undo();
}
if ((i+1)*BUCK<m) {
for (int j=i*BUCK;j<(i+1)*BUCK;j++) remove(j);
}
}
for (int i=0;i<q;i++) cout << ans[i] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10728 KB |
Output is correct |
3 |
Correct |
2 ms |
10900 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10720 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10724 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
4 ms |
10772 KB |
Output is correct |
17 |
Correct |
3 ms |
10768 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
4 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10716 KB |
Output is correct |
23 |
Correct |
4 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10584 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10728 KB |
Output is correct |
3 |
Correct |
2 ms |
10900 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10720 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10724 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
4 ms |
10772 KB |
Output is correct |
17 |
Correct |
3 ms |
10768 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
4 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10716 KB |
Output is correct |
23 |
Correct |
4 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10584 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
17 ms |
10844 KB |
Output is correct |
31 |
Correct |
25 ms |
10844 KB |
Output is correct |
32 |
Correct |
25 ms |
10840 KB |
Output is correct |
33 |
Correct |
26 ms |
11100 KB |
Output is correct |
34 |
Correct |
26 ms |
10840 KB |
Output is correct |
35 |
Correct |
5 ms |
10844 KB |
Output is correct |
36 |
Correct |
5 ms |
10840 KB |
Output is correct |
37 |
Correct |
5 ms |
10844 KB |
Output is correct |
38 |
Correct |
25 ms |
11076 KB |
Output is correct |
39 |
Correct |
28 ms |
10840 KB |
Output is correct |
40 |
Correct |
27 ms |
10840 KB |
Output is correct |
41 |
Correct |
5 ms |
10844 KB |
Output is correct |
42 |
Correct |
5 ms |
11304 KB |
Output is correct |
43 |
Correct |
5 ms |
10952 KB |
Output is correct |
44 |
Correct |
29 ms |
10844 KB |
Output is correct |
45 |
Correct |
33 ms |
10844 KB |
Output is correct |
46 |
Correct |
25 ms |
10840 KB |
Output is correct |
47 |
Correct |
5 ms |
10844 KB |
Output is correct |
48 |
Correct |
5 ms |
10844 KB |
Output is correct |
49 |
Correct |
5 ms |
10844 KB |
Output is correct |
50 |
Correct |
26 ms |
10844 KB |
Output is correct |
51 |
Correct |
27 ms |
10996 KB |
Output is correct |
52 |
Correct |
25 ms |
10844 KB |
Output is correct |
53 |
Correct |
28 ms |
10976 KB |
Output is correct |
54 |
Correct |
33 ms |
10964 KB |
Output is correct |
55 |
Correct |
25 ms |
10996 KB |
Output is correct |
56 |
Correct |
23 ms |
10844 KB |
Output is correct |
57 |
Correct |
2 ms |
10840 KB |
Output is correct |
58 |
Correct |
3 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
23 ms |
10760 KB |
Output is correct |
4 |
Correct |
2125 ms |
32596 KB |
Output is correct |
5 |
Correct |
1404 ms |
37296 KB |
Output is correct |
6 |
Correct |
1855 ms |
40000 KB |
Output is correct |
7 |
Runtime error |
2947 ms |
83580 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
415 ms |
25208 KB |
Output is correct |
3 |
Correct |
1076 ms |
27852 KB |
Output is correct |
4 |
Correct |
589 ms |
28868 KB |
Output is correct |
5 |
Runtime error |
113 ms |
67776 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10584 KB |
Output is correct |
3 |
Correct |
23 ms |
10864 KB |
Output is correct |
4 |
Correct |
2960 ms |
30020 KB |
Output is correct |
5 |
Correct |
2931 ms |
30148 KB |
Output is correct |
6 |
Correct |
3558 ms |
36224 KB |
Output is correct |
7 |
Runtime error |
4128 ms |
70948 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10728 KB |
Output is correct |
3 |
Correct |
2 ms |
10900 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10720 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10724 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
4 ms |
10772 KB |
Output is correct |
17 |
Correct |
3 ms |
10768 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
4 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10716 KB |
Output is correct |
23 |
Correct |
4 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10584 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
17 ms |
10844 KB |
Output is correct |
31 |
Correct |
25 ms |
10844 KB |
Output is correct |
32 |
Correct |
25 ms |
10840 KB |
Output is correct |
33 |
Correct |
26 ms |
11100 KB |
Output is correct |
34 |
Correct |
26 ms |
10840 KB |
Output is correct |
35 |
Correct |
5 ms |
10844 KB |
Output is correct |
36 |
Correct |
5 ms |
10840 KB |
Output is correct |
37 |
Correct |
5 ms |
10844 KB |
Output is correct |
38 |
Correct |
25 ms |
11076 KB |
Output is correct |
39 |
Correct |
28 ms |
10840 KB |
Output is correct |
40 |
Correct |
27 ms |
10840 KB |
Output is correct |
41 |
Correct |
5 ms |
10844 KB |
Output is correct |
42 |
Correct |
5 ms |
11304 KB |
Output is correct |
43 |
Correct |
5 ms |
10952 KB |
Output is correct |
44 |
Correct |
29 ms |
10844 KB |
Output is correct |
45 |
Correct |
33 ms |
10844 KB |
Output is correct |
46 |
Correct |
25 ms |
10840 KB |
Output is correct |
47 |
Correct |
5 ms |
10844 KB |
Output is correct |
48 |
Correct |
5 ms |
10844 KB |
Output is correct |
49 |
Correct |
5 ms |
10844 KB |
Output is correct |
50 |
Correct |
26 ms |
10844 KB |
Output is correct |
51 |
Correct |
27 ms |
10996 KB |
Output is correct |
52 |
Correct |
25 ms |
10844 KB |
Output is correct |
53 |
Correct |
28 ms |
10976 KB |
Output is correct |
54 |
Correct |
33 ms |
10964 KB |
Output is correct |
55 |
Correct |
25 ms |
10996 KB |
Output is correct |
56 |
Correct |
23 ms |
10844 KB |
Output is correct |
57 |
Correct |
2 ms |
10840 KB |
Output is correct |
58 |
Correct |
3 ms |
10840 KB |
Output is correct |
59 |
Correct |
1 ms |
10584 KB |
Output is correct |
60 |
Correct |
4 ms |
10588 KB |
Output is correct |
61 |
Correct |
23 ms |
10760 KB |
Output is correct |
62 |
Correct |
2125 ms |
32596 KB |
Output is correct |
63 |
Correct |
1404 ms |
37296 KB |
Output is correct |
64 |
Correct |
1855 ms |
40000 KB |
Output is correct |
65 |
Runtime error |
2947 ms |
83580 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |