답안 #1105808

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1105808 2024-10-27T22:59:12 Z _rain_ Palembang Bridges (APIO15_bridge) C++14
100 / 100
144 ms 12576 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define name "main"
#define int long long
typedef pair<int,int> ii;
#define fi first 
#define se second

const int N=(int)1e5;
bool print=false;
vector<ii>sett;
vector<ll>nen;
int n,k,cnt=0;
ll f[N+2];
bool used[N*2+2];

bool cmp(ii a,ii b){
	return a.fi+a.se<b.fi+b.se;
}
class BIT{
public:
	vector<ll>tot;
	vector<int> cnt,one;
	int n;
	void init(int _n){
		n=_n;
		tot.assign(_n+2,0);
		cnt.assign(_n+2,0);
		one.assign(_n+2,0);
		return;
	}
	void upd_tot(int id,int val){
		for(;id<=n;id+=(id&(-id)))
			tot[id]+=val;
		return;
	}
	void upd_cnt(int id,int val){
		for(;id<=n;id+=(id&(-id)))
			cnt[id]+=val;
		return;
	}
	void upd_one(int id,int val){
		for(;id<=n;id+=(id&(-id)))
			one[id]+=val;
	}
	int Get_tot(int id){
		ll sum=0;
		for(;id;id-=(id&(-id)))
			sum+=tot[id];
		return sum;
	}
	ll Get_cnt(int id){
		ll sum=0;
		for(;id;id-=(id&(-id)))
			sum+=cnt[id];

		return sum;
	}
	ll Get_one(int id){
		ll sum=0;
		for(;id;id-=(id&(-id)))
			sum+=one[id];
		return sum;
	}
	ll sum_range(int t,int l,int r){
		if (t==1){
			return Get_tot(r)-Get_tot(l-1);
		}
		if (t==2){
			return Get_cnt(r)-Get_cnt(l-1);
		}
		if (t==3){
			return Get_one(r)-Get_one(l-1);	
		}
	}
};	
BIT tree;

ll F(int i){
	int l=1,r=nen.size(),p=1;
	while(l<=r){
		int m=(l+r)>>1;
		if (tree.sum_range(2,1,m)<=(cnt+1)/2){
			l=m+1,p=m;
		}
		else r=m-1;
	}
	return nen[p]*tree.sum_range(2,1,p)-tree.sum_range(1,1,p)+tree.sum_range(1,p+1,nen.size())-nen[p]*tree.sum_range(2,p+1,nen.size())+(ll)i;
}

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>k>>n;
	ll ans=0;
	nen.push_back(-1);
	for (int i=1;i<=n;++i){
		char c1,c2; int a,b;
		cin>>c1>>a>>c2>>b;
		if (c1==c2) ans+=abs(a-b);
		else {
			nen.push_back(a);
			nen.push_back(b);
			sett.push_back({a,b});
		}
	}
	sort(nen.begin(),nen.end()); nen.resize(unique(nen.begin(),nen.end())-nen.begin());
	sort(sett.begin(),sett.end(),cmp);
	tree.init(nen.size());
	for (int i=1;i<=sett.size();++i){
		int a=sett[i-1].fi,b=sett[i-1].se;
		a=upper_bound(nen.begin(),nen.end(),a)-nen.begin();
		b=upper_bound(nen.begin(),nen.end(),b)-nen.begin();
		tree.upd_cnt(a,1),tree.upd_cnt(b,1);
		tree.upd_tot(a,nen[a-1]),tree.upd_tot(b,nen[b-1]);
		cnt+=2;
		f[i]=F(i);
	}
	if (k==1) ans=ans+f[sett.size()];
	else{
		tree.init(nen.size());
		cnt=0;
		ll add=(ll)1e18;
		for (int i=sett.size();i>=1;--i){
			int a=sett[i-1].fi,b=sett[i-1].se;
			a=upper_bound(nen.begin(),nen.end(),a)-nen.begin();
			b=upper_bound(nen.begin(),nen.end(),b)-nen.begin();
			tree.upd_cnt(a,1),tree.upd_cnt(b,1);
			tree.upd_tot(a,nen[a-1]),tree.upd_tot(b,nen[b-1]);
			cnt+=2;
			add=min(add,F(sett.size()-i+1)+f[i-1]);
		}
		ans=min(ans+f[sett.size()],ans+add);
	}
	cout<<ans;
	exit(0);
}

Compilation message

bridge.cpp: In function 'int32_t main()':
bridge.cpp:112:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |  for (int i=1;i<=sett.size();++i){
      |               ~^~~~~~~~~~~~~
bridge.cpp: In member function 'll BIT::sum_range(long long int, long long int, long long int)':
bridge.cpp:77:2: warning: control reaches end of non-void function [-Wreturn-type]
   77 |  }
      |  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 23 ms 5828 KB Output is correct
13 Correct 95 ms 10156 KB Output is correct
14 Correct 53 ms 5560 KB Output is correct
15 Correct 54 ms 5556 KB Output is correct
16 Correct 25 ms 5816 KB Output is correct
17 Correct 74 ms 10164 KB Output is correct
18 Correct 66 ms 10156 KB Output is correct
19 Correct 72 ms 9904 KB Output is correct
20 Correct 25 ms 5816 KB Output is correct
21 Correct 68 ms 10216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 508 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 592 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 504 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 2 ms 848 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 24 ms 6500 KB Output is correct
26 Correct 41 ms 6584 KB Output is correct
27 Correct 143 ms 11736 KB Output is correct
28 Correct 144 ms 12460 KB Output is correct
29 Correct 144 ms 12576 KB Output is correct
30 Correct 73 ms 6336 KB Output is correct
31 Correct 27 ms 7084 KB Output is correct
32 Correct 99 ms 12456 KB Output is correct
33 Correct 100 ms 12016 KB Output is correct
34 Correct 117 ms 12180 KB Output is correct
35 Correct 30 ms 7436 KB Output is correct
36 Correct 100 ms 12212 KB Output is correct
37 Correct 23 ms 6328 KB Output is correct