Submission #554585

# Submission time Handle Problem Language Result Execution time Memory
554585 2022-04-28T19:30:15 Z Arvin Reconstruction Project (JOI22_reconstruction) C++11
100 / 100
1976 ms 28836 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
 
const int maxN = 505;
 
struct DSU {
	int parent[maxN], sz[maxN];
	
	void reset(){
		for(int x=0;x<maxN;x++){
			parent[x] = x;
			sz[x] = 1;
		}
	}
	
	int getParent(int x){
		if(parent[x] == x) return x;
		return parent[x] = getParent(parent[x]);
	}
	
	bool merge(int x, int y){
		int a = getParent(x);
		int b = getParent(y);
		
		if(a != b){
			if(sz[a] < sz[b]) swap(a, b);
			
			parent[b] = a;
			sz[a] += sz[b];
			return true;
		}
		return false;
	}
	
	bool check(int x, int y){
		return getParent(x) == getParent(y);
	}
};
 
DSU dsu;
 
int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	vector<array<int, 3>> edge;
	for(int x=0;x<m;x++){
		int a, b, w;
		cin >> a >> b >> w;
		
		a--; b--;
		edge.push_back({w, a, b});
	}
	
	sort(edge.begin(), edge.end());
	
	vector<array<int, 3>> v;
	for(int x=0;x<m;x++){
		dsu.reset();
		
		int l = x-1;
		while(l >= 0){
			dsu.merge(edge[l][1], edge[l][2]);
			if(dsu.check(edge[x][1], edge[x][2])) break;
			l--;
		}
		
		dsu.reset();
		
		int r = x+1;
		while(r < m){
			dsu.merge(edge[r][1], edge[r][2]);
			if(dsu.check(edge[x][1], edge[x][2])) break;
			r++;
		}
		
		int valL = (l >= 0 ? (edge[l][0]+edge[x][0]+1)/2 : -1);
		int valR = (r < m ? (edge[x][0]+edge[r][0]+1)/2 : 1e9 + 1);
		
		v.push_back({valL, -1, edge[x][0]});
		v.push_back({edge[x][0], 2, -2 * edge[x][0]});
		v.push_back({valR, -1, edge[x][0]});
	}
	
	sort(v.begin(), v.end());
	
	int q;
	cin >> q;
	
	int pos = 0;
	ll mul = 0, val = 0;
	while(q--){
		ll cur;
		cin >> cur;
		
		while(pos < v.size() && v[pos][0] <= cur){
			mul += v[pos][1];
			val += v[pos][2];
			pos++;
		}
		
		cout << mul*cur + val << "\n";
	}
    return 0;
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:103:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |   while(pos < v.size() && v[pos][0] <= cur){
      |         ~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 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 1 ms 320 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 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 1 ms 320 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1859 ms 9520 KB Output is correct
21 Correct 1361 ms 7728 KB Output is correct
22 Correct 1414 ms 7716 KB Output is correct
23 Correct 1517 ms 7848 KB Output is correct
24 Correct 1725 ms 7892 KB Output is correct
25 Correct 1757 ms 7764 KB Output is correct
26 Correct 1802 ms 9468 KB Output is correct
27 Correct 1793 ms 9532 KB Output is correct
28 Correct 1789 ms 9272 KB Output is correct
29 Correct 1132 ms 9284 KB Output is correct
30 Correct 1803 ms 9572 KB Output is correct
31 Correct 1801 ms 9516 KB Output is correct
32 Correct 1784 ms 9508 KB Output is correct
33 Correct 1786 ms 7844 KB Output is correct
34 Correct 796 ms 9588 KB Output is correct
35 Correct 1780 ms 9648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1717 ms 25768 KB Output is correct
5 Correct 1712 ms 26072 KB Output is correct
6 Correct 1695 ms 26180 KB Output is correct
7 Correct 965 ms 28296 KB Output is correct
8 Correct 726 ms 28104 KB Output is correct
9 Correct 656 ms 28676 KB Output is correct
10 Correct 1703 ms 26824 KB Output is correct
11 Correct 722 ms 28836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 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 1 ms 320 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 201 ms 18124 KB Output is correct
21 Correct 180 ms 17100 KB Output is correct
22 Correct 192 ms 17864 KB Output is correct
23 Correct 201 ms 17420 KB Output is correct
24 Correct 208 ms 17672 KB Output is correct
25 Correct 198 ms 17680 KB Output is correct
26 Correct 204 ms 17480 KB Output is correct
27 Correct 191 ms 17660 KB Output is correct
28 Correct 192 ms 22056 KB Output is correct
29 Correct 196 ms 22208 KB Output is correct
30 Correct 188 ms 22064 KB Output is correct
31 Correct 188 ms 21828 KB Output is correct
32 Correct 195 ms 22476 KB Output is correct
33 Correct 189 ms 21964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 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 1 ms 320 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1859 ms 9520 KB Output is correct
21 Correct 1361 ms 7728 KB Output is correct
22 Correct 1414 ms 7716 KB Output is correct
23 Correct 1517 ms 7848 KB Output is correct
24 Correct 1725 ms 7892 KB Output is correct
25 Correct 1757 ms 7764 KB Output is correct
26 Correct 1802 ms 9468 KB Output is correct
27 Correct 1793 ms 9532 KB Output is correct
28 Correct 1789 ms 9272 KB Output is correct
29 Correct 1132 ms 9284 KB Output is correct
30 Correct 1803 ms 9572 KB Output is correct
31 Correct 1801 ms 9516 KB Output is correct
32 Correct 1784 ms 9508 KB Output is correct
33 Correct 1786 ms 7844 KB Output is correct
34 Correct 796 ms 9588 KB Output is correct
35 Correct 1780 ms 9648 KB Output is correct
36 Correct 1781 ms 9860 KB Output is correct
37 Correct 1371 ms 9620 KB Output is correct
38 Correct 1397 ms 8168 KB Output is correct
39 Correct 1527 ms 7800 KB Output is correct
40 Correct 1722 ms 7928 KB Output is correct
41 Correct 1751 ms 7876 KB Output is correct
42 Correct 1770 ms 8788 KB Output is correct
43 Correct 1806 ms 8924 KB Output is correct
44 Correct 1795 ms 9792 KB Output is correct
45 Correct 1141 ms 9528 KB Output is correct
46 Correct 1780 ms 9652 KB Output is correct
47 Correct 1800 ms 9672 KB Output is correct
48 Correct 1786 ms 9252 KB Output is correct
49 Correct 1774 ms 7912 KB Output is correct
50 Correct 785 ms 7864 KB Output is correct
51 Correct 1781 ms 7864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 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 1 ms 320 KB Output is correct
11 Correct 0 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 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1859 ms 9520 KB Output is correct
21 Correct 1361 ms 7728 KB Output is correct
22 Correct 1414 ms 7716 KB Output is correct
23 Correct 1517 ms 7848 KB Output is correct
24 Correct 1725 ms 7892 KB Output is correct
25 Correct 1757 ms 7764 KB Output is correct
26 Correct 1802 ms 9468 KB Output is correct
27 Correct 1793 ms 9532 KB Output is correct
28 Correct 1789 ms 9272 KB Output is correct
29 Correct 1132 ms 9284 KB Output is correct
30 Correct 1803 ms 9572 KB Output is correct
31 Correct 1801 ms 9516 KB Output is correct
32 Correct 1784 ms 9508 KB Output is correct
33 Correct 1786 ms 7844 KB Output is correct
34 Correct 796 ms 9588 KB Output is correct
35 Correct 1780 ms 9648 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1717 ms 25768 KB Output is correct
40 Correct 1712 ms 26072 KB Output is correct
41 Correct 1695 ms 26180 KB Output is correct
42 Correct 965 ms 28296 KB Output is correct
43 Correct 726 ms 28104 KB Output is correct
44 Correct 656 ms 28676 KB Output is correct
45 Correct 1703 ms 26824 KB Output is correct
46 Correct 722 ms 28836 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 201 ms 18124 KB Output is correct
49 Correct 180 ms 17100 KB Output is correct
50 Correct 192 ms 17864 KB Output is correct
51 Correct 201 ms 17420 KB Output is correct
52 Correct 208 ms 17672 KB Output is correct
53 Correct 198 ms 17680 KB Output is correct
54 Correct 204 ms 17480 KB Output is correct
55 Correct 191 ms 17660 KB Output is correct
56 Correct 192 ms 22056 KB Output is correct
57 Correct 196 ms 22208 KB Output is correct
58 Correct 188 ms 22064 KB Output is correct
59 Correct 188 ms 21828 KB Output is correct
60 Correct 195 ms 22476 KB Output is correct
61 Correct 189 ms 21964 KB Output is correct
62 Correct 1781 ms 9860 KB Output is correct
63 Correct 1371 ms 9620 KB Output is correct
64 Correct 1397 ms 8168 KB Output is correct
65 Correct 1527 ms 7800 KB Output is correct
66 Correct 1722 ms 7928 KB Output is correct
67 Correct 1751 ms 7876 KB Output is correct
68 Correct 1770 ms 8788 KB Output is correct
69 Correct 1806 ms 8924 KB Output is correct
70 Correct 1795 ms 9792 KB Output is correct
71 Correct 1141 ms 9528 KB Output is correct
72 Correct 1780 ms 9652 KB Output is correct
73 Correct 1800 ms 9672 KB Output is correct
74 Correct 1786 ms 9252 KB Output is correct
75 Correct 1774 ms 7912 KB Output is correct
76 Correct 785 ms 7864 KB Output is correct
77 Correct 1781 ms 7864 KB Output is correct
78 Correct 1970 ms 18400 KB Output is correct
79 Correct 1503 ms 26184 KB Output is correct
80 Correct 1562 ms 27720 KB Output is correct
81 Correct 1705 ms 27348 KB Output is correct
82 Correct 1884 ms 26572 KB Output is correct
83 Correct 1929 ms 26116 KB Output is correct
84 Correct 1967 ms 23500 KB Output is correct
85 Correct 1954 ms 26444 KB Output is correct
86 Correct 1976 ms 15464 KB Output is correct
87 Correct 1302 ms 20908 KB Output is correct
88 Correct 1949 ms 26408 KB Output is correct
89 Correct 1935 ms 26392 KB Output is correct
90 Correct 1954 ms 24668 KB Output is correct
91 Correct 1948 ms 15312 KB Output is correct
92 Correct 950 ms 18160 KB Output is correct
93 Correct 1938 ms 16392 KB Output is correct