답안 #568852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568852 2022-05-26T09:29:33 Z errorgorn Reconstruction Project (JOI22_reconstruction) C++17
70 / 100
5000 ms 798536 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);
	}
}

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;
	
	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;
	//}
	
	cin>>q;
	rep(x,0,q) cin>>qu[x];
	
	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;
      |        ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5040 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5040 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2583 ms 788332 KB Output is correct
21 Correct 1856 ms 747732 KB Output is correct
22 Correct 1945 ms 779596 KB Output is correct
23 Correct 1981 ms 786616 KB Output is correct
24 Correct 2208 ms 787780 KB Output is correct
25 Correct 2640 ms 788284 KB Output is correct
26 Correct 2503 ms 788756 KB Output is correct
27 Correct 2495 ms 788604 KB Output is correct
28 Correct 2507 ms 788492 KB Output is correct
29 Correct 2513 ms 788684 KB Output is correct
30 Correct 2526 ms 788568 KB Output is correct
31 Correct 2570 ms 788420 KB Output is correct
32 Correct 2544 ms 788504 KB Output is correct
33 Correct 2529 ms 788340 KB Output is correct
34 Correct 2514 ms 788636 KB Output is correct
35 Correct 2565 ms 788596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Execution timed out 5053 ms 798536 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5040 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 517 ms 40900 KB Output is correct
21 Correct 499 ms 40484 KB Output is correct
22 Correct 503 ms 40528 KB Output is correct
23 Correct 493 ms 40448 KB Output is correct
24 Correct 495 ms 40520 KB Output is correct
25 Correct 491 ms 40576 KB Output is correct
26 Correct 411 ms 40520 KB Output is correct
27 Correct 266 ms 40616 KB Output is correct
28 Correct 520 ms 40696 KB Output is correct
29 Correct 482 ms 40636 KB Output is correct
30 Correct 372 ms 40676 KB Output is correct
31 Correct 369 ms 40616 KB Output is correct
32 Correct 231 ms 40948 KB Output is correct
33 Correct 249 ms 40148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5040 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2583 ms 788332 KB Output is correct
21 Correct 1856 ms 747732 KB Output is correct
22 Correct 1945 ms 779596 KB Output is correct
23 Correct 1981 ms 786616 KB Output is correct
24 Correct 2208 ms 787780 KB Output is correct
25 Correct 2640 ms 788284 KB Output is correct
26 Correct 2503 ms 788756 KB Output is correct
27 Correct 2495 ms 788604 KB Output is correct
28 Correct 2507 ms 788492 KB Output is correct
29 Correct 2513 ms 788684 KB Output is correct
30 Correct 2526 ms 788568 KB Output is correct
31 Correct 2570 ms 788420 KB Output is correct
32 Correct 2544 ms 788504 KB Output is correct
33 Correct 2529 ms 788340 KB Output is correct
34 Correct 2514 ms 788636 KB Output is correct
35 Correct 2565 ms 788596 KB Output is correct
36 Correct 3211 ms 789272 KB Output is correct
37 Correct 2407 ms 749324 KB Output is correct
38 Correct 2586 ms 780120 KB Output is correct
39 Correct 2627 ms 787396 KB Output is correct
40 Correct 2921 ms 788200 KB Output is correct
41 Correct 3325 ms 789192 KB Output is correct
42 Correct 3303 ms 789004 KB Output is correct
43 Correct 3217 ms 789332 KB Output is correct
44 Correct 2693 ms 789300 KB Output is correct
45 Correct 2552 ms 789224 KB Output is correct
46 Correct 3135 ms 789252 KB Output is correct
47 Correct 3175 ms 789096 KB Output is correct
48 Correct 3180 ms 789328 KB Output is correct
49 Correct 3139 ms 789244 KB Output is correct
50 Correct 2523 ms 789188 KB Output is correct
51 Correct 2539 ms 788920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5040 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2583 ms 788332 KB Output is correct
21 Correct 1856 ms 747732 KB Output is correct
22 Correct 1945 ms 779596 KB Output is correct
23 Correct 1981 ms 786616 KB Output is correct
24 Correct 2208 ms 787780 KB Output is correct
25 Correct 2640 ms 788284 KB Output is correct
26 Correct 2503 ms 788756 KB Output is correct
27 Correct 2495 ms 788604 KB Output is correct
28 Correct 2507 ms 788492 KB Output is correct
29 Correct 2513 ms 788684 KB Output is correct
30 Correct 2526 ms 788568 KB Output is correct
31 Correct 2570 ms 788420 KB Output is correct
32 Correct 2544 ms 788504 KB Output is correct
33 Correct 2529 ms 788340 KB Output is correct
34 Correct 2514 ms 788636 KB Output is correct
35 Correct 2565 ms 788596 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 5036 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Execution timed out 5053 ms 798536 KB Time limit exceeded
40 Halted 0 ms 0 KB -