[HNOI2002]营业额统计

/*http://www.lydsy.com/JudgeOnline/problem.php?id=1588*/

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int maxn=2000005;

int L[maxn],R[maxn],S[maxn],V[maxn];

int n,p[maxn];
int cnt_node,root;

void Right_Rotate(int &t){
	int k=L[t];
	L[t]=R[k];
	R[k]=t;
	S[k]=S[t];
	S[t]=S[L[t]]+S[R[t]]+1;
	t=k;
}

void Left_Rotate(int &t){
	int k=R[t];
	R[t]=L[k];
	L[k]=t;
	S[k]=S[t];
	S[t]=S[L[t]]+S[R[t]]+1;
	t=k;
}

void maintain(int &t,bool flag){
	if(flag)
		if(S[R[R[t]]]>S[L[t]])
			Left_Rotate(t);
		else if(S[L[R[t]]]>S[L[t]])
			Right_Rotate(R[t]),Left_Rotate(t);
		else
			return ;
	else
		if(S[L[L[t]]]>S[R[t]])
			Right_Rotate(t);
		else if(S[R[L[t]]]>S[R[t]])
			Left_Rotate(L[t]),Right_Rotate(t);
		else
			return ;
	maintain(L[t],0);
	maintain(R[t],1);
	maintain(t,0);
	maintain(t,1);
}

void Init(){
	cnt_node=root=S[0]=0;
}

void Insert(int &t,int v){
	if(t){
		S[t]++;
		if(v<V[t])Insert(L[t],v);
		else Insert(R[t],v);
		maintain(t,v>=V[t]);
	}else{
		V[t=++cnt_node]=v;
		S[t]=1;
		L[t]=R[t]=0;
	}
}
/* v没有前驱的话就返回v 因为不知道是SBT中本身存在v
还是返回没有前驱,所以加一个p[maxn]标记一下*/

int Pred(int t,int v){
	if(!t)return v;
	if(v<=V[t]) return Pred(L[t],v);
	else{
		int tmp=Pred(R[t],v);
		return tmp==v?V[t]:tmp;
	}
}

int Succ(int t,int v){
	if(!t)return v;
	if(v>=V[t])return Succ(R[t],v);
	else{
		int tmp=Succ(L[t],v);
		return tmp==v?V[t]:tmp;
	}
}

int Min(int a,int b){
	return a<b?a:b;
}

int main(){
	int i,j,a,x,y;
	Init();
//	memset(p,0,sizeof(p));
	scanf("%d%d",&n,&a);
	Insert(root,a);
	int ans=a;
	p[a]=1;
	for(i=1;i<n;i++){
		if(scanf("%d",&a)==EOF)a=0;
		if(!p[a]){
			int x=a-Pred(root,a);
			int y=Succ(root,a)-a;
			Insert(root,a);
			if(x==0)x=maxn;if(y==0)y=maxn;
			ans+=Min(x,y);
			p[a]=1;
		}
	}
	printf("%d\n",ans);
	return 0;
}

根据引用\[1\]和引用\[2\]的描述,题目中的影魔拥有n个灵魂,每个灵魂有一个战斗力ki。对于任意一对灵魂对i,j (i<j),如果不存在ks (i<s<j)大于ki或者kj,则会为影魔提供p1的攻击力。另一种情况是,如果存在一个位置k,满足ki<c<kj或者kj<c<ki,则会为影魔提供p2的攻击力。其他情况下的灵魂对不会为影魔提供攻击力。 根据引用\[3\]的描述,我们可以从左到右进行枚举。对于情况1,当扫到r\[i\]时,更新l\[i\]的贡献。对于情况2.1,当扫到l\[i\]时,更新区间\[i+1,r\[i\]-1\]的贡献。对于情况2.2,当扫到r\[i\]时,更新区间\[l\[i\]+1,i-1\]的贡献。 因此,对于给定的区间\[l,r\],我们可以根据上述方法计算出区间内所有下标二元组i,j (l<=i<j<=r)的贡献之和。 #### 引用[.reference_title] - *1* *3* [P3722 [AH2017/HNOI2017]影魔(树状数组)](https://blog.csdn.net/li_wen_zhuo/article/details/115446022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [洛谷3722 AH2017/HNOI2017 影魔 线段树 单调栈](https://blog.csdn.net/forever_shi/article/details/119649910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值