Submission #772482

# Submission time Handle Problem Language Result Execution time Memory
772482 2023-07-04T07:15:35 Z minhcool Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
3869 ms 129272 KB
#define local
#ifndef local
#include ""
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
 
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
 
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 4e6 + 5;
 
const int oo = 1e18 + 7, mod = 1e9 + 7;
 
mt19937 rng(1);
 
int rnd(int l, int r){
	int temp = rng() % (r - l + 1);
	return abs(temp) + l;
}
 
int n, m;
 
int le[N], ri[N];
 
vector<iii> edge;
 
vector<int> diffs;
 
bool cmp(iii a, iii b){
	return a.se < b.se;
}
 
vector<iii> cur_mst;
 
int rt[N], sz[N];
 
int root(int x){
	return (x == rt[x] ? x : rt[x] = root(rt[x]));
}
 
bool merge(int x, int y){
	x = root(x), y = root(y);
	if(x == y) return 0;
	if(sz[x] < sz[y]) swap(x, y);
	sz[x] += sz[y];
	rt[y] = x;
	return 1;
}
 
vector<ii> updates[N];
 
#ifdef local
void process(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int x, y, w;
		cin >> x >> y >> w;
		edge.pb({{x, y}, w});
	}
	sort(edge.begin(), edge.end(), cmp);
	for(int i = 0; i < m; i++) le[i] = ri[i] = -oo;
	for(int i = 1; i < edge.size(); i++){
		cur_mst.pb({edge[i - 1].fi, i - 1});
		iii it = edge[i];
		for(int j = 1; j <= n; j++){
			rt[j] = j, sz[j] = 1;
		}
		vector<iii> nw;
		for(int j = cur_mst.size() - 1; j >= 0; j--){
			if(merge(cur_mst[j].fi.fi, cur_mst[j].fi.se)){
				nw.pb(cur_mst[j]);
				if(root(it.fi.fi) == root(it.fi.se) && le[i] < 0) le[i] = cur_mst[j].se;
			}
		}
		reverse(nw.begin(), nw.end());
		cur_mst = nw;
	}
	cur_mst.clear();
	for(int i = (int)edge.size() - 2; i >= 0; i--){
		cur_mst.pb({edge[i + 1].fi, i + 1});
		iii it = edge[i];
		for(int j = 1; j <= n; j++){
			rt[j] = j, sz[j] = 1;
		}
		vector<iii> nw;
		for(int j = cur_mst.size() - 1; j >= 0; j--){
			if(merge(cur_mst[j].fi.fi, cur_mst[j].fi.se)){
				nw.pb(cur_mst[j]);
				if(root(it.fi.fi) == root(it.fi.se) && ri[i] < 0) ri[i] = cur_mst[j].se;
			}
		}
		reverse(nw.begin(), nw.end());
		cur_mst = nw;
	//	cout << "OK " << i << "\n";
	//	for(auto it : cur_mst) cout << it.fi.fi << " " << it.fi.se << " " << it.se << "\n";
	}
	for(int i = 0; i < m; i++){
		int temp1 = (le[i] < 0 ? 0 : (edge[le[i]].se + edge[i].se + 2) / 2);
		int temp2 = (ri[i] < 0 ? 2e9 : (edge[i].se + edge[ri[i]].se) / 2);
	//	cout << i << " " << le[i] << " " << ri[i] << " " << temp1 << " " << temp2 << "\n";
		diffs.pb(temp1);
		diffs.pb(temp2 + 1);
	}
	sort(diffs.begin(), diffs.end());
	diffs.resize(unique(diffs.begin(), diffs.end()) - diffs.begin());
	for(int i = 0; i < m; i++){
		int temp1 = (le[i] < 0 ? 0 : (edge[le[i]].se + edge[i].se + 2) / 2);
		int temp2 = (ri[i] < 0 ? 2e9 : (edge[i].se + edge[ri[i]].se) / 2);
		int pos1 = lower_bound(diffs.begin(), diffs.end(), temp1) - diffs.begin();
		int pos2 = lower_bound(diffs.begin(), diffs.end(), temp2 + 1) - diffs.begin();
		updates[pos1].pb({i, 1});
		updates[pos2].pb({i, -1});
		//diffs.pb(temp1);
		//diffs.pb(temp2 + 1);	
	}
	multiset<int> mls;
	int q;
	cin >> q;
	int itr = 0;
	while(q--){
		int x;
		cin >> x;
		while(itr < diffs.size() && diffs[itr] <= x){
			for(auto it : updates[itr]){
		//		cout << edge[it.fi].se << " " << it.se << "\n";
				if(it.se > 0) mls.insert(edge[it.fi].se);
				else mls.erase(mls.find(edge[it.fi].se));
			} 
			itr++;
		}
		int total = 0, mx = 0;
		//for(auto it : mls) cout << it << " ";
	//	cout << "\n";
		for(auto it : mls){
			total += abs(it - x);
			mx = max(mx, it - x);
		}
		if(mls.size() != (n - 1)) total += (n - 1 - mls.size()) * mx;
		cout << total << "\n";
	}
	//for(int i = 0; i < 
}
 
signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	process();
}
#endif

Compilation message

reconstruction.cpp: In function 'void process()':
reconstruction.cpp:72:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for(int i = 1; i < edge.size(); i++){
      |                 ~~^~~~~~~~~~~~~
reconstruction.cpp:133:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |   while(itr < diffs.size() && diffs[itr] <= x){
      |         ~~~~^~~~~~~~~~~~~~
reconstruction.cpp:148:17: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  148 |   if(mls.size() != (n - 1)) total += (n - 1 - mls.size()) * mx;
      |      ~~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94256 KB Output is correct
2 Correct 43 ms 94152 KB Output is correct
3 Correct 44 ms 94188 KB Output is correct
4 Correct 44 ms 94172 KB Output is correct
5 Correct 43 ms 94216 KB Output is correct
6 Correct 42 ms 94264 KB Output is correct
7 Correct 49 ms 94196 KB Output is correct
8 Correct 43 ms 94288 KB Output is correct
9 Correct 44 ms 94392 KB Output is correct
10 Correct 43 ms 94152 KB Output is correct
11 Correct 49 ms 94308 KB Output is correct
12 Correct 43 ms 94260 KB Output is correct
13 Correct 44 ms 94204 KB Output is correct
14 Correct 44 ms 94272 KB Output is correct
15 Correct 46 ms 94184 KB Output is correct
16 Correct 43 ms 94232 KB Output is correct
17 Correct 43 ms 94176 KB Output is correct
18 Correct 44 ms 94248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94256 KB Output is correct
2 Correct 43 ms 94152 KB Output is correct
3 Correct 44 ms 94188 KB Output is correct
4 Correct 44 ms 94172 KB Output is correct
5 Correct 43 ms 94216 KB Output is correct
6 Correct 42 ms 94264 KB Output is correct
7 Correct 49 ms 94196 KB Output is correct
8 Correct 43 ms 94288 KB Output is correct
9 Correct 44 ms 94392 KB Output is correct
10 Correct 43 ms 94152 KB Output is correct
11 Correct 49 ms 94308 KB Output is correct
12 Correct 43 ms 94260 KB Output is correct
13 Correct 44 ms 94204 KB Output is correct
14 Correct 44 ms 94272 KB Output is correct
15 Correct 46 ms 94184 KB Output is correct
16 Correct 43 ms 94232 KB Output is correct
17 Correct 43 ms 94176 KB Output is correct
18 Correct 44 ms 94248 KB Output is correct
19 Correct 50 ms 94236 KB Output is correct
20 Correct 1735 ms 104472 KB Output is correct
21 Correct 1451 ms 104540 KB Output is correct
22 Correct 1503 ms 104608 KB Output is correct
23 Correct 1628 ms 104476 KB Output is correct
24 Correct 1722 ms 104476 KB Output is correct
25 Correct 1706 ms 104400 KB Output is correct
26 Correct 1770 ms 104480 KB Output is correct
27 Correct 1730 ms 104352 KB Output is correct
28 Correct 1715 ms 103840 KB Output is correct
29 Correct 1700 ms 103964 KB Output is correct
30 Correct 1738 ms 104400 KB Output is correct
31 Correct 1775 ms 104560 KB Output is correct
32 Correct 1743 ms 104484 KB Output is correct
33 Correct 1743 ms 104500 KB Output is correct
34 Correct 1712 ms 105884 KB Output is correct
35 Correct 1762 ms 104580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94168 KB Output is correct
2 Correct 44 ms 94184 KB Output is correct
3 Correct 45 ms 94216 KB Output is correct
4 Correct 3638 ms 115524 KB Output is correct
5 Correct 3689 ms 126884 KB Output is correct
6 Correct 3790 ms 126956 KB Output is correct
7 Correct 2880 ms 128844 KB Output is correct
8 Correct 2621 ms 128988 KB Output is correct
9 Correct 2618 ms 128920 KB Output is correct
10 Correct 3581 ms 127256 KB Output is correct
11 Correct 2616 ms 129036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94256 KB Output is correct
2 Correct 43 ms 94152 KB Output is correct
3 Correct 44 ms 94188 KB Output is correct
4 Correct 44 ms 94172 KB Output is correct
5 Correct 43 ms 94216 KB Output is correct
6 Correct 42 ms 94264 KB Output is correct
7 Correct 49 ms 94196 KB Output is correct
8 Correct 43 ms 94288 KB Output is correct
9 Correct 44 ms 94392 KB Output is correct
10 Correct 43 ms 94152 KB Output is correct
11 Correct 49 ms 94308 KB Output is correct
12 Correct 43 ms 94260 KB Output is correct
13 Correct 44 ms 94204 KB Output is correct
14 Correct 44 ms 94272 KB Output is correct
15 Correct 46 ms 94184 KB Output is correct
16 Correct 43 ms 94232 KB Output is correct
17 Correct 43 ms 94176 KB Output is correct
18 Correct 44 ms 94248 KB Output is correct
19 Correct 41 ms 94292 KB Output is correct
20 Correct 2021 ms 106584 KB Output is correct
21 Correct 2024 ms 106708 KB Output is correct
22 Correct 2020 ms 106696 KB Output is correct
23 Correct 2024 ms 106652 KB Output is correct
24 Correct 2052 ms 106804 KB Output is correct
25 Correct 2033 ms 106688 KB Output is correct
26 Correct 2027 ms 106676 KB Output is correct
27 Correct 1997 ms 106688 KB Output is correct
28 Correct 2011 ms 106620 KB Output is correct
29 Correct 2005 ms 106580 KB Output is correct
30 Correct 2066 ms 106692 KB Output is correct
31 Correct 1983 ms 106640 KB Output is correct
32 Correct 1913 ms 107156 KB Output is correct
33 Correct 2083 ms 106488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94256 KB Output is correct
2 Correct 43 ms 94152 KB Output is correct
3 Correct 44 ms 94188 KB Output is correct
4 Correct 44 ms 94172 KB Output is correct
5 Correct 43 ms 94216 KB Output is correct
6 Correct 42 ms 94264 KB Output is correct
7 Correct 49 ms 94196 KB Output is correct
8 Correct 43 ms 94288 KB Output is correct
9 Correct 44 ms 94392 KB Output is correct
10 Correct 43 ms 94152 KB Output is correct
11 Correct 49 ms 94308 KB Output is correct
12 Correct 43 ms 94260 KB Output is correct
13 Correct 44 ms 94204 KB Output is correct
14 Correct 44 ms 94272 KB Output is correct
15 Correct 46 ms 94184 KB Output is correct
16 Correct 43 ms 94232 KB Output is correct
17 Correct 43 ms 94176 KB Output is correct
18 Correct 44 ms 94248 KB Output is correct
19 Correct 50 ms 94236 KB Output is correct
20 Correct 1735 ms 104472 KB Output is correct
21 Correct 1451 ms 104540 KB Output is correct
22 Correct 1503 ms 104608 KB Output is correct
23 Correct 1628 ms 104476 KB Output is correct
24 Correct 1722 ms 104476 KB Output is correct
25 Correct 1706 ms 104400 KB Output is correct
26 Correct 1770 ms 104480 KB Output is correct
27 Correct 1730 ms 104352 KB Output is correct
28 Correct 1715 ms 103840 KB Output is correct
29 Correct 1700 ms 103964 KB Output is correct
30 Correct 1738 ms 104400 KB Output is correct
31 Correct 1775 ms 104560 KB Output is correct
32 Correct 1743 ms 104484 KB Output is correct
33 Correct 1743 ms 104500 KB Output is correct
34 Correct 1712 ms 105884 KB Output is correct
35 Correct 1762 ms 104580 KB Output is correct
36 Correct 1784 ms 104632 KB Output is correct
37 Correct 1495 ms 104736 KB Output is correct
38 Correct 1556 ms 106528 KB Output is correct
39 Correct 1649 ms 106644 KB Output is correct
40 Correct 1766 ms 106520 KB Output is correct
41 Correct 1804 ms 106512 KB Output is correct
42 Correct 1805 ms 106552 KB Output is correct
43 Correct 1785 ms 106520 KB Output is correct
44 Correct 1766 ms 106096 KB Output is correct
45 Correct 1727 ms 106076 KB Output is correct
46 Correct 1789 ms 106584 KB Output is correct
47 Correct 1791 ms 106524 KB Output is correct
48 Correct 1783 ms 106512 KB Output is correct
49 Correct 1774 ms 106508 KB Output is correct
50 Correct 1732 ms 107960 KB Output is correct
51 Correct 1776 ms 106512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94256 KB Output is correct
2 Correct 43 ms 94152 KB Output is correct
3 Correct 44 ms 94188 KB Output is correct
4 Correct 44 ms 94172 KB Output is correct
5 Correct 43 ms 94216 KB Output is correct
6 Correct 42 ms 94264 KB Output is correct
7 Correct 49 ms 94196 KB Output is correct
8 Correct 43 ms 94288 KB Output is correct
9 Correct 44 ms 94392 KB Output is correct
10 Correct 43 ms 94152 KB Output is correct
11 Correct 49 ms 94308 KB Output is correct
12 Correct 43 ms 94260 KB Output is correct
13 Correct 44 ms 94204 KB Output is correct
14 Correct 44 ms 94272 KB Output is correct
15 Correct 46 ms 94184 KB Output is correct
16 Correct 43 ms 94232 KB Output is correct
17 Correct 43 ms 94176 KB Output is correct
18 Correct 44 ms 94248 KB Output is correct
19 Correct 50 ms 94236 KB Output is correct
20 Correct 1735 ms 104472 KB Output is correct
21 Correct 1451 ms 104540 KB Output is correct
22 Correct 1503 ms 104608 KB Output is correct
23 Correct 1628 ms 104476 KB Output is correct
24 Correct 1722 ms 104476 KB Output is correct
25 Correct 1706 ms 104400 KB Output is correct
26 Correct 1770 ms 104480 KB Output is correct
27 Correct 1730 ms 104352 KB Output is correct
28 Correct 1715 ms 103840 KB Output is correct
29 Correct 1700 ms 103964 KB Output is correct
30 Correct 1738 ms 104400 KB Output is correct
31 Correct 1775 ms 104560 KB Output is correct
32 Correct 1743 ms 104484 KB Output is correct
33 Correct 1743 ms 104500 KB Output is correct
34 Correct 1712 ms 105884 KB Output is correct
35 Correct 1762 ms 104580 KB Output is correct
36 Correct 44 ms 94168 KB Output is correct
37 Correct 44 ms 94184 KB Output is correct
38 Correct 45 ms 94216 KB Output is correct
39 Correct 3638 ms 115524 KB Output is correct
40 Correct 3689 ms 126884 KB Output is correct
41 Correct 3790 ms 126956 KB Output is correct
42 Correct 2880 ms 128844 KB Output is correct
43 Correct 2621 ms 128988 KB Output is correct
44 Correct 2618 ms 128920 KB Output is correct
45 Correct 3581 ms 127256 KB Output is correct
46 Correct 2616 ms 129036 KB Output is correct
47 Correct 41 ms 94292 KB Output is correct
48 Correct 2021 ms 106584 KB Output is correct
49 Correct 2024 ms 106708 KB Output is correct
50 Correct 2020 ms 106696 KB Output is correct
51 Correct 2024 ms 106652 KB Output is correct
52 Correct 2052 ms 106804 KB Output is correct
53 Correct 2033 ms 106688 KB Output is correct
54 Correct 2027 ms 106676 KB Output is correct
55 Correct 1997 ms 106688 KB Output is correct
56 Correct 2011 ms 106620 KB Output is correct
57 Correct 2005 ms 106580 KB Output is correct
58 Correct 2066 ms 106692 KB Output is correct
59 Correct 1983 ms 106640 KB Output is correct
60 Correct 1913 ms 107156 KB Output is correct
61 Correct 2083 ms 106488 KB Output is correct
62 Correct 1784 ms 104632 KB Output is correct
63 Correct 1495 ms 104736 KB Output is correct
64 Correct 1556 ms 106528 KB Output is correct
65 Correct 1649 ms 106644 KB Output is correct
66 Correct 1766 ms 106520 KB Output is correct
67 Correct 1804 ms 106512 KB Output is correct
68 Correct 1805 ms 106552 KB Output is correct
69 Correct 1785 ms 106520 KB Output is correct
70 Correct 1766 ms 106096 KB Output is correct
71 Correct 1727 ms 106076 KB Output is correct
72 Correct 1789 ms 106584 KB Output is correct
73 Correct 1791 ms 106524 KB Output is correct
74 Correct 1783 ms 106512 KB Output is correct
75 Correct 1774 ms 106508 KB Output is correct
76 Correct 1732 ms 107960 KB Output is correct
77 Correct 1776 ms 106512 KB Output is correct
78 Correct 3789 ms 125880 KB Output is correct
79 Correct 3451 ms 127900 KB Output is correct
80 Correct 3575 ms 126868 KB Output is correct
81 Correct 3670 ms 126960 KB Output is correct
82 Correct 3794 ms 125964 KB Output is correct
83 Correct 3832 ms 125840 KB Output is correct
84 Correct 3804 ms 125876 KB Output is correct
85 Correct 3869 ms 125864 KB Output is correct
86 Correct 3695 ms 125436 KB Output is correct
87 Correct 3563 ms 127156 KB Output is correct
88 Correct 3814 ms 125952 KB Output is correct
89 Correct 3772 ms 125840 KB Output is correct
90 Correct 3788 ms 125944 KB Output is correct
91 Correct 3812 ms 125816 KB Output is correct
92 Correct 3553 ms 129272 KB Output is correct
93 Correct 3743 ms 127052 KB Output is correct