您的位置:首页 > 其它

hdu 1698 Just a Hook

2012-02-27 17:44 232 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树水题

View Code

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define L(x)(x<<1)
#define R(x)(x<<1|1)
#define MID(x,y) ((x+y)>>1)
const int MAX=100005;
struct Tnode{
int sum,left,right;
}node[MAX*4];
int num[MAX];
void init()
{
memset(node,0,sizeof(node));
}
void Build(int t,int l,int r)
{
node[t].left=l;
node[t].right=r;
if(l+1==r)
{
node[t].sum=1;
return ;
}
int mid=MID(l,r);
Build(L(t),l,mid);
Build(R(t),mid,r);
}
void update(int t,int l,int r,int sum)
{
if(node[t].left==l&&node[t].right==r)
{
node[t].sum=sum;
return ;
}
if(node[t].sum>0){
node[L(t)].sum=node[t].sum;
node[R(t)].sum=node[t].sum;
node[t].sum=-1;
}
int mid=MID(node[t].left,node[t].right);
if(l>=mid) update(R(t),l,r,sum);
else if(r<=mid) update(L(t),l,r,sum);
else {
update(L(t),l,mid,sum);
update(R(t),mid,r,sum);
}
}
int query(int t,int l,int r)
{

if( node[t].sum>0)
{
return (r-l)*(node[t].sum);
}
int mid = MID(node[t].left,node[t].right);
if( l >= mid )
return query(R(t),l,r);
else
if( r <= mid )
return query(L(t),l,r);
else
return   query(L(t),l,mid)+query(R(t),mid,r);

}
int main()
{
int ind = 1,to,m,n,x,y,z,g=0;
char s[10];
scanf("%d",&to);
while(to--)
{      g++;
scanf("%d%d",&n,&m);
init();
Build(1,0,n);

while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x-1,y,z);
}

printf("Case %d: The total value of the hook is %d.\n",g,query(1,0,n));

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