线段树成段更新,设置延迟标记,很好的方法,想出这个方法的人确实很吊
注意细节,有些小地方错了真的很难发现~
#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;
}