Submission #781999

# Submission time Handle Problem Language Result Execution time Memory
781999 2023-07-13T14:38:58 Z Arturgo Tourism (JOI23_tourism) C++14
100 / 100
297 ms 40748 KB
#include <bits/stdc++.h>
using namespace std;

const int INIT = (1 << 17) - 1;

int sommes[(1 << 18)];

void ajoute(int pos, int val) {
	pos += (1 << 17);
	while(pos != 0) {
		sommes[pos] += val;
		pos /= 2;
	}
}

int somme_entre(int deb, int fin) {
	deb += (1 << 17);
	fin += (1 << 17);
	
	int somme = 0;
	while(deb < fin) {
		if(deb % 2 == 1) {
			somme += sommes[deb];
			deb++;
		}
		if(fin % 2 == 1) {
			fin--;
			somme += sommes[fin];
		}
		deb /= 2;
		fin /= 2;
	}
	
	return somme;
}

struct Req {
	int deb, fin, id;
};

struct Inter {
	int deb, fin, couleur;
};

int N, M, Q;

vector<int> voisins[100 * 1000];
vector<int> positions;
vector<Req> reqs[100 * 1000];
int reponses[100 * 1000];
int profs[100 * 1000];

int posPeigne;
int gauches[100 * 1000];
int droites[100 * 1000];

int remontes[20][100 * 1000];

int id_chaine[100 * 1000];
vector<vector<Inter>> chaines;

bool est_parent(int u, int v) {
	return gauches[u] <= gauches[v] && droites[v] <= droites[u];
}

bool est_parent_strict(int u, int v) {
	if(u == -1) return true;
	return gauches[u] < gauches[v] && droites[v] < droites[u];
}

pair<int, int> up(pair<int, int> a, pair<int, int> b) {
	int l, r;
	
	if(gauches[a.first] < gauches[b.first])
		l = a.first;
	else
		l = b.first;
	
	if(gauches[a.second] < gauches[b.second])
		r = b.second;
	else
		r = a.second;
	
	return {l, r};
}

pair<int, int> arbre_dfs[(1 << 18)];

pair<int, int> get_bords(int deb, int fin) {
	pair<int, int> sol = {positions[deb], positions[deb]};

	deb += (1 << 17);
	fin += (1 << 17);	
	
	while(deb < fin) {
		if(deb % 2 == 1) {
			sol = up(sol, arbre_dfs[deb]);
			deb++;
		}
		if(fin % 2 == 1) {
			fin--;
			sol = up(sol, arbre_dfs[fin]);
		}
		deb /= 2;
		fin /= 2;
	}
	
	return sol;
}

int build_ds(int u, int p = -1, int prof = 0) {
	remontes[0][u] = p;
	profs[u] = prof;
	gauches[u] = posPeigne++;
	
	int taille = 1;
	pair<int, int> mel_fils = {-1, -1};
	
	for(int v : voisins[u]) {
		if(v == p) continue;
		int ret = build_ds(v, u, prof + 1);
		taille += ret;
		
		mel_fils = max(mel_fils, {ret, id_chaine[v]});
	}
	
	if(mel_fils.first == -1) {
		id_chaine[u] = chaines.size();
		chaines.push_back({});
		chaines.back().push_back({p, u, INIT});
	}
	else {
		id_chaine[u] = mel_fils.second;
		chaines[mel_fils.second].push_back({p, u, INIT});
	}
	
	droites[u] = posPeigne++;
	return taille;
}

int lca(int u, int v) {
	if(est_parent(u, v)) return u;

	for(int rem = 19;rem >= 0;rem--) {
		int w = remontes[rem][u];
		if(w != -1 && !est_parent(w, v)) {
			u = w;
		}
	}
	
	return remontes[0][u];
}

int get_prof(int u) {
	if(u == -1) return -1;
	return profs[u];
}

void recolorie(int u, int color) {
	while(u != -1) {
		vector<Inter>& chaine = chaines[id_chaine[u]];
		
		int start = chaine.back().deb;
		int end;
		int last_col;
		
		while(!chaine.empty()
		 && est_parent_strict(chaine.back().deb, u)) {
			end = chaine.back().fin;
			last_col = chaine.back().couleur;
			
			int sz = get_prof(chaine.back().fin)
			- get_prof(chaine.back().deb);
			int col = chaine.back().couleur;
			ajoute(col, -sz);
			
			chaine.pop_back();
		}
		
		if(u != end) {
			chaine.push_back({u, end, last_col});
			ajoute(last_col, 
				get_prof(end)
				- get_prof(u)
			);
		}
		
		chaine.push_back({start, u, color});
		ajoute(color, 
			get_prof(u)
			- get_prof(start)
		);
		
		u = start;
	}
}

void solve() {
	build_ds(0);
	
	for(int prof = 1;prof < 20;prof++) {
		for(int i = 0;i < N;i++) {
			if(remontes[prof - 1][i] == -1)
				remontes[prof][i] = -1;
			else
				remontes[prof][i] = remontes[prof - 1][
					remontes[prof - 1][i]
				];
		}
	}
	
	for(int i = 0;i < M;i++) {
		arbre_dfs[(1 << 17) + i] = {positions[i], positions[i]};
	}
	
	for(int i = (1 << 17) - 1;i > 0;i--) {
		arbre_dfs[i] = up(arbre_dfs[2 * i], arbre_dfs[2 * i + 1]);
	}
	
	for(int l = M - 1;l >= 0;l--) {
		recolorie(positions[l], l);
		
		for(Req r : reqs[l]) {
			int res = somme_entre(l, r.fin);
			
			pair<int, int> bords = get_bords(l, r.fin);
			
			res -= profs[lca(
				bords.first, bords.second
			)];
			
			reponses[r.id] = res;
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin >> N >> M >> Q;
	
	for(int i = 0;i < N - 1;i++) {
		int deb, fin;
		cin >> deb >> fin;
		deb--; fin--;
		voisins[deb].push_back(fin);
		voisins[fin].push_back(deb);
	}
	
	for(int i = 0;i < M;i++) {
		int pos;
		cin >> pos;
		pos--;
		positions.push_back(pos);
	}
	
	for(int i = 0;i < Q;i++) {
		int deb, fin;
		cin >> deb >> fin;
		deb--;
		reqs[deb].push_back({deb, fin, i});
	}
	
	solve();
	
	for(int i = 0;i < Q;i++) {
		cout << reponses[i] << endl;
	}
	return 0;
}

Compilation message

tourism.cpp: In function 'void recolorie(int, int)':
tourism.cpp:9:6: warning: 'last_col' may be used uninitialized in this function [-Wmaybe-uninitialized]
    9 |  pos += (1 << 17);
      |  ~~~~^~~~~~~~~~~~
tourism.cpp:165:7: note: 'last_col' was declared here
  165 |   int last_col;
      |       ^~~~~~~~
tourism.cpp:156:16: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
  156 |  return profs[u];
      |                ^
tourism.cpp:164:7: note: 'end' was declared here
  164 |   int end;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6200 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 4 ms 6228 KB Output is correct
5 Correct 4 ms 6228 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6196 KB Output is correct
8 Correct 4 ms 6228 KB Output is correct
9 Correct 4 ms 6200 KB Output is correct
10 Correct 4 ms 6280 KB Output is correct
11 Correct 4 ms 6204 KB Output is correct
12 Correct 4 ms 6228 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6196 KB Output is correct
15 Correct 4 ms 6228 KB Output is correct
16 Correct 4 ms 6228 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 4 ms 6272 KB Output is correct
19 Correct 4 ms 6196 KB Output is correct
20 Correct 4 ms 6228 KB Output is correct
21 Correct 4 ms 6228 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 4 ms 6228 KB Output is correct
24 Correct 4 ms 6204 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 5 ms 6196 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 5 ms 6196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6200 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 4 ms 6228 KB Output is correct
5 Correct 4 ms 6228 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6196 KB Output is correct
8 Correct 4 ms 6228 KB Output is correct
9 Correct 4 ms 6200 KB Output is correct
10 Correct 4 ms 6280 KB Output is correct
11 Correct 4 ms 6204 KB Output is correct
12 Correct 4 ms 6228 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6196 KB Output is correct
15 Correct 4 ms 6228 KB Output is correct
16 Correct 4 ms 6228 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 4 ms 6272 KB Output is correct
19 Correct 4 ms 6196 KB Output is correct
20 Correct 4 ms 6228 KB Output is correct
21 Correct 4 ms 6228 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 4 ms 6228 KB Output is correct
24 Correct 4 ms 6204 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 5 ms 6196 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 5 ms 6196 KB Output is correct
30 Correct 6 ms 6484 KB Output is correct
31 Correct 7 ms 6464 KB Output is correct
32 Correct 9 ms 6720 KB Output is correct
33 Correct 8 ms 6676 KB Output is correct
34 Correct 7 ms 6612 KB Output is correct
35 Correct 7 ms 6680 KB Output is correct
36 Correct 7 ms 6612 KB Output is correct
37 Correct 7 ms 6612 KB Output is correct
38 Correct 7 ms 6740 KB Output is correct
39 Correct 7 ms 6740 KB Output is correct
40 Correct 7 ms 6844 KB Output is correct
41 Correct 7 ms 6832 KB Output is correct
42 Correct 7 ms 6868 KB Output is correct
43 Correct 7 ms 6740 KB Output is correct
44 Correct 7 ms 6732 KB Output is correct
45 Correct 7 ms 6612 KB Output is correct
46 Correct 7 ms 6740 KB Output is correct
47 Correct 8 ms 6696 KB Output is correct
48 Correct 8 ms 6696 KB Output is correct
49 Correct 8 ms 6712 KB Output is correct
50 Correct 7 ms 6612 KB Output is correct
51 Correct 7 ms 6612 KB Output is correct
52 Correct 7 ms 6656 KB Output is correct
53 Correct 7 ms 6720 KB Output is correct
54 Correct 12 ms 6612 KB Output is correct
55 Correct 7 ms 6612 KB Output is correct
56 Correct 8 ms 6212 KB Output is correct
57 Correct 6 ms 6520 KB Output is correct
58 Correct 7 ms 6544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6192 KB Output is correct
2 Correct 4 ms 6228 KB Output is correct
3 Correct 6 ms 6332 KB Output is correct
4 Correct 153 ms 27240 KB Output is correct
5 Correct 136 ms 32612 KB Output is correct
6 Correct 129 ms 36168 KB Output is correct
7 Correct 207 ms 40624 KB Output is correct
8 Correct 210 ms 40696 KB Output is correct
9 Correct 202 ms 40668 KB Output is correct
10 Correct 212 ms 40748 KB Output is correct
11 Correct 214 ms 40632 KB Output is correct
12 Correct 180 ms 40096 KB Output is correct
13 Correct 222 ms 40124 KB Output is correct
14 Correct 179 ms 40152 KB Output is correct
15 Correct 155 ms 35696 KB Output is correct
16 Correct 172 ms 40356 KB Output is correct
17 Correct 161 ms 12216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 52 ms 16916 KB Output is correct
3 Correct 73 ms 18592 KB Output is correct
4 Correct 75 ms 19508 KB Output is correct
5 Correct 98 ms 25732 KB Output is correct
6 Correct 117 ms 25716 KB Output is correct
7 Correct 104 ms 25616 KB Output is correct
8 Correct 97 ms 25644 KB Output is correct
9 Correct 102 ms 25660 KB Output is correct
10 Correct 99 ms 25724 KB Output is correct
11 Correct 122 ms 25804 KB Output is correct
12 Correct 102 ms 25776 KB Output is correct
13 Correct 157 ms 26156 KB Output is correct
14 Correct 142 ms 27244 KB Output is correct
15 Correct 218 ms 30700 KB Output is correct
16 Correct 115 ms 26244 KB Output is correct
17 Correct 164 ms 26240 KB Output is correct
18 Correct 114 ms 26192 KB Output is correct
19 Correct 82 ms 24288 KB Output is correct
20 Correct 88 ms 24120 KB Output is correct
21 Correct 79 ms 24132 KB Output is correct
22 Correct 80 ms 24248 KB Output is correct
23 Correct 76 ms 24164 KB Output is correct
24 Correct 76 ms 24144 KB Output is correct
25 Correct 79 ms 24100 KB Output is correct
26 Correct 79 ms 24164 KB Output is correct
27 Correct 82 ms 24284 KB Output is correct
28 Correct 81 ms 24176 KB Output is correct
29 Correct 84 ms 24256 KB Output is correct
30 Correct 99 ms 24660 KB Output is correct
31 Correct 95 ms 24716 KB Output is correct
32 Correct 127 ms 25228 KB Output is correct
33 Correct 151 ms 26664 KB Output is correct
34 Correct 230 ms 29208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 6 ms 6228 KB Output is correct
4 Correct 226 ms 21988 KB Output is correct
5 Correct 258 ms 22252 KB Output is correct
6 Correct 253 ms 27144 KB Output is correct
7 Correct 271 ms 29836 KB Output is correct
8 Correct 279 ms 29872 KB Output is correct
9 Correct 281 ms 29836 KB Output is correct
10 Correct 288 ms 30004 KB Output is correct
11 Correct 270 ms 29756 KB Output is correct
12 Correct 289 ms 29868 KB Output is correct
13 Correct 297 ms 29896 KB Output is correct
14 Correct 157 ms 12164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6200 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 4 ms 6228 KB Output is correct
5 Correct 4 ms 6228 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6196 KB Output is correct
8 Correct 4 ms 6228 KB Output is correct
9 Correct 4 ms 6200 KB Output is correct
10 Correct 4 ms 6280 KB Output is correct
11 Correct 4 ms 6204 KB Output is correct
12 Correct 4 ms 6228 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6196 KB Output is correct
15 Correct 4 ms 6228 KB Output is correct
16 Correct 4 ms 6228 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 4 ms 6272 KB Output is correct
19 Correct 4 ms 6196 KB Output is correct
20 Correct 4 ms 6228 KB Output is correct
21 Correct 4 ms 6228 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 4 ms 6228 KB Output is correct
24 Correct 4 ms 6204 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 5 ms 6196 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 5 ms 6196 KB Output is correct
30 Correct 6 ms 6484 KB Output is correct
31 Correct 7 ms 6464 KB Output is correct
32 Correct 9 ms 6720 KB Output is correct
33 Correct 8 ms 6676 KB Output is correct
34 Correct 7 ms 6612 KB Output is correct
35 Correct 7 ms 6680 KB Output is correct
36 Correct 7 ms 6612 KB Output is correct
37 Correct 7 ms 6612 KB Output is correct
38 Correct 7 ms 6740 KB Output is correct
39 Correct 7 ms 6740 KB Output is correct
40 Correct 7 ms 6844 KB Output is correct
41 Correct 7 ms 6832 KB Output is correct
42 Correct 7 ms 6868 KB Output is correct
43 Correct 7 ms 6740 KB Output is correct
44 Correct 7 ms 6732 KB Output is correct
45 Correct 7 ms 6612 KB Output is correct
46 Correct 7 ms 6740 KB Output is correct
47 Correct 8 ms 6696 KB Output is correct
48 Correct 8 ms 6696 KB Output is correct
49 Correct 8 ms 6712 KB Output is correct
50 Correct 7 ms 6612 KB Output is correct
51 Correct 7 ms 6612 KB Output is correct
52 Correct 7 ms 6656 KB Output is correct
53 Correct 7 ms 6720 KB Output is correct
54 Correct 12 ms 6612 KB Output is correct
55 Correct 7 ms 6612 KB Output is correct
56 Correct 8 ms 6212 KB Output is correct
57 Correct 6 ms 6520 KB Output is correct
58 Correct 7 ms 6544 KB Output is correct
59 Correct 5 ms 6192 KB Output is correct
60 Correct 4 ms 6228 KB Output is correct
61 Correct 6 ms 6332 KB Output is correct
62 Correct 153 ms 27240 KB Output is correct
63 Correct 136 ms 32612 KB Output is correct
64 Correct 129 ms 36168 KB Output is correct
65 Correct 207 ms 40624 KB Output is correct
66 Correct 210 ms 40696 KB Output is correct
67 Correct 202 ms 40668 KB Output is correct
68 Correct 212 ms 40748 KB Output is correct
69 Correct 214 ms 40632 KB Output is correct
70 Correct 180 ms 40096 KB Output is correct
71 Correct 222 ms 40124 KB Output is correct
72 Correct 179 ms 40152 KB Output is correct
73 Correct 155 ms 35696 KB Output is correct
74 Correct 172 ms 40356 KB Output is correct
75 Correct 161 ms 12216 KB Output is correct
76 Correct 3 ms 6100 KB Output is correct
77 Correct 52 ms 16916 KB Output is correct
78 Correct 73 ms 18592 KB Output is correct
79 Correct 75 ms 19508 KB Output is correct
80 Correct 98 ms 25732 KB Output is correct
81 Correct 117 ms 25716 KB Output is correct
82 Correct 104 ms 25616 KB Output is correct
83 Correct 97 ms 25644 KB Output is correct
84 Correct 102 ms 25660 KB Output is correct
85 Correct 99 ms 25724 KB Output is correct
86 Correct 122 ms 25804 KB Output is correct
87 Correct 102 ms 25776 KB Output is correct
88 Correct 157 ms 26156 KB Output is correct
89 Correct 142 ms 27244 KB Output is correct
90 Correct 218 ms 30700 KB Output is correct
91 Correct 115 ms 26244 KB Output is correct
92 Correct 164 ms 26240 KB Output is correct
93 Correct 114 ms 26192 KB Output is correct
94 Correct 82 ms 24288 KB Output is correct
95 Correct 88 ms 24120 KB Output is correct
96 Correct 79 ms 24132 KB Output is correct
97 Correct 80 ms 24248 KB Output is correct
98 Correct 76 ms 24164 KB Output is correct
99 Correct 76 ms 24144 KB Output is correct
100 Correct 79 ms 24100 KB Output is correct
101 Correct 79 ms 24164 KB Output is correct
102 Correct 82 ms 24284 KB Output is correct
103 Correct 81 ms 24176 KB Output is correct
104 Correct 84 ms 24256 KB Output is correct
105 Correct 99 ms 24660 KB Output is correct
106 Correct 95 ms 24716 KB Output is correct
107 Correct 127 ms 25228 KB Output is correct
108 Correct 151 ms 26664 KB Output is correct
109 Correct 230 ms 29208 KB Output is correct
110 Correct 3 ms 6100 KB Output is correct
111 Correct 3 ms 6100 KB Output is correct
112 Correct 6 ms 6228 KB Output is correct
113 Correct 226 ms 21988 KB Output is correct
114 Correct 258 ms 22252 KB Output is correct
115 Correct 253 ms 27144 KB Output is correct
116 Correct 271 ms 29836 KB Output is correct
117 Correct 279 ms 29872 KB Output is correct
118 Correct 281 ms 29836 KB Output is correct
119 Correct 288 ms 30004 KB Output is correct
120 Correct 270 ms 29756 KB Output is correct
121 Correct 289 ms 29868 KB Output is correct
122 Correct 297 ms 29896 KB Output is correct
123 Correct 157 ms 12164 KB Output is correct
124 Correct 238 ms 29436 KB Output is correct
125 Correct 186 ms 26792 KB Output is correct
126 Correct 270 ms 29992 KB Output is correct
127 Correct 268 ms 30052 KB Output is correct
128 Correct 271 ms 29932 KB Output is correct
129 Correct 257 ms 30000 KB Output is correct
130 Correct 271 ms 30116 KB Output is correct
131 Correct 223 ms 38592 KB Output is correct
132 Correct 222 ms 40488 KB Output is correct
133 Correct 224 ms 34160 KB Output is correct
134 Correct 228 ms 28492 KB Output is correct
135 Correct 234 ms 28588 KB Output is correct
136 Correct 242 ms 28504 KB Output is correct
137 Correct 237 ms 32444 KB Output is correct
138 Correct 236 ms 32372 KB Output is correct
139 Correct 225 ms 32436 KB Output is correct
140 Correct 221 ms 32396 KB Output is correct
141 Correct 234 ms 32440 KB Output is correct
142 Correct 253 ms 32376 KB Output is correct
143 Correct 162 ms 25328 KB Output is correct
144 Correct 235 ms 29668 KB Output is correct