hdu 1698 Just a Hook 线段树区间更新
2014-03-17 11:42
411 查看
基本的线段更新(lazy标记) 线段树代码敲起来还是挺爽的
#include<stdio.h> const int N=100005; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int sum[N<<2],lazy[N<<2]; void push_up(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void push_down(int l,int r,int rt){ if(lazy[rt]){ int m=(l+r)>>1; sum[rt<<1]=(m+1-l)*lazy[rt]; sum[rt<<1|1]=(r-m)*lazy[rt]; lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];//don't forget lazy[rt]=0; } } void build(int l,int r,int rt){ lazy[rt]=0; if(l==r){ sum[rt]=1; return; } int m=(l+r)>>1; build(lson); build(rson); push_up(rt); } void update(int a,int b,int c,int l,int r,int rt){ if(a<=l&&b>=r){ sum[rt]=(r+1-l)*c; lazy[rt]=c; return; } int m=(l+r)>>1; push_down(l,r,rt); if(a<=m) update(a,b,c,lson); if(b>m) update(a,b,c,rson); push_up(rt); } int query(int a,int b,int l,int r,int rt){ if(a<=l&&b>=r) return sum[rt]; int m=(l+r)>>1,rst=0; push_down(l,r,rt); if(a<=m) rst+=query(a,b,lson); if(b>m) rst+=query(a,b,rson); return rst; } int main(){ int tt,cases=1,n,q,x,y,z; scanf("%d",&tt); while(tt--){ scanf("%d",&n); build(1,n,1); scanf("%d",&q); while(q--){ scanf("%d %d %d",&x,&y,&z); update(x,y,z,1,n,1); } printf("Case %d: The total value of the hook is %d.\n",cases++,sum[1]); } }
相关文章推荐
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
- Just a Hook (HDU_1698) 线段树+区间更新
- HDU 1698 Just a Hook 线段树区间更新(值进行覆盖)
- hdu 1698(区间更新线段树)Just a Hook
- HDU 1698 Just a Hook(线段树 区间更新 + 查询区间和)
- HDU 1698 Just a Hook 线段树区间更新、
- HDU 1698 Just a Hook(线段树:区间更新)
- hdu 1698 Just a Hook 线段树区间更新
- hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
- HDU 1698 Just a Hook(线段树区间更新)
- HDU 1698 Just a Hook 线段树区间更新(值进行覆盖)
- HDU 1698 Just a Hook 线段树 区间更新 惰性标记
- HDU - 1698 D - Just a Hook (线段树区间更新)
- 线段树(区间更新) hdu-1698-Just a Hook
- HDU 1698 Just a Hook 线段树区间更新(值进行覆盖)
- hdu 1698:Just a Hook(线段树,区间更新)
- hdu - 1698 - Just a Hook(线段树(区间更新))
- HDU 1698 Just a Hook——区间更新的线段树
- Just a Hook (hdu 1698 线段树区间更新)
- Just a Hook HDU - 1698(线段树区间更新)