比赛 20241022 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 电梯 最终得分 100
用户昵称 flyfree 运行时间 1.212 s
代码语言 C++ 内存使用 6.81 MiB
提交时间 2024-10-22 11:03:49
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100010
inline ll read(){
	ll x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
struct node{
	ll l,r,id;
};
struct node_tr{
	ll l,r,maxz;
}tr[MAXN*4];
void push_up(ll now){
	tr[now].maxz=max(tr[now*2].maxz,tr[now*2+1].maxz);
}
void build(ll now,ll l,ll r){
	tr[now].l=l,tr[now].r=r;
	if(l==r)return;
	ll mid=(l+r)/2;
	build(now*2,l,mid);
	build(now*2+1,mid+1,r);
}
void ad_(ll now,ll loc,ll  val){
	if(tr[now].l==tr[now].r){
		tr[now].maxz+=val;
		return;
	}
	ll mid=(tr[now].l+tr[now].r)/2;
	if(loc<=mid)ad_(now*2,loc,val);
	else ad_(now*2+1,loc,val);
	push_up(now);
}
ll re_(ll now,ll l,ll r){
	if(tr[now].l>=l&&tr[now].r<=r)return tr[now].maxz;
	ll mid=(tr[now].l+tr[now].r)/2,ans=0;
	if(l<=mid)ans=max(ans,re_(now*2,l,r));
	if(r>mid)ans=max(ans,re_(now*2+1,l,r));
	return ans;
}
ll n;
ll a[MAXN],t[MAXN],dp[MAXN];
deque <node> q;
ll find1(ll l,ll r,ll val){
	while(l<r){
		ll mid=(l+r+1)/2;
		if(dp[mid]<=val)l=mid;
		else r=mid-1;
	}
	return l;
}
ll re_ans(ll l,ll r){
	return max(dp[l],t[r])+re_(1,l+1,r)*2;
}
ll find2(ll l,ll r,ll id,ll p){
	while(l<r){
		ll mid=(l+r)/2;
		if(re_ans(id,mid)>=re_ans(p,mid))r=mid;
		else l=mid+1;
	}
	return l;
}
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	n=read();
	q.push_front((node){1,n,0});
	build(1,1,n);
	for(int i=1;i<=n;i++){
		t[i]=read(),a[i]=read();
		ad_(1,i,a[i]);
	}
	for(int i=1;i<=n;i++){
		while(!q.empty()&&q.front().r<i)q.pop_front();
		ll loc=find1(0,i-1,t[i]);
//		cout<<loc<<" "<<q.front().id<<" "<<re_(1,loc+1,i)<<endl;
		dp[i]=re_ans(q.front().id,i);
//		cout<<q.front().id<<" ";
//		cout<<dp[i]<<endl;
		if(q.front().id<=loc)dp[i]=t[i]+re_(1,loc+1,i)*2;
		else dp[i]=min(t[i]+re_(1,loc+1,i)*2,re_ans(q.front().id,i));
		ll now=n+1;
		while(!q.empty()){
//			cout<<q.back().l<<" "<<q.back().r<<" "<<q.back().id<<" "<<re_ans(i,q.back().l)<<" "<<re_ans(q.back().id,q.back().l)<<endl;
			if(re_ans(i,q.back().l)<=re_ans(q.back().id,q.back().l)){
				now=q.back().l;
				q.pop_back();
			}else break;
		}
		if(q.empty())q.push_back((node){i+1,n,i});
		else if(re_ans(i,q.back().r)>re_ans(q.back().id,q.back().r)){
//			cout<<re_ans(i,q.back().r)<<" "<<re_ans(q.back().id,q.back().r)<<endl;
			if(now<=n)q.push_back((node){now,n,i});
		}else{
			now=find2(q.back().l,q.back().r,q.back().id,i);
			ll r_l=q.back().l,r_id=q.back().id;
			q.pop_back();
			if(now>r_l)q.push_back((node){r_l,now-1,r_id});
			q.push_back((node){now,n,i});
		}
//		cout<<now<<" ";
//		cout<<dp[i]<<endl;
	}
	cout<<dp[n];
	return 0;
}