hdu 1698 Just a Hook
2012-02-27 17:44
232 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
线段树水题
View Code
线段树水题
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; }
相关文章推荐
- 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 Just a Hook(区间更新)
- HDU 1698 Just a Hook (线段树,区间更新)
- 【线段树(lazy)】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 线状树经典模型之区间变动bool标记,上下同时更新
- HDU 1698 Just a Hook 线段树区间更新(值进行覆盖)
- HDU 1698 Just a Hook(线段树lazy成段更新)