zoukankan      html  css  js  c++  java
  • HDU 1698 线段树-区间替换以及区间求和

    Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 33883    Accepted Submission(s): 16535


     

    Problem Description
    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.
     
    Input
    The 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.


    线段树题目,题目要求改变一段区间中的值,直接替换不是加上或者减去一个值,这就和普通的线段树的区间更新。原来我是想用树状数组写的。因为前段时间刚好学到了树状数组的区间更新和区间求和以及单点更新。但是无奈发现对于区间替换,树状数组无能为力,虽然它很方便很方便。

    但是没有办法,于是使用线段树写,开始的时候对于这样的区间替换没有多大的概念,但是通过一遍一遍的调试代码,不断的手推,手推,终于发现对于区间替换,我们需要改变的更新函数以及pushdown函数中的值,

    其中最最最精髓的就是

    sum[root<<1].val=sum[root].lazy*(m-(m>>1));

    sum[root<<1|1].val=sum[root].lazy*(m>>1);

    那么上面的是什么意思呢

    其中的m是更新的时候的针对的区间的长度。

    这一题注意最后的答案要有一个点号。坑爹啊。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const ll mod = 1e9+7;
    const int maxn =100100;
    int a[maxn<<2];
    struct point
    {
        int val;
        int lazy;
    };
    point sum[maxn<<2];
    void build(int root,int nsta,int nend)
    {
        sum[root].lazy=0;
        if(nsta==nend)
        {
            sum[root].val=a[nsta];
        }
        else
        {
            int mid=(nsta+nend)>>1;
            build(root<<1,nsta,mid);
            build(root<<1|1,mid+1,nend);
            sum[root].val=sum[root<<1].val+sum[root<<1|1].val;
        }
    }
    int query(int root,int nsta,int nend,int qsta,int qend)
    {
        if(qsta>nend||qend<nsta)
            return mod;
        if(qsta<=nsta&&qend>=nend)
        {
            return sum[root].val;
        }
        else
        {
            int mid=(nsta+nend)>>1;
            query(root<<1,nsta,mid,qsta,qend);
            query(root<<1|1,mid+1,nend,qsta,qend);
        }
    }
    int pushdown(int root,int m)
    {
        if(sum[root].lazy!=0)
        {
            sum[root<<1].lazy=sum[root].lazy;
            sum[root<<1|1].lazy=sum[root].lazy;
            sum[root<<1].val=(m-(m>>1))*sum[root].lazy;
            sum[root<<1|1].val=(m>>1)*sum[root].lazy;
            sum[root].lazy=0;
        }
    }
    void update(int root,int nsta,int nend,int usta,int uend,int uval)
    {
        if(usta>nend||uend<nsta)
        {
            return ;
        }
        if(usta<=nsta&&uend>=nend)
        {
            sum[root].lazy=uval;
            sum[root].val=(nend-nsta+1)*sum[root].lazy;
            return ;
        }
        else
        {
            pushdown(root,nend-nsta+1);
            int mid=(nsta+nend)>>1;
            update(root<<1,nsta,mid,usta,uend,uval);
            update(root<<1|1,mid+1,nend,usta,uend,uval);
            sum[root].val=sum[root<<1].val+sum[root<<1|1].val;
            return ;
        }
    }
    int main()
    {
       int n,num,q;
       scanf("%d",&n);
       int g=0;
       while(n--)
       {
           scanf("%d",&num);
           scanf("%d",&q);
           int i,j,k;
           for(i=1;i<=num;i++)
           {
               a[i]=1;
           }
           build(1,1,num);
           for(i=0;i<q;i++)
           {
               int x,y,z;
               scanf("%d%d%d",&x,&y,&z);
               update(1,1,num,x,y,z);
           }
           printf("Case %d: The total value of the hook is %d.
    ",++g,query(1,1,num,1,num));
       }
        return 0;
    }
    

      

  • 相关阅读:
    SVN增加文件后,文件无法自动包括在项目中的原因
    关于浏览器和IIS基础的简单理解
    关联查询之速度优化
    <>这个符号表示泛型的意思
    傅里叶变换
    distinct和group by的性能比较
    VS2010 代码前出现虚线
    SQL的 like 中间字符通配 用法
    模态对话框中的window.close关闭时会打开新页面
    最大差值
  • 原文地址:https://www.cnblogs.com/yewa/p/7785195.html
Copyright © 2011-2022 走看看