답안 #596825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596825 2022-07-15T07:09:11 Z 장태환(#8448) Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
2136 ms 154876 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
int N, M;
int uf[501];
int siz[501];
vector<pair<int, int>>ur;
vector<pair<int, int>>sr;
int f(int n)
{
	return uf[n] == n ? n : f(uf[n]);
}
int u(int n, int m)
{
	n = f(n);
	m = f(m);
	if (n == m)
		return 0;
	sr.push_back({ n,siz[n] });
	siz[n] += siz[m];
	ur.push_back({ m,m });
	uf[m] = n;
	return 1;
}
int q[1 << 20];
int ans[1 << 20];
void dfs(vector<pair<int,pair<int,int>>>a,int s,int e,int rs,int ls,int rc,int lc)
{
	if (s > e)
		return;
	int m = (s + e) / 2;
	vector<pair<int, pair<int, int>>>le, rf;
	int i;
	for (i = 0; i < a.size(); i++)
	{
		if (a[i].first < q[m])
		{
			le.push_back(a[i]);
		}
		else
			rf.push_back(a[i]);
	}
	int r = 0;
	int l = le.size();
	int er = 0;
	ans[m] -= rs - rc * q[m];
	ans[m] -= lc * q[m] - ls;
	vector<int>li, ri;
	while (r < rf.size() || l)
	{
		if (l == 0)
		{
			if(u(rf[r].second.first,rf[r].second.second))
				ri.push_back(r);
			r++;
		}
		else if (r == rf.size())
		{
			l--;
			if (u(le[l].second.first, le[l].second.second))
				li.push_back(l);
		}
		else
		{
			if (rf[r].first - q[m] < q[m] - le[l-1].first)
			{
				if (u(rf[r].second.first, rf[r].second.second))
					ri.push_back(r);
				r++;
			}
			else
			{
				l--;
				if (u(le[l].second.first, le[l].second.second))
					li.push_back(l);
			}
		}
	}
	reverse(li.begin(), li.end());
	for (i = 0; i < ri.size()+li.size(); i++)
	{
		uf[ur.back().first] = ur.back().second;
		siz[sr.back().first] =sr.back().second;
		ur.pop_back();
		sr.pop_back();
	}
	int nls = 0;
	int nlc = 0;
	vector<pair<int, pair<int, int>>>rn;
	int cch = 0;
	for (i = 0; i < le.size(); i++)
	{
		while (cch < li.size() && li[cch] < i)
			cch++;
		if (cch < li.size() && li[cch] == i)
		{
			ans[m] += q[m] - le[i].first;
			rn.push_back(le[i]);
		}
	}
	cch = 0;
	for (i = 0; i < rf.size(); i++)
	{
		while (cch < ri.size() && ri[cch] < i)
			cch++;
		if (cch<ri.size()&&ri[cch] == i)
		{
			ans[m] += rf[i].first - q[m];
			if (rf[i].first >= q[e])
			{
				nls += rf[i].first;
				nlc++;
				u(rf[i].second.first, rf[i].second.second);
			}
			else
			{
				rn.push_back(rf[i]);
			}
		}
		else
		{
			rn.push_back(rf[i]);
		}
		
	}
	
	dfs(rn, m + 1, e, rs, ls + nls, rc, lc + nlc);
	while (nlc--)
	{
		uf[ur.back().first] = ur.back().second;
		siz[sr.back().first] = sr.back().second;
		ur.pop_back();
		sr.pop_back();
	}
	nlc = 0;
	nls = 0;
	vector<pair<int, pair<int, int>>>ln;
	cch = 0;
	for (i = 0; i < le.size(); i++)
	{
		while (cch < li.size() && li[cch] < i)
			cch++;
		if (cch < li.size() && li[cch] == i)
		{
			if (le[i].first <= q[s])
			{
				nls += le[i].first;
				nlc++;
				u(le[i].second.first, le[i].second.second);
			}
			else
			{
				ln.push_back(le[i]);
			}
		}
		else
		{
			ln.push_back(le[i]);
		}
	}
	cch = 0;
	for (i = 0; i < rf.size(); i++)
	{
		while (cch < ri.size() && ri[cch] < i)
			cch++;
		if (cch < ri.size() && ri[cch] == i)
		{
			ln.push_back(rf[i]);
		}
	}
	
	dfs(ln, s, m-1, rs + nls, ls, rc + nlc, lc);
	while (nlc--)
	{
		uf[ur.back().first] = ur.back().second;
		siz[sr.back().first] = sr.back().second;
		ur.pop_back();
		sr.pop_back();
	}
	nlc = 0;
	nls = 0;
	
}
signed main()
{
	cin >> N >> M;
	int i;
	for (i = 0; i < N; i++)
	{
		uf[i] = i;
		siz[i] = 1;
	}
	vector<pair<int, pair<int, int>>>t;
	for (i = 0; i < M; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		t.push_back({ c,{a,b} });
	}
	sort(t.begin(), t.end());
	int Q;
	cin >> Q;
	for (i = 0; i < Q; i++)
	{
		cin >> q[i];
	}
	dfs(t, 0, Q - 1, 0, 0, 0, 0);
	for (i = 0; i < Q; i++)
		cout << ans[i] << '\n';
}

Compilation message

reconstruction.cpp: In function 'void dfs(std::vector<std::pair<long long int, std::pair<long long int, long long int> > >, long long int, long long int, long long int, long long int, long long int, long long int)':
reconstruction.cpp:34:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  for (i = 0; i < a.size(); i++)
      |              ~~^~~~~~~~~~
reconstruction.cpp:49:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  while (r < rf.size() || l)
      |         ~~^~~~~~~~~~~
reconstruction.cpp:57:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   else if (r == rf.size())
      |            ~~^~~~~~~~~~~~
reconstruction.cpp:80:16: 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]
   80 |  for (i = 0; i < ri.size()+li.size(); i++)
      |              ~~^~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:91:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for (i = 0; i < le.size(); i++)
      |              ~~^~~~~~~~~~~
reconstruction.cpp:93:14: 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]
   93 |   while (cch < li.size() && li[cch] < i)
      |          ~~~~^~~~~~~~~~~
reconstruction.cpp:95:11: 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]
   95 |   if (cch < li.size() && li[cch] == i)
      |       ~~~~^~~~~~~~~~~
reconstruction.cpp:102:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |  for (i = 0; i < rf.size(); i++)
      |              ~~^~~~~~~~~~~
reconstruction.cpp:104:14: 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]
  104 |   while (cch < ri.size() && ri[cch] < i)
      |          ~~~~^~~~~~~~~~~
reconstruction.cpp:106:10: 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]
  106 |   if (cch<ri.size()&&ri[cch] == i)
      |       ~~~^~~~~~~~~~
reconstruction.cpp:139:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |  for (i = 0; i < le.size(); i++)
      |              ~~^~~~~~~~~~~
reconstruction.cpp:141:14: 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]
  141 |   while (cch < li.size() && li[cch] < i)
      |          ~~~~^~~~~~~~~~~
reconstruction.cpp:143:11: 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]
  143 |   if (cch < li.size() && li[cch] == i)
      |       ~~~~^~~~~~~~~~~
reconstruction.cpp:162:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |  for (i = 0; i < rf.size(); i++)
      |              ~~^~~~~~~~~~~
reconstruction.cpp:164:14: 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]
  164 |   while (cch < ri.size() && ri[cch] < i)
      |          ~~~~^~~~~~~~~~~
reconstruction.cpp:166:11: 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]
  166 |   if (cch < ri.size() && ri[cch] == i)
      |       ~~~~^~~~~~~~~~~
reconstruction.cpp:45:6: warning: unused variable 'er' [-Wunused-variable]
   45 |  int er = 0;
      |      ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 195 ms 22448 KB Output is correct
21 Correct 120 ms 21424 KB Output is correct
22 Correct 141 ms 19304 KB Output is correct
23 Correct 120 ms 22760 KB Output is correct
24 Correct 147 ms 21412 KB Output is correct
25 Correct 219 ms 23152 KB Output is correct
26 Correct 178 ms 18976 KB Output is correct
27 Correct 182 ms 18844 KB Output is correct
28 Correct 181 ms 18192 KB Output is correct
29 Correct 227 ms 26712 KB Output is correct
30 Correct 200 ms 22084 KB Output is correct
31 Correct 175 ms 17896 KB Output is correct
32 Correct 185 ms 22156 KB Output is correct
33 Correct 186 ms 26840 KB Output is correct
34 Correct 117 ms 33960 KB Output is correct
35 Correct 152 ms 22900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1569 ms 44464 KB Output is correct
5 Correct 1578 ms 41740 KB Output is correct
6 Correct 1608 ms 41772 KB Output is correct
7 Correct 1420 ms 46844 KB Output is correct
8 Correct 1230 ms 43764 KB Output is correct
9 Correct 894 ms 43612 KB Output is correct
10 Correct 751 ms 93148 KB Output is correct
11 Correct 685 ms 93544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 544 ms 30452 KB Output is correct
21 Correct 521 ms 30348 KB Output is correct
22 Correct 573 ms 30064 KB Output is correct
23 Correct 529 ms 30068 KB Output is correct
24 Correct 574 ms 30028 KB Output is correct
25 Correct 530 ms 30232 KB Output is correct
26 Correct 537 ms 30020 KB Output is correct
27 Correct 532 ms 30048 KB Output is correct
28 Correct 512 ms 30060 KB Output is correct
29 Correct 526 ms 30124 KB Output is correct
30 Correct 497 ms 30112 KB Output is correct
31 Correct 509 ms 30048 KB Output is correct
32 Correct 484 ms 30228 KB Output is correct
33 Correct 492 ms 29200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 195 ms 22448 KB Output is correct
21 Correct 120 ms 21424 KB Output is correct
22 Correct 141 ms 19304 KB Output is correct
23 Correct 120 ms 22760 KB Output is correct
24 Correct 147 ms 21412 KB Output is correct
25 Correct 219 ms 23152 KB Output is correct
26 Correct 178 ms 18976 KB Output is correct
27 Correct 182 ms 18844 KB Output is correct
28 Correct 181 ms 18192 KB Output is correct
29 Correct 227 ms 26712 KB Output is correct
30 Correct 200 ms 22084 KB Output is correct
31 Correct 175 ms 17896 KB Output is correct
32 Correct 185 ms 22156 KB Output is correct
33 Correct 186 ms 26840 KB Output is correct
34 Correct 117 ms 33960 KB Output is correct
35 Correct 152 ms 22900 KB Output is correct
36 Correct 635 ms 20196 KB Output is correct
37 Correct 438 ms 20008 KB Output is correct
38 Correct 428 ms 20120 KB Output is correct
39 Correct 458 ms 19960 KB Output is correct
40 Correct 555 ms 20088 KB Output is correct
41 Correct 706 ms 20060 KB Output is correct
42 Correct 649 ms 20084 KB Output is correct
43 Correct 665 ms 20044 KB Output is correct
44 Correct 593 ms 19836 KB Output is correct
45 Correct 541 ms 27896 KB Output is correct
46 Correct 666 ms 19960 KB Output is correct
47 Correct 639 ms 19756 KB Output is correct
48 Correct 580 ms 63976 KB Output is correct
49 Correct 602 ms 66016 KB Output is correct
50 Correct 184 ms 111616 KB Output is correct
51 Correct 385 ms 62008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 195 ms 22448 KB Output is correct
21 Correct 120 ms 21424 KB Output is correct
22 Correct 141 ms 19304 KB Output is correct
23 Correct 120 ms 22760 KB Output is correct
24 Correct 147 ms 21412 KB Output is correct
25 Correct 219 ms 23152 KB Output is correct
26 Correct 178 ms 18976 KB Output is correct
27 Correct 182 ms 18844 KB Output is correct
28 Correct 181 ms 18192 KB Output is correct
29 Correct 227 ms 26712 KB Output is correct
30 Correct 200 ms 22084 KB Output is correct
31 Correct 175 ms 17896 KB Output is correct
32 Correct 185 ms 22156 KB Output is correct
33 Correct 186 ms 26840 KB Output is correct
34 Correct 117 ms 33960 KB Output is correct
35 Correct 152 ms 22900 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1569 ms 44464 KB Output is correct
40 Correct 1578 ms 41740 KB Output is correct
41 Correct 1608 ms 41772 KB Output is correct
42 Correct 1420 ms 46844 KB Output is correct
43 Correct 1230 ms 43764 KB Output is correct
44 Correct 894 ms 43612 KB Output is correct
45 Correct 751 ms 93148 KB Output is correct
46 Correct 685 ms 93544 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 544 ms 30452 KB Output is correct
49 Correct 521 ms 30348 KB Output is correct
50 Correct 573 ms 30064 KB Output is correct
51 Correct 529 ms 30068 KB Output is correct
52 Correct 574 ms 30028 KB Output is correct
53 Correct 530 ms 30232 KB Output is correct
54 Correct 537 ms 30020 KB Output is correct
55 Correct 532 ms 30048 KB Output is correct
56 Correct 512 ms 30060 KB Output is correct
57 Correct 526 ms 30124 KB Output is correct
58 Correct 497 ms 30112 KB Output is correct
59 Correct 509 ms 30048 KB Output is correct
60 Correct 484 ms 30228 KB Output is correct
61 Correct 492 ms 29200 KB Output is correct
62 Correct 635 ms 20196 KB Output is correct
63 Correct 438 ms 20008 KB Output is correct
64 Correct 428 ms 20120 KB Output is correct
65 Correct 458 ms 19960 KB Output is correct
66 Correct 555 ms 20088 KB Output is correct
67 Correct 706 ms 20060 KB Output is correct
68 Correct 649 ms 20084 KB Output is correct
69 Correct 665 ms 20044 KB Output is correct
70 Correct 593 ms 19836 KB Output is correct
71 Correct 541 ms 27896 KB Output is correct
72 Correct 666 ms 19960 KB Output is correct
73 Correct 639 ms 19756 KB Output is correct
74 Correct 580 ms 63976 KB Output is correct
75 Correct 602 ms 66016 KB Output is correct
76 Correct 184 ms 111616 KB Output is correct
77 Correct 385 ms 62008 KB Output is correct
78 Correct 2075 ms 43572 KB Output is correct
79 Correct 1834 ms 45592 KB Output is correct
80 Correct 1847 ms 44536 KB Output is correct
81 Correct 1850 ms 41496 KB Output is correct
82 Correct 1918 ms 43576 KB Output is correct
83 Correct 2111 ms 43736 KB Output is correct
84 Correct 2090 ms 43524 KB Output is correct
85 Correct 2090 ms 43548 KB Output is correct
86 Correct 1607 ms 43144 KB Output is correct
87 Correct 1191 ms 45480 KB Output is correct
88 Correct 2041 ms 43428 KB Output is correct
89 Correct 2136 ms 43524 KB Output is correct
90 Correct 1668 ms 98204 KB Output is correct
91 Correct 1651 ms 92092 KB Output is correct
92 Correct 681 ms 154876 KB Output is correct
93 Correct 959 ms 92880 KB Output is correct