#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M=1e6+10;
const int N=1e3+10;
const int mod=1e9+7;
int n,m,q,c[M];
vector<int> ke[M];
int num[M],tail[M],cnt = 0, up[M][20], d[M];
void dfs(int u,int p)
{
num[u] = ++cnt;
for(int i =1;i <= 18; i++) up[u][i] = up[up[u][i-1]][i-1];
for(int v : ke[u]) {
if(v == p) continue;
up[v][0] = u;
d[v] = d[u] + 1;
dfs(v,u);
}
tail[u] = cnt;
}
bool anc(int u,int v)
{
return num[u] <= num[v] && tail[u] >= tail[v];
}
int lca(int u,int v)
{
if(anc(u,v)) return u;
if(anc(v,u)) return v;
for(int i = 18;i >=0 ; i--) if(!anc(up[u][i],v)) u = up[u][i];
return up[u][0];
}
int dist(int u,int v)
{
int x = lca(u,v);
return d[u] + d[v] - 2 * d[x];
}
bool cmp(const int &a,const int &b)
{
return num[a] < num[b];
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r"))
{
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task "tourism"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> m >> q;
for(int i = 1;i <= n - 1; i++) {
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
up[1][0] = 1;
dfs(1,0);
for(int i = 1;i <= m; i++) cin >> c[i];
while(q--) {
int l,r;
cin >> l >> r;
// cout << l << " " << r << "\n";
vector<int> Q;
for(int i = l;i <= r; i++) Q.pb(c[i]);
sort(Q.begin(),Q.end(),cmp);
int sz = Q.size();
for(int i = 0;i <= sz - 2; i++) {
Q.pb(lca(Q[i],Q[i + 1]));
}
//for(int v : Q) cout << v << " ";
sort(Q.begin(),Q.end(),cmp);
Q.resize(unique(Q.begin(),Q.end()) - Q.begin());
stack<int> sk;
int kq = 0;
for(int v : Q) {
while(!sk.empty() && !anc(sk.top(),v)) sk.pop();
if(!sk.empty()) kq += dist(sk.top(),v);
sk.push(v);
}
cout << kq + 1 << "\n";
}
}
Compilation message
tourism.cpp: In function 'int32_t main()':
tourism.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("1.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen("1.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
tourism.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:68:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
25168 KB |
Output is correct |
2 |
Correct |
15 ms |
25168 KB |
Output is correct |
3 |
Correct |
14 ms |
25168 KB |
Output is correct |
4 |
Correct |
22 ms |
25168 KB |
Output is correct |
5 |
Correct |
16 ms |
25168 KB |
Output is correct |
6 |
Correct |
17 ms |
25300 KB |
Output is correct |
7 |
Correct |
17 ms |
25168 KB |
Output is correct |
8 |
Correct |
16 ms |
25168 KB |
Output is correct |
9 |
Correct |
20 ms |
25168 KB |
Output is correct |
10 |
Correct |
20 ms |
25168 KB |
Output is correct |
11 |
Correct |
20 ms |
25168 KB |
Output is correct |
12 |
Correct |
30 ms |
25168 KB |
Output is correct |
13 |
Correct |
36 ms |
25340 KB |
Output is correct |
14 |
Correct |
29 ms |
25168 KB |
Output is correct |
15 |
Correct |
19 ms |
25168 KB |
Output is correct |
16 |
Correct |
17 ms |
25168 KB |
Output is correct |
17 |
Correct |
24 ms |
25168 KB |
Output is correct |
18 |
Correct |
19 ms |
25168 KB |
Output is correct |
19 |
Correct |
18 ms |
25392 KB |
Output is correct |
20 |
Correct |
18 ms |
25336 KB |
Output is correct |
21 |
Correct |
18 ms |
25168 KB |
Output is correct |
22 |
Correct |
18 ms |
25168 KB |
Output is correct |
23 |
Correct |
19 ms |
25168 KB |
Output is correct |
24 |
Correct |
19 ms |
25404 KB |
Output is correct |
25 |
Correct |
20 ms |
25416 KB |
Output is correct |
26 |
Correct |
18 ms |
25168 KB |
Output is correct |
27 |
Correct |
16 ms |
25168 KB |
Output is correct |
28 |
Correct |
15 ms |
25168 KB |
Output is correct |
29 |
Correct |
15 ms |
25168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
25168 KB |
Output is correct |
2 |
Correct |
15 ms |
25168 KB |
Output is correct |
3 |
Correct |
14 ms |
25168 KB |
Output is correct |
4 |
Correct |
22 ms |
25168 KB |
Output is correct |
5 |
Correct |
16 ms |
25168 KB |
Output is correct |
6 |
Correct |
17 ms |
25300 KB |
Output is correct |
7 |
Correct |
17 ms |
25168 KB |
Output is correct |
8 |
Correct |
16 ms |
25168 KB |
Output is correct |
9 |
Correct |
20 ms |
25168 KB |
Output is correct |
10 |
Correct |
20 ms |
25168 KB |
Output is correct |
11 |
Correct |
20 ms |
25168 KB |
Output is correct |
12 |
Correct |
30 ms |
25168 KB |
Output is correct |
13 |
Correct |
36 ms |
25340 KB |
Output is correct |
14 |
Correct |
29 ms |
25168 KB |
Output is correct |
15 |
Correct |
19 ms |
25168 KB |
Output is correct |
16 |
Correct |
17 ms |
25168 KB |
Output is correct |
17 |
Correct |
24 ms |
25168 KB |
Output is correct |
18 |
Correct |
19 ms |
25168 KB |
Output is correct |
19 |
Correct |
18 ms |
25392 KB |
Output is correct |
20 |
Correct |
18 ms |
25336 KB |
Output is correct |
21 |
Correct |
18 ms |
25168 KB |
Output is correct |
22 |
Correct |
18 ms |
25168 KB |
Output is correct |
23 |
Correct |
19 ms |
25168 KB |
Output is correct |
24 |
Correct |
19 ms |
25404 KB |
Output is correct |
25 |
Correct |
20 ms |
25416 KB |
Output is correct |
26 |
Correct |
18 ms |
25168 KB |
Output is correct |
27 |
Correct |
16 ms |
25168 KB |
Output is correct |
28 |
Correct |
15 ms |
25168 KB |
Output is correct |
29 |
Correct |
15 ms |
25168 KB |
Output is correct |
30 |
Correct |
172 ms |
25740 KB |
Output is correct |
31 |
Correct |
246 ms |
25696 KB |
Output is correct |
32 |
Correct |
343 ms |
25680 KB |
Output is correct |
33 |
Correct |
349 ms |
25680 KB |
Output is correct |
34 |
Correct |
341 ms |
25856 KB |
Output is correct |
35 |
Correct |
1022 ms |
25824 KB |
Output is correct |
36 |
Correct |
970 ms |
25880 KB |
Output is correct |
37 |
Correct |
1004 ms |
25872 KB |
Output is correct |
38 |
Correct |
298 ms |
25936 KB |
Output is correct |
39 |
Correct |
284 ms |
25920 KB |
Output is correct |
40 |
Correct |
278 ms |
25936 KB |
Output is correct |
41 |
Correct |
975 ms |
25948 KB |
Output is correct |
42 |
Correct |
926 ms |
25948 KB |
Output is correct |
43 |
Correct |
1000 ms |
25892 KB |
Output is correct |
44 |
Correct |
280 ms |
25680 KB |
Output is correct |
45 |
Correct |
295 ms |
25680 KB |
Output is correct |
46 |
Correct |
292 ms |
25912 KB |
Output is correct |
47 |
Correct |
971 ms |
25896 KB |
Output is correct |
48 |
Correct |
918 ms |
25680 KB |
Output is correct |
49 |
Correct |
919 ms |
25856 KB |
Output is correct |
50 |
Correct |
278 ms |
25680 KB |
Output is correct |
51 |
Correct |
300 ms |
25840 KB |
Output is correct |
52 |
Correct |
287 ms |
25936 KB |
Output is correct |
53 |
Correct |
296 ms |
25880 KB |
Output is correct |
54 |
Correct |
278 ms |
25680 KB |
Output is correct |
55 |
Correct |
267 ms |
25844 KB |
Output is correct |
56 |
Correct |
82 ms |
25168 KB |
Output is correct |
57 |
Correct |
17 ms |
25936 KB |
Output is correct |
58 |
Correct |
15 ms |
25696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25168 KB |
Output is correct |
2 |
Correct |
14 ms |
25304 KB |
Output is correct |
3 |
Correct |
84 ms |
25424 KB |
Output is correct |
4 |
Execution timed out |
5077 ms |
45568 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
25168 KB |
Output is correct |
2 |
Correct |
56 ms |
38216 KB |
Output is correct |
3 |
Correct |
67 ms |
40016 KB |
Output is correct |
4 |
Correct |
63 ms |
40528 KB |
Output is correct |
5 |
Correct |
112 ms |
49852 KB |
Output is correct |
6 |
Correct |
118 ms |
49336 KB |
Output is correct |
7 |
Correct |
110 ms |
48244 KB |
Output is correct |
8 |
Correct |
122 ms |
47944 KB |
Output is correct |
9 |
Correct |
113 ms |
48808 KB |
Output is correct |
10 |
Correct |
101 ms |
49480 KB |
Output is correct |
11 |
Correct |
109 ms |
49480 KB |
Output is correct |
12 |
Correct |
102 ms |
49232 KB |
Output is correct |
13 |
Correct |
98 ms |
49492 KB |
Output is correct |
14 |
Correct |
106 ms |
49836 KB |
Output is correct |
15 |
Correct |
97 ms |
50808 KB |
Output is correct |
16 |
Correct |
112 ms |
50804 KB |
Output is correct |
17 |
Correct |
107 ms |
48832 KB |
Output is correct |
18 |
Correct |
107 ms |
50112 KB |
Output is correct |
19 |
Correct |
121 ms |
51088 KB |
Output is correct |
20 |
Correct |
109 ms |
50872 KB |
Output is correct |
21 |
Correct |
115 ms |
49688 KB |
Output is correct |
22 |
Correct |
104 ms |
49484 KB |
Output is correct |
23 |
Correct |
117 ms |
49288 KB |
Output is correct |
24 |
Correct |
119 ms |
47432 KB |
Output is correct |
25 |
Correct |
121 ms |
48252 KB |
Output is correct |
26 |
Correct |
100 ms |
47176 KB |
Output is correct |
27 |
Correct |
102 ms |
47176 KB |
Output is correct |
28 |
Correct |
99 ms |
48712 KB |
Output is correct |
29 |
Correct |
99 ms |
47176 KB |
Output is correct |
30 |
Correct |
100 ms |
48968 KB |
Output is correct |
31 |
Correct |
87 ms |
47176 KB |
Output is correct |
32 |
Correct |
98 ms |
49224 KB |
Output is correct |
33 |
Correct |
99 ms |
49488 KB |
Output is correct |
34 |
Correct |
95 ms |
50388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
25168 KB |
Output is correct |
2 |
Correct |
17 ms |
25168 KB |
Output is correct |
3 |
Correct |
84 ms |
25408 KB |
Output is correct |
4 |
Execution timed out |
5062 ms |
43048 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
25168 KB |
Output is correct |
2 |
Correct |
15 ms |
25168 KB |
Output is correct |
3 |
Correct |
14 ms |
25168 KB |
Output is correct |
4 |
Correct |
22 ms |
25168 KB |
Output is correct |
5 |
Correct |
16 ms |
25168 KB |
Output is correct |
6 |
Correct |
17 ms |
25300 KB |
Output is correct |
7 |
Correct |
17 ms |
25168 KB |
Output is correct |
8 |
Correct |
16 ms |
25168 KB |
Output is correct |
9 |
Correct |
20 ms |
25168 KB |
Output is correct |
10 |
Correct |
20 ms |
25168 KB |
Output is correct |
11 |
Correct |
20 ms |
25168 KB |
Output is correct |
12 |
Correct |
30 ms |
25168 KB |
Output is correct |
13 |
Correct |
36 ms |
25340 KB |
Output is correct |
14 |
Correct |
29 ms |
25168 KB |
Output is correct |
15 |
Correct |
19 ms |
25168 KB |
Output is correct |
16 |
Correct |
17 ms |
25168 KB |
Output is correct |
17 |
Correct |
24 ms |
25168 KB |
Output is correct |
18 |
Correct |
19 ms |
25168 KB |
Output is correct |
19 |
Correct |
18 ms |
25392 KB |
Output is correct |
20 |
Correct |
18 ms |
25336 KB |
Output is correct |
21 |
Correct |
18 ms |
25168 KB |
Output is correct |
22 |
Correct |
18 ms |
25168 KB |
Output is correct |
23 |
Correct |
19 ms |
25168 KB |
Output is correct |
24 |
Correct |
19 ms |
25404 KB |
Output is correct |
25 |
Correct |
20 ms |
25416 KB |
Output is correct |
26 |
Correct |
18 ms |
25168 KB |
Output is correct |
27 |
Correct |
16 ms |
25168 KB |
Output is correct |
28 |
Correct |
15 ms |
25168 KB |
Output is correct |
29 |
Correct |
15 ms |
25168 KB |
Output is correct |
30 |
Correct |
172 ms |
25740 KB |
Output is correct |
31 |
Correct |
246 ms |
25696 KB |
Output is correct |
32 |
Correct |
343 ms |
25680 KB |
Output is correct |
33 |
Correct |
349 ms |
25680 KB |
Output is correct |
34 |
Correct |
341 ms |
25856 KB |
Output is correct |
35 |
Correct |
1022 ms |
25824 KB |
Output is correct |
36 |
Correct |
970 ms |
25880 KB |
Output is correct |
37 |
Correct |
1004 ms |
25872 KB |
Output is correct |
38 |
Correct |
298 ms |
25936 KB |
Output is correct |
39 |
Correct |
284 ms |
25920 KB |
Output is correct |
40 |
Correct |
278 ms |
25936 KB |
Output is correct |
41 |
Correct |
975 ms |
25948 KB |
Output is correct |
42 |
Correct |
926 ms |
25948 KB |
Output is correct |
43 |
Correct |
1000 ms |
25892 KB |
Output is correct |
44 |
Correct |
280 ms |
25680 KB |
Output is correct |
45 |
Correct |
295 ms |
25680 KB |
Output is correct |
46 |
Correct |
292 ms |
25912 KB |
Output is correct |
47 |
Correct |
971 ms |
25896 KB |
Output is correct |
48 |
Correct |
918 ms |
25680 KB |
Output is correct |
49 |
Correct |
919 ms |
25856 KB |
Output is correct |
50 |
Correct |
278 ms |
25680 KB |
Output is correct |
51 |
Correct |
300 ms |
25840 KB |
Output is correct |
52 |
Correct |
287 ms |
25936 KB |
Output is correct |
53 |
Correct |
296 ms |
25880 KB |
Output is correct |
54 |
Correct |
278 ms |
25680 KB |
Output is correct |
55 |
Correct |
267 ms |
25844 KB |
Output is correct |
56 |
Correct |
82 ms |
25168 KB |
Output is correct |
57 |
Correct |
17 ms |
25936 KB |
Output is correct |
58 |
Correct |
15 ms |
25696 KB |
Output is correct |
59 |
Correct |
15 ms |
25168 KB |
Output is correct |
60 |
Correct |
14 ms |
25304 KB |
Output is correct |
61 |
Correct |
84 ms |
25424 KB |
Output is correct |
62 |
Execution timed out |
5077 ms |
45568 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |