답안 #889628

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889628 2023-12-20T04:07:30 Z vjudge1 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 44540 KB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 998244353;
const int N = 1e5;


pair<int, int> chmax(pair<int, int> A, pair<int, int> B){
	if(A.ff > B.ff) return A;
	return B;
}

pair<int, int> chmin(pair<int, int> A, pair<int, int> B){
	if(A.ff > B.ff) return B;
	return A;
}

struct sparse{
	vector<vector<pair<int, int> > > m_table;
	vector<vector<pair<int, int> > >  ma_table;
	int n;
	
	sparse(int sz){
		n = sz;
		m_table.resize(18);
		ma_table.resize(18);
		for(auto &i : m_table){
			i.resize(sz);
		} 
		for(auto &i : ma_table) i.resize(sz);
	};
	
	void build(vector<pair<int, int>> &a){
		for(int lg = 0; lg < 18; lg++){
			for(int i = 0;i < n; i++){
				if(lg == 0){
					m_table[lg][i] = a[i];
					ma_table[lg][i] = a[i];
				}else{
					int k = 1<<(lg-1);
					m_table[lg][i] = chmin(m_table[lg-1][i], m_table[lg-1][min(n - 1, i +  k)]);
					ma_table[lg][i] = chmax(ma_table[lg-1][i], ma_table[lg-1][min(n - 1, i +  k)]);
				}
			}
		}
	}
	
	pair<int, int> get_max(int l, int r){
		int k = __lg(r-l+1);
		return chmax(ma_table[k][l], ma_table[k][r-(1<<k) + 1]);
	}
	
	pair<int, int> get_min(int l, int r){
		int k = __lg(r-l+1);
		return chmin(m_table[k][l], m_table[k][r-(1<<k) + 1]);
	}
	
		
};





vector<int> g[N+1];
int depth[N+1], c[N+1];
int timer=1;
int tin[N+1], tout[N+1];
int up[N+1][18];
int n, m, q;

void dfs(int v, int par){
	up[v][0] = par;
	tin[v] = timer++;
	for(int i = 1;i < 18; i++){
		up[v][i] = up[up[v][i-1]][i-1];
	}
	for(int to : g[v]){
		if(to == par) continue;
		depth[to] = depth[v] + 1;
		dfs(to, v);
	}
	tout[v] = timer++;
}

int lca(int a, int b){
	if(depth[a] > depth[b]) swap(a, b);
	int k = depth[b] - depth[a];
	for(int i = 0;i < 18; i++){
		if(k & (1<<i)){
			b = up[b][i];
		}
	}
	if(a == b) return a;
	for(int i = 17; i >= 0; i--){
		if(up[a][i] != up[b][i]){
			a = up[a][i];
			b = up[b][i];
		}
	}
	return up[a][0];
}

int vert;
vector<int> vec;


int in_subtree(int v){
	int it = lower_bound(all(vec), tin[v]) - vec.begin();
	if(it < (int)vec.size() && vec[it] <= tout[v]) return 1;
	return 0;
}

void ans(int v, int par){
	if(!in_subtree(v)) return;
	vert++;
	for(int to : g[v]){
		if(to == par) continue;
		ans(to, v);
	}
}





signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	 cin >> n >> m >> q;
	for(int i = 1;i < n; i++){
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 0;i < m; i++){
		cin >> c[i];
	}
	dfs(1, 1);
	vector<pair<int, int> > vals;
	for(int i = 0;i < m; i++){
		vals.push_back(make_pair(tin[c[i]], c[i]));
	}
	sparse table(m);
	table.build(vals);
	while(q--){
		int l, r; cin >> l >> r;
		l--, r--;
		int mn = c[l], mx = c[l];
		vec.clear();
		for(int i = l;i <= r; i++){
			vec.push_back(tin[c[i]]);
			if(tin[mn] > tin[c[i]]){
				mn = c[i];
			}
			if(tin[mx] < tin[c[i]]){
				mx = c[i];
			}
		}
		
		mn = table.get_min(l, r).ss;
		mx = table.get_max(l, r).ss;
		sort(all(vec));
		vec.erase(unique(all(vec)), vec.end());
		vert = 0;
		int lc = lca(mn, mx);
		ans(lc, up[lc][0]);
		
		cout << vert << '\n';
		
	}
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 5208 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4964 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 5 ms 5212 KB Output is correct
10 Correct 4 ms 5212 KB Output is correct
11 Correct 5 ms 5212 KB Output is correct
12 Correct 7 ms 5212 KB Output is correct
13 Correct 6 ms 5212 KB Output is correct
14 Correct 6 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 5 ms 5104 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 4 ms 5212 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 4 ms 5208 KB Output is correct
22 Correct 4 ms 5096 KB Output is correct
23 Correct 4 ms 5212 KB Output is correct
24 Correct 4 ms 5212 KB Output is correct
25 Correct 4 ms 5212 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 1 ms 4956 KB Output is correct
28 Correct 1 ms 4956 KB Output is correct
29 Correct 1 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 5208 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4964 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 5 ms 5212 KB Output is correct
10 Correct 4 ms 5212 KB Output is correct
11 Correct 5 ms 5212 KB Output is correct
12 Correct 7 ms 5212 KB Output is correct
13 Correct 6 ms 5212 KB Output is correct
14 Correct 6 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 5 ms 5104 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 4 ms 5212 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 4 ms 5208 KB Output is correct
22 Correct 4 ms 5096 KB Output is correct
23 Correct 4 ms 5212 KB Output is correct
24 Correct 4 ms 5212 KB Output is correct
25 Correct 4 ms 5212 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 1 ms 4956 KB Output is correct
28 Correct 1 ms 4956 KB Output is correct
29 Correct 1 ms 5212 KB Output is correct
30 Correct 92 ms 5716 KB Output is correct
31 Correct 123 ms 5468 KB Output is correct
32 Correct 190 ms 5880 KB Output is correct
33 Correct 189 ms 5724 KB Output is correct
34 Correct 190 ms 5980 KB Output is correct
35 Correct 362 ms 5892 KB Output is correct
36 Correct 367 ms 5972 KB Output is correct
37 Correct 366 ms 5724 KB Output is correct
38 Correct 181 ms 5724 KB Output is correct
39 Correct 171 ms 6232 KB Output is correct
40 Correct 174 ms 5972 KB Output is correct
41 Correct 343 ms 5980 KB Output is correct
42 Correct 341 ms 5980 KB Output is correct
43 Correct 341 ms 5940 KB Output is correct
44 Correct 167 ms 5724 KB Output is correct
45 Correct 169 ms 5904 KB Output is correct
46 Correct 172 ms 5724 KB Output is correct
47 Correct 340 ms 5972 KB Output is correct
48 Correct 330 ms 5900 KB Output is correct
49 Correct 333 ms 5908 KB Output is correct
50 Correct 174 ms 5724 KB Output is correct
51 Correct 188 ms 5884 KB Output is correct
52 Correct 182 ms 5720 KB Output is correct
53 Correct 175 ms 5720 KB Output is correct
54 Correct 178 ms 5884 KB Output is correct
55 Correct 180 ms 5884 KB Output is correct
56 Correct 11 ms 5496 KB Output is correct
57 Correct 2 ms 5212 KB Output is correct
58 Correct 3 ms 5724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 11 ms 5464 KB Output is correct
4 Execution timed out 5089 ms 41216 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 73 ms 27136 KB Output is correct
3 Correct 104 ms 37360 KB Output is correct
4 Correct 91 ms 33000 KB Output is correct
5 Correct 90 ms 44540 KB Output is correct
6 Correct 108 ms 43976 KB Output is correct
7 Correct 129 ms 43984 KB Output is correct
8 Correct 128 ms 43844 KB Output is correct
9 Correct 153 ms 43684 KB Output is correct
10 Correct 142 ms 43692 KB Output is correct
11 Correct 183 ms 43940 KB Output is correct
12 Correct 131 ms 43856 KB Output is correct
13 Correct 145 ms 43776 KB Output is correct
14 Correct 146 ms 43896 KB Output is correct
15 Correct 89 ms 43980 KB Output is correct
16 Correct 115 ms 43976 KB Output is correct
17 Correct 116 ms 43976 KB Output is correct
18 Correct 116 ms 43972 KB Output is correct
19 Correct 79 ms 44468 KB Output is correct
20 Correct 74 ms 44180 KB Output is correct
21 Correct 80 ms 43708 KB Output is correct
22 Correct 90 ms 43716 KB Output is correct
23 Correct 113 ms 43908 KB Output is correct
24 Correct 152 ms 43832 KB Output is correct
25 Correct 195 ms 43836 KB Output is correct
26 Correct 247 ms 43716 KB Output is correct
27 Correct 364 ms 43852 KB Output is correct
28 Correct 447 ms 43972 KB Output is correct
29 Correct 553 ms 43720 KB Output is correct
30 Correct 688 ms 43896 KB Output is correct
31 Correct 770 ms 44104 KB Output is correct
32 Correct 866 ms 43976 KB Output is correct
33 Correct 676 ms 44192 KB Output is correct
34 Correct 92 ms 43964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 11 ms 5692 KB Output is correct
4 Execution timed out 5067 ms 38088 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 5208 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4964 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 5 ms 5212 KB Output is correct
10 Correct 4 ms 5212 KB Output is correct
11 Correct 5 ms 5212 KB Output is correct
12 Correct 7 ms 5212 KB Output is correct
13 Correct 6 ms 5212 KB Output is correct
14 Correct 6 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 5 ms 5104 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 4 ms 5212 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 4 ms 5208 KB Output is correct
22 Correct 4 ms 5096 KB Output is correct
23 Correct 4 ms 5212 KB Output is correct
24 Correct 4 ms 5212 KB Output is correct
25 Correct 4 ms 5212 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 1 ms 4956 KB Output is correct
28 Correct 1 ms 4956 KB Output is correct
29 Correct 1 ms 5212 KB Output is correct
30 Correct 92 ms 5716 KB Output is correct
31 Correct 123 ms 5468 KB Output is correct
32 Correct 190 ms 5880 KB Output is correct
33 Correct 189 ms 5724 KB Output is correct
34 Correct 190 ms 5980 KB Output is correct
35 Correct 362 ms 5892 KB Output is correct
36 Correct 367 ms 5972 KB Output is correct
37 Correct 366 ms 5724 KB Output is correct
38 Correct 181 ms 5724 KB Output is correct
39 Correct 171 ms 6232 KB Output is correct
40 Correct 174 ms 5972 KB Output is correct
41 Correct 343 ms 5980 KB Output is correct
42 Correct 341 ms 5980 KB Output is correct
43 Correct 341 ms 5940 KB Output is correct
44 Correct 167 ms 5724 KB Output is correct
45 Correct 169 ms 5904 KB Output is correct
46 Correct 172 ms 5724 KB Output is correct
47 Correct 340 ms 5972 KB Output is correct
48 Correct 330 ms 5900 KB Output is correct
49 Correct 333 ms 5908 KB Output is correct
50 Correct 174 ms 5724 KB Output is correct
51 Correct 188 ms 5884 KB Output is correct
52 Correct 182 ms 5720 KB Output is correct
53 Correct 175 ms 5720 KB Output is correct
54 Correct 178 ms 5884 KB Output is correct
55 Correct 180 ms 5884 KB Output is correct
56 Correct 11 ms 5496 KB Output is correct
57 Correct 2 ms 5212 KB Output is correct
58 Correct 3 ms 5724 KB Output is correct
59 Correct 1 ms 4956 KB Output is correct
60 Correct 1 ms 4956 KB Output is correct
61 Correct 11 ms 5464 KB Output is correct
62 Execution timed out 5089 ms 41216 KB Time limit exceeded
63 Halted 0 ms 0 KB -