zoukankan      html  css  js  c++  java
  • hdu 1698 Just a Hook 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.

    Pudge wants to know the total value of the hook after performing the operations.
    You may consider the original hook is made up of cupreous sticks.

    题意描述:给出n个数,初始化为1,然后Q个操作,每个操作x,y,z如下:

    z为1:更新从第x个数到第y个数连续区间为1.

    z为2:更新从第x个数到第y个数连续区间为2.

    z为3:更新从第x个数到第y个数连续区间为3.

    最后求出这个n个数的和。

    算法分析:线段树区间更改的做法,设立sum[]和col[](lazy标记)。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define inf 0x7fffffff
     8 using namespace std;
     9 const int maxn=100000+10;
    10 
    11 int n,q;
    12 int sum[maxn<<2],col[maxn<<2];
    13 
    14 void PushUP(int rt) {sum[rt]=sum[rt<<1]+sum[rt<<1|1]; }
    15 void PushDown(int rt,int m)
    16 {
    17     if (col[rt]!=-1)
    18     {
    19         col[rt<<1]=col[rt<<1|1]=col[rt];
    20         sum[rt<<1]=(m-m/2)*col[rt];
    21         sum[rt<<1|1]=(m/2)*col[rt];
    22         col[rt]=-1;
    23     }
    24 }
    25 
    26 void build(int l,int r,int rt)
    27 {
    28     col[rt]=-1;
    29     if (l==r)
    30     {
    31         sum[rt]=1;
    32         return;
    33     }
    34     int m=(l+r)>>1;
    35     build(l,m,rt<<1);
    36     build(m+1,r,rt<<1|1);
    37     PushUP(rt);
    38 }
    39 
    40 void update(int l,int r,int rt,int x,int y,int z)
    41 {
    42     if (x<=l && r<=y)
    43     {
    44         col[rt]=z ;sum[rt]=(r-l+1)*z;
    45         return;
    46     }
    47     int mid=(l+r)>>1;
    48     PushDown(rt,r-l+1);
    49     if (y<=mid) update(l,mid,rt<<1,x,y,z);
    50     else if (x>mid) update(mid+1,r,rt<<1|1,x,y,z);
    51     else {update(l,mid,rt<<1,x,mid,z);update(mid+1,r,rt<<1|1,mid+1,y,z); }
    52     PushUP(rt);
    53 }
    54 
    55 void query(int l,int r,int rt)
    56 {
    57     if (l==r) return;
    58     PushDown(rt,r-l+1);
    59     int mid=(l+r)>>1;
    60     query(l,mid,rt<<1);
    61     query(mid+1,r,rt<<1|1);
    62     PushUP(rt);
    63 }
    64 
    65 int main()
    66 {
    67     int t,ncase=1;scanf("%d",&t);
    68     while (t--)
    69     {
    70         scanf("%d%d",&n,&q);
    71         int x,y,z;
    72         build(1,n,1);
    73         //cout<<sum[1]<<endl;
    74         for (int i=1 ;i<=q ;i++)
    75         {
    76             scanf("%d%d%d",&x,&y,&z);
    77             update(1,n,1,x,y,z);
    78         }
    79         query(1,n,1);
    80         printf("Case %d: The total value of the hook is %d.
    ",ncase++,sum[1]);
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    JAVA的向上转型 和 向下转型
    Mobx 常用函数整理
    HTTP错误代码对应
    D3官网上的力导图
    用D3画简单的力导图
    nodejs 爬图片
    nodejs 爬热搜
    canvas 图片旋转 导出下载
    canvas 电子签名
    canvas 音乐跳动效果
  • 原文地址:https://www.cnblogs.com/huangxf/p/4391145.html
Copyright © 2011-2022 走看看