Submission #782002

# Submission time Handle Problem Language Result Execution time Memory
782002 2023-07-13T14:40:39 Z Arturgo Tourism (JOI23_tourism) C++14
100 / 100
229 ms 38080 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] << "\n";
	}
	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 5 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 3 ms 6228 KB Output is correct
5 Correct 4 ms 6192 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6228 KB Output is correct
8 Correct 3 ms 6192 KB Output is correct
9 Correct 4 ms 6228 KB Output is correct
10 Correct 4 ms 6228 KB Output is correct
11 Correct 3 ms 6228 KB Output is correct
12 Correct 4 ms 6224 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6228 KB Output is correct
15 Correct 4 ms 6200 KB Output is correct
16 Correct 3 ms 6188 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 3 ms 6228 KB Output is correct
19 Correct 3 ms 6228 KB Output is correct
20 Correct 4 ms 6200 KB Output is correct
21 Correct 3 ms 6204 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 3 ms 6228 KB Output is correct
24 Correct 4 ms 6228 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 4 ms 6228 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 3 ms 6228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 3 ms 6228 KB Output is correct
5 Correct 4 ms 6192 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6228 KB Output is correct
8 Correct 3 ms 6192 KB Output is correct
9 Correct 4 ms 6228 KB Output is correct
10 Correct 4 ms 6228 KB Output is correct
11 Correct 3 ms 6228 KB Output is correct
12 Correct 4 ms 6224 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6228 KB Output is correct
15 Correct 4 ms 6200 KB Output is correct
16 Correct 3 ms 6188 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 3 ms 6228 KB Output is correct
19 Correct 3 ms 6228 KB Output is correct
20 Correct 4 ms 6200 KB Output is correct
21 Correct 3 ms 6204 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 3 ms 6228 KB Output is correct
24 Correct 4 ms 6228 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 4 ms 6228 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 3 ms 6228 KB Output is correct
30 Correct 5 ms 6468 KB Output is correct
31 Correct 5 ms 6484 KB Output is correct
32 Correct 6 ms 6652 KB Output is correct
33 Correct 6 ms 6612 KB Output is correct
34 Correct 5 ms 6612 KB Output is correct
35 Correct 5 ms 6584 KB Output is correct
36 Correct 5 ms 6596 KB Output is correct
37 Correct 5 ms 6588 KB Output is correct
38 Correct 5 ms 6716 KB Output is correct
39 Correct 5 ms 6780 KB Output is correct
40 Correct 6 ms 6868 KB Output is correct
41 Correct 5 ms 6868 KB Output is correct
42 Correct 5 ms 6868 KB Output is correct
43 Correct 5 ms 6764 KB Output is correct
44 Correct 5 ms 6724 KB Output is correct
45 Correct 6 ms 6612 KB Output is correct
46 Correct 6 ms 6740 KB Output is correct
47 Correct 5 ms 6612 KB Output is correct
48 Correct 5 ms 6596 KB Output is correct
49 Correct 5 ms 6720 KB Output is correct
50 Correct 5 ms 6612 KB Output is correct
51 Correct 5 ms 6720 KB Output is correct
52 Correct 5 ms 6612 KB Output is correct
53 Correct 5 ms 6612 KB Output is correct
54 Correct 6 ms 6612 KB Output is correct
55 Correct 5 ms 6724 KB Output is correct
56 Correct 5 ms 6228 KB Output is correct
57 Correct 6 ms 6612 KB Output is correct
58 Correct 5 ms 6612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 4 ms 6228 KB Output is correct
4 Correct 81 ms 25492 KB Output is correct
5 Correct 79 ms 30876 KB Output is correct
6 Correct 100 ms 34320 KB Output is correct
7 Correct 111 ms 38028 KB Output is correct
8 Correct 119 ms 38080 KB Output is correct
9 Correct 118 ms 38008 KB Output is correct
10 Correct 125 ms 38008 KB Output is correct
11 Correct 120 ms 37980 KB Output is correct
12 Correct 88 ms 37784 KB Output is correct
13 Correct 105 ms 37692 KB Output is correct
14 Correct 88 ms 37728 KB Output is correct
15 Correct 54 ms 34404 KB Output is correct
16 Correct 83 ms 37728 KB Output is correct
17 Correct 69 ms 11068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6100 KB Output is correct
2 Correct 71 ms 16276 KB Output is correct
3 Correct 75 ms 17664 KB Output is correct
4 Correct 80 ms 18724 KB Output is correct
5 Correct 99 ms 24256 KB Output is correct
6 Correct 129 ms 24276 KB Output is correct
7 Correct 115 ms 24212 KB Output is correct
8 Correct 98 ms 24176 KB Output is correct
9 Correct 105 ms 24308 KB Output is correct
10 Correct 101 ms 24204 KB Output is correct
11 Correct 138 ms 24196 KB Output is correct
12 Correct 102 ms 24312 KB Output is correct
13 Correct 130 ms 24604 KB Output is correct
14 Correct 122 ms 25392 KB Output is correct
15 Correct 121 ms 27996 KB Output is correct
16 Correct 110 ms 24580 KB Output is correct
17 Correct 121 ms 24652 KB Output is correct
18 Correct 114 ms 24560 KB Output is correct
19 Correct 79 ms 22720 KB Output is correct
20 Correct 80 ms 22668 KB Output is correct
21 Correct 105 ms 22704 KB Output is correct
22 Correct 81 ms 22708 KB Output is correct
23 Correct 87 ms 22652 KB Output is correct
24 Correct 83 ms 22712 KB Output is correct
25 Correct 79 ms 22632 KB Output is correct
26 Correct 96 ms 22788 KB Output is correct
27 Correct 88 ms 22732 KB Output is correct
28 Correct 81 ms 22696 KB Output is correct
29 Correct 81 ms 22780 KB Output is correct
30 Correct 85 ms 22940 KB Output is correct
31 Correct 112 ms 23120 KB Output is correct
32 Correct 100 ms 23456 KB Output is correct
33 Correct 113 ms 24504 KB Output is correct
34 Correct 110 ms 26520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6100 KB Output is correct
2 Correct 4 ms 6228 KB Output is correct
3 Correct 4 ms 6228 KB Output is correct
4 Correct 141 ms 19996 KB Output is correct
5 Correct 140 ms 20308 KB Output is correct
6 Correct 166 ms 24832 KB Output is correct
7 Correct 179 ms 27236 KB Output is correct
8 Correct 179 ms 27160 KB Output is correct
9 Correct 211 ms 27220 KB Output is correct
10 Correct 184 ms 27184 KB Output is correct
11 Correct 174 ms 27244 KB Output is correct
12 Correct 201 ms 27180 KB Output is correct
13 Correct 200 ms 27212 KB Output is correct
14 Correct 64 ms 11048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Correct 3 ms 6228 KB Output is correct
5 Correct 4 ms 6192 KB Output is correct
6 Correct 3 ms 6228 KB Output is correct
7 Correct 4 ms 6228 KB Output is correct
8 Correct 3 ms 6192 KB Output is correct
9 Correct 4 ms 6228 KB Output is correct
10 Correct 4 ms 6228 KB Output is correct
11 Correct 3 ms 6228 KB Output is correct
12 Correct 4 ms 6224 KB Output is correct
13 Correct 4 ms 6228 KB Output is correct
14 Correct 4 ms 6228 KB Output is correct
15 Correct 4 ms 6200 KB Output is correct
16 Correct 3 ms 6188 KB Output is correct
17 Correct 4 ms 6228 KB Output is correct
18 Correct 3 ms 6228 KB Output is correct
19 Correct 3 ms 6228 KB Output is correct
20 Correct 4 ms 6200 KB Output is correct
21 Correct 3 ms 6204 KB Output is correct
22 Correct 4 ms 6228 KB Output is correct
23 Correct 3 ms 6228 KB Output is correct
24 Correct 4 ms 6228 KB Output is correct
25 Correct 4 ms 6228 KB Output is correct
26 Correct 4 ms 6228 KB Output is correct
27 Correct 4 ms 6196 KB Output is correct
28 Correct 3 ms 6228 KB Output is correct
29 Correct 3 ms 6228 KB Output is correct
30 Correct 5 ms 6468 KB Output is correct
31 Correct 5 ms 6484 KB Output is correct
32 Correct 6 ms 6652 KB Output is correct
33 Correct 6 ms 6612 KB Output is correct
34 Correct 5 ms 6612 KB Output is correct
35 Correct 5 ms 6584 KB Output is correct
36 Correct 5 ms 6596 KB Output is correct
37 Correct 5 ms 6588 KB Output is correct
38 Correct 5 ms 6716 KB Output is correct
39 Correct 5 ms 6780 KB Output is correct
40 Correct 6 ms 6868 KB Output is correct
41 Correct 5 ms 6868 KB Output is correct
42 Correct 5 ms 6868 KB Output is correct
43 Correct 5 ms 6764 KB Output is correct
44 Correct 5 ms 6724 KB Output is correct
45 Correct 6 ms 6612 KB Output is correct
46 Correct 6 ms 6740 KB Output is correct
47 Correct 5 ms 6612 KB Output is correct
48 Correct 5 ms 6596 KB Output is correct
49 Correct 5 ms 6720 KB Output is correct
50 Correct 5 ms 6612 KB Output is correct
51 Correct 5 ms 6720 KB Output is correct
52 Correct 5 ms 6612 KB Output is correct
53 Correct 5 ms 6612 KB Output is correct
54 Correct 6 ms 6612 KB Output is correct
55 Correct 5 ms 6724 KB Output is correct
56 Correct 5 ms 6228 KB Output is correct
57 Correct 6 ms 6612 KB Output is correct
58 Correct 5 ms 6612 KB Output is correct
59 Correct 4 ms 6100 KB Output is correct
60 Correct 4 ms 6100 KB Output is correct
61 Correct 4 ms 6228 KB Output is correct
62 Correct 81 ms 25492 KB Output is correct
63 Correct 79 ms 30876 KB Output is correct
64 Correct 100 ms 34320 KB Output is correct
65 Correct 111 ms 38028 KB Output is correct
66 Correct 119 ms 38080 KB Output is correct
67 Correct 118 ms 38008 KB Output is correct
68 Correct 125 ms 38008 KB Output is correct
69 Correct 120 ms 37980 KB Output is correct
70 Correct 88 ms 37784 KB Output is correct
71 Correct 105 ms 37692 KB Output is correct
72 Correct 88 ms 37728 KB Output is correct
73 Correct 54 ms 34404 KB Output is correct
74 Correct 83 ms 37728 KB Output is correct
75 Correct 69 ms 11068 KB Output is correct
76 Correct 4 ms 6100 KB Output is correct
77 Correct 71 ms 16276 KB Output is correct
78 Correct 75 ms 17664 KB Output is correct
79 Correct 80 ms 18724 KB Output is correct
80 Correct 99 ms 24256 KB Output is correct
81 Correct 129 ms 24276 KB Output is correct
82 Correct 115 ms 24212 KB Output is correct
83 Correct 98 ms 24176 KB Output is correct
84 Correct 105 ms 24308 KB Output is correct
85 Correct 101 ms 24204 KB Output is correct
86 Correct 138 ms 24196 KB Output is correct
87 Correct 102 ms 24312 KB Output is correct
88 Correct 130 ms 24604 KB Output is correct
89 Correct 122 ms 25392 KB Output is correct
90 Correct 121 ms 27996 KB Output is correct
91 Correct 110 ms 24580 KB Output is correct
92 Correct 121 ms 24652 KB Output is correct
93 Correct 114 ms 24560 KB Output is correct
94 Correct 79 ms 22720 KB Output is correct
95 Correct 80 ms 22668 KB Output is correct
96 Correct 105 ms 22704 KB Output is correct
97 Correct 81 ms 22708 KB Output is correct
98 Correct 87 ms 22652 KB Output is correct
99 Correct 83 ms 22712 KB Output is correct
100 Correct 79 ms 22632 KB Output is correct
101 Correct 96 ms 22788 KB Output is correct
102 Correct 88 ms 22732 KB Output is correct
103 Correct 81 ms 22696 KB Output is correct
104 Correct 81 ms 22780 KB Output is correct
105 Correct 85 ms 22940 KB Output is correct
106 Correct 112 ms 23120 KB Output is correct
107 Correct 100 ms 23456 KB Output is correct
108 Correct 113 ms 24504 KB Output is correct
109 Correct 110 ms 26520 KB Output is correct
110 Correct 4 ms 6100 KB Output is correct
111 Correct 4 ms 6228 KB Output is correct
112 Correct 4 ms 6228 KB Output is correct
113 Correct 141 ms 19996 KB Output is correct
114 Correct 140 ms 20308 KB Output is correct
115 Correct 166 ms 24832 KB Output is correct
116 Correct 179 ms 27236 KB Output is correct
117 Correct 179 ms 27160 KB Output is correct
118 Correct 211 ms 27220 KB Output is correct
119 Correct 184 ms 27184 KB Output is correct
120 Correct 174 ms 27244 KB Output is correct
121 Correct 201 ms 27180 KB Output is correct
122 Correct 200 ms 27212 KB Output is correct
123 Correct 64 ms 11048 KB Output is correct
124 Correct 188 ms 26904 KB Output is correct
125 Correct 129 ms 24912 KB Output is correct
126 Correct 165 ms 27356 KB Output is correct
127 Correct 192 ms 27400 KB Output is correct
128 Correct 229 ms 27416 KB Output is correct
129 Correct 165 ms 27408 KB Output is correct
130 Correct 191 ms 27364 KB Output is correct
131 Correct 169 ms 36000 KB Output is correct
132 Correct 146 ms 37796 KB Output is correct
133 Correct 163 ms 31532 KB Output is correct
134 Correct 198 ms 25932 KB Output is correct
135 Correct 146 ms 25864 KB Output is correct
136 Correct 154 ms 25892 KB Output is correct
137 Correct 144 ms 29760 KB Output is correct
138 Correct 138 ms 29796 KB Output is correct
139 Correct 130 ms 29752 KB Output is correct
140 Correct 138 ms 29752 KB Output is correct
141 Correct 140 ms 29668 KB Output is correct
142 Correct 132 ms 29784 KB Output is correct
143 Correct 64 ms 24004 KB Output is correct
144 Correct 132 ms 26984 KB Output is correct