Submission #974750

# Submission time Handle Problem Language Result Execution time Memory
974750 2024-05-03T18:04:24 Z happy_node Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
3862 ms 25336 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
const int MX=505;
 
int N,M,Q;
 
set<array<int,3>> adj[MX];
 
const int inf=2e9;

pair<int,int> dist[MX];
 
pair<int,int> bfs(int S, int T) {
	queue<int> q;
	q.push(S);

	for(int i=1;i<=N;i++) dist[i]={-1,-1};
	dist[S]={inf,inf};
 
	while(!q.empty()) {
		auto v=q.front(); q.pop();
		if(v==T) break;
		for(auto [u,w,ii]:adj[v]) {
			if(dist[u].first!=-1) continue;
 
			dist[u]=min(make_pair(w,ii),dist[v]);
			q.push(u);
		}
	}
 
	return dist[T];
}
 
pair<int,int> BFS(int S, int T) {
	queue<int> q;
	q.push(S);
	
	for(int i=1;i<=N;i++) dist[i]={-1,-1};
	dist[S]={0,0};
 
	while(!q.empty()) {
		auto v=q.front(); q.pop();
		if(v==T) break;
		for(auto [u,w,ii]:adj[v]) {
			if(dist[u].first!=-1) continue;
 
			dist[u]=max(make_pair(w,ii),dist[v]);
			q.push(u);
		}
	}
 
	return dist[T];
}
 
array<int,3> e[100000+5];
set<array<int,4>> E;
 
int L[100000+5], R[100000+5];
 
int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
 
	cin>>N>>M;
 
	for(int i=0;i<M;i++) {
		int u,v,w;
		cin>>u>>v>>w;
		e[i]={w,u,v};
	}

	sort(e,e+M);
 
	for(int i=0;i<M;i++) {
		auto [w,u,v]=e[i];

		auto [ww,ii]=bfs(u,v);
		auto [zz,uu,vv]=e[ii];

		if(ww!=-1) {
			adj[uu].erase({vv,ww,ii});
			adj[vv].erase({uu,ww,ii});
			E.erase({ww,uu,vv,ii});
		}

		adj[u].insert({v,w,i});
		adj[v].insert({u,w,i});
		E.insert({w,u,v,i});
 
		if(ww!=-1)
			L[i]=(w+ww),assert(ww<=w);
		else
			L[i]=0;
	}
 
	E.clear();
	for(int i=1;i<=N;i++) adj[i].clear();
 
	for(int i=M-1;i>=0;i--) {
		auto [w,u,v]=e[i];

		auto [ww,ii]=BFS(u,v);
		auto [zz,uu,vv]=e[ii];

		if(ww!=-1) {
			adj[uu].erase({vv,ww,ii});
			adj[vv].erase({uu,ww,ii});
			E.erase({ww,uu,vv,ii});
		}
		adj[u].insert({v,w,i});
		adj[v].insert({u,w,i});
		E.insert({w,u,v,i});

		if(ww!=-1)
			R[i]=(w+ww),assert(ww>=w);
		else
			R[i]=2e9+1;
	}
 
	vector<pair<int,int>> add, del;
 
	for(int i=0;i<M;i++) {
		add.push_back({L[i],i});
		del.push_back({R[i],i});
	}
 
	sort(add.begin(),add.end());
	sort(del.begin(),del.end());
 
	int pa=-1,pd=-1;
	set<int> curEdges;
 
	cin>>Q;
	for(int i=0;i<Q;i++) {
		ll x;
		cin>>x;
 
		while(pa+1<add.size() && add[pa+1].first<=2*x) {
			curEdges.insert(add[pa+1].second);
			pa++;
		} 
 
		while(pd+1<del.size() && del[pd+1].first<=2*x) {
			curEdges.erase(del[pd+1].second);
			pd++;
		}
 
		ll res=0;
		for(auto y:curEdges) res+=abs(e[y][0]-x);
 
		cout<<res<<'\n';
	}
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:139:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |   while(pa+1<add.size() && add[pa+1].first<=2*x) {
      |         ~~~~^~~~~~~~~~~
reconstruction.cpp:144:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |   while(pd+1<del.size() && del[pd+1].first<=2*x) {
      |         ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 488 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 488 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1451 ms 6524 KB Output is correct
21 Correct 217 ms 6820 KB Output is correct
22 Correct 240 ms 6592 KB Output is correct
23 Correct 294 ms 7316 KB Output is correct
24 Correct 631 ms 7108 KB Output is correct
25 Correct 1423 ms 7284 KB Output is correct
26 Correct 1440 ms 6848 KB Output is correct
27 Correct 1445 ms 6896 KB Output is correct
28 Correct 1553 ms 6888 KB Output is correct
29 Correct 906 ms 7452 KB Output is correct
30 Correct 1448 ms 6824 KB Output is correct
31 Correct 1441 ms 7264 KB Output is correct
32 Correct 1434 ms 7888 KB Output is correct
33 Correct 1427 ms 5972 KB Output is correct
34 Correct 848 ms 5388 KB Output is correct
35 Correct 1473 ms 7900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2367 ms 21996 KB Output is correct
5 Correct 2407 ms 21212 KB Output is correct
6 Correct 2353 ms 21120 KB Output is correct
7 Correct 2378 ms 22928 KB Output is correct
8 Correct 2385 ms 23032 KB Output is correct
9 Correct 2299 ms 23112 KB Output is correct
10 Correct 2787 ms 23168 KB Output is correct
11 Correct 2415 ms 25336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 488 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 2054 ms 18248 KB Output is correct
21 Correct 2054 ms 18416 KB Output is correct
22 Correct 2074 ms 18364 KB Output is correct
23 Correct 2053 ms 18356 KB Output is correct
24 Correct 2079 ms 18328 KB Output is correct
25 Correct 2041 ms 18108 KB Output is correct
26 Correct 2056 ms 18280 KB Output is correct
27 Correct 2071 ms 18468 KB Output is correct
28 Correct 2066 ms 18008 KB Output is correct
29 Correct 2041 ms 18212 KB Output is correct
30 Correct 2077 ms 18524 KB Output is correct
31 Correct 2063 ms 18000 KB Output is correct
32 Correct 2103 ms 18828 KB Output is correct
33 Correct 2129 ms 18076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 488 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1451 ms 6524 KB Output is correct
21 Correct 217 ms 6820 KB Output is correct
22 Correct 240 ms 6592 KB Output is correct
23 Correct 294 ms 7316 KB Output is correct
24 Correct 631 ms 7108 KB Output is correct
25 Correct 1423 ms 7284 KB Output is correct
26 Correct 1440 ms 6848 KB Output is correct
27 Correct 1445 ms 6896 KB Output is correct
28 Correct 1553 ms 6888 KB Output is correct
29 Correct 906 ms 7452 KB Output is correct
30 Correct 1448 ms 6824 KB Output is correct
31 Correct 1441 ms 7264 KB Output is correct
32 Correct 1434 ms 7888 KB Output is correct
33 Correct 1427 ms 5972 KB Output is correct
34 Correct 848 ms 5388 KB Output is correct
35 Correct 1473 ms 7900 KB Output is correct
36 Correct 1500 ms 5632 KB Output is correct
37 Correct 284 ms 5980 KB Output is correct
38 Correct 290 ms 5624 KB Output is correct
39 Correct 365 ms 5880 KB Output is correct
40 Correct 679 ms 5660 KB Output is correct
41 Correct 1462 ms 5764 KB Output is correct
42 Correct 1519 ms 6084 KB Output is correct
43 Correct 1498 ms 5716 KB Output is correct
44 Correct 1580 ms 5672 KB Output is correct
45 Correct 968 ms 6672 KB Output is correct
46 Correct 1481 ms 5960 KB Output is correct
47 Correct 1487 ms 5888 KB Output is correct
48 Correct 1482 ms 8020 KB Output is correct
49 Correct 1506 ms 5868 KB Output is correct
50 Correct 904 ms 5712 KB Output is correct
51 Correct 1507 ms 8144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 488 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1451 ms 6524 KB Output is correct
21 Correct 217 ms 6820 KB Output is correct
22 Correct 240 ms 6592 KB Output is correct
23 Correct 294 ms 7316 KB Output is correct
24 Correct 631 ms 7108 KB Output is correct
25 Correct 1423 ms 7284 KB Output is correct
26 Correct 1440 ms 6848 KB Output is correct
27 Correct 1445 ms 6896 KB Output is correct
28 Correct 1553 ms 6888 KB Output is correct
29 Correct 906 ms 7452 KB Output is correct
30 Correct 1448 ms 6824 KB Output is correct
31 Correct 1441 ms 7264 KB Output is correct
32 Correct 1434 ms 7888 KB Output is correct
33 Correct 1427 ms 5972 KB Output is correct
34 Correct 848 ms 5388 KB Output is correct
35 Correct 1473 ms 7900 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 2367 ms 21996 KB Output is correct
40 Correct 2407 ms 21212 KB Output is correct
41 Correct 2353 ms 21120 KB Output is correct
42 Correct 2378 ms 22928 KB Output is correct
43 Correct 2385 ms 23032 KB Output is correct
44 Correct 2299 ms 23112 KB Output is correct
45 Correct 2787 ms 23168 KB Output is correct
46 Correct 2415 ms 25336 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 2054 ms 18248 KB Output is correct
49 Correct 2054 ms 18416 KB Output is correct
50 Correct 2074 ms 18364 KB Output is correct
51 Correct 2053 ms 18356 KB Output is correct
52 Correct 2079 ms 18328 KB Output is correct
53 Correct 2041 ms 18108 KB Output is correct
54 Correct 2056 ms 18280 KB Output is correct
55 Correct 2071 ms 18468 KB Output is correct
56 Correct 2066 ms 18008 KB Output is correct
57 Correct 2041 ms 18212 KB Output is correct
58 Correct 2077 ms 18524 KB Output is correct
59 Correct 2063 ms 18000 KB Output is correct
60 Correct 2103 ms 18828 KB Output is correct
61 Correct 2129 ms 18076 KB Output is correct
62 Correct 1500 ms 5632 KB Output is correct
63 Correct 284 ms 5980 KB Output is correct
64 Correct 290 ms 5624 KB Output is correct
65 Correct 365 ms 5880 KB Output is correct
66 Correct 679 ms 5660 KB Output is correct
67 Correct 1462 ms 5764 KB Output is correct
68 Correct 1519 ms 6084 KB Output is correct
69 Correct 1498 ms 5716 KB Output is correct
70 Correct 1580 ms 5672 KB Output is correct
71 Correct 968 ms 6672 KB Output is correct
72 Correct 1481 ms 5960 KB Output is correct
73 Correct 1487 ms 5888 KB Output is correct
74 Correct 1482 ms 8020 KB Output is correct
75 Correct 1506 ms 5868 KB Output is correct
76 Correct 904 ms 5712 KB Output is correct
77 Correct 1507 ms 8144 KB Output is correct
78 Correct 3576 ms 20120 KB Output is correct
79 Correct 2443 ms 22084 KB Output is correct
80 Correct 2492 ms 21380 KB Output is correct
81 Correct 2555 ms 21280 KB Output is correct
82 Correct 2736 ms 20180 KB Output is correct
83 Correct 3566 ms 20152 KB Output is correct
84 Correct 3513 ms 20412 KB Output is correct
85 Correct 3508 ms 20376 KB Output is correct
86 Correct 3710 ms 20220 KB Output is correct
87 Correct 3862 ms 22300 KB Output is correct
88 Correct 3506 ms 20200 KB Output is correct
89 Correct 3563 ms 20368 KB Output is correct
90 Correct 3704 ms 22340 KB Output is correct
91 Correct 3485 ms 20244 KB Output is correct
92 Correct 2930 ms 22968 KB Output is correct
93 Correct 3640 ms 23320 KB Output is correct