zoukankan      html  css  js  c++  java
  • HDU1698Just a Hook区间更新+线段树成段更新

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 

    Now Pudge wants to do some operations on the hook. 

    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. 

    InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. 
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. 
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 
    Output

      For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example. 
    Sample Input

    1
    10
    2
    1 5 2
    5 9 3

    Sample Output

    Case 1: The total value of the hook is 24.


    题意:
      
    首先给出数据的组数1,然后给出个数10,代表n从1——10,初始值全是1。再给出2,代表接下来有两个区间,1 5 2代表把区间[1,5]里面所有元素的值变为2,以此类推。求区间总和

        样例解释:

        1 给出1组样例
        10 区间1-10 默认区间1-10所有元素为1
        2 2次更改
        1 5 2 把区间1-5所有的元素更改为2
        5 9 3 把区间5-9所有的元素更改为2


    左移是乘,右移是除,左移n,就是乘以2的n次方

    思路:
       区间更改(即线段树区间成段更新)
    ,因为要把这一段区间内的所有元素全部变成相同的。写的时候注意lazy的标记向下传递的操作即可

       还有就是最后输出的时候可以不用去查询,看了别人的博客才知道可以直接输出sum[1],因为所有的子节点和都是向上传递的,最后最顶上的那个元素记录的就是整个大区间的和。

        求(询问)区间所有元素之和

    这个代码是不用查询,直接输出的。

     1 #include<iostream>
     2 #include<string.h>
     3 #include<cmath>
     4 #include<stdio.h>
     5 #include<algorithm>
     6 #include<stdlib.h>
     7 #include<iomanip>
     8 #define inf 0x3f3f3f3f
     9 #define ios() std::ios::sync_with_stdio(false)
    10 #define cin() cin.tie(0)
    11 #define cout() cout.tie(0)
    12 #define mem1(a) memset(a,0,sizeof(a))
    13 #define mem2(b) memset(b,'\0',sizeof(b))
    14 typedef long long ll;
    15 const int N=100020;
    16 using namespace std;
    17 
    18 //区间更新
    19 //线段树成段更新
    20 
    21 //struct node
    22 //{
    23 //    int left;
    24 //    int right;
    25 //    int co;
    26 //}sum[N*4];
    27 
    28 int sum[4*N];
    29 int lazy[4*N];
    30 
    31 void pushdown(int i,int len)
    32 {
    33     if(lazy[i]!=inf)
    34     {
    35         lazy[i<<1]=lazy[i<<1|1]=lazy[i];
    36         sum[i<<1]=(len-(len>>1))*lazy[i];
    37         sum[i<<1|1]=(len>>1)*lazy[i];
    38         lazy[i]=inf;
    39     }
    40 }
    41 
    42 void build(int L,int R,int i)
    43 {
    44 //    s[i].left=L;
    45 //    s[i].right=R;
    46     sum[i]=1;
    47     lazy[i]=inf;
    48     if(L==R)
    49         return;
    50     int mid=(L+R)/2;
    51     build(L,mid,i<<1);
    52     build(mid+1,R,i<<1|1);
    53     sum[i]=sum[i<<1]+sum[i<<1|1];
    54 }
    55 
    56 void update(int left,int right,int change,int L,int R,int i)
    57 {
    58     if(left<=L&&right>=R)
    59     {
    60         lazy[i]=change;
    61         sum[i]=change*(R-L+1);
    62         return;
    63     }
    64     pushdown(i,R-L+1);
    65     int mid=(R+L)/2;
    66     if(left<=mid)
    67         update(left,right,change,L,mid,i<<1);
    68     if(right>mid)
    69         update(left,right,change,mid+1,R,i<<1|1);
    70      sum[i]=sum[i<<1]+sum[i<<1|1];
    71 }
    72 
    73 //int query(int i)
    74 //{
    75 //   return sum[i]=sum[i<<1]+sum[i<<1|1];
    76 //}
    77 
    78 int main()
    79 {
    80     int t,len,m;
    81     scanf("%d",&t);
    82     int tt=1;
    83     while(t--)
    84     {
    85         mem1(sum);
    86         mem1(lazy);
    87         scanf("%d %d",&len,&m);
    88         build(1,len,1);
    89         int a,b,change;
    90         for(int i=1;i<=m;i++)
    91         {
    92             scanf("%d %d %d",&a,&b,&change);
    93             update(a,b,change,1,len,1);
    94         }
    95         printf("Case %d: The total value of the hook is %d.\n",tt++,sum[1]);
    96     }
    97     return 0;
    98 }
    View Code

    这个代码是按照模板写的,进行了查询操作,比较容易想到,比较容易按照模板写。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 using namespace std;
     5 const int N=100020;
     6 
     7 int a[4*N];
     8 int lazy[4*N];
     9 
    10 void pushdown(int i,int len)
    11 {
    12     if(lazy[i])//如果懒惰标记为真,说明之前有过懒惰标记,现在需要进行更新
    13     {
    14         lazy[i<<1]=lazy[i];
    15         lazy[i<<1|1]=lazy[i];
    16         a[i<<1]=(len-(len>>1))*lazy[i];
    17         a[i<<1|1]=(len>>1)*lazy[i];
    18         //右边上下顺序换一下就不对了,是29,不是24
    19         lazy[i]=0;//由于懒惰标记向下传递,所以当前节点的懒惰标记取消
    20     }
    21 }
    22 
    23 //build(1,n,1)
    24 void build(int L,int R,int i)
    25 {
    26     //s[i].left=L;
    27     //s[i].right=R;
    28     //lazy[i]=0;//每次传入的节点懒惰标记为0
    29     if(L==R)
    30     {
    31         a[i]=1;//题目给出的样例为10 区间1-10 默认区间1-10所有元素为1
    32         return;//别忘了写
    33     }
    34     int mid=(L+R)>>1;
    35     build(L,mid,i<<1);
    36     build(mid+1,R,i<<1|1);
    37     a[i]=a[i<<1]+a[i<<1|1];//别忘了
    38 }
    39 
    40 void update(int left,int right,int change,int L,int R,int i)
    41 {
    42     if(left<=L&&right>=R)
    43     {
    44         lazy[i]=change;
    45         a[i]=change*(R-L+1);//把这个区间里面所有元素相加
    46         return;//记得返回到上一步
    47     }
    48     //懒惰标记向下推
    49     pushdown(i,R-L+1);//别忘了
    50     int mid=(R+L)>>1;
    51     if(left<=mid)
    52         update(left,right,change,L,mid,i<<1);
    53     if(right>mid)//
    54         update(left,right,change,mid+1,R,i<<1|1);
    55     a[i]=a[i<<1]+a[i<<1|1];
    56 }
    57 
    58 int query(int left,int right,int L,int R,int i)
    59 {
    60     if(L==R)
    61         return a[i];//别忘了
    62     pushdown(i,R-L+1);//别忘了
    63     int ans=0;
    64     int mid=(L+R)>>1;
    65     if(left<=mid)
    66         ans+=query(left,right,L,mid,i<<1);
    67     if(right>mid)
    68     //if(mid<R)不知道为什么这样也对??
    69         ans+=query(left,right,mid+1,R,i<<1|1);
    70     //a[i]=a[i<<1]+a[i<<1|1];
    71     return ans;
    72 }
    73 
    74 int main()
    75 {
    76     int ttt=1,t;
    77     while(~scanf("%d",&t))
    78     {
    79         while(t--)
    80         {
    81             memset(lazy,0,sizeof(lazy));
    82             int n;
    83             scanf("%d",&n);
    84             build(1,n,1);//知道总节点,开始建树
    85 
    86             int tt;
    87             scanf("%d",&tt);
    88             int a,b,change;
    89             for(int i=0; i<tt; i++)
    90             {
    91                 scanf("%d %d %d",&a,&b,&change);
    92                 update(a,b,change,1,n,1);
    93             }
    94 //            printf("Case %d: The total value of the hook is %d.\n",ttt++,query(1,n))
    95             printf("Case %d: The total value of the hook is %d.\n",ttt++,query(1,n,1,n,1));
    96         }
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    javascript 自定义事件
    javascript 实现HashTable(哈希表)
    NHibernate输出SQL语句
    Asp.net MVC Comet推送
    MySQL 数据备份与还原
    Mysql -- 慢查询
    cookie 的HttpOnly 和 Secure 属性
    Ubuntu -- 反射shell nc
    docker 访问宿主机网络
    k8s 配置文件 详解
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11291766.html
Copyright © 2011-2022 走看看