Submission #798434

# Submission time Handle Problem Language Result Execution time Memory
798434 2023-07-30T17:25:12 Z peijar Wild Boar (JOI18_wild_boar) C++17
100 / 100
15935 ms 684700 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
 
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
 
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
 
using pii = pair<int, int>;
using vi = vector<int>;
 
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}
 
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
 
const int MAX_N = 2005, MAX_ARRET = 1 << 17;
int nbNode, nbEdge, nbReq, nbArret;
vector<int> adj[MAX_N];
int xorNode[MAX_N];
int poids[MAX_N];
int arrets[MAX_ARRET];
int vus[MAX_N][MAX_N];
int VuTime = 0;
 
struct Chemin {
	int len, fst, lst;
	bool operator<(const Chemin &oth) const {
		return tie(len, fst, lst) < tie(oth.len, oth.fst, oth.lst);
	}
	bool operator==(const Chemin &oth) const {
		return tie(len, fst, lst) == tie(oth.len, oth.fst, oth.lst);
	}
};
 
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
 
string to_string(const Chemin c) {
	return "(len=" + to_string(c.len) + ", fst=" + to_string(c.fst) + ", lst=" + to_string(c.lst) + ")";
}
 
int cntFirst[MAX_N + 1], cntLast[MAX_N + 1];
vector<Chemin> compressTab (vector<Chemin> &chemins) {
	#ifdef DEBUG
	assert(is_sorted(all(chemins)));
	#endif
	VuTime++;
	vector<Chemin> ret;
	for (Chemin chem : chemins) {
		if (ret.size() > 8)
			break;
		dbg(chem);
		if (cntFirst[1+chem.fst] == 3 or cntLast[1+chem.lst] == 3 or vus[1+chem.fst][1+chem.lst] == VuTime)
			continue;
		ret.push_back(chem);
		cntFirst[1+chem.fst]++;
		cntLast[1+chem.lst]++;
		vus[1+chem.fst][1+chem.lst] = VuTime;
	}
	for (Chemin chem : ret) {
		cntFirst[1+chem.fst]--;
		cntLast[1+chem.lst]--;
	}
	return ret;
};
 
/// i-eme feuille = candidats pour X[i] -> X[i+1]
vector<Chemin> tree[2*MAX_ARRET];
const int sizeTree = MAX_ARRET;
vector<Chemin> merge(const vector<Chemin> &v1, const vector<Chemin> &v2) {
	vector<Chemin> nv;
	for (const Chemin &c1 : v1) for (const Chemin &c2 : v2) {
		if (c1.lst != c2.fst) {
			int newFst = (c1.fst == -1 ? c2.fst : c1.fst);
			nv.push_back({c1.len + c2.len, newFst, c2.lst});
		}
	}
	sort(all(nv));
	return compressTab(nv);
}
 
int leftRightLeaf(int idx) {
	if (idx < sizeTree) idx = 2*idx+1;
	while (idx < sizeTree) idx *= 2;
	return idx - sizeTree;
}
void _recalc(int idx) {
	if (leftRightLeaf(idx) >= nbArret-1) { // todo check
		tree[idx] = tree[2*idx];
	} else {
		tree[idx] = merge(tree[2*idx], tree[2*idx+1]);
	}
}
void buildTree() {
	for (int node = sizeTree-1; node >= 1; --node) {
		_recalc(node);
	}
}
 
void update(int idx, vector<Chemin> v) {
	idx += sizeTree;
	tree[idx] = v;
	while (idx > 1) {
		idx /= 2;
		_recalc(idx);
	}
}
 
vector<Chemin> matrice[MAX_N][MAX_N];
void buildMatrice() {
	auto interesting = [&] (const Chemin &add, vector<Chemin> &V) {
		V.insert(upper_bound(all(V), add), add);
		V = compressTab(V);
		return (find(all(V), add) != end(V));
	};
	rep(depart, 0, nbNode) {
		auto& cand = matrice[depart];
		cand[depart].push_back({0, -1, -1});
		minpq<pair<Chemin, int>> pq;
		pq.push({cand[depart].back(), depart});
		vector<int> nbVus(nbNode);
		while (!pq.empty()) {
			auto [oldChemin, node] = pq.top();
			pq.pop();
			nbVus[node]++;
			// oldChemin.len == dist[node]
			dbg(depart, node);
			if (find(all(cand[node]), oldChemin) == end(cand[node])) {
				continue;
			}
			for (int iEdge : adj[node]) {
				if (iEdge == oldChemin.lst) continue;
				int voisin = xorNode[iEdge]^node;
				Chemin nouvChemin {
					oldChemin.len + poids[iEdge],
					(oldChemin.fst == -1 ? iEdge : oldChemin.fst),
					iEdge
				};
				if (interesting(nouvChemin, cand[voisin])) {
					dbg(voisin, cand[voisin], nouvChemin);
					pq.push({nouvChemin, voisin});
				}
			}
		}
		rep(node, 0, nbNode) {
			cand[node] = compressTab(cand[node]);
			assert(sz(cand[node]) <= 9);
		}
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> nbNode >> nbEdge >> nbReq >> nbArret;
	rep(iEdge, 0, nbEdge) {
		int u, v; cin >> u >> v;
		--u, --v;
		cin >> poids[iEdge];
		xorNode[iEdge] = u^v;
		adj[u].push_back(iEdge);
		adj[v].push_back(iEdge);
	}
	buildMatrice();
	rep(iArret, 0, nbArret) {
		cin >> arrets[iArret];
		--arrets[iArret];
		if (iArret > 0) {
			tree[sizeTree + iArret-1] = matrice[arrets[iArret-1]][arrets[iArret]];
		}
	}
	buildTree();
	rep(iReq, 0, nbReq) {
		int indice, val;
		cin >> indice >> val;
		--indice, --val;
		arrets[indice] = val;
		if (indice > 0) {
			update(indice-1, matrice[arrets[indice-1]][arrets[indice]]);
		}
		if (indice+1 < nbArret) {
			update(indice, matrice[arrets[indice]][arrets[indice+1]]);
		}
		int ans = -1;
		if (!tree[1].empty()) ans = tree[1].front().len;
		cout << ans << '\n';	
	}
}
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100960 KB Output is correct
2 Correct 45 ms 100976 KB Output is correct
3 Correct 45 ms 100972 KB Output is correct
4 Correct 47 ms 100988 KB Output is correct
5 Correct 45 ms 100892 KB Output is correct
6 Correct 44 ms 100940 KB Output is correct
7 Correct 45 ms 100912 KB Output is correct
8 Correct 45 ms 100940 KB Output is correct
9 Correct 45 ms 100940 KB Output is correct
10 Correct 46 ms 100944 KB Output is correct
11 Correct 46 ms 100948 KB Output is correct
12 Correct 45 ms 101004 KB Output is correct
13 Correct 46 ms 100988 KB Output is correct
14 Correct 50 ms 101028 KB Output is correct
15 Correct 45 ms 100888 KB Output is correct
16 Correct 44 ms 100948 KB Output is correct
17 Correct 45 ms 101020 KB Output is correct
18 Correct 45 ms 100936 KB Output is correct
19 Correct 44 ms 100948 KB Output is correct
20 Correct 46 ms 100864 KB Output is correct
21 Correct 49 ms 101000 KB Output is correct
22 Correct 46 ms 100916 KB Output is correct
23 Correct 46 ms 101000 KB Output is correct
24 Correct 46 ms 100940 KB Output is correct
25 Correct 45 ms 100980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100960 KB Output is correct
2 Correct 45 ms 100976 KB Output is correct
3 Correct 45 ms 100972 KB Output is correct
4 Correct 47 ms 100988 KB Output is correct
5 Correct 45 ms 100892 KB Output is correct
6 Correct 44 ms 100940 KB Output is correct
7 Correct 45 ms 100912 KB Output is correct
8 Correct 45 ms 100940 KB Output is correct
9 Correct 45 ms 100940 KB Output is correct
10 Correct 46 ms 100944 KB Output is correct
11 Correct 46 ms 100948 KB Output is correct
12 Correct 45 ms 101004 KB Output is correct
13 Correct 46 ms 100988 KB Output is correct
14 Correct 50 ms 101028 KB Output is correct
15 Correct 45 ms 100888 KB Output is correct
16 Correct 44 ms 100948 KB Output is correct
17 Correct 45 ms 101020 KB Output is correct
18 Correct 45 ms 100936 KB Output is correct
19 Correct 44 ms 100948 KB Output is correct
20 Correct 46 ms 100864 KB Output is correct
21 Correct 49 ms 101000 KB Output is correct
22 Correct 46 ms 100916 KB Output is correct
23 Correct 46 ms 101000 KB Output is correct
24 Correct 46 ms 100940 KB Output is correct
25 Correct 45 ms 100980 KB Output is correct
26 Correct 47 ms 101332 KB Output is correct
27 Correct 62 ms 107772 KB Output is correct
28 Correct 60 ms 106984 KB Output is correct
29 Correct 432 ms 167804 KB Output is correct
30 Correct 461 ms 168424 KB Output is correct
31 Correct 379 ms 168640 KB Output is correct
32 Correct 388 ms 168756 KB Output is correct
33 Correct 423 ms 165172 KB Output is correct
34 Correct 432 ms 165960 KB Output is correct
35 Correct 350 ms 171848 KB Output is correct
36 Correct 421 ms 171300 KB Output is correct
37 Correct 456 ms 168372 KB Output is correct
38 Correct 381 ms 157228 KB Output is correct
39 Correct 418 ms 172972 KB Output is correct
40 Correct 381 ms 157516 KB Output is correct
41 Correct 379 ms 157340 KB Output is correct
42 Correct 393 ms 174272 KB Output is correct
43 Correct 300 ms 147928 KB Output is correct
44 Correct 297 ms 147548 KB Output is correct
45 Correct 129 ms 122188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100960 KB Output is correct
2 Correct 45 ms 100976 KB Output is correct
3 Correct 45 ms 100972 KB Output is correct
4 Correct 47 ms 100988 KB Output is correct
5 Correct 45 ms 100892 KB Output is correct
6 Correct 44 ms 100940 KB Output is correct
7 Correct 45 ms 100912 KB Output is correct
8 Correct 45 ms 100940 KB Output is correct
9 Correct 45 ms 100940 KB Output is correct
10 Correct 46 ms 100944 KB Output is correct
11 Correct 46 ms 100948 KB Output is correct
12 Correct 45 ms 101004 KB Output is correct
13 Correct 46 ms 100988 KB Output is correct
14 Correct 50 ms 101028 KB Output is correct
15 Correct 45 ms 100888 KB Output is correct
16 Correct 44 ms 100948 KB Output is correct
17 Correct 45 ms 101020 KB Output is correct
18 Correct 45 ms 100936 KB Output is correct
19 Correct 44 ms 100948 KB Output is correct
20 Correct 46 ms 100864 KB Output is correct
21 Correct 49 ms 101000 KB Output is correct
22 Correct 46 ms 100916 KB Output is correct
23 Correct 46 ms 101000 KB Output is correct
24 Correct 46 ms 100940 KB Output is correct
25 Correct 45 ms 100980 KB Output is correct
26 Correct 47 ms 101332 KB Output is correct
27 Correct 62 ms 107772 KB Output is correct
28 Correct 60 ms 106984 KB Output is correct
29 Correct 432 ms 167804 KB Output is correct
30 Correct 461 ms 168424 KB Output is correct
31 Correct 379 ms 168640 KB Output is correct
32 Correct 388 ms 168756 KB Output is correct
33 Correct 423 ms 165172 KB Output is correct
34 Correct 432 ms 165960 KB Output is correct
35 Correct 350 ms 171848 KB Output is correct
36 Correct 421 ms 171300 KB Output is correct
37 Correct 456 ms 168372 KB Output is correct
38 Correct 381 ms 157228 KB Output is correct
39 Correct 418 ms 172972 KB Output is correct
40 Correct 381 ms 157516 KB Output is correct
41 Correct 379 ms 157340 KB Output is correct
42 Correct 393 ms 174272 KB Output is correct
43 Correct 300 ms 147928 KB Output is correct
44 Correct 297 ms 147548 KB Output is correct
45 Correct 129 ms 122188 KB Output is correct
46 Correct 381 ms 119156 KB Output is correct
47 Correct 3559 ms 284564 KB Output is correct
48 Correct 4866 ms 378036 KB Output is correct
49 Correct 5757 ms 471764 KB Output is correct
50 Correct 5708 ms 479192 KB Output is correct
51 Correct 5765 ms 486408 KB Output is correct
52 Correct 7033 ms 533476 KB Output is correct
53 Correct 7084 ms 535316 KB Output is correct
54 Correct 7096 ms 531160 KB Output is correct
55 Correct 7089 ms 528196 KB Output is correct
56 Correct 7309 ms 572156 KB Output is correct
57 Correct 7922 ms 618964 KB Output is correct
58 Correct 7867 ms 637860 KB Output is correct
59 Correct 7589 ms 665380 KB Output is correct
60 Correct 7080 ms 674188 KB Output is correct
61 Correct 6827 ms 674900 KB Output is correct
62 Correct 6496 ms 666164 KB Output is correct
63 Correct 5880 ms 644684 KB Output is correct
64 Correct 1839 ms 396500 KB Output is correct
65 Correct 1860 ms 396640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100960 KB Output is correct
2 Correct 45 ms 100976 KB Output is correct
3 Correct 45 ms 100972 KB Output is correct
4 Correct 47 ms 100988 KB Output is correct
5 Correct 45 ms 100892 KB Output is correct
6 Correct 44 ms 100940 KB Output is correct
7 Correct 45 ms 100912 KB Output is correct
8 Correct 45 ms 100940 KB Output is correct
9 Correct 45 ms 100940 KB Output is correct
10 Correct 46 ms 100944 KB Output is correct
11 Correct 46 ms 100948 KB Output is correct
12 Correct 45 ms 101004 KB Output is correct
13 Correct 46 ms 100988 KB Output is correct
14 Correct 50 ms 101028 KB Output is correct
15 Correct 45 ms 100888 KB Output is correct
16 Correct 44 ms 100948 KB Output is correct
17 Correct 45 ms 101020 KB Output is correct
18 Correct 45 ms 100936 KB Output is correct
19 Correct 44 ms 100948 KB Output is correct
20 Correct 46 ms 100864 KB Output is correct
21 Correct 49 ms 101000 KB Output is correct
22 Correct 46 ms 100916 KB Output is correct
23 Correct 46 ms 101000 KB Output is correct
24 Correct 46 ms 100940 KB Output is correct
25 Correct 45 ms 100980 KB Output is correct
26 Correct 47 ms 101332 KB Output is correct
27 Correct 62 ms 107772 KB Output is correct
28 Correct 60 ms 106984 KB Output is correct
29 Correct 432 ms 167804 KB Output is correct
30 Correct 461 ms 168424 KB Output is correct
31 Correct 379 ms 168640 KB Output is correct
32 Correct 388 ms 168756 KB Output is correct
33 Correct 423 ms 165172 KB Output is correct
34 Correct 432 ms 165960 KB Output is correct
35 Correct 350 ms 171848 KB Output is correct
36 Correct 421 ms 171300 KB Output is correct
37 Correct 456 ms 168372 KB Output is correct
38 Correct 381 ms 157228 KB Output is correct
39 Correct 418 ms 172972 KB Output is correct
40 Correct 381 ms 157516 KB Output is correct
41 Correct 379 ms 157340 KB Output is correct
42 Correct 393 ms 174272 KB Output is correct
43 Correct 300 ms 147928 KB Output is correct
44 Correct 297 ms 147548 KB Output is correct
45 Correct 129 ms 122188 KB Output is correct
46 Correct 381 ms 119156 KB Output is correct
47 Correct 3559 ms 284564 KB Output is correct
48 Correct 4866 ms 378036 KB Output is correct
49 Correct 5757 ms 471764 KB Output is correct
50 Correct 5708 ms 479192 KB Output is correct
51 Correct 5765 ms 486408 KB Output is correct
52 Correct 7033 ms 533476 KB Output is correct
53 Correct 7084 ms 535316 KB Output is correct
54 Correct 7096 ms 531160 KB Output is correct
55 Correct 7089 ms 528196 KB Output is correct
56 Correct 7309 ms 572156 KB Output is correct
57 Correct 7922 ms 618964 KB Output is correct
58 Correct 7867 ms 637860 KB Output is correct
59 Correct 7589 ms 665380 KB Output is correct
60 Correct 7080 ms 674188 KB Output is correct
61 Correct 6827 ms 674900 KB Output is correct
62 Correct 6496 ms 666164 KB Output is correct
63 Correct 5880 ms 644684 KB Output is correct
64 Correct 1839 ms 396500 KB Output is correct
65 Correct 1860 ms 396640 KB Output is correct
66 Correct 304 ms 153736 KB Output is correct
67 Correct 433 ms 147068 KB Output is correct
68 Correct 1062 ms 264428 KB Output is correct
69 Correct 1184 ms 268036 KB Output is correct
70 Correct 1266 ms 271184 KB Output is correct
71 Correct 13007 ms 287592 KB Output is correct
72 Correct 15042 ms 414252 KB Output is correct
73 Correct 14258 ms 533424 KB Output is correct
74 Correct 13967 ms 543932 KB Output is correct
75 Correct 14211 ms 534880 KB Output is correct
76 Correct 15063 ms 469576 KB Output is correct
77 Correct 15935 ms 489668 KB Output is correct
78 Correct 10994 ms 505300 KB Output is correct
79 Correct 13447 ms 623864 KB Output is correct
80 Correct 13000 ms 658124 KB Output is correct
81 Correct 15001 ms 576104 KB Output is correct
82 Correct 10685 ms 684700 KB Output is correct
83 Correct 15551 ms 602692 KB Output is correct
84 Correct 8154 ms 655024 KB Output is correct
85 Correct 2848 ms 398308 KB Output is correct