답안 #779362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
779362 2023-07-11T11:12:27 Z ymm Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
991 ms 31700 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 512;

namespace dsu {
	struct edge {
		int v, u, w;
		int nxt;
	} edges[(1<<27)/sizeof(edge)];

	int new_edge() {
		static int nxt = 1;
		return nxt++;
	}

	int st;

	void push(int v, int u, int w) {
		int e = new_edge();
		edges[e].v = v;
		edges[e].u = u;
		edges[e].w = w;
		edges[e].nxt = st;
		st = e;
	}

	int par[N];
	int sz[N];

	int rt(int v) { return par[v] == -1? v: rt(par[v]); }
	bool unite(int v, int u) {
		v = rt(v);
		u = rt(u);
		if (v == u)
			return 0;
		if (sz[v] < sz[u])
			swap(v, u);
		sz[v] += sz[u];
		par[u] = v;
		return 1;
	}

	int Do(int v, int u) {
		fill(par, par+N, -1);
		fill(sz, sz+N, 1);
		for (int *e = &st; *e;) {
			int x = edges[*e].v;
			int y = edges[*e].u;
			if (!unite(x, y)) {
				*e = edges[*e].nxt;
				continue;
			}
			v = rt(v);
			u = rt(u);
			if (v == u)
				return edges[*e].w;
			e = &edges[*e].nxt;
		}
		return -1;
	}
}

vector<pair<int,pii>> E;
vector<pii> ev;
int n, m;

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> m;
	Loop (i,0,m) {
		int v, u, w;
		cin >> v >> u >> w;
		--v; --u;
		E.push_back({w, {v, u}});
	}
	sort(E.begin(), E.end());
	Loop (i,0,E.size()) {
		auto [w, dard] = E[i];
		auto [v, u] = dard;
		int x = dsu::Do(v, u);
		if (x == -1) {
			ev.push_back({0, w});
			//cerr << w << ' ' << ev.back().first << '\n';
		} else if (x != w) {
			ev.push_back({(x+w+2)/2, w});
			//cerr << w << ' ' << ev.back().first << '\n';
		}
		dsu::push(v, u, w);
	}
	dsu::st = 0;
	for (int l = m, r = m; r > 0; r = l) {
		while (l > 0 && E[l-1].first == E[r-1].first)
			--l;
		Loop (i,l,r) {
			auto [w, dard] = E[i];
			auto [v, u] = dard;
			int x = dsu::Do(v, u);
			if (x != -1 && x != w) {
				ev.push_back({(x+w)/2+1, ~w});
				//cerr << w << ' ' << x << ' ' << ev.back().first << "-" << '\n';
			}
			dsu::push(v, u, w);
		}
	}
	sort(ev.begin(), ev.end());
	int p = 0;
	ll ans = 0;
	int q;
	cin >> q;
	multiset<int> Sr;
	ll suml = 0, sumr = 0;
	ll cntl = 0, cntr = 0;
	while (q--) {
		int x;
		cin >> x;
		multiset<int>::iterator it;
		while (Sr.size() && *(it = Sr.begin()) < x) {
			sumr -= *it;
			suml += *it;
			cntr--;
			cntl++;
			Sr.erase(it);
		}
		while (p < ev.size() && ev[p].first <= x) {
			int dard = ev[p++].second;
			if (dard < 0) {
				dard = ~dard;
				if (dard < x) {
					suml -= dard;
					cntl--;
				} else {
					sumr -= dard;
					cntr--;
					auto it = Sr.find(dard);
					assert(it != Sr.end());
					Sr.erase(it);
				}
			} else {
				if (dard < x) {
					suml += dard;
					cntl++;
				} else {
					sumr += dard;
					cntr++;
					Sr.insert(dard);
				}
			}
		}
		ll ans = (cntl*x - suml) + (sumr - cntr*x);
		//cerr << cntl << " " << suml << " - " << cntr << " " << sumr << '\n';
		cout << ans << '\n';
		//cerr << "Hi\n";
	}
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:131:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   while (p < ev.size() && ev[p].first <= x) {
      |          ~~^~~~~~~~~~~
reconstruction.cpp:114:5: warning: unused variable 'ans' [-Wunused-variable]
  114 |  ll ans = 0;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 745 ms 8956 KB Output is correct
21 Correct 354 ms 8872 KB Output is correct
22 Correct 358 ms 8928 KB Output is correct
23 Correct 411 ms 8840 KB Output is correct
24 Correct 547 ms 8860 KB Output is correct
25 Correct 743 ms 8936 KB Output is correct
26 Correct 742 ms 8928 KB Output is correct
27 Correct 720 ms 8888 KB Output is correct
28 Correct 711 ms 8992 KB Output is correct
29 Correct 339 ms 6548 KB Output is correct
30 Correct 764 ms 8872 KB Output is correct
31 Correct 740 ms 8916 KB Output is correct
32 Correct 723 ms 8904 KB Output is correct
33 Correct 755 ms 8860 KB Output is correct
34 Correct 294 ms 6588 KB Output is correct
35 Correct 728 ms 8812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 576 ms 29668 KB Output is correct
5 Correct 598 ms 29652 KB Output is correct
6 Correct 584 ms 29600 KB Output is correct
7 Correct 318 ms 31536 KB Output is correct
8 Correct 254 ms 31596 KB Output is correct
9 Correct 247 ms 31700 KB Output is correct
10 Correct 571 ms 29732 KB Output is correct
11 Correct 263 ms 31680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 173 ms 22252 KB Output is correct
21 Correct 160 ms 22420 KB Output is correct
22 Correct 157 ms 22348 KB Output is correct
23 Correct 175 ms 22348 KB Output is correct
24 Correct 164 ms 22260 KB Output is correct
25 Correct 184 ms 22224 KB Output is correct
26 Correct 170 ms 22324 KB Output is correct
27 Correct 152 ms 22368 KB Output is correct
28 Correct 157 ms 22244 KB Output is correct
29 Correct 156 ms 22336 KB Output is correct
30 Correct 165 ms 22364 KB Output is correct
31 Correct 155 ms 22148 KB Output is correct
32 Correct 149 ms 22860 KB Output is correct
33 Correct 157 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 745 ms 8956 KB Output is correct
21 Correct 354 ms 8872 KB Output is correct
22 Correct 358 ms 8928 KB Output is correct
23 Correct 411 ms 8840 KB Output is correct
24 Correct 547 ms 8860 KB Output is correct
25 Correct 743 ms 8936 KB Output is correct
26 Correct 742 ms 8928 KB Output is correct
27 Correct 720 ms 8888 KB Output is correct
28 Correct 711 ms 8992 KB Output is correct
29 Correct 339 ms 6548 KB Output is correct
30 Correct 764 ms 8872 KB Output is correct
31 Correct 740 ms 8916 KB Output is correct
32 Correct 723 ms 8904 KB Output is correct
33 Correct 755 ms 8860 KB Output is correct
34 Correct 294 ms 6588 KB Output is correct
35 Correct 728 ms 8812 KB Output is correct
36 Correct 748 ms 9272 KB Output is correct
37 Correct 376 ms 9336 KB Output is correct
38 Correct 421 ms 9208 KB Output is correct
39 Correct 449 ms 9300 KB Output is correct
40 Correct 604 ms 9280 KB Output is correct
41 Correct 744 ms 9248 KB Output is correct
42 Correct 769 ms 9296 KB Output is correct
43 Correct 825 ms 9208 KB Output is correct
44 Correct 792 ms 9280 KB Output is correct
45 Correct 316 ms 6960 KB Output is correct
46 Correct 776 ms 9296 KB Output is correct
47 Correct 767 ms 9244 KB Output is correct
48 Correct 787 ms 9228 KB Output is correct
49 Correct 760 ms 9292 KB Output is correct
50 Correct 367 ms 7024 KB Output is correct
51 Correct 743 ms 9240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 745 ms 8956 KB Output is correct
21 Correct 354 ms 8872 KB Output is correct
22 Correct 358 ms 8928 KB Output is correct
23 Correct 411 ms 8840 KB Output is correct
24 Correct 547 ms 8860 KB Output is correct
25 Correct 743 ms 8936 KB Output is correct
26 Correct 742 ms 8928 KB Output is correct
27 Correct 720 ms 8888 KB Output is correct
28 Correct 711 ms 8992 KB Output is correct
29 Correct 339 ms 6548 KB Output is correct
30 Correct 764 ms 8872 KB Output is correct
31 Correct 740 ms 8916 KB Output is correct
32 Correct 723 ms 8904 KB Output is correct
33 Correct 755 ms 8860 KB Output is correct
34 Correct 294 ms 6588 KB Output is correct
35 Correct 728 ms 8812 KB Output is correct
36 Correct 1 ms 328 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 576 ms 29668 KB Output is correct
40 Correct 598 ms 29652 KB Output is correct
41 Correct 584 ms 29600 KB Output is correct
42 Correct 318 ms 31536 KB Output is correct
43 Correct 254 ms 31596 KB Output is correct
44 Correct 247 ms 31700 KB Output is correct
45 Correct 571 ms 29732 KB Output is correct
46 Correct 263 ms 31680 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 173 ms 22252 KB Output is correct
49 Correct 160 ms 22420 KB Output is correct
50 Correct 157 ms 22348 KB Output is correct
51 Correct 175 ms 22348 KB Output is correct
52 Correct 164 ms 22260 KB Output is correct
53 Correct 184 ms 22224 KB Output is correct
54 Correct 170 ms 22324 KB Output is correct
55 Correct 152 ms 22368 KB Output is correct
56 Correct 157 ms 22244 KB Output is correct
57 Correct 156 ms 22336 KB Output is correct
58 Correct 165 ms 22364 KB Output is correct
59 Correct 155 ms 22148 KB Output is correct
60 Correct 149 ms 22860 KB Output is correct
61 Correct 157 ms 22124 KB Output is correct
62 Correct 748 ms 9272 KB Output is correct
63 Correct 376 ms 9336 KB Output is correct
64 Correct 421 ms 9208 KB Output is correct
65 Correct 449 ms 9300 KB Output is correct
66 Correct 604 ms 9280 KB Output is correct
67 Correct 744 ms 9248 KB Output is correct
68 Correct 769 ms 9296 KB Output is correct
69 Correct 825 ms 9208 KB Output is correct
70 Correct 792 ms 9280 KB Output is correct
71 Correct 316 ms 6960 KB Output is correct
72 Correct 776 ms 9296 KB Output is correct
73 Correct 767 ms 9244 KB Output is correct
74 Correct 787 ms 9228 KB Output is correct
75 Correct 760 ms 9292 KB Output is correct
76 Correct 367 ms 7024 KB Output is correct
77 Correct 743 ms 9240 KB Output is correct
78 Correct 970 ms 28556 KB Output is correct
79 Correct 591 ms 30604 KB Output is correct
80 Correct 540 ms 29648 KB Output is correct
81 Correct 638 ms 29532 KB Output is correct
82 Correct 776 ms 28648 KB Output is correct
83 Correct 881 ms 28548 KB Output is correct
84 Correct 922 ms 28576 KB Output is correct
85 Correct 991 ms 28632 KB Output is correct
86 Correct 974 ms 28608 KB Output is correct
87 Correct 444 ms 27864 KB Output is correct
88 Correct 880 ms 28604 KB Output is correct
89 Correct 883 ms 28572 KB Output is correct
90 Correct 902 ms 28628 KB Output is correct
91 Correct 917 ms 28480 KB Output is correct
92 Correct 437 ms 29172 KB Output is correct
93 Correct 868 ms 29732 KB Output is correct