#include <stdio.h>
#include <string.h>
#define maxn 100000
struct node{
int l,r,w,to;//to记录该段的修改情况,0为未修改
}e[maxn*4];
int d[50005];
void build(int a,int b,int c)
{
if(a==b)
{
e[c].l=e[c].r=a;
e[c].w=1;
e[c].to=0;
return;
}
e[c].l=a;
e[c].r=b;
e[c].to=0;
int mid=(a+b)/2;
build(a,mid,2*c);
build(mid+1,b,2*c+1);
e[c].w=e[2*c].w+e[2*c+1].w;
}
void update(int a,int b,int c,int val)
{
if(e[c].l==a&&e[c].r==b)
{
e[c].to=val;
return;
}
int mid=(e[c].l+e[c].r)/2;
if(e[c].to!=0) //如果查找当前修改段时碰到其他修改段,则将其他修改段向下拓展
{
update(e[c].l,mid,2*c,e[c].to);
update(mid+1,e[c].r,2*c+1,e[c].to);
e[c].to=0;
}
if(b<=mid)
update(a,b,2*c,val);
else if(a>mid)
update(a,b,2*c+1,val);
else
{
update(a,mid,2*c,val);
update(mid+1,b,2*c+1,val);
}
}
int query(int a,int b,int c,int val)
{
if(a==b)
{
if(val!=0)
e[c].w=val;
else if(e[c].to!=0)
e[c].w=e[c].to;
return e[c].w;
}
if(val!=0)
e[c].to=val;
else
val=e[c].to;
int mid=(e[c].l+e[c].r)/2;
if(b<=mid)
return query(a,b,2*c,val);
else if(a>mid)
return query(a,b,2*c+1,val);
else
return query(a,mid,2*c,val)+query(mid+1,b,2*c+1,val);
}
int main()
{
int n,m;
int T,tt=0;
char s[11];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int i,j,k,a,b,c;
build(1,n,1);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,1,c);
}
printf("Case %d: The total value of the hook is %d.\n",++tt,query(1,n,1,0));
}
return 0;
}
hdu 1698 Just a Hook 线段树成段修改
最新推荐文章于 2018-10-06 21:06:10 发布