Submission #798431

# Submission time Handle Problem Language Result Execution time Memory
798431 2023-07-30T17:19:05 Z hugo_pm Wild Boar (JOI18_wild_boar) C++17
100 / 100
16003 ms 686160 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 100940 KB Output is correct
2 Correct 43 ms 100936 KB Output is correct
3 Correct 47 ms 101116 KB Output is correct
4 Correct 49 ms 101008 KB Output is correct
5 Correct 43 ms 100940 KB Output is correct
6 Correct 43 ms 101000 KB Output is correct
7 Correct 45 ms 100904 KB Output is correct
8 Correct 42 ms 100912 KB Output is correct
9 Correct 44 ms 100916 KB Output is correct
10 Correct 42 ms 100884 KB Output is correct
11 Correct 43 ms 101004 KB Output is correct
12 Correct 46 ms 101052 KB Output is correct
13 Correct 52 ms 100904 KB Output is correct
14 Correct 43 ms 101008 KB Output is correct
15 Correct 43 ms 100908 KB Output is correct
16 Correct 44 ms 100880 KB Output is correct
17 Correct 45 ms 100988 KB Output is correct
18 Correct 44 ms 100912 KB Output is correct
19 Correct 43 ms 100964 KB Output is correct
20 Correct 49 ms 100976 KB Output is correct
21 Correct 43 ms 100928 KB Output is correct
22 Correct 43 ms 101004 KB Output is correct
23 Correct 43 ms 100940 KB Output is correct
24 Correct 43 ms 100944 KB Output is correct
25 Correct 43 ms 101004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100940 KB Output is correct
2 Correct 43 ms 100936 KB Output is correct
3 Correct 47 ms 101116 KB Output is correct
4 Correct 49 ms 101008 KB Output is correct
5 Correct 43 ms 100940 KB Output is correct
6 Correct 43 ms 101000 KB Output is correct
7 Correct 45 ms 100904 KB Output is correct
8 Correct 42 ms 100912 KB Output is correct
9 Correct 44 ms 100916 KB Output is correct
10 Correct 42 ms 100884 KB Output is correct
11 Correct 43 ms 101004 KB Output is correct
12 Correct 46 ms 101052 KB Output is correct
13 Correct 52 ms 100904 KB Output is correct
14 Correct 43 ms 101008 KB Output is correct
15 Correct 43 ms 100908 KB Output is correct
16 Correct 44 ms 100880 KB Output is correct
17 Correct 45 ms 100988 KB Output is correct
18 Correct 44 ms 100912 KB Output is correct
19 Correct 43 ms 100964 KB Output is correct
20 Correct 49 ms 100976 KB Output is correct
21 Correct 43 ms 100928 KB Output is correct
22 Correct 43 ms 101004 KB Output is correct
23 Correct 43 ms 100940 KB Output is correct
24 Correct 43 ms 100944 KB Output is correct
25 Correct 43 ms 101004 KB Output is correct
26 Correct 46 ms 101264 KB Output is correct
27 Correct 59 ms 107620 KB Output is correct
28 Correct 58 ms 106780 KB Output is correct
29 Correct 442 ms 167704 KB Output is correct
30 Correct 473 ms 168276 KB Output is correct
31 Correct 373 ms 168596 KB Output is correct
32 Correct 377 ms 168740 KB Output is correct
33 Correct 440 ms 165016 KB Output is correct
34 Correct 427 ms 165712 KB Output is correct
35 Correct 359 ms 171672 KB Output is correct
36 Correct 408 ms 171212 KB Output is correct
37 Correct 441 ms 168332 KB Output is correct
38 Correct 357 ms 157000 KB Output is correct
39 Correct 442 ms 172904 KB Output is correct
40 Correct 380 ms 157256 KB Output is correct
41 Correct 362 ms 157116 KB Output is correct
42 Correct 343 ms 174156 KB Output is correct
43 Correct 288 ms 147660 KB Output is correct
44 Correct 300 ms 147252 KB Output is correct
45 Correct 110 ms 121928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100940 KB Output is correct
2 Correct 43 ms 100936 KB Output is correct
3 Correct 47 ms 101116 KB Output is correct
4 Correct 49 ms 101008 KB Output is correct
5 Correct 43 ms 100940 KB Output is correct
6 Correct 43 ms 101000 KB Output is correct
7 Correct 45 ms 100904 KB Output is correct
8 Correct 42 ms 100912 KB Output is correct
9 Correct 44 ms 100916 KB Output is correct
10 Correct 42 ms 100884 KB Output is correct
11 Correct 43 ms 101004 KB Output is correct
12 Correct 46 ms 101052 KB Output is correct
13 Correct 52 ms 100904 KB Output is correct
14 Correct 43 ms 101008 KB Output is correct
15 Correct 43 ms 100908 KB Output is correct
16 Correct 44 ms 100880 KB Output is correct
17 Correct 45 ms 100988 KB Output is correct
18 Correct 44 ms 100912 KB Output is correct
19 Correct 43 ms 100964 KB Output is correct
20 Correct 49 ms 100976 KB Output is correct
21 Correct 43 ms 100928 KB Output is correct
22 Correct 43 ms 101004 KB Output is correct
23 Correct 43 ms 100940 KB Output is correct
24 Correct 43 ms 100944 KB Output is correct
25 Correct 43 ms 101004 KB Output is correct
26 Correct 46 ms 101264 KB Output is correct
27 Correct 59 ms 107620 KB Output is correct
28 Correct 58 ms 106780 KB Output is correct
29 Correct 442 ms 167704 KB Output is correct
30 Correct 473 ms 168276 KB Output is correct
31 Correct 373 ms 168596 KB Output is correct
32 Correct 377 ms 168740 KB Output is correct
33 Correct 440 ms 165016 KB Output is correct
34 Correct 427 ms 165712 KB Output is correct
35 Correct 359 ms 171672 KB Output is correct
36 Correct 408 ms 171212 KB Output is correct
37 Correct 441 ms 168332 KB Output is correct
38 Correct 357 ms 157000 KB Output is correct
39 Correct 442 ms 172904 KB Output is correct
40 Correct 380 ms 157256 KB Output is correct
41 Correct 362 ms 157116 KB Output is correct
42 Correct 343 ms 174156 KB Output is correct
43 Correct 288 ms 147660 KB Output is correct
44 Correct 300 ms 147252 KB Output is correct
45 Correct 110 ms 121928 KB Output is correct
46 Correct 376 ms 119224 KB Output is correct
47 Correct 3696 ms 284472 KB Output is correct
48 Correct 5006 ms 377924 KB Output is correct
49 Correct 5676 ms 471748 KB Output is correct
50 Correct 5833 ms 479148 KB Output is correct
51 Correct 5955 ms 486392 KB Output is correct
52 Correct 7192 ms 533656 KB Output is correct
53 Correct 7193 ms 535340 KB Output is correct
54 Correct 7212 ms 531180 KB Output is correct
55 Correct 6885 ms 528220 KB Output is correct
56 Correct 7340 ms 572176 KB Output is correct
57 Correct 7613 ms 618956 KB Output is correct
58 Correct 7482 ms 637940 KB Output is correct
59 Correct 7442 ms 665572 KB Output is correct
60 Correct 7087 ms 674580 KB Output is correct
61 Correct 6699 ms 675392 KB Output is correct
62 Correct 6310 ms 666588 KB Output is correct
63 Correct 5845 ms 645024 KB Output is correct
64 Correct 1823 ms 396856 KB Output is correct
65 Correct 1822 ms 396800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 100940 KB Output is correct
2 Correct 43 ms 100936 KB Output is correct
3 Correct 47 ms 101116 KB Output is correct
4 Correct 49 ms 101008 KB Output is correct
5 Correct 43 ms 100940 KB Output is correct
6 Correct 43 ms 101000 KB Output is correct
7 Correct 45 ms 100904 KB Output is correct
8 Correct 42 ms 100912 KB Output is correct
9 Correct 44 ms 100916 KB Output is correct
10 Correct 42 ms 100884 KB Output is correct
11 Correct 43 ms 101004 KB Output is correct
12 Correct 46 ms 101052 KB Output is correct
13 Correct 52 ms 100904 KB Output is correct
14 Correct 43 ms 101008 KB Output is correct
15 Correct 43 ms 100908 KB Output is correct
16 Correct 44 ms 100880 KB Output is correct
17 Correct 45 ms 100988 KB Output is correct
18 Correct 44 ms 100912 KB Output is correct
19 Correct 43 ms 100964 KB Output is correct
20 Correct 49 ms 100976 KB Output is correct
21 Correct 43 ms 100928 KB Output is correct
22 Correct 43 ms 101004 KB Output is correct
23 Correct 43 ms 100940 KB Output is correct
24 Correct 43 ms 100944 KB Output is correct
25 Correct 43 ms 101004 KB Output is correct
26 Correct 46 ms 101264 KB Output is correct
27 Correct 59 ms 107620 KB Output is correct
28 Correct 58 ms 106780 KB Output is correct
29 Correct 442 ms 167704 KB Output is correct
30 Correct 473 ms 168276 KB Output is correct
31 Correct 373 ms 168596 KB Output is correct
32 Correct 377 ms 168740 KB Output is correct
33 Correct 440 ms 165016 KB Output is correct
34 Correct 427 ms 165712 KB Output is correct
35 Correct 359 ms 171672 KB Output is correct
36 Correct 408 ms 171212 KB Output is correct
37 Correct 441 ms 168332 KB Output is correct
38 Correct 357 ms 157000 KB Output is correct
39 Correct 442 ms 172904 KB Output is correct
40 Correct 380 ms 157256 KB Output is correct
41 Correct 362 ms 157116 KB Output is correct
42 Correct 343 ms 174156 KB Output is correct
43 Correct 288 ms 147660 KB Output is correct
44 Correct 300 ms 147252 KB Output is correct
45 Correct 110 ms 121928 KB Output is correct
46 Correct 376 ms 119224 KB Output is correct
47 Correct 3696 ms 284472 KB Output is correct
48 Correct 5006 ms 377924 KB Output is correct
49 Correct 5676 ms 471748 KB Output is correct
50 Correct 5833 ms 479148 KB Output is correct
51 Correct 5955 ms 486392 KB Output is correct
52 Correct 7192 ms 533656 KB Output is correct
53 Correct 7193 ms 535340 KB Output is correct
54 Correct 7212 ms 531180 KB Output is correct
55 Correct 6885 ms 528220 KB Output is correct
56 Correct 7340 ms 572176 KB Output is correct
57 Correct 7613 ms 618956 KB Output is correct
58 Correct 7482 ms 637940 KB Output is correct
59 Correct 7442 ms 665572 KB Output is correct
60 Correct 7087 ms 674580 KB Output is correct
61 Correct 6699 ms 675392 KB Output is correct
62 Correct 6310 ms 666588 KB Output is correct
63 Correct 5845 ms 645024 KB Output is correct
64 Correct 1823 ms 396856 KB Output is correct
65 Correct 1822 ms 396800 KB Output is correct
66 Correct 291 ms 153796 KB Output is correct
67 Correct 425 ms 147652 KB Output is correct
68 Correct 1036 ms 264472 KB Output is correct
69 Correct 1230 ms 268616 KB Output is correct
70 Correct 1243 ms 272652 KB Output is correct
71 Correct 12837 ms 288548 KB Output is correct
72 Correct 14813 ms 414784 KB Output is correct
73 Correct 14104 ms 534604 KB Output is correct
74 Correct 13849 ms 544924 KB Output is correct
75 Correct 14027 ms 535932 KB Output is correct
76 Correct 15500 ms 470244 KB Output is correct
77 Correct 16003 ms 490204 KB Output is correct
78 Correct 11174 ms 505788 KB Output is correct
79 Correct 13386 ms 624812 KB Output is correct
80 Correct 12776 ms 659388 KB Output is correct
81 Correct 15157 ms 576676 KB Output is correct
82 Correct 10967 ms 686160 KB Output is correct
83 Correct 15611 ms 603664 KB Output is correct
84 Correct 8357 ms 656520 KB Output is correct
85 Correct 2867 ms 399644 KB Output is correct