hdu

http://acm.hdu.edu.cn/showproblem.php?pid=1698

n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<=z<=3),问更新完后的总价值。

线段树的区间更新,需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新或者询问的时候.

这题只需要输出总区间的信息,即直接输出1结点的信息.

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <string>
  7 #include <algorithm>
  8 #include <string>
  9 #include <set>
 10 #include <functional>
 11 #include <numeric>
 12 #include <sstream>
 13 #include <stack>
 14 #include <map>
 15 #include <queue>
 16 //#pragma comment(linker, "/STACK:102400000,102400000")
 17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 18 
 19 #define ll long long
 20 #define inf 0x7f7f7f7f
 21 #define lc l,m,rt<<1
 22 #define rc m + 1,r,rt<<1|1
 23 #define pi acos(-1.0)
 24 
 25 #define L(x)    (x) << 1
 26 #define R(x)    (x) << 1 | 1
 27 #define MID(l, r)   (l + r) >> 1
 28 #define Min(x, y)   (x) < (y) ? (x) : (y)
 29 #define Max(x, y)   (x) < (y) ? (y) : (x)
 30 #define E(x)        (1 << (x))
 31 #define iabs(x)     (x) < 0 ? -(x) : (x)
 32 #define OUT(x)  printf("%I64d
", x)
 33 #define lowbit(x)   (x)&(-x)
 34 #define Read()  freopen("a.txt", "r", stdin)
 35 #define Write() freopen("b.txt", "w", stdout);
 36 #define maxn 100010
 37 #define maxv 210
 38 #define mod 1000000000
 39 using namespace std;
 40 #define lson l,m,rt<<1
 41 #define rson m+1,r,rt<<1|1
 42 int sum[maxn<<2];
 43 int col[maxn<<2];
 44 
 45 void PushUp(int rt)
 46 {
 47     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 48 }
 49 
 50 void PushDown(int rt,int m)
 51 {
 52     if(col[rt])
 53     {
 54         col[rt<<1]=col[rt<<1|1]=col[rt];
 55         sum[rt<<1]=(m-(m>>1))*col[rt];
 56         sum[rt<<1|1]=(m>>1)*col[rt];
 57         col[rt]=0;
 58     }
 59 }
 60 
 61 void build(int l,int r,int rt)
 62 {
 63     col[rt]=0;
 64     sum[rt]=1;
 65     if(l==r) return;
 66     int m=(l+r)>>1;
 67     build(lson);
 68     build(rson);
 69     PushUp(rt);
 70 }
 71 
 72 void update(int L,int R,int c,int l,int r,int rt)
 73 {
 74     if(L<=l&&r<=R)
 75     {
 76         col[rt]=c;
 77         sum[rt]=c*(r-l+1);
 78         return;
 79     }
 80     PushDown(rt,r-l+1);
 81     int m=(l+r)>>1;
 82     if(L<=m) update(L,R,c,lson);
 83     if(R>m) update(L,R,c,rson);
 84     PushUp(rt);
 85 }
 86 int main()
 87 {
 88     //freopen("a.txt","r",stdin);
 89     int t,n,m,a,b,c,j=1;
 90     scanf("%d",&t);
 91     while(t--)
 92     {
 93         scanf("%d%d",&n,&m);
 94         build(1,n,1);
 95         for(int i=0;i<m;i++)
 96         {
 97             scanf("%d%d%d",&a,&b,&c);
 98             update(a,b,c,1,n,1);
 99         }
100         printf("Case %d: The total value of the hook is %d.
",j++,sum[1]);
101     }
102     return 0;
103 }
原文地址:https://www.cnblogs.com/nowandforever/p/4572502.html