Submission #763605

# Submission time Handle Problem Language Result Execution time Memory
763605 2023-06-22T13:49:06 Z minhcool Tourism (JOI23_tourism) C++17
100 / 100
3866 ms 43904 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")
      |
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7332 KB Output is correct
4 Correct 6 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7484 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7388 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7524 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 5 ms 7520 KB Output is correct
19 Correct 5 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 7508 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 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7508 KB Output is correct
29 Correct 4 ms 7480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7332 KB Output is correct
4 Correct 6 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7484 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7388 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7524 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 5 ms 7520 KB Output is correct
19 Correct 5 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 7508 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 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7508 KB Output is correct
29 Correct 4 ms 7480 KB Output is correct
30 Correct 11 ms 7864 KB Output is correct
31 Correct 14 ms 7764 KB Output is correct
32 Correct 16 ms 8004 KB Output is correct
33 Correct 16 ms 8020 KB Output is correct
34 Correct 19 ms 8012 KB Output is correct
35 Correct 6 ms 7892 KB Output is correct
36 Correct 6 ms 8020 KB Output is correct
37 Correct 6 ms 7892 KB Output is correct
38 Correct 16 ms 8092 KB Output is correct
39 Correct 16 ms 8020 KB Output is correct
40 Correct 16 ms 8084 KB Output is correct
41 Correct 6 ms 8020 KB Output is correct
42 Correct 6 ms 8148 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 16 ms 8064 KB Output is correct
45 Correct 20 ms 8040 KB Output is correct
46 Correct 17 ms 8060 KB Output is correct
47 Correct 6 ms 8020 KB Output is correct
48 Correct 6 ms 8020 KB Output is correct
49 Correct 6 ms 8020 KB Output is correct
50 Correct 15 ms 8020 KB Output is correct
51 Correct 15 ms 8020 KB Output is correct
52 Correct 16 ms 7964 KB Output is correct
53 Correct 16 ms 8020 KB Output is correct
54 Correct 15 ms 8020 KB Output is correct
55 Correct 15 ms 8020 KB Output is correct
56 Correct 5 ms 7432 KB Output is correct
57 Correct 5 ms 7892 KB Output is correct
58 Correct 6 ms 7892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7508 KB Output is correct
4 Correct 1444 ms 26600 KB Output is correct
5 Correct 1230 ms 34184 KB Output is correct
6 Correct 1602 ms 37416 KB Output is correct
7 Correct 2544 ms 41072 KB Output is correct
8 Correct 2666 ms 43824 KB Output is correct
9 Correct 2596 ms 43904 KB Output is correct
10 Correct 2570 ms 43840 KB Output is correct
11 Correct 2684 ms 43828 KB Output is correct
12 Correct 267 ms 43616 KB Output is correct
13 Correct 289 ms 43656 KB Output is correct
14 Correct 274 ms 43608 KB Output is correct
15 Correct 73 ms 40912 KB Output is correct
16 Correct 160 ms 43456 KB Output is correct
17 Correct 103 ms 10996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 58 ms 21260 KB Output is correct
3 Correct 84 ms 22500 KB Output is correct
4 Correct 76 ms 24476 KB Output is correct
5 Correct 75 ms 32476 KB Output is correct
6 Correct 90 ms 32564 KB Output is correct
7 Correct 93 ms 32492 KB Output is correct
8 Correct 118 ms 32588 KB Output is correct
9 Correct 99 ms 32564 KB Output is correct
10 Correct 111 ms 32464 KB Output is correct
11 Correct 125 ms 32588 KB Output is correct
12 Correct 129 ms 32652 KB Output is correct
13 Correct 138 ms 32836 KB Output is correct
14 Correct 144 ms 33156 KB Output is correct
15 Correct 203 ms 34440 KB Output is correct
16 Correct 121 ms 32784 KB Output is correct
17 Correct 130 ms 32852 KB Output is correct
18 Correct 120 ms 32856 KB Output is correct
19 Correct 72 ms 32512 KB Output is correct
20 Correct 75 ms 32496 KB Output is correct
21 Correct 83 ms 32480 KB Output is correct
22 Correct 98 ms 32632 KB Output is correct
23 Correct 90 ms 32580 KB Output is correct
24 Correct 94 ms 32504 KB Output is correct
25 Correct 102 ms 32452 KB Output is correct
26 Correct 97 ms 32588 KB Output is correct
27 Correct 126 ms 32636 KB Output is correct
28 Correct 131 ms 32592 KB Output is correct
29 Correct 125 ms 32648 KB Output is correct
30 Correct 128 ms 32776 KB Output is correct
31 Correct 131 ms 32940 KB Output is correct
32 Correct 136 ms 33140 KB Output is correct
33 Correct 149 ms 33628 KB Output is correct
34 Correct 176 ms 34548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 6 ms 7508 KB Output is correct
4 Correct 1774 ms 23768 KB Output is correct
5 Correct 1855 ms 26380 KB Output is correct
6 Correct 2609 ms 33556 KB Output is correct
7 Correct 3102 ms 37540 KB Output is correct
8 Correct 3348 ms 37544 KB Output is correct
9 Correct 3715 ms 37552 KB Output is correct
10 Correct 3542 ms 37536 KB Output is correct
11 Correct 3130 ms 37536 KB Output is correct
12 Correct 3429 ms 37544 KB Output is correct
13 Correct 3543 ms 37548 KB Output is correct
14 Correct 107 ms 10996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7332 KB Output is correct
4 Correct 6 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7484 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 5 ms 7388 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7524 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 5 ms 7520 KB Output is correct
19 Correct 5 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 7508 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 7508 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 5 ms 7508 KB Output is correct
29 Correct 4 ms 7480 KB Output is correct
30 Correct 11 ms 7864 KB Output is correct
31 Correct 14 ms 7764 KB Output is correct
32 Correct 16 ms 8004 KB Output is correct
33 Correct 16 ms 8020 KB Output is correct
34 Correct 19 ms 8012 KB Output is correct
35 Correct 6 ms 7892 KB Output is correct
36 Correct 6 ms 8020 KB Output is correct
37 Correct 6 ms 7892 KB Output is correct
38 Correct 16 ms 8092 KB Output is correct
39 Correct 16 ms 8020 KB Output is correct
40 Correct 16 ms 8084 KB Output is correct
41 Correct 6 ms 8020 KB Output is correct
42 Correct 6 ms 8148 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 16 ms 8064 KB Output is correct
45 Correct 20 ms 8040 KB Output is correct
46 Correct 17 ms 8060 KB Output is correct
47 Correct 6 ms 8020 KB Output is correct
48 Correct 6 ms 8020 KB Output is correct
49 Correct 6 ms 8020 KB Output is correct
50 Correct 15 ms 8020 KB Output is correct
51 Correct 15 ms 8020 KB Output is correct
52 Correct 16 ms 7964 KB Output is correct
53 Correct 16 ms 8020 KB Output is correct
54 Correct 15 ms 8020 KB Output is correct
55 Correct 15 ms 8020 KB Output is correct
56 Correct 5 ms 7432 KB Output is correct
57 Correct 5 ms 7892 KB Output is correct
58 Correct 6 ms 7892 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 5 ms 7380 KB Output is correct
61 Correct 5 ms 7508 KB Output is correct
62 Correct 1444 ms 26600 KB Output is correct
63 Correct 1230 ms 34184 KB Output is correct
64 Correct 1602 ms 37416 KB Output is correct
65 Correct 2544 ms 41072 KB Output is correct
66 Correct 2666 ms 43824 KB Output is correct
67 Correct 2596 ms 43904 KB Output is correct
68 Correct 2570 ms 43840 KB Output is correct
69 Correct 2684 ms 43828 KB Output is correct
70 Correct 267 ms 43616 KB Output is correct
71 Correct 289 ms 43656 KB Output is correct
72 Correct 274 ms 43608 KB Output is correct
73 Correct 73 ms 40912 KB Output is correct
74 Correct 160 ms 43456 KB Output is correct
75 Correct 103 ms 10996 KB Output is correct
76 Correct 4 ms 7380 KB Output is correct
77 Correct 58 ms 21260 KB Output is correct
78 Correct 84 ms 22500 KB Output is correct
79 Correct 76 ms 24476 KB Output is correct
80 Correct 75 ms 32476 KB Output is correct
81 Correct 90 ms 32564 KB Output is correct
82 Correct 93 ms 32492 KB Output is correct
83 Correct 118 ms 32588 KB Output is correct
84 Correct 99 ms 32564 KB Output is correct
85 Correct 111 ms 32464 KB Output is correct
86 Correct 125 ms 32588 KB Output is correct
87 Correct 129 ms 32652 KB Output is correct
88 Correct 138 ms 32836 KB Output is correct
89 Correct 144 ms 33156 KB Output is correct
90 Correct 203 ms 34440 KB Output is correct
91 Correct 121 ms 32784 KB Output is correct
92 Correct 130 ms 32852 KB Output is correct
93 Correct 120 ms 32856 KB Output is correct
94 Correct 72 ms 32512 KB Output is correct
95 Correct 75 ms 32496 KB Output is correct
96 Correct 83 ms 32480 KB Output is correct
97 Correct 98 ms 32632 KB Output is correct
98 Correct 90 ms 32580 KB Output is correct
99 Correct 94 ms 32504 KB Output is correct
100 Correct 102 ms 32452 KB Output is correct
101 Correct 97 ms 32588 KB Output is correct
102 Correct 126 ms 32636 KB Output is correct
103 Correct 131 ms 32592 KB Output is correct
104 Correct 125 ms 32648 KB Output is correct
105 Correct 128 ms 32776 KB Output is correct
106 Correct 131 ms 32940 KB Output is correct
107 Correct 136 ms 33140 KB Output is correct
108 Correct 149 ms 33628 KB Output is correct
109 Correct 176 ms 34548 KB Output is correct
110 Correct 4 ms 7380 KB Output is correct
111 Correct 4 ms 7380 KB Output is correct
112 Correct 6 ms 7508 KB Output is correct
113 Correct 1774 ms 23768 KB Output is correct
114 Correct 1855 ms 26380 KB Output is correct
115 Correct 2609 ms 33556 KB Output is correct
116 Correct 3102 ms 37540 KB Output is correct
117 Correct 3348 ms 37544 KB Output is correct
118 Correct 3715 ms 37552 KB Output is correct
119 Correct 3542 ms 37536 KB Output is correct
120 Correct 3130 ms 37536 KB Output is correct
121 Correct 3429 ms 37544 KB Output is correct
122 Correct 3543 ms 37548 KB Output is correct
123 Correct 107 ms 10996 KB Output is correct
124 Correct 3184 ms 37388 KB Output is correct
125 Correct 1690 ms 35164 KB Output is correct
126 Correct 3173 ms 37656 KB Output is correct
127 Correct 3579 ms 37644 KB Output is correct
128 Correct 3451 ms 37656 KB Output is correct
129 Correct 3565 ms 37652 KB Output is correct
130 Correct 3866 ms 37652 KB Output is correct
131 Correct 2932 ms 42792 KB Output is correct
132 Correct 2735 ms 43768 KB Output is correct
133 Correct 2659 ms 40344 KB Output is correct
134 Correct 3538 ms 37748 KB Output is correct
135 Correct 3198 ms 37828 KB Output is correct
136 Correct 3158 ms 37696 KB Output is correct
137 Correct 3198 ms 38216 KB Output is correct
138 Correct 3295 ms 38216 KB Output is correct
139 Correct 3195 ms 38208 KB Output is correct
140 Correct 3129 ms 38204 KB Output is correct
141 Correct 3332 ms 38200 KB Output is correct
142 Correct 3389 ms 38228 KB Output is correct
143 Correct 85 ms 33700 KB Output is correct
144 Correct 184 ms 37292 KB Output is correct