#include <iostream>
#include <algorithm>
#include <vector>
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;
}
};
Query que[MAXN];
vector <int> v[MAXN];
vector <int> arr, isl;
int c[MAXN];
int fe[MAXN], le[MAXN], in[MAXN], dep[MAXN], ind[MAXN];
int sp[MAXN][20], stepen[MAXN];
int n, m, q, cnt;
bool cmpi(int a, int b) {
return in[a]<in[b];
}
void dfs(int x, int p) {
in[x]=cnt;
cnt++;
fe[x]=arr.size();
for (auto i:v[x]) {
if (i==p) continue;
ind[arr.size()]=x;
arr.push_back(x);
dep[i]=dep[x]+1;
dfs(i,x);
}
le[x]=arr.size();
ind[arr.size()]=x;
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])];
}
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=1;i<=m;i++) cin >> c[i];
int ans;
for (int i=0;i<q;i++) {
cin >> que[i].l >> que[i].r;
if (!isl.empty()) isl.clear();
for (int j=que[i].l;j<=que[i].r;j++) isl.push_back(c[j]);
sort(isl.begin(),isl.end(),cmpi);
ans=0;
for (int j=1;j<isl.size();j++) ans+=getdist(isl[j-1],isl[j]);
if (isl.size()>1) ans+=getdist(isl[0],isl.back());
cout << ans/2+1 << endl;
que[i].ind=i;
}
return 0;
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int j=1;j<isl.size();j++) ans+=getdist(isl[j-1],isl[j]);
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9088 KB |
Output is correct |
6 |
Correct |
2 ms |
9108 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9092 KB |
Output is correct |
11 |
Correct |
4 ms |
9052 KB |
Output is correct |
12 |
Correct |
8 ms |
9128 KB |
Output is correct |
13 |
Correct |
7 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
9124 KB |
Output is correct |
15 |
Correct |
3 ms |
9048 KB |
Output is correct |
16 |
Correct |
3 ms |
9048 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9092 KB |
Output is correct |
19 |
Correct |
3 ms |
9304 KB |
Output is correct |
20 |
Correct |
4 ms |
9128 KB |
Output is correct |
21 |
Correct |
4 ms |
9048 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
3 ms |
9052 KB |
Output is correct |
26 |
Correct |
3 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9088 KB |
Output is correct |
6 |
Correct |
2 ms |
9108 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9092 KB |
Output is correct |
11 |
Correct |
4 ms |
9052 KB |
Output is correct |
12 |
Correct |
8 ms |
9128 KB |
Output is correct |
13 |
Correct |
7 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
9124 KB |
Output is correct |
15 |
Correct |
3 ms |
9048 KB |
Output is correct |
16 |
Correct |
3 ms |
9048 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9092 KB |
Output is correct |
19 |
Correct |
3 ms |
9304 KB |
Output is correct |
20 |
Correct |
4 ms |
9128 KB |
Output is correct |
21 |
Correct |
4 ms |
9048 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
3 ms |
9052 KB |
Output is correct |
26 |
Correct |
3 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
30 |
Correct |
42 ms |
9048 KB |
Output is correct |
31 |
Correct |
61 ms |
9216 KB |
Output is correct |
32 |
Correct |
100 ms |
9052 KB |
Output is correct |
33 |
Correct |
89 ms |
9252 KB |
Output is correct |
34 |
Correct |
88 ms |
9264 KB |
Output is correct |
35 |
Correct |
280 ms |
9048 KB |
Output is correct |
36 |
Correct |
282 ms |
9568 KB |
Output is correct |
37 |
Correct |
278 ms |
9236 KB |
Output is correct |
38 |
Correct |
90 ms |
9308 KB |
Output is correct |
39 |
Correct |
89 ms |
9304 KB |
Output is correct |
40 |
Correct |
86 ms |
9304 KB |
Output is correct |
41 |
Correct |
278 ms |
9304 KB |
Output is correct |
42 |
Correct |
281 ms |
9556 KB |
Output is correct |
43 |
Correct |
283 ms |
9340 KB |
Output is correct |
44 |
Correct |
86 ms |
9304 KB |
Output is correct |
45 |
Correct |
99 ms |
9296 KB |
Output is correct |
46 |
Correct |
97 ms |
9308 KB |
Output is correct |
47 |
Correct |
283 ms |
9292 KB |
Output is correct |
48 |
Correct |
277 ms |
9292 KB |
Output is correct |
49 |
Correct |
283 ms |
9296 KB |
Output is correct |
50 |
Correct |
87 ms |
9256 KB |
Output is correct |
51 |
Correct |
91 ms |
9052 KB |
Output is correct |
52 |
Correct |
95 ms |
9308 KB |
Output is correct |
53 |
Correct |
100 ms |
9052 KB |
Output is correct |
54 |
Correct |
90 ms |
9256 KB |
Output is correct |
55 |
Correct |
87 ms |
9048 KB |
Output is correct |
56 |
Correct |
35 ms |
9564 KB |
Output is correct |
57 |
Correct |
3 ms |
9052 KB |
Output is correct |
58 |
Correct |
3 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9056 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
28 ms |
9104 KB |
Output is correct |
4 |
Runtime error |
43 ms |
41436 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
Output is correct |
2 |
Incorrect |
25 ms |
16556 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
29 ms |
9052 KB |
Output is correct |
4 |
Runtime error |
39 ms |
33236 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9088 KB |
Output is correct |
6 |
Correct |
2 ms |
9108 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9092 KB |
Output is correct |
11 |
Correct |
4 ms |
9052 KB |
Output is correct |
12 |
Correct |
8 ms |
9128 KB |
Output is correct |
13 |
Correct |
7 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
9124 KB |
Output is correct |
15 |
Correct |
3 ms |
9048 KB |
Output is correct |
16 |
Correct |
3 ms |
9048 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9092 KB |
Output is correct |
19 |
Correct |
3 ms |
9304 KB |
Output is correct |
20 |
Correct |
4 ms |
9128 KB |
Output is correct |
21 |
Correct |
4 ms |
9048 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
3 ms |
9052 KB |
Output is correct |
26 |
Correct |
3 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
2 ms |
9052 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
30 |
Correct |
42 ms |
9048 KB |
Output is correct |
31 |
Correct |
61 ms |
9216 KB |
Output is correct |
32 |
Correct |
100 ms |
9052 KB |
Output is correct |
33 |
Correct |
89 ms |
9252 KB |
Output is correct |
34 |
Correct |
88 ms |
9264 KB |
Output is correct |
35 |
Correct |
280 ms |
9048 KB |
Output is correct |
36 |
Correct |
282 ms |
9568 KB |
Output is correct |
37 |
Correct |
278 ms |
9236 KB |
Output is correct |
38 |
Correct |
90 ms |
9308 KB |
Output is correct |
39 |
Correct |
89 ms |
9304 KB |
Output is correct |
40 |
Correct |
86 ms |
9304 KB |
Output is correct |
41 |
Correct |
278 ms |
9304 KB |
Output is correct |
42 |
Correct |
281 ms |
9556 KB |
Output is correct |
43 |
Correct |
283 ms |
9340 KB |
Output is correct |
44 |
Correct |
86 ms |
9304 KB |
Output is correct |
45 |
Correct |
99 ms |
9296 KB |
Output is correct |
46 |
Correct |
97 ms |
9308 KB |
Output is correct |
47 |
Correct |
283 ms |
9292 KB |
Output is correct |
48 |
Correct |
277 ms |
9292 KB |
Output is correct |
49 |
Correct |
283 ms |
9296 KB |
Output is correct |
50 |
Correct |
87 ms |
9256 KB |
Output is correct |
51 |
Correct |
91 ms |
9052 KB |
Output is correct |
52 |
Correct |
95 ms |
9308 KB |
Output is correct |
53 |
Correct |
100 ms |
9052 KB |
Output is correct |
54 |
Correct |
90 ms |
9256 KB |
Output is correct |
55 |
Correct |
87 ms |
9048 KB |
Output is correct |
56 |
Correct |
35 ms |
9564 KB |
Output is correct |
57 |
Correct |
3 ms |
9052 KB |
Output is correct |
58 |
Correct |
3 ms |
9052 KB |
Output is correct |
59 |
Correct |
2 ms |
9056 KB |
Output is correct |
60 |
Correct |
2 ms |
9052 KB |
Output is correct |
61 |
Correct |
28 ms |
9104 KB |
Output is correct |
62 |
Runtime error |
43 ms |
41436 KB |
Execution killed with signal 11 |
63 |
Halted |
0 ms |
0 KB |
- |