답안 #763606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
763606 2023-06-22T13:49:51 Z CDuong Tourism (JOI23_tourism) C++17
100 / 100
3744 ms 41004 KB
#pragma GCC optimize("unroll-loops")
#pragma gcc optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 3e5 + 5;

const int oo = 1e9 + 7, mod = 1e9 + 7;

/*
What I am thinking:
We just find their lca + total edges covered?
*/

int n, m, q;

vector<int> Adj[N];

int arr[N];

int cnt;
int d[N], mn_d[N][20], lg[N];
int d2[N];


int le[N], ri[N], pos[N];

void dfs(int u, int p){
	cnt++;
	le[u] = pos[u] = cnt;
	mn_d[cnt][0] = u;
	for(auto v : Adj[u]){
		if(v == p) continue;
		d[v] = d[u] + 1;
		dfs(v, u);
		cnt++;
		mn_d[cnt][0] = u;
	}
	ri[u] = cnt;
}

void prep(){
	for(int i = 1; i <= 19; i++){
		for(int j = 1; (j + (1LL << i) - 1) <= (n << 1); j++){
			int x = mn_d[j][i - 1], y = mn_d[j + (1LL << (i - 1))][i - 1];
			mn_d[j][i] = (d[x] < d[y] ? x : y);
		}
	}
	for(int i = 2; i <= (n << 1); i++) lg[i] = lg[i >> 1] + 1;
}

int lca(int x, int y){
	//x = pos[x], y = pos[y];
	if(x > y) swap(x, y);
	int k = lg[y - x + 1];
	int a = mn_d[x][k], b = mn_d[y - (1LL << k) + 1][k];
	return (d[a] < d[b] ? a : b);
}

bool cmp(int a, int b){
	return (pos[a] < pos[b]);
}

vector<iii> queries;

const int S = 400;

bool cmpp(iii a, iii b){
	if(a.fi.fi / S != b.fi.fi / S) return (a.fi.fi < b.fi.fi);
	else if(!((a.fi.fi / S) & 1)) return (a.fi.se < b.fi.se);
	else return (a.fi.se > b.fi.se);
}

int cntt[N];

int BIT[N];

int answer[N];

int cur_ans = 0;

int total = 0;

void upd(int pos, int val){
	assert(pos);
	total += val;
	for(; pos <= (n << 1); pos += pos & -pos) BIT[pos] += val;
}

int get(int pos){
	int ans = 0;
	for(; pos; pos -= pos & -pos) ans += BIT[pos];
	return ans;
}

int find(int val){// first num >= val, assume that there is a number like this
	int answer = 0, mx = (2 * n + 1);
	for(int i = 18; i >= 0; i--){
		if((answer + (1LL << i)) >= mx) continue;
		if(val > BIT[answer + (1LL << i)]){
			val -= BIT[answer + (1LL << i)];
			answer += (1LL << i);
		}
	}
	return answer + 1;
}

int nxtt[N], lstt[N];

void ins(int x){// insert into the set
	//assert(cur.find(x) == cur.end());
	/*
	if(!cur.size()){
		cur.insert(x);
		return;
	}*/
	int lst = -oo, nxt = oo;
	int temp = get(x);
	if(temp > 0){
		lst = find(temp);
		nxt = nxtt[lst];
	}
	else if(temp < total) nxt = find(temp + 1); 
	/*
	set<int>::iterator it = cur.lower_bound(x);
	if(it != cur.end()) nxt = (*it);
	if(it != cur.begin()){
		it--;
		lst = (*it);
	}*/
	if(lst != -oo && nxt != oo) cur_ans -= (d2[nxt] - d[lca(lst, nxt)]);
	if(lst != -oo) cur_ans += (d2[x] - d[lca(lst, x)]);
	if(nxt != oo) cur_ans += (d2[nxt] - d[lca(nxt, x)]);
	lstt[x] = lst, nxtt[x] = nxt;
	if(lstt[x] != -oo) nxtt[lstt[x]] = x;
	if(nxtt[x] != oo) lstt[nxtt[x]] = x;
	//cur.insert(x);
	upd(x, 1);
}

void er(int x){// erase from the set
	int nxt = oo, lst = -oo;
	/*
	if(temp > 1) lst = find(temp - 1);
	if(temp < total) nxt = find(temp + 1);*/
	lst = lstt[x];
	nxt = nxtt[x];
	/*
	set<int>::iterator it = cur.find(x);
	assert(it != cur.end());
	int lst = -oo, nxt = oo;
	it++;
	if(it != cur.end()) nxt = (*it);
	it--;
	if(it != cur.begin()){
		it--;
		lst = (*it);
		it++;
	}*/
	if(lst != -oo && nxt != oo) cur_ans += (d2[nxt] - d[lca(lst, nxt)]);
	if(lst != -oo) cur_ans -= (d2[x] - d[lca(lst, x)]);
	if(nxt != oo) cur_ans -= (d2[nxt] - d[lca(nxt, x)]);
	if(lstt[x] != -oo) nxtt[lstt[x]] = nxtt[x];
	if(nxtt[x] != oo) lstt[nxtt[x]] = lstt[x];
	upd(x, -1);
	//assert(se.find(x) != se.end());
}

void process(){
	cin >> n >> m >> q;
	for(int i = 1; i < n; i++){
		int x, y;
		cin >> x >> y;
		Adj[x].pb(y);
		Adj[y].pb(x);
	}
	dfs(1, 1);
	prep();
	for(int i = 1; i <= n; i++) d2[pos[i]] = d[i];
	for(int i = 1; i <= m; i++) cin >> arr[i];
	for(int i = 0; i < q; i++){
		int l, r;
		cin >> l >> r;
		queries.pb({{l, r}, i});
	}
	sort(queries.begin(), queries.end(), cmpp);
	int lstl = 1, lstr = 0;
	for(int i = 1; i <= n; i++){
		lstt[i] = -oo, nxtt[i] = oo;
	}
	for(auto it : queries){
		while(lstr < it.fi.se){
			lstr++;
			if(!cntt[arr[lstr]]) ins(pos[arr[lstr]]);
			cntt[arr[lstr]]++;
		}
		while(lstl > it.fi.fi){
			lstl--;
			if(!cntt[arr[lstl]]) ins(pos[arr[lstl]]);
			cntt[arr[lstl]]++;
		}
		//for(auto it : cur) cout << it << " ";
		//cout << cur_ans << "\n";
		while(lstr > it.fi.se){
			cntt[arr[lstr]]--;
			if(!cntt[arr[lstr]]) er(pos[arr[lstr]]);
			lstr--;
		}
		while(lstl < it.fi.fi){
			cntt[arr[lstl]]--;
			if(!cntt[arr[lstl]]) er(pos[arr[lstl]]);
			lstl++;
		}
		//cout << cur_ans << "\n";
		//cout << it.fi.fi << " " << it.fi.se << " " << lstl << " " << lstr << "\n";
		//for(auto it : cur) cout << it << " ";
		//cout << "\n";
		if(total){
			int a = find(1), b = find(total);
			answer[it.se] = cur_ans + d2[a] - d[lca(a, b)] + 1;
		}
	}
	for(int i = 0; i < q; i++) cout << answer[i] << "\n";
}

signed main(){
	ios_base::sync_with_stdio(0);
	// freopen("tourist.inp", "r", stdin);
	// freopen("tourist.out", "w", stdout);
	cin.tie(0);
	process();
}


Compilation message

tourism.cpp:2: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    2 | #pragma gcc optimize("Ofast")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7492 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7432 KB Output is correct
15 Correct 6 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 7 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 5 ms 7516 KB Output is correct
27 Correct 6 ms 7380 KB Output is correct
28 Correct 6 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7492 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7432 KB Output is correct
15 Correct 6 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 7 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 5 ms 7516 KB Output is correct
27 Correct 6 ms 7380 KB Output is correct
28 Correct 6 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 12 ms 7764 KB Output is correct
31 Correct 15 ms 7764 KB Output is correct
32 Correct 20 ms 8020 KB Output is correct
33 Correct 16 ms 8008 KB Output is correct
34 Correct 28 ms 8000 KB Output is correct
35 Correct 6 ms 7896 KB Output is correct
36 Correct 6 ms 8020 KB Output is correct
37 Correct 6 ms 8020 KB Output is correct
38 Correct 15 ms 8092 KB Output is correct
39 Correct 16 ms 8020 KB Output is correct
40 Correct 16 ms 8108 KB Output is correct
41 Correct 6 ms 8148 KB Output is correct
42 Correct 5 ms 8056 KB Output is correct
43 Correct 6 ms 8020 KB Output is correct
44 Correct 16 ms 8060 KB Output is correct
45 Correct 16 ms 8020 KB Output is correct
46 Correct 18 ms 8064 KB Output is correct
47 Correct 6 ms 8020 KB Output is correct
48 Correct 6 ms 8020 KB Output is correct
49 Correct 9 ms 8020 KB Output is correct
50 Correct 16 ms 8016 KB Output is correct
51 Correct 16 ms 8016 KB Output is correct
52 Correct 20 ms 8020 KB Output is correct
53 Correct 16 ms 8016 KB Output is correct
54 Correct 16 ms 8016 KB Output is correct
55 Correct 19 ms 8016 KB Output is correct
56 Correct 5 ms 7508 KB Output is correct
57 Correct 5 ms 7972 KB Output is correct
58 Correct 6 ms 7920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7460 KB Output is correct
3 Correct 5 ms 7488 KB Output is correct
4 Correct 1420 ms 26600 KB Output is correct
5 Correct 1339 ms 34192 KB Output is correct
6 Correct 1875 ms 37412 KB Output is correct
7 Correct 2641 ms 40952 KB Output is correct
8 Correct 2869 ms 40952 KB Output is correct
9 Correct 2967 ms 40960 KB Output is correct
10 Correct 3174 ms 40992 KB Output is correct
11 Correct 2771 ms 40940 KB Output is correct
12 Correct 256 ms 40948 KB Output is correct
13 Correct 276 ms 40964 KB Output is correct
14 Correct 288 ms 41004 KB Output is correct
15 Correct 69 ms 39448 KB Output is correct
16 Correct 181 ms 40564 KB Output is correct
17 Correct 103 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 76 ms 21240 KB Output is correct
3 Correct 83 ms 22512 KB Output is correct
4 Correct 81 ms 24412 KB Output is correct
5 Correct 80 ms 32564 KB Output is correct
6 Correct 95 ms 32524 KB Output is correct
7 Correct 135 ms 32588 KB Output is correct
8 Correct 97 ms 32452 KB Output is correct
9 Correct 107 ms 32464 KB Output is correct
10 Correct 108 ms 32576 KB Output is correct
11 Correct 127 ms 32584 KB Output is correct
12 Correct 135 ms 32656 KB Output is correct
13 Correct 135 ms 32780 KB Output is correct
14 Correct 145 ms 33232 KB Output is correct
15 Correct 234 ms 34436 KB Output is correct
16 Correct 164 ms 32816 KB Output is correct
17 Correct 149 ms 32820 KB Output is correct
18 Correct 136 ms 32856 KB Output is correct
19 Correct 90 ms 32500 KB Output is correct
20 Correct 80 ms 32604 KB Output is correct
21 Correct 96 ms 32584 KB Output is correct
22 Correct 105 ms 32600 KB Output is correct
23 Correct 90 ms 32592 KB Output is correct
24 Correct 98 ms 32480 KB Output is correct
25 Correct 101 ms 32516 KB Output is correct
26 Correct 97 ms 32552 KB Output is correct
27 Correct 131 ms 32620 KB Output is correct
28 Correct 140 ms 32576 KB Output is correct
29 Correct 141 ms 32652 KB Output is correct
30 Correct 150 ms 32784 KB Output is correct
31 Correct 131 ms 32952 KB Output is correct
32 Correct 139 ms 33072 KB Output is correct
33 Correct 154 ms 33636 KB Output is correct
34 Correct 174 ms 34572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7516 KB Output is correct
4 Correct 1899 ms 23708 KB Output is correct
5 Correct 1948 ms 24184 KB Output is correct
6 Correct 3229 ms 30880 KB Output is correct
7 Correct 3327 ms 34668 KB Output is correct
8 Correct 3391 ms 34672 KB Output is correct
9 Correct 3315 ms 34668 KB Output is correct
10 Correct 3744 ms 34672 KB Output is correct
11 Correct 3390 ms 34668 KB Output is correct
12 Correct 3040 ms 34668 KB Output is correct
13 Correct 3466 ms 34668 KB Output is correct
14 Correct 103 ms 9648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7492 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7432 KB Output is correct
15 Correct 6 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 7 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7516 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 5 ms 7516 KB Output is correct
27 Correct 6 ms 7380 KB Output is correct
28 Correct 6 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 12 ms 7764 KB Output is correct
31 Correct 15 ms 7764 KB Output is correct
32 Correct 20 ms 8020 KB Output is correct
33 Correct 16 ms 8008 KB Output is correct
34 Correct 28 ms 8000 KB Output is correct
35 Correct 6 ms 7896 KB Output is correct
36 Correct 6 ms 8020 KB Output is correct
37 Correct 6 ms 8020 KB Output is correct
38 Correct 15 ms 8092 KB Output is correct
39 Correct 16 ms 8020 KB Output is correct
40 Correct 16 ms 8108 KB Output is correct
41 Correct 6 ms 8148 KB Output is correct
42 Correct 5 ms 8056 KB Output is correct
43 Correct 6 ms 8020 KB Output is correct
44 Correct 16 ms 8060 KB Output is correct
45 Correct 16 ms 8020 KB Output is correct
46 Correct 18 ms 8064 KB Output is correct
47 Correct 6 ms 8020 KB Output is correct
48 Correct 6 ms 8020 KB Output is correct
49 Correct 9 ms 8020 KB Output is correct
50 Correct 16 ms 8016 KB Output is correct
51 Correct 16 ms 8016 KB Output is correct
52 Correct 20 ms 8020 KB Output is correct
53 Correct 16 ms 8016 KB Output is correct
54 Correct 16 ms 8016 KB Output is correct
55 Correct 19 ms 8016 KB Output is correct
56 Correct 5 ms 7508 KB Output is correct
57 Correct 5 ms 7972 KB Output is correct
58 Correct 6 ms 7920 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 4 ms 7460 KB Output is correct
61 Correct 5 ms 7488 KB Output is correct
62 Correct 1420 ms 26600 KB Output is correct
63 Correct 1339 ms 34192 KB Output is correct
64 Correct 1875 ms 37412 KB Output is correct
65 Correct 2641 ms 40952 KB Output is correct
66 Correct 2869 ms 40952 KB Output is correct
67 Correct 2967 ms 40960 KB Output is correct
68 Correct 3174 ms 40992 KB Output is correct
69 Correct 2771 ms 40940 KB Output is correct
70 Correct 256 ms 40948 KB Output is correct
71 Correct 276 ms 40964 KB Output is correct
72 Correct 288 ms 41004 KB Output is correct
73 Correct 69 ms 39448 KB Output is correct
74 Correct 181 ms 40564 KB Output is correct
75 Correct 103 ms 9708 KB Output is correct
76 Correct 4 ms 7380 KB Output is correct
77 Correct 76 ms 21240 KB Output is correct
78 Correct 83 ms 22512 KB Output is correct
79 Correct 81 ms 24412 KB Output is correct
80 Correct 80 ms 32564 KB Output is correct
81 Correct 95 ms 32524 KB Output is correct
82 Correct 135 ms 32588 KB Output is correct
83 Correct 97 ms 32452 KB Output is correct
84 Correct 107 ms 32464 KB Output is correct
85 Correct 108 ms 32576 KB Output is correct
86 Correct 127 ms 32584 KB Output is correct
87 Correct 135 ms 32656 KB Output is correct
88 Correct 135 ms 32780 KB Output is correct
89 Correct 145 ms 33232 KB Output is correct
90 Correct 234 ms 34436 KB Output is correct
91 Correct 164 ms 32816 KB Output is correct
92 Correct 149 ms 32820 KB Output is correct
93 Correct 136 ms 32856 KB Output is correct
94 Correct 90 ms 32500 KB Output is correct
95 Correct 80 ms 32604 KB Output is correct
96 Correct 96 ms 32584 KB Output is correct
97 Correct 105 ms 32600 KB Output is correct
98 Correct 90 ms 32592 KB Output is correct
99 Correct 98 ms 32480 KB Output is correct
100 Correct 101 ms 32516 KB Output is correct
101 Correct 97 ms 32552 KB Output is correct
102 Correct 131 ms 32620 KB Output is correct
103 Correct 140 ms 32576 KB Output is correct
104 Correct 141 ms 32652 KB Output is correct
105 Correct 150 ms 32784 KB Output is correct
106 Correct 131 ms 32952 KB Output is correct
107 Correct 139 ms 33072 KB Output is correct
108 Correct 154 ms 33636 KB Output is correct
109 Correct 174 ms 34572 KB Output is correct
110 Correct 4 ms 7380 KB Output is correct
111 Correct 4 ms 7380 KB Output is correct
112 Correct 5 ms 7516 KB Output is correct
113 Correct 1899 ms 23708 KB Output is correct
114 Correct 1948 ms 24184 KB Output is correct
115 Correct 3229 ms 30880 KB Output is correct
116 Correct 3327 ms 34668 KB Output is correct
117 Correct 3391 ms 34672 KB Output is correct
118 Correct 3315 ms 34668 KB Output is correct
119 Correct 3744 ms 34672 KB Output is correct
120 Correct 3390 ms 34668 KB Output is correct
121 Correct 3040 ms 34668 KB Output is correct
122 Correct 3466 ms 34668 KB Output is correct
123 Correct 103 ms 9648 KB Output is correct
124 Correct 2811 ms 34632 KB Output is correct
125 Correct 1649 ms 33032 KB Output is correct
126 Correct 3406 ms 34776 KB Output is correct
127 Correct 3442 ms 34772 KB Output is correct
128 Correct 3711 ms 34780 KB Output is correct
129 Correct 3149 ms 34768 KB Output is correct
130 Correct 3194 ms 34776 KB Output is correct
131 Correct 2613 ms 39920 KB Output is correct
132 Correct 2571 ms 40888 KB Output is correct
133 Correct 2545 ms 37472 KB Output is correct
134 Correct 2881 ms 34872 KB Output is correct
135 Correct 2835 ms 34824 KB Output is correct
136 Correct 2867 ms 34824 KB Output is correct
137 Correct 3052 ms 35328 KB Output is correct
138 Correct 2933 ms 35316 KB Output is correct
139 Correct 3082 ms 35320 KB Output is correct
140 Correct 3117 ms 35320 KB Output is correct
141 Correct 2981 ms 35316 KB Output is correct
142 Correct 2884 ms 35444 KB Output is correct
143 Correct 79 ms 32064 KB Output is correct
144 Correct 177 ms 34380 KB Output is correct