Submission #568871

# Submission time Handle Problem Language Result Execution time Memory
568871 2022-05-26T09:48:34 Z errorgorn Reconstruction Project (JOI22_reconstruction) C++17
77 / 100
3431 ms 789948 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound

#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct UFDS{
	int p[505];
	int s[505];
	vector<ii> roll;
	
	UFDS(){
		rep(x,0,505) p[x]=x,s[x]=1;
		roll.clear();
	}
	
	int par(int i){
		while (p[i]!=i) i=p[i];
		return i;
	}
	
	bool unions(int i,int j){
		i=par(i),j=par(j);
		if (i==j) return false;
		
		if (s[i]>s[j]) swap(i,j);
		p[i]=j;
		s[j]+=s[i];
		roll.pub({i,j});
		
		return true;
	}
	
	void rollback(int ss){
		while (sz(roll)>ss){
			int i,j;
			tie(i,j)=roll.back(); roll.pob();
			
			p[i]=i;
			s[j]-=s[i];
		}
	}
} ufds;

struct UFDS2{
	int p[505];
	
	void reset(){
		rep(x,0,505) p[x]=x;
	}
	
	int par(int i){
		if (p[i]==i) return i;
		else return p[i]=par(p[i]);
	}
	
	bool unions(int i,int j){
		i=par(i),j=par(j);
		if (i==j) return false;
		p[i]=j;
		return true;
	}
} ufds2;

int n,m,q;
struct E{
	int u,v;
	int w;
};

E ee[100005];

void fix(vector<int> &v){
	vector<int> temp;
	
	ufds2.reset();
	while (!v.empty()){
		if (ufds2.unions(ee[v.back()].u,ee[v.back()].v)){
			temp.pub(v.back());
		}
		v.pob();
	}
	
	v=temp;
	reverse(all(v));
}

vector<int> f[100005];
vector<int> b[100005];

int qu[1000005];
int split[1000005];
long long ans[1000005];

void solve(int l,int r,vector<int> vl,vector<int> vr,ll cans,ll lu,ll ru){
	int m=l+r>>1;
	
	int ss=sz(ufds.roll);
	
	vector<int> vl1,vl2;
	vector<int> vr1,vr2;
	
	ll lans=0,rans=0;
	ll lu2=lu,ru2=ru;
	
	while (!vl.empty() || !vr.empty()){
		if (vr.empty() ||
			(!vl.empty() && qu[m]-ee[vl.back()].w<=ee[vr.back()].w-qu[m])){
			
			if (ufds.unions(ee[vl.back()].u,ee[vl.back()].v)){
				lans+=-ee[vl.back()].w;
				lu2++;
				
				vl1.pub(vl.back());
			}
			else{
				vl2.pub(vl.back());
			}
			vl.pob();
		}
		else{
			if (ufds.unions(ee[vr.back()].u,ee[vr.back()].v)){
				rans+=ee[vr.back()].w;
				ru2++;
				
				vr1.pub(vr.back());
			}
			else{
				vr2.pub(vr.back());
			}
			vr.pob();
		}
	}
	
	ans[m]=cans+lans+rans+lu2*qu[m]-ru2*qu[m];
	
	reverse(all(vl1)),reverse(all(vl2));
	reverse(all(vr1)),reverse(all(vr2));
	
	//cout<<"debug: "<<l<<" "<<m<<" "<<r<<endl;
	//for (auto it:vl1) cout<<it<<" "; cout<<endl;
	//for (auto it:vl2) cout<<it<<" "; cout<<endl;
	//for (auto it:vr1) cout<<it<<" "; cout<<endl;
	//for (auto it:vr2) cout<<it<<" "; cout<<endl;
	
	//vl1,vr1 are those things added at this stage
	ufds.rollback(ss);
	if (l!=m){
		for (auto it:vl1) ufds.unions(ee[it].u,ee[it].v);
		solve(l,m-1,vl2,vr1,cans+lans,lu2,ru);
		ufds.rollback(ss);
	}
	if (m!=r){
		for (auto it:vr1) ufds.unions(ee[it].u,ee[it].v);
		solve(m+1,r,vl1,vr2,cans+rans,lu,ru2);
		ufds.rollback(ss);
	}
}

namespace subtask3{
	vector<int> w[100005];
	int idx[100005];
	
	void solve(){
		int a,b,c;
		rep(x,1,m+1) w[ee[x].u].pub(ee[x].w);
		
		rep(x,1,n) sort(all(w[x]));
		
		rep(i,0,q){
			long long ans=0;
			rep(x,1,n){
				while (idx[x]+1<sz(w[x]) && abs(w[x][idx[x]+1]-qu[i])<abs(w[x][idx[x]]-qu[i])){
					idx[x]++;
				}
				ans+=abs(w[x][idx[x]]-qu[i]);
			}
			
			cout<<ans<<endl;
		}
	}
}

signed main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>n>>m;
	rep(x,1,m+1) cin>>ee[x].u>>ee[x].v>>ee[x].w;
	cin>>q;
	rep(x,0,q) cin>>qu[x];
	
	if (m>1000 && q>20000){
		subtask3::solve();
		return 0;
	}
	
	sort(ee+1,ee+m+1,[](E i,E j){
		return i.w<j.w;
	});
	
	rep(x,1,m+1){
		f[x].pub(x);
		fix(f[x]);
		f[x+1]=f[x];
	}
	rep(x,m+1,1){
		b[x].pub(x);
		fix(b[x]);
		b[x-1]=b[x];
	}
	
	//rep(x,1,m+1) cout<<x<<" "; cout<<endl;
	//rep(x,1,m+1) cout<<ee[x].u<<" "; cout<<endl;
	//rep(x,1,m+1) cout<<ee[x].v<<" "; cout<<endl;
	
	//rep(x,1,m+1){
		//cout<<x<<": "; for (auto it:f[x]) cout<<it<<" "; cout<<endl;
	//}
	
	split[0]=1;
	rep(x,0,q){
		while (split[x]<=m && ee[split[x]].w<=qu[x]) split[x]++;
		split[x+1]=split[x];
	}
	
	//rep(x,0,q) cout<<split[x]<<" "; cout<<endl;
	
	rep(x,1,m+2){
		int l=lb(split,split+q,x)-split;
		int r=ub(split,split+q,x)-split-1;
		
		if (l<=r) solve(l,r,f[x-1],b[x],0,0,0);
	}
	
	rep(x,0,q) cout<<ans[x]<<endl;
}

Compilation message

reconstruction.cpp: In function 'void solve(int, int, std::vector<int>, std::vector<int>, long long int, long long int, long long int)':
reconstruction.cpp:112:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  112 |  int m=l+r>>1;
      |        ~^~
reconstruction.cpp: In function 'void subtask3::solve()':
reconstruction.cpp:181:7: warning: unused variable 'a' [-Wunused-variable]
  181 |   int a,b,c;
      |       ^
reconstruction.cpp:181:9: warning: unused variable 'b' [-Wunused-variable]
  181 |   int a,b,c;
      |         ^
reconstruction.cpp:181:11: warning: unused variable 'c' [-Wunused-variable]
  181 |   int a,b,c;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7400 KB Output is correct
2 Correct 6 ms 7364 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 6 ms 7396 KB Output is correct
5 Correct 5 ms 7396 KB Output is correct
6 Correct 5 ms 7396 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 6 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7332 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7396 KB Output is correct
16 Correct 5 ms 7364 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7400 KB Output is correct
2 Correct 6 ms 7364 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 6 ms 7396 KB Output is correct
5 Correct 5 ms 7396 KB Output is correct
6 Correct 5 ms 7396 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 6 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7332 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7396 KB Output is correct
16 Correct 5 ms 7364 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 2616 ms 789444 KB Output is correct
21 Correct 1932 ms 748828 KB Output is correct
22 Correct 2013 ms 780492 KB Output is correct
23 Correct 2009 ms 787544 KB Output is correct
24 Correct 2277 ms 788796 KB Output is correct
25 Correct 2760 ms 789304 KB Output is correct
26 Correct 2613 ms 789492 KB Output is correct
27 Correct 2547 ms 789504 KB Output is correct
28 Correct 2526 ms 789452 KB Output is correct
29 Correct 2567 ms 789612 KB Output is correct
30 Correct 2535 ms 789436 KB Output is correct
31 Correct 2556 ms 789384 KB Output is correct
32 Correct 2617 ms 789580 KB Output is correct
33 Correct 2609 ms 789352 KB Output is correct
34 Correct 2650 ms 789244 KB Output is correct
35 Correct 2623 ms 789336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7392 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 2505 ms 24764 KB Output is correct
5 Correct 2527 ms 25532 KB Output is correct
6 Correct 2511 ms 25452 KB Output is correct
7 Correct 2484 ms 27428 KB Output is correct
8 Correct 1832 ms 27800 KB Output is correct
9 Correct 1596 ms 27472 KB Output is correct
10 Correct 2542 ms 25488 KB Output is correct
11 Correct 1821 ms 27672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7400 KB Output is correct
2 Correct 6 ms 7364 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 6 ms 7396 KB Output is correct
5 Correct 5 ms 7396 KB Output is correct
6 Correct 5 ms 7396 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 6 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7332 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7396 KB Output is correct
16 Correct 5 ms 7364 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 6 ms 7380 KB Output is correct
20 Correct 526 ms 41764 KB Output is correct
21 Correct 493 ms 41596 KB Output is correct
22 Correct 544 ms 41764 KB Output is correct
23 Correct 532 ms 41684 KB Output is correct
24 Correct 514 ms 41752 KB Output is correct
25 Correct 522 ms 41696 KB Output is correct
26 Correct 435 ms 41744 KB Output is correct
27 Correct 237 ms 41596 KB Output is correct
28 Correct 496 ms 41756 KB Output is correct
29 Correct 511 ms 41728 KB Output is correct
30 Correct 408 ms 41816 KB Output is correct
31 Correct 406 ms 41680 KB Output is correct
32 Correct 255 ms 42192 KB Output is correct
33 Correct 244 ms 41340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7400 KB Output is correct
2 Correct 6 ms 7364 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 6 ms 7396 KB Output is correct
5 Correct 5 ms 7396 KB Output is correct
6 Correct 5 ms 7396 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 6 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7332 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7396 KB Output is correct
16 Correct 5 ms 7364 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 2616 ms 789444 KB Output is correct
21 Correct 1932 ms 748828 KB Output is correct
22 Correct 2013 ms 780492 KB Output is correct
23 Correct 2009 ms 787544 KB Output is correct
24 Correct 2277 ms 788796 KB Output is correct
25 Correct 2760 ms 789304 KB Output is correct
26 Correct 2613 ms 789492 KB Output is correct
27 Correct 2547 ms 789504 KB Output is correct
28 Correct 2526 ms 789452 KB Output is correct
29 Correct 2567 ms 789612 KB Output is correct
30 Correct 2535 ms 789436 KB Output is correct
31 Correct 2556 ms 789384 KB Output is correct
32 Correct 2617 ms 789580 KB Output is correct
33 Correct 2609 ms 789352 KB Output is correct
34 Correct 2650 ms 789244 KB Output is correct
35 Correct 2623 ms 789336 KB Output is correct
36 Correct 3311 ms 789948 KB Output is correct
37 Correct 2484 ms 749636 KB Output is correct
38 Correct 2680 ms 780316 KB Output is correct
39 Correct 2702 ms 787780 KB Output is correct
40 Correct 3021 ms 789136 KB Output is correct
41 Correct 3431 ms 789532 KB Output is correct
42 Correct 3378 ms 789572 KB Output is correct
43 Correct 3164 ms 789684 KB Output is correct
44 Correct 2716 ms 789784 KB Output is correct
45 Correct 2592 ms 789752 KB Output is correct
46 Correct 3205 ms 789620 KB Output is correct
47 Correct 3269 ms 789616 KB Output is correct
48 Correct 3231 ms 789704 KB Output is correct
49 Correct 3175 ms 789784 KB Output is correct
50 Correct 2545 ms 789652 KB Output is correct
51 Correct 2555 ms 789704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7400 KB Output is correct
2 Correct 6 ms 7364 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 6 ms 7396 KB Output is correct
5 Correct 5 ms 7396 KB Output is correct
6 Correct 5 ms 7396 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 6 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7332 KB Output is correct
12 Correct 5 ms 7380 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7396 KB Output is correct
16 Correct 5 ms 7364 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 2616 ms 789444 KB Output is correct
21 Correct 1932 ms 748828 KB Output is correct
22 Correct 2013 ms 780492 KB Output is correct
23 Correct 2009 ms 787544 KB Output is correct
24 Correct 2277 ms 788796 KB Output is correct
25 Correct 2760 ms 789304 KB Output is correct
26 Correct 2613 ms 789492 KB Output is correct
27 Correct 2547 ms 789504 KB Output is correct
28 Correct 2526 ms 789452 KB Output is correct
29 Correct 2567 ms 789612 KB Output is correct
30 Correct 2535 ms 789436 KB Output is correct
31 Correct 2556 ms 789384 KB Output is correct
32 Correct 2617 ms 789580 KB Output is correct
33 Correct 2609 ms 789352 KB Output is correct
34 Correct 2650 ms 789244 KB Output is correct
35 Correct 2623 ms 789336 KB Output is correct
36 Correct 5 ms 7392 KB Output is correct
37 Correct 5 ms 7380 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 2505 ms 24764 KB Output is correct
40 Correct 2527 ms 25532 KB Output is correct
41 Correct 2511 ms 25452 KB Output is correct
42 Correct 2484 ms 27428 KB Output is correct
43 Correct 1832 ms 27800 KB Output is correct
44 Correct 1596 ms 27472 KB Output is correct
45 Correct 2542 ms 25488 KB Output is correct
46 Correct 1821 ms 27672 KB Output is correct
47 Correct 6 ms 7380 KB Output is correct
48 Correct 526 ms 41764 KB Output is correct
49 Correct 493 ms 41596 KB Output is correct
50 Correct 544 ms 41764 KB Output is correct
51 Correct 532 ms 41684 KB Output is correct
52 Correct 514 ms 41752 KB Output is correct
53 Correct 522 ms 41696 KB Output is correct
54 Correct 435 ms 41744 KB Output is correct
55 Correct 237 ms 41596 KB Output is correct
56 Correct 496 ms 41756 KB Output is correct
57 Correct 511 ms 41728 KB Output is correct
58 Correct 408 ms 41816 KB Output is correct
59 Correct 406 ms 41680 KB Output is correct
60 Correct 255 ms 42192 KB Output is correct
61 Correct 244 ms 41340 KB Output is correct
62 Correct 3311 ms 789948 KB Output is correct
63 Correct 2484 ms 749636 KB Output is correct
64 Correct 2680 ms 780316 KB Output is correct
65 Correct 2702 ms 787780 KB Output is correct
66 Correct 3021 ms 789136 KB Output is correct
67 Correct 3431 ms 789532 KB Output is correct
68 Correct 3378 ms 789572 KB Output is correct
69 Correct 3164 ms 789684 KB Output is correct
70 Correct 2716 ms 789784 KB Output is correct
71 Correct 2592 ms 789752 KB Output is correct
72 Correct 3205 ms 789620 KB Output is correct
73 Correct 3269 ms 789616 KB Output is correct
74 Correct 3231 ms 789704 KB Output is correct
75 Correct 3175 ms 789784 KB Output is correct
76 Correct 2545 ms 789652 KB Output is correct
77 Correct 2555 ms 789704 KB Output is correct
78 Runtime error 164 ms 30240 KB Execution killed with signal 11
79 Halted 0 ms 0 KB -