您的位置:首页 > 其它

hdu-------(1698)Just a Hook(线段树区间更新)

2014-08-08 08:16 543 查看

Just a Hook

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17124 Accepted Submission(s): 8547


[align=left]Problem Description[/align]
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

#include<cstring>
#include<cstdio>
const int maxn=100005;
struct node
{
int lef,rig,sum;
int cnt;
int mid(){
return lef+(rig-lef>>1);
}
};

node sac[maxn*3];

void Build(int left,int right,int pos)
{
sac[pos]=(node){left,right,1};
if(left==right)return ;
int mid=sac[pos].mid();
Build(left,mid,pos<<1);
Build(mid+1,right,pos<<1|1);
sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
}
void Update(int left,int right,int pos,int val)
{
if(left<=sac[pos].lef&&sac[pos].rig<=right){
sac[pos].sum=val*(sac[pos].rig-sac[pos].lef+1);
sac[pos].cnt=val;
return ;
}
if(sac[pos].cnt!=0){ //向下更新一次
sac[pos<<1].sum=sac[pos].cnt*(sac[pos<<1].rig-sac[pos<<1].lef+1);
sac[pos<<1|1].sum=sac[pos].cnt*(sac[pos<<1|1].rig-sac[pos<<1|1].lef+1);
sac[pos<<1|1].cnt=sac[pos<<1].cnt=sac[pos].cnt;
sac[pos].cnt=0;
}
int mid=sac[pos].mid();
if(mid>=left)
Update(left,right,pos<<1,val);
if(mid<right)
Update(left,right,pos<<1|1,val);
sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
}
int main()
{
int test,n,Q;
int a,b,c;
scanf("%d",&test);
for(int i=1;i<=test;i++){
scanf("%d%d",&n,&Q);
Build(1,n,1);
while(Q--)
{
scanf("%d%d%d",&a,&b,&c);
Update(a,b,1,c);
}
printf("Case %d: The total value of the hook is %d.\n",i,sac[1].sum);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: