답안 #798433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798433 2023-07-30T17:22:38 Z hugo_pm Wild Boar (JOI18_wild_boar) C++17
100 / 100
15781 ms 685616 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;
	int lvl = 0;
	while (idx > 1) {
		idx /= 2;
		if (((2*idx+1) << lvl) - sizeTree >= nbArret-1) { // todo check
			tree[idx] = tree[2*idx];
		} else {
			tree[idx] = merge(tree[2*idx], tree[2*idx+1]);
		}
		++lvl;
	}
}

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';	
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 100928 KB Output is correct
2 Correct 49 ms 100892 KB Output is correct
3 Correct 49 ms 100936 KB Output is correct
4 Correct 49 ms 100988 KB Output is correct
5 Correct 50 ms 100956 KB Output is correct
6 Correct 49 ms 100940 KB Output is correct
7 Correct 57 ms 100896 KB Output is correct
8 Correct 48 ms 101008 KB Output is correct
9 Correct 49 ms 100920 KB Output is correct
10 Correct 47 ms 100940 KB Output is correct
11 Correct 48 ms 100924 KB Output is correct
12 Correct 53 ms 100976 KB Output is correct
13 Correct 49 ms 100916 KB Output is correct
14 Correct 48 ms 100948 KB Output is correct
15 Correct 57 ms 100996 KB Output is correct
16 Correct 49 ms 101000 KB Output is correct
17 Correct 51 ms 100916 KB Output is correct
18 Correct 49 ms 100940 KB Output is correct
19 Correct 56 ms 100916 KB Output is correct
20 Correct 50 ms 100932 KB Output is correct
21 Correct 49 ms 100912 KB Output is correct
22 Correct 50 ms 100932 KB Output is correct
23 Correct 56 ms 101004 KB Output is correct
24 Correct 48 ms 100916 KB Output is correct
25 Correct 49 ms 101004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 100928 KB Output is correct
2 Correct 49 ms 100892 KB Output is correct
3 Correct 49 ms 100936 KB Output is correct
4 Correct 49 ms 100988 KB Output is correct
5 Correct 50 ms 100956 KB Output is correct
6 Correct 49 ms 100940 KB Output is correct
7 Correct 57 ms 100896 KB Output is correct
8 Correct 48 ms 101008 KB Output is correct
9 Correct 49 ms 100920 KB Output is correct
10 Correct 47 ms 100940 KB Output is correct
11 Correct 48 ms 100924 KB Output is correct
12 Correct 53 ms 100976 KB Output is correct
13 Correct 49 ms 100916 KB Output is correct
14 Correct 48 ms 100948 KB Output is correct
15 Correct 57 ms 100996 KB Output is correct
16 Correct 49 ms 101000 KB Output is correct
17 Correct 51 ms 100916 KB Output is correct
18 Correct 49 ms 100940 KB Output is correct
19 Correct 56 ms 100916 KB Output is correct
20 Correct 50 ms 100932 KB Output is correct
21 Correct 49 ms 100912 KB Output is correct
22 Correct 50 ms 100932 KB Output is correct
23 Correct 56 ms 101004 KB Output is correct
24 Correct 48 ms 100916 KB Output is correct
25 Correct 49 ms 101004 KB Output is correct
26 Correct 51 ms 101256 KB Output is correct
27 Correct 71 ms 107744 KB Output is correct
28 Correct 63 ms 106952 KB Output is correct
29 Correct 439 ms 167792 KB Output is correct
30 Correct 447 ms 168408 KB Output is correct
31 Correct 378 ms 168600 KB Output is correct
32 Correct 373 ms 168832 KB Output is correct
33 Correct 437 ms 165168 KB Output is correct
34 Correct 428 ms 165888 KB Output is correct
35 Correct 369 ms 171892 KB Output is correct
36 Correct 418 ms 171276 KB Output is correct
37 Correct 469 ms 168420 KB Output is correct
38 Correct 365 ms 157304 KB Output is correct
39 Correct 441 ms 173144 KB Output is correct
40 Correct 368 ms 157440 KB Output is correct
41 Correct 366 ms 157312 KB Output is correct
42 Correct 349 ms 174228 KB Output is correct
43 Correct 291 ms 147952 KB Output is correct
44 Correct 289 ms 147548 KB Output is correct
45 Correct 112 ms 122152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 100928 KB Output is correct
2 Correct 49 ms 100892 KB Output is correct
3 Correct 49 ms 100936 KB Output is correct
4 Correct 49 ms 100988 KB Output is correct
5 Correct 50 ms 100956 KB Output is correct
6 Correct 49 ms 100940 KB Output is correct
7 Correct 57 ms 100896 KB Output is correct
8 Correct 48 ms 101008 KB Output is correct
9 Correct 49 ms 100920 KB Output is correct
10 Correct 47 ms 100940 KB Output is correct
11 Correct 48 ms 100924 KB Output is correct
12 Correct 53 ms 100976 KB Output is correct
13 Correct 49 ms 100916 KB Output is correct
14 Correct 48 ms 100948 KB Output is correct
15 Correct 57 ms 100996 KB Output is correct
16 Correct 49 ms 101000 KB Output is correct
17 Correct 51 ms 100916 KB Output is correct
18 Correct 49 ms 100940 KB Output is correct
19 Correct 56 ms 100916 KB Output is correct
20 Correct 50 ms 100932 KB Output is correct
21 Correct 49 ms 100912 KB Output is correct
22 Correct 50 ms 100932 KB Output is correct
23 Correct 56 ms 101004 KB Output is correct
24 Correct 48 ms 100916 KB Output is correct
25 Correct 49 ms 101004 KB Output is correct
26 Correct 51 ms 101256 KB Output is correct
27 Correct 71 ms 107744 KB Output is correct
28 Correct 63 ms 106952 KB Output is correct
29 Correct 439 ms 167792 KB Output is correct
30 Correct 447 ms 168408 KB Output is correct
31 Correct 378 ms 168600 KB Output is correct
32 Correct 373 ms 168832 KB Output is correct
33 Correct 437 ms 165168 KB Output is correct
34 Correct 428 ms 165888 KB Output is correct
35 Correct 369 ms 171892 KB Output is correct
36 Correct 418 ms 171276 KB Output is correct
37 Correct 469 ms 168420 KB Output is correct
38 Correct 365 ms 157304 KB Output is correct
39 Correct 441 ms 173144 KB Output is correct
40 Correct 368 ms 157440 KB Output is correct
41 Correct 366 ms 157312 KB Output is correct
42 Correct 349 ms 174228 KB Output is correct
43 Correct 291 ms 147952 KB Output is correct
44 Correct 289 ms 147548 KB Output is correct
45 Correct 112 ms 122152 KB Output is correct
46 Correct 376 ms 119192 KB Output is correct
47 Correct 3514 ms 284604 KB Output is correct
48 Correct 4827 ms 377996 KB Output is correct
49 Correct 5674 ms 471760 KB Output is correct
50 Correct 5622 ms 479064 KB Output is correct
51 Correct 5729 ms 486476 KB Output is correct
52 Correct 7062 ms 533488 KB Output is correct
53 Correct 7054 ms 535232 KB Output is correct
54 Correct 7004 ms 531244 KB Output is correct
55 Correct 6945 ms 528208 KB Output is correct
56 Correct 7221 ms 572068 KB Output is correct
57 Correct 7843 ms 619064 KB Output is correct
58 Correct 7397 ms 637832 KB Output is correct
59 Correct 7411 ms 665596 KB Output is correct
60 Correct 7001 ms 674516 KB Output is correct
61 Correct 6676 ms 675272 KB Output is correct
62 Correct 6471 ms 666452 KB Output is correct
63 Correct 5807 ms 644992 KB Output is correct
64 Correct 1835 ms 396864 KB Output is correct
65 Correct 1835 ms 396936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 100928 KB Output is correct
2 Correct 49 ms 100892 KB Output is correct
3 Correct 49 ms 100936 KB Output is correct
4 Correct 49 ms 100988 KB Output is correct
5 Correct 50 ms 100956 KB Output is correct
6 Correct 49 ms 100940 KB Output is correct
7 Correct 57 ms 100896 KB Output is correct
8 Correct 48 ms 101008 KB Output is correct
9 Correct 49 ms 100920 KB Output is correct
10 Correct 47 ms 100940 KB Output is correct
11 Correct 48 ms 100924 KB Output is correct
12 Correct 53 ms 100976 KB Output is correct
13 Correct 49 ms 100916 KB Output is correct
14 Correct 48 ms 100948 KB Output is correct
15 Correct 57 ms 100996 KB Output is correct
16 Correct 49 ms 101000 KB Output is correct
17 Correct 51 ms 100916 KB Output is correct
18 Correct 49 ms 100940 KB Output is correct
19 Correct 56 ms 100916 KB Output is correct
20 Correct 50 ms 100932 KB Output is correct
21 Correct 49 ms 100912 KB Output is correct
22 Correct 50 ms 100932 KB Output is correct
23 Correct 56 ms 101004 KB Output is correct
24 Correct 48 ms 100916 KB Output is correct
25 Correct 49 ms 101004 KB Output is correct
26 Correct 51 ms 101256 KB Output is correct
27 Correct 71 ms 107744 KB Output is correct
28 Correct 63 ms 106952 KB Output is correct
29 Correct 439 ms 167792 KB Output is correct
30 Correct 447 ms 168408 KB Output is correct
31 Correct 378 ms 168600 KB Output is correct
32 Correct 373 ms 168832 KB Output is correct
33 Correct 437 ms 165168 KB Output is correct
34 Correct 428 ms 165888 KB Output is correct
35 Correct 369 ms 171892 KB Output is correct
36 Correct 418 ms 171276 KB Output is correct
37 Correct 469 ms 168420 KB Output is correct
38 Correct 365 ms 157304 KB Output is correct
39 Correct 441 ms 173144 KB Output is correct
40 Correct 368 ms 157440 KB Output is correct
41 Correct 366 ms 157312 KB Output is correct
42 Correct 349 ms 174228 KB Output is correct
43 Correct 291 ms 147952 KB Output is correct
44 Correct 289 ms 147548 KB Output is correct
45 Correct 112 ms 122152 KB Output is correct
46 Correct 376 ms 119192 KB Output is correct
47 Correct 3514 ms 284604 KB Output is correct
48 Correct 4827 ms 377996 KB Output is correct
49 Correct 5674 ms 471760 KB Output is correct
50 Correct 5622 ms 479064 KB Output is correct
51 Correct 5729 ms 486476 KB Output is correct
52 Correct 7062 ms 533488 KB Output is correct
53 Correct 7054 ms 535232 KB Output is correct
54 Correct 7004 ms 531244 KB Output is correct
55 Correct 6945 ms 528208 KB Output is correct
56 Correct 7221 ms 572068 KB Output is correct
57 Correct 7843 ms 619064 KB Output is correct
58 Correct 7397 ms 637832 KB Output is correct
59 Correct 7411 ms 665596 KB Output is correct
60 Correct 7001 ms 674516 KB Output is correct
61 Correct 6676 ms 675272 KB Output is correct
62 Correct 6471 ms 666452 KB Output is correct
63 Correct 5807 ms 644992 KB Output is correct
64 Correct 1835 ms 396864 KB Output is correct
65 Correct 1835 ms 396936 KB Output is correct
66 Correct 296 ms 153804 KB Output is correct
67 Correct 407 ms 147620 KB Output is correct
68 Correct 1071 ms 264608 KB Output is correct
69 Correct 1207 ms 268900 KB Output is correct
70 Correct 1264 ms 272456 KB Output is correct
71 Correct 12773 ms 288492 KB Output is correct
72 Correct 15042 ms 414776 KB Output is correct
73 Correct 14026 ms 534508 KB Output is correct
74 Correct 13854 ms 544880 KB Output is correct
75 Correct 13898 ms 535936 KB Output is correct
76 Correct 15033 ms 470252 KB Output is correct
77 Correct 15781 ms 490268 KB Output is correct
78 Correct 11283 ms 505756 KB Output is correct
79 Correct 13216 ms 624568 KB Output is correct
80 Correct 12797 ms 658856 KB Output is correct
81 Correct 15083 ms 576684 KB Output is correct
82 Correct 10782 ms 685616 KB Output is correct
83 Correct 15375 ms 603296 KB Output is correct
84 Correct 8130 ms 655404 KB Output is correct
85 Correct 2741 ms 398464 KB Output is correct