Submission #889193

# Submission time Handle Problem Language Result Execution time Memory
889193 2023-12-19T07:10:06 Z thunopro Tourism (JOI23_tourism) C++14
59 / 100
5000 ms 48360 KB
#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" ; 
	}
}

bool is_sub3 = true ; 

vii event_query [maxn] ; 
int weight [maxn] ; 
int ans [maxn] ; 

int bit [maxn] ; 

void update ( int x , int y ) 
{
	while ( x < maxn ) bit [x] += y , x += (x&-x) ; 
}
int get ( int x ) 
{
	int res = 0 ; 
	while ( x ) res += bit [x] , x -= (x&-x) ; 
	return res ; 
}

void UPD ( int u , int v , int w ) 
{
	while ( dep [u] > dep [v] ) 
	{
		update (weight[u],-1) ;
		weight [u] = w ;  
		update (weight[u],1) ; 
		u = par [u][0] ; 
	}
	while ( dep [v] > dep [u] ) 
	{
		update (weight[v],-1) ; 
		weight [v] = w ; 
		update (weight[v],1) ; 
		v = par [v][0] ; 
	}
	while ( u != v ) 
	{
		update (weight[u],-1) ; update (weight[v],-1) ; 
		weight [u] = weight [v] = w ;  
		update (weight[u],1) ; update (weight[v],1) ; 
		u = par [u][0] , v = par [v][0] ; 
	}
}
void sub3 () 
{
	for ( int i = 2 ; i <= n ; i ++ ) weight [i] = m + 2 , update (weight[i],1) ; 
	for ( int i = 1 ; i <= nq ; i ++ ) 
	{
		int l = query [i].fi , r = query [i].se ; 
		event_query [l] . pb ({r,i}) ; 
	}
	for ( int i = m ; i >= 1 ; i -- ) 
	{
		if ( i != m ) UPD (seeing[i],seeing[i+1],i+1) ; 
		for ( auto x : event_query [i] ) ans [x.se] = get (x.fi) + 1 ; 
	}
	for ( int i = 1 ; i <= nq ; i ++ ) cout << ans [i] << "\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 ; 
		if ( u != (i+1)/2 || v != i + 1 ) is_sub3 = 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 if ( is_sub3 ) sub3 () ;
	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) ;
      |  ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 5 ms 13708 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 4 ms 13656 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 4 ms 13660 KB Output is correct
9 Correct 5 ms 13660 KB Output is correct
10 Correct 5 ms 13656 KB Output is correct
11 Correct 5 ms 13616 KB Output is correct
12 Correct 10 ms 13720 KB Output is correct
13 Correct 10 ms 13656 KB Output is correct
14 Correct 10 ms 13468 KB Output is correct
15 Correct 5 ms 13656 KB Output is correct
16 Correct 5 ms 13612 KB Output is correct
17 Correct 5 ms 13660 KB Output is correct
18 Correct 5 ms 13660 KB Output is correct
19 Correct 5 ms 13660 KB Output is correct
20 Correct 5 ms 13660 KB Output is correct
21 Correct 5 ms 13660 KB Output is correct
22 Correct 5 ms 13720 KB Output is correct
23 Correct 6 ms 13900 KB Output is correct
24 Correct 5 ms 13612 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13660 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 5 ms 13708 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 4 ms 13656 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 4 ms 13660 KB Output is correct
9 Correct 5 ms 13660 KB Output is correct
10 Correct 5 ms 13656 KB Output is correct
11 Correct 5 ms 13616 KB Output is correct
12 Correct 10 ms 13720 KB Output is correct
13 Correct 10 ms 13656 KB Output is correct
14 Correct 10 ms 13468 KB Output is correct
15 Correct 5 ms 13656 KB Output is correct
16 Correct 5 ms 13612 KB Output is correct
17 Correct 5 ms 13660 KB Output is correct
18 Correct 5 ms 13660 KB Output is correct
19 Correct 5 ms 13660 KB Output is correct
20 Correct 5 ms 13660 KB Output is correct
21 Correct 5 ms 13660 KB Output is correct
22 Correct 5 ms 13720 KB Output is correct
23 Correct 6 ms 13900 KB Output is correct
24 Correct 5 ms 13612 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13660 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 65 ms 13660 KB Output is correct
31 Correct 92 ms 13848 KB Output is correct
32 Correct 138 ms 13920 KB Output is correct
33 Correct 133 ms 13912 KB Output is correct
34 Correct 132 ms 13916 KB Output is correct
35 Correct 382 ms 13916 KB Output is correct
36 Correct 378 ms 13912 KB Output is correct
37 Correct 378 ms 13928 KB Output is correct
38 Correct 115 ms 13912 KB Output is correct
39 Correct 115 ms 14016 KB Output is correct
40 Correct 115 ms 13912 KB Output is correct
41 Correct 345 ms 14028 KB Output is correct
42 Correct 342 ms 13916 KB Output is correct
43 Correct 346 ms 13984 KB Output is correct
44 Correct 112 ms 13912 KB Output is correct
45 Correct 118 ms 13916 KB Output is correct
46 Correct 117 ms 13968 KB Output is correct
47 Correct 344 ms 13944 KB Output is correct
48 Correct 372 ms 13916 KB Output is correct
49 Correct 345 ms 13912 KB Output is correct
50 Correct 129 ms 13920 KB Output is correct
51 Correct 129 ms 13912 KB Output is correct
52 Correct 130 ms 13920 KB Output is correct
53 Correct 126 ms 13912 KB Output is correct
54 Correct 140 ms 13916 KB Output is correct
55 Correct 125 ms 13912 KB Output is correct
56 Correct 3 ms 13656 KB Output is correct
57 Correct 4 ms 13924 KB Output is correct
58 Correct 4 ms 13880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 46 ms 39252 KB Output is correct
5 Correct 50 ms 37716 KB Output is correct
6 Correct 48 ms 42888 KB Output is correct
7 Correct 67 ms 48360 KB Output is correct
8 Correct 63 ms 48096 KB Output is correct
9 Correct 65 ms 48224 KB Output is correct
10 Correct 71 ms 48212 KB Output is correct
11 Correct 62 ms 48208 KB Output is correct
12 Correct 61 ms 48208 KB Output is correct
13 Correct 59 ms 48208 KB Output is correct
14 Correct 60 ms 48208 KB Output is correct
15 Correct 45 ms 33364 KB Output is correct
16 Correct 62 ms 47760 KB Output is correct
17 Correct 32 ms 27996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 34 ms 21272 KB Output is correct
3 Correct 42 ms 21460 KB Output is correct
4 Correct 36 ms 21628 KB Output is correct
5 Correct 64 ms 27816 KB Output is correct
6 Correct 67 ms 27472 KB Output is correct
7 Correct 72 ms 27172 KB Output is correct
8 Correct 62 ms 27192 KB Output is correct
9 Correct 65 ms 26972 KB Output is correct
10 Correct 65 ms 26944 KB Output is correct
11 Correct 73 ms 26964 KB Output is correct
12 Correct 61 ms 26960 KB Output is correct
13 Correct 65 ms 27200 KB Output is correct
14 Correct 65 ms 27180 KB Output is correct
15 Correct 78 ms 27220 KB Output is correct
16 Correct 62 ms 27380 KB Output is correct
17 Correct 63 ms 27472 KB Output is correct
18 Correct 63 ms 27476 KB Output is correct
19 Correct 63 ms 27852 KB Output is correct
20 Correct 56 ms 27484 KB Output is correct
21 Correct 88 ms 27392 KB Output is correct
22 Correct 59 ms 27076 KB Output is correct
23 Correct 65 ms 26988 KB Output is correct
24 Correct 61 ms 26960 KB Output is correct
25 Correct 65 ms 27176 KB Output is correct
26 Correct 64 ms 27216 KB Output is correct
27 Correct 66 ms 27192 KB Output is correct
28 Correct 67 ms 27164 KB Output is correct
29 Correct 68 ms 26996 KB Output is correct
30 Correct 74 ms 27220 KB Output is correct
31 Correct 72 ms 27084 KB Output is correct
32 Correct 70 ms 27228 KB Output is correct
33 Correct 75 ms 27216 KB Output is correct
34 Correct 65 ms 27204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 148 ms 25668 KB Output is correct
5 Correct 120 ms 27988 KB Output is correct
6 Correct 148 ms 31572 KB Output is correct
7 Correct 151 ms 34736 KB Output is correct
8 Correct 146 ms 34644 KB Output is correct
9 Correct 145 ms 34644 KB Output is correct
10 Correct 147 ms 34644 KB Output is correct
11 Correct 173 ms 34640 KB Output is correct
12 Correct 153 ms 34968 KB Output is correct
13 Correct 142 ms 34516 KB Output is correct
14 Correct 31 ms 29520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 5 ms 13708 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 4 ms 13656 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 4 ms 13660 KB Output is correct
9 Correct 5 ms 13660 KB Output is correct
10 Correct 5 ms 13656 KB Output is correct
11 Correct 5 ms 13616 KB Output is correct
12 Correct 10 ms 13720 KB Output is correct
13 Correct 10 ms 13656 KB Output is correct
14 Correct 10 ms 13468 KB Output is correct
15 Correct 5 ms 13656 KB Output is correct
16 Correct 5 ms 13612 KB Output is correct
17 Correct 5 ms 13660 KB Output is correct
18 Correct 5 ms 13660 KB Output is correct
19 Correct 5 ms 13660 KB Output is correct
20 Correct 5 ms 13660 KB Output is correct
21 Correct 5 ms 13660 KB Output is correct
22 Correct 5 ms 13720 KB Output is correct
23 Correct 6 ms 13900 KB Output is correct
24 Correct 5 ms 13612 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13660 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 65 ms 13660 KB Output is correct
31 Correct 92 ms 13848 KB Output is correct
32 Correct 138 ms 13920 KB Output is correct
33 Correct 133 ms 13912 KB Output is correct
34 Correct 132 ms 13916 KB Output is correct
35 Correct 382 ms 13916 KB Output is correct
36 Correct 378 ms 13912 KB Output is correct
37 Correct 378 ms 13928 KB Output is correct
38 Correct 115 ms 13912 KB Output is correct
39 Correct 115 ms 14016 KB Output is correct
40 Correct 115 ms 13912 KB Output is correct
41 Correct 345 ms 14028 KB Output is correct
42 Correct 342 ms 13916 KB Output is correct
43 Correct 346 ms 13984 KB Output is correct
44 Correct 112 ms 13912 KB Output is correct
45 Correct 118 ms 13916 KB Output is correct
46 Correct 117 ms 13968 KB Output is correct
47 Correct 344 ms 13944 KB Output is correct
48 Correct 372 ms 13916 KB Output is correct
49 Correct 345 ms 13912 KB Output is correct
50 Correct 129 ms 13920 KB Output is correct
51 Correct 129 ms 13912 KB Output is correct
52 Correct 130 ms 13920 KB Output is correct
53 Correct 126 ms 13912 KB Output is correct
54 Correct 140 ms 13916 KB Output is correct
55 Correct 125 ms 13912 KB Output is correct
56 Correct 3 ms 13656 KB Output is correct
57 Correct 4 ms 13924 KB Output is correct
58 Correct 4 ms 13880 KB Output is correct
59 Correct 3 ms 13660 KB Output is correct
60 Correct 3 ms 13660 KB Output is correct
61 Correct 3 ms 13660 KB Output is correct
62 Correct 46 ms 39252 KB Output is correct
63 Correct 50 ms 37716 KB Output is correct
64 Correct 48 ms 42888 KB Output is correct
65 Correct 67 ms 48360 KB Output is correct
66 Correct 63 ms 48096 KB Output is correct
67 Correct 65 ms 48224 KB Output is correct
68 Correct 71 ms 48212 KB Output is correct
69 Correct 62 ms 48208 KB Output is correct
70 Correct 61 ms 48208 KB Output is correct
71 Correct 59 ms 48208 KB Output is correct
72 Correct 60 ms 48208 KB Output is correct
73 Correct 45 ms 33364 KB Output is correct
74 Correct 62 ms 47760 KB Output is correct
75 Correct 32 ms 27996 KB Output is correct
76 Correct 3 ms 15708 KB Output is correct
77 Correct 34 ms 21272 KB Output is correct
78 Correct 42 ms 21460 KB Output is correct
79 Correct 36 ms 21628 KB Output is correct
80 Correct 64 ms 27816 KB Output is correct
81 Correct 67 ms 27472 KB Output is correct
82 Correct 72 ms 27172 KB Output is correct
83 Correct 62 ms 27192 KB Output is correct
84 Correct 65 ms 26972 KB Output is correct
85 Correct 65 ms 26944 KB Output is correct
86 Correct 73 ms 26964 KB Output is correct
87 Correct 61 ms 26960 KB Output is correct
88 Correct 65 ms 27200 KB Output is correct
89 Correct 65 ms 27180 KB Output is correct
90 Correct 78 ms 27220 KB Output is correct
91 Correct 62 ms 27380 KB Output is correct
92 Correct 63 ms 27472 KB Output is correct
93 Correct 63 ms 27476 KB Output is correct
94 Correct 63 ms 27852 KB Output is correct
95 Correct 56 ms 27484 KB Output is correct
96 Correct 88 ms 27392 KB Output is correct
97 Correct 59 ms 27076 KB Output is correct
98 Correct 65 ms 26988 KB Output is correct
99 Correct 61 ms 26960 KB Output is correct
100 Correct 65 ms 27176 KB Output is correct
101 Correct 64 ms 27216 KB Output is correct
102 Correct 66 ms 27192 KB Output is correct
103 Correct 67 ms 27164 KB Output is correct
104 Correct 68 ms 26996 KB Output is correct
105 Correct 74 ms 27220 KB Output is correct
106 Correct 72 ms 27084 KB Output is correct
107 Correct 70 ms 27228 KB Output is correct
108 Correct 75 ms 27216 KB Output is correct
109 Correct 65 ms 27204 KB Output is correct
110 Correct 3 ms 15704 KB Output is correct
111 Correct 3 ms 13660 KB Output is correct
112 Correct 3 ms 13660 KB Output is correct
113 Correct 148 ms 25668 KB Output is correct
114 Correct 120 ms 27988 KB Output is correct
115 Correct 148 ms 31572 KB Output is correct
116 Correct 151 ms 34736 KB Output is correct
117 Correct 146 ms 34644 KB Output is correct
118 Correct 145 ms 34644 KB Output is correct
119 Correct 147 ms 34644 KB Output is correct
120 Correct 173 ms 34640 KB Output is correct
121 Correct 153 ms 34968 KB Output is correct
122 Correct 142 ms 34516 KB Output is correct
123 Correct 31 ms 29520 KB Output is correct
124 Execution timed out 5043 ms 30980 KB Time limit exceeded
125 Halted 0 ms 0 KB -