hdu 1698 Just a Hook

13 篇文章 0 订阅

线段树成段更新,设置延迟标记,很好的方法,想出这个方法的人确实很吊

注意细节,有些小地方错了真的很难发现~

#include<iostream>
#define maxn 111111
using namespace std;
int n,a,b,m;
struct stu
{
	int l,r,sum,flag;
};
stu mapp[maxn*4];
void build(int l,int r,int count)
{
	mapp[count].l=l;
	mapp[count].r=r;
	mapp[count].flag=0;
	if(l==r)
	{
		mapp[count].sum=1;
		mapp[count].flag=1;
		return;
	} 
	int mid=(l+r)/2;
	build(l,mid,count*2);
	build(mid+1,r,count*2+1);
	mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
void pushdown(int count,int x)
{
	if(mapp[count].flag)
	{
		mapp[count*2].flag=mapp[count].flag;
		mapp[count*2+1].flag=mapp[count].flag;
		mapp[count*2].sum=mapp[count].flag*((x+1)/2);
		mapp[count*2+1].sum=mapp[count].flag*(x/2);
		mapp[count].flag=0;
	}
}
void updata(int l,int r,int count)
{
	if(l>=a&&r<=b)
	{
		mapp[count].sum=m*(r-l+1);
		mapp[count].flag=m;
		return ;
	}
	
	pushdown(count,r-l+1);
	
	int mid=(l+r)/2;
	if(a<=mid) updata(l,mid,count*2);
	if(b>=mid+1) updata(mid+1,r,count*2+1);
	mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
int main()
{
	cin.sync_with_stdio(false);
	int t;
	cin>>t;
	int casee=1;
	while(t--)
	{
		cin>>n;
		build(1,n,1);
		int s;
		cin>>s;
		for(int i=0;i<s;i++)
		{
			cin>>a>>b>>m;
			updata(1,n,1);
		}
		cout<<"Case "<<casee++<<": The total value of the hook is ";
		cout<<mapp[1].sum<<"."<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值