现在的位置: 首页 > 综合 > 正文

http://acm.hdu.edu.cn/showproblem.php?pid=1698&&线段树 成段替换 延迟标记

2014年03月11日 ⁄ 综合 ⁄ 共 938字 ⁄ 字号 评论关闭

ORZ

#include<cstdio>
#include<string.h>
using namespace std;
const int N=100005;
int num[N<<2],col[N<<2];
#define lson l, mid, rt << 1
#define rson mid+1, r, rt << 1 | 1
void PushUp(int rt)
{
 num[rt]=num[rt<<1]+num[rt<<1|1];
}
void PushDown(int rt,int len)
{
 if(col[rt])
 {
   col[rt<<1]=col[rt];
   col[rt<<1|1]=col[rt];
   
   num[rt<<1]=(len-(len>>1))*col[rt];
   num[rt<<1|1]=(len>>1)*col[rt];
   col[rt]=0;
 }
}
void build(int l,int r,int rt)
{  
	col[rt]=0;
     num[rt]=1;
   if(l==r)
      return;
    int mid=(l+r)>>1;
   build(lson);
     build(rson);
   PushUp(rt);
}
void update(int L,int R,int v,int l,int r,int rt)
{
   if(L<=l&&R>=r)//刚开始这里写反了写成L>=l一直runtime error 各种弱爆了  爱酱DBL抓狂
   {
     col[rt]=v;
	 num[rt]=v*(r-l+1);
	 return;
   }
   PushDown(rt,r-l+1);
    int mid=(l+r)>>1;
	 if(mid>=L)
		 update(L,R,v,lson);
	 if(mid<R)
		 update(L,R,v,rson);
	 PushUp(rt);
}
int main()
{
	int t;
	int a,b,c,n,m;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
	scanf("%d%d",&n,&m);
	build(1,n,1);
	 //scanf("%d",&m);
	 while(m--)
	 {
	  scanf("%d%d%d",&a,&b,&c);
	  update(a,b,c,1,n,1);
	 }
	 printf("Case %d: The total value of the hook is %d.\n",i,num[1]);
	}
  return 0;
}

抱歉!评论已关闭.