#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define re exit(0);
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef vector<ll> vl ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
typedef pair<ll,ll> pll ;
void add ( int &a , int b )
{
a += b ;
if ( a < 0 ) a += mod ;
if ( a >= mod ) a -= mod ;
}
template < typename T > void chkmin (T &a , T b) { if (a>b) a=b ;}
template < typename T > void chkmax (T &a , T b) { if (a<b) a=b ;}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
// freopen ("bai1.out","w",stdout) ;
}
int n , m , nq ;
vi adjList [maxn] ;
int seeing [maxn] ;
pii query [maxn] ;
int par [maxn][20] , dep [maxn] , tin [maxn] , timeDfs ;
void dfs ( int u = 1 )
{
tin [u] = ++ timeDfs ;
for ( auto v : adjList [u] )
{
if ( v == par [u][0] ) continue ;
par [v][0] = u , dep [v] = dep [u] + 1 ;
for ( int i = 1 ; i <= LOG ; i ++ ) par [v][i] = par[par[v][i-1]][i-1] ;
dfs (v) ;
}
}
int lca ( int u , int v )
{
if ( dep [u] < dep [v] ) swap (u,v) ;
int h = dep [u] - dep [v] ;
for ( int i = LOG ; i >= 0 ; i -- ) if ( h >> i & 1 ) u = par [u][i] ;
if ( u == v ) return u ;
for ( int i = LOG ; i >= 0 ; i -- ) if ( par [u][i] != par [v][i] ) u = par [u][i] , v = par [v][i] ;
return par [u][0] ;
}
int dis ( int u , int v )
{
return dep [u] + dep [v] - 2*dep[lca(u,v)] ;
}
bool cmp ( int u , int v )
{
return tin [u] < tin [v] ;
}
void sub1 ()
{
for ( int run = 1 ; run <= nq ; run ++ )
{
int l = query [run].fi , r = query [run].se ;
vi v ;
for ( int i = l ; i <= r ; i ++ ) v . pb (seeing[i]) ;
sort (v.begin(),v.end(),cmp) ;
int last = 0 ;
int res = 0 ;
for ( auto x : v )
{
if ( last ) res += dis (last,x) ;
last = x ;
}
res += dis (last,v[0]) ;
cout << res / 2 + 1 << "\n" ;
}
}
bool is_line = true ;
int L [maxn][20][2] ;
int get_max ( int l , int r )
{
int x = __lg(r-l+1) ;
return max (L[l][x][1],L[r-(1<<x)+1][x][1]) ;
}
int get_min ( int l , int r )
{
int x = __lg(r-l+1) ;
return min (L[l][x][0],L[r-(1<<x)+1][x][0]) ;
}
void sub2 ()
{
for ( int i = 1 ; i <= m ; i ++ ) L [i][0][0] = L [i][0][1] = seeing [i] ;
for ( int j = 1 ; j <= __lg (m) ; j ++ )
{
for ( int i = 1 ; i <= m-(1<<j)+1 ; i ++ )
{
L [i][j][0] = min ( L[i][j-1][0],L[i+(1<<(j-1))][j-1][0]) ;
L [i][j][1] = max ( L[i][j-1][1],L[i+(1<<(j-1))][j-1][1]) ;
}
}
for ( int run = 1 ; run <= nq ; run ++ )
{
int l = query [run].fi , r = query [run].se ;
cout << get_max (l,r) - get_min (l,r) + 1 << "\n" ;
}
}
int main ()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
// rf () ;
cin >> n >> m >> nq ;
for ( int i = 1 ; i < n ; i ++ )
{
int u , v ; cin >> u >> v ;
if ( u != i || v != i + 1 ) is_line = false ;
adjList [u] . pb (v) ;
adjList [v] . pb (u) ;
}
for ( int i = 1 ; i <= m ; i ++ ) cin >> seeing [i] ;
for ( int i = 1 ; i <= nq ; i ++ ) cin >> query [i].fi >> query [i].se ;
dfs () ;
if ( is_line ) sub2 () ;
else sub1 () ;
}
Compilation message
tourism.cpp: In function 'void rf()':
tourism.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8792 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
9 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8724 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8728 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
4 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8536 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8792 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
9 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8724 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8728 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
4 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8536 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
64 ms |
8876 KB |
Output is correct |
31 |
Correct |
96 ms |
9040 KB |
Output is correct |
32 |
Correct |
137 ms |
10944 KB |
Output is correct |
33 |
Correct |
133 ms |
10952 KB |
Output is correct |
34 |
Correct |
132 ms |
10948 KB |
Output is correct |
35 |
Correct |
382 ms |
10844 KB |
Output is correct |
36 |
Correct |
376 ms |
10952 KB |
Output is correct |
37 |
Correct |
382 ms |
11092 KB |
Output is correct |
38 |
Correct |
115 ms |
10844 KB |
Output is correct |
39 |
Correct |
112 ms |
11032 KB |
Output is correct |
40 |
Correct |
117 ms |
11088 KB |
Output is correct |
41 |
Correct |
339 ms |
10840 KB |
Output is correct |
42 |
Correct |
344 ms |
10840 KB |
Output is correct |
43 |
Correct |
342 ms |
10840 KB |
Output is correct |
44 |
Correct |
113 ms |
10988 KB |
Output is correct |
45 |
Correct |
122 ms |
11100 KB |
Output is correct |
46 |
Correct |
117 ms |
11000 KB |
Output is correct |
47 |
Correct |
343 ms |
10972 KB |
Output is correct |
48 |
Correct |
348 ms |
10840 KB |
Output is correct |
49 |
Correct |
347 ms |
10840 KB |
Output is correct |
50 |
Correct |
127 ms |
10960 KB |
Output is correct |
51 |
Correct |
128 ms |
10844 KB |
Output is correct |
52 |
Correct |
128 ms |
10844 KB |
Output is correct |
53 |
Correct |
124 ms |
10844 KB |
Output is correct |
54 |
Correct |
130 ms |
11088 KB |
Output is correct |
55 |
Correct |
126 ms |
10840 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
3 ms |
10844 KB |
Output is correct |
58 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8544 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
41 ms |
35484 KB |
Output is correct |
5 |
Correct |
42 ms |
35884 KB |
Output is correct |
6 |
Correct |
49 ms |
41064 KB |
Output is correct |
7 |
Correct |
62 ms |
44300 KB |
Output is correct |
8 |
Correct |
59 ms |
44264 KB |
Output is correct |
9 |
Correct |
64 ms |
44228 KB |
Output is correct |
10 |
Correct |
63 ms |
44528 KB |
Output is correct |
11 |
Correct |
71 ms |
44112 KB |
Output is correct |
12 |
Correct |
58 ms |
44288 KB |
Output is correct |
13 |
Correct |
58 ms |
44164 KB |
Output is correct |
14 |
Correct |
58 ms |
44116 KB |
Output is correct |
15 |
Correct |
43 ms |
27280 KB |
Output is correct |
16 |
Correct |
60 ms |
43752 KB |
Output is correct |
17 |
Correct |
30 ms |
25424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
31 ms |
17152 KB |
Output is correct |
3 |
Correct |
37 ms |
17244 KB |
Output is correct |
4 |
Correct |
35 ms |
17500 KB |
Output is correct |
5 |
Correct |
61 ms |
21456 KB |
Output is correct |
6 |
Correct |
72 ms |
21420 KB |
Output is correct |
7 |
Correct |
72 ms |
21080 KB |
Output is correct |
8 |
Correct |
66 ms |
20816 KB |
Output is correct |
9 |
Correct |
57 ms |
21072 KB |
Output is correct |
10 |
Correct |
58 ms |
20860 KB |
Output is correct |
11 |
Correct |
62 ms |
20816 KB |
Output is correct |
12 |
Correct |
58 ms |
21028 KB |
Output is correct |
13 |
Correct |
59 ms |
20820 KB |
Output is correct |
14 |
Correct |
65 ms |
21096 KB |
Output is correct |
15 |
Correct |
64 ms |
21076 KB |
Output is correct |
16 |
Correct |
58 ms |
21328 KB |
Output is correct |
17 |
Correct |
59 ms |
21328 KB |
Output is correct |
18 |
Correct |
60 ms |
21304 KB |
Output is correct |
19 |
Correct |
58 ms |
21456 KB |
Output is correct |
20 |
Correct |
61 ms |
21332 KB |
Output is correct |
21 |
Correct |
62 ms |
21176 KB |
Output is correct |
22 |
Correct |
56 ms |
21072 KB |
Output is correct |
23 |
Correct |
64 ms |
21040 KB |
Output is correct |
24 |
Correct |
57 ms |
20824 KB |
Output is correct |
25 |
Correct |
59 ms |
21024 KB |
Output is correct |
26 |
Correct |
66 ms |
20820 KB |
Output is correct |
27 |
Correct |
64 ms |
21004 KB |
Output is correct |
28 |
Correct |
76 ms |
20928 KB |
Output is correct |
29 |
Correct |
64 ms |
20816 KB |
Output is correct |
30 |
Correct |
67 ms |
21080 KB |
Output is correct |
31 |
Correct |
65 ms |
21072 KB |
Output is correct |
32 |
Correct |
75 ms |
21076 KB |
Output is correct |
33 |
Correct |
78 ms |
21112 KB |
Output is correct |
34 |
Correct |
63 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Execution timed out |
5085 ms |
18096 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8792 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
9 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8724 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8728 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
4 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8536 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
64 ms |
8876 KB |
Output is correct |
31 |
Correct |
96 ms |
9040 KB |
Output is correct |
32 |
Correct |
137 ms |
10944 KB |
Output is correct |
33 |
Correct |
133 ms |
10952 KB |
Output is correct |
34 |
Correct |
132 ms |
10948 KB |
Output is correct |
35 |
Correct |
382 ms |
10844 KB |
Output is correct |
36 |
Correct |
376 ms |
10952 KB |
Output is correct |
37 |
Correct |
382 ms |
11092 KB |
Output is correct |
38 |
Correct |
115 ms |
10844 KB |
Output is correct |
39 |
Correct |
112 ms |
11032 KB |
Output is correct |
40 |
Correct |
117 ms |
11088 KB |
Output is correct |
41 |
Correct |
339 ms |
10840 KB |
Output is correct |
42 |
Correct |
344 ms |
10840 KB |
Output is correct |
43 |
Correct |
342 ms |
10840 KB |
Output is correct |
44 |
Correct |
113 ms |
10988 KB |
Output is correct |
45 |
Correct |
122 ms |
11100 KB |
Output is correct |
46 |
Correct |
117 ms |
11000 KB |
Output is correct |
47 |
Correct |
343 ms |
10972 KB |
Output is correct |
48 |
Correct |
348 ms |
10840 KB |
Output is correct |
49 |
Correct |
347 ms |
10840 KB |
Output is correct |
50 |
Correct |
127 ms |
10960 KB |
Output is correct |
51 |
Correct |
128 ms |
10844 KB |
Output is correct |
52 |
Correct |
128 ms |
10844 KB |
Output is correct |
53 |
Correct |
124 ms |
10844 KB |
Output is correct |
54 |
Correct |
130 ms |
11088 KB |
Output is correct |
55 |
Correct |
126 ms |
10840 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
3 ms |
10844 KB |
Output is correct |
58 |
Correct |
3 ms |
10844 KB |
Output is correct |
59 |
Correct |
2 ms |
8544 KB |
Output is correct |
60 |
Correct |
2 ms |
8540 KB |
Output is correct |
61 |
Correct |
2 ms |
10844 KB |
Output is correct |
62 |
Correct |
41 ms |
35484 KB |
Output is correct |
63 |
Correct |
42 ms |
35884 KB |
Output is correct |
64 |
Correct |
49 ms |
41064 KB |
Output is correct |
65 |
Correct |
62 ms |
44300 KB |
Output is correct |
66 |
Correct |
59 ms |
44264 KB |
Output is correct |
67 |
Correct |
64 ms |
44228 KB |
Output is correct |
68 |
Correct |
63 ms |
44528 KB |
Output is correct |
69 |
Correct |
71 ms |
44112 KB |
Output is correct |
70 |
Correct |
58 ms |
44288 KB |
Output is correct |
71 |
Correct |
58 ms |
44164 KB |
Output is correct |
72 |
Correct |
58 ms |
44116 KB |
Output is correct |
73 |
Correct |
43 ms |
27280 KB |
Output is correct |
74 |
Correct |
60 ms |
43752 KB |
Output is correct |
75 |
Correct |
30 ms |
25424 KB |
Output is correct |
76 |
Correct |
2 ms |
8540 KB |
Output is correct |
77 |
Correct |
31 ms |
17152 KB |
Output is correct |
78 |
Correct |
37 ms |
17244 KB |
Output is correct |
79 |
Correct |
35 ms |
17500 KB |
Output is correct |
80 |
Correct |
61 ms |
21456 KB |
Output is correct |
81 |
Correct |
72 ms |
21420 KB |
Output is correct |
82 |
Correct |
72 ms |
21080 KB |
Output is correct |
83 |
Correct |
66 ms |
20816 KB |
Output is correct |
84 |
Correct |
57 ms |
21072 KB |
Output is correct |
85 |
Correct |
58 ms |
20860 KB |
Output is correct |
86 |
Correct |
62 ms |
20816 KB |
Output is correct |
87 |
Correct |
58 ms |
21028 KB |
Output is correct |
88 |
Correct |
59 ms |
20820 KB |
Output is correct |
89 |
Correct |
65 ms |
21096 KB |
Output is correct |
90 |
Correct |
64 ms |
21076 KB |
Output is correct |
91 |
Correct |
58 ms |
21328 KB |
Output is correct |
92 |
Correct |
59 ms |
21328 KB |
Output is correct |
93 |
Correct |
60 ms |
21304 KB |
Output is correct |
94 |
Correct |
58 ms |
21456 KB |
Output is correct |
95 |
Correct |
61 ms |
21332 KB |
Output is correct |
96 |
Correct |
62 ms |
21176 KB |
Output is correct |
97 |
Correct |
56 ms |
21072 KB |
Output is correct |
98 |
Correct |
64 ms |
21040 KB |
Output is correct |
99 |
Correct |
57 ms |
20824 KB |
Output is correct |
100 |
Correct |
59 ms |
21024 KB |
Output is correct |
101 |
Correct |
66 ms |
20820 KB |
Output is correct |
102 |
Correct |
64 ms |
21004 KB |
Output is correct |
103 |
Correct |
76 ms |
20928 KB |
Output is correct |
104 |
Correct |
64 ms |
20816 KB |
Output is correct |
105 |
Correct |
67 ms |
21080 KB |
Output is correct |
106 |
Correct |
65 ms |
21072 KB |
Output is correct |
107 |
Correct |
75 ms |
21076 KB |
Output is correct |
108 |
Correct |
78 ms |
21112 KB |
Output is correct |
109 |
Correct |
63 ms |
21072 KB |
Output is correct |
110 |
Correct |
1 ms |
8540 KB |
Output is correct |
111 |
Correct |
1 ms |
8540 KB |
Output is correct |
112 |
Correct |
3 ms |
10844 KB |
Output is correct |
113 |
Execution timed out |
5085 ms |
18096 KB |
Time limit exceeded |
114 |
Halted |
0 ms |
0 KB |
- |