Submission #445149

# Submission time Handle Problem Language Result Execution time Memory
445149 2021-07-16T15:46:54 Z keta_tsimakuridze Building Bridges (CEOI17_building) C++14
30 / 100
106 ms 9296 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
using namespace std;
const int N=2e6+5,mod=1e9+7;
int t,a[N],b[N],n;
pair<ll,ll>tree[4*N];
ll f(ll k,ll x,ll b){
	return k*x+b;
}
void insert(int u,int l,int r,ll k,ll b) {
	if(l==r) {
		if(f(tree[u].f,l,tree[u].s) > f(k,l,b)) {
			tree[u] = {k,b};
		}
		return;
	}
	int mid = (l+r)/2;
	if(k < tree[u].f) {
		swap(k,tree[u].f);
		swap(b,tree[u].s);
	}
	if(f(k,mid,b) >= f(tree[u].f,mid,tree[u].s)) {
		insert(2*u,l,mid,k,b);
	}
	else {
		insert(2*u+1,mid+1,r,tree[u].f,tree[u].s);
		tree[u] = {k,b};
	}
}
ll getans(int u,int ind,int l,int r) {
	if(l==r) {
		return f(tree[u].f,ind,tree[u].s);
	}
	int mid = (l+r)/2;
	if(ind <= mid) return min(f(tree[u].f,ind,tree[u].s),getans(2*u,ind,l,mid));
	return min(f(tree[u].f,ind,tree[u].s),getans(2*u+1,ind,mid+1,r));
}
void build(int u,int l,int r) {
	tree[u] = {1e7,1e17};
	if(l==r) return;
	int mid = (l+r)/2;
	build(2*u,l,mid);
	build(2*u+1,mid+1,r);
}
 main(){
	cin>>n;
	int mx = 0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		mx = max(mx,a[i]);
	}
	mx *= 2;
	for(int i=1;i<=n;i++){
		cin>>b[i];
		b[i] += b[i-1];
	}
	build(1,1,mx);
	insert(1,1,mx,-2*a[1],-b[1]+a[1]*a[1]);
	for(int i=2;i<=n;i++) {
		ll ans = getans(1,a[i],1,mx) + a[i]*a[i] + b[i-1];
		insert(1,1,mx,-2*a[i],ans-b[i]+a[i]*a[i]);
		if(i==n) cout<<ans;
	}
}

Compilation message

building.cpp:47:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   47 |  main(){
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 106 ms 9296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Incorrect 106 ms 9296 KB Output isn't correct
7 Halted 0 ms 0 KB -