HDU 1698 Just a Hook 区间更新 lazy标记

lazy标记

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <sstream>
  5 #include <string>
  6 #include <algorithm>
  7 #include <list>
  8 #include <map>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <cstdlib>
 14 #include <conio.h>
 15 using namespace std;
 16 #define clc(a,b) memset(a,b,sizeof(a))
 17 #define inf 0x3f3f3f3f
 18 const int N=100010;
 19 const int MOD = 1e9+7;
 20 #define LL long long
 21 double const pi = acos(-1);
 22 void fre() {
 23     freopen("in.txt","r",stdin);
 24 }
 25 // inline int r() {
 26 //     int x=0,f=1;char ch=getchar();
 27 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
 28 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
 29 // }
 30 int n;
 31 struct  node{
 32     int l,r,sum;
 33     int lazy;
 34 }tree[N<<2];
 35 
 36 void pushdown(int rt){
 37     if(tree[rt].lazy!=0){
 38         tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
 39         tree[rt<<1].sum=(tree[rt<<1].r-tree[rt<<1].l+1)*tree[rt<<1].lazy;
 40         tree[rt<<1|1].sum=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*tree[rt<<1|1].lazy;
 41         tree[rt].lazy=0;
 42     }
 43 }
 44 
 45 void updata(int l,int r,int x,int L,int R,int rt){
 46     if(tree[rt].lazy==x) return;
 47     if(l<=L&&r>=R){
 48         tree[rt].lazy=x;
 49         tree[rt].sum=x*(R-L+1);
 50         return;
 51     }
 52     pushdown(rt);
 53     int mid=(L+R)>>1;
 54     if(r<=mid)
 55         updata(l,r,x,L,mid,rt<<1);
 56     else if(l>mid)
 57         updata(l,r,x,mid+1,R,rt<<1|1);
 58     else{
 59         updata(l,r,x,L,mid,rt<<1);
 60         updata(l,r,x,mid+1,R,rt<<1|1);
 61     }
 62     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
 63     return;
 64 }
 65 
 66 void build(int l,int r,int rt){
 67      tree[rt].l=l;
 68      tree[rt].r=r;
 69      tree[rt].lazy=1;
 70      if(l==r){
 71         tree[rt].sum=1;
 72         return;
 73      }
 74      int mid=(l+r)>>1;
 75      build(l,mid,rt<<1);
 76      build(mid+1,r,rt<<1|1);
 77      tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
 78      return;
 79 }
 80 
 81 int main(){
 82     // fre();
 83     int T,q,l,r,x;
 84     scanf("%d",&T);
 85     int cas=1;
 86     while(T--){
 87         scanf("%d",&n);
 88         build(1,n,1);
 89         // for(int i=1;i<=18;i++)
 90         // {
 91         //     printf("%d\n",tree[i].sum);
 92         // }
 93         // getch();
 94         scanf("%d",&q);
 95         while(q--){
 96             scanf("%d%d%d",&l,&r,&x);
 97             updata(l,r,x,1,n,1);
 98            // for(int i=1;i<=25;i++){
 99            //  printf("%d:%d\n",i,tree[i].sum);
100            // }
101            // system("pause");
102         }
103         printf("Case %d: The total value of the hook is %d.\n",cas++,tree[1].sum);
104     }
105     return 0;
106 }

 

posted @ 2016-06-14 22:36  yyblues  阅读(164)  评论(0编辑  收藏  举报