• java-HDU1698(线段树的区间更新,和区间查询)


    HDU1698:

    题目意思:

    Just a Hook

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


    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.
     
    我相信之所以很多人不会做这道题,是因为看不懂题目,其实我也是百度翻译的,哈哈。
    解题思路就是:线段树的区间更新,区间查询,其中区间更新用了懒惰数组。
    代码实现如下:
    package Combat.com;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main
    {
        static final int MAX = 100005;
        static int lazy[] = new int[4*MAX];
        static int node[] = new int[4*MAX]; 
        public static void main(String []args)
        {
            Scanner cin = new Scanner(System.in);
            int T = cin.nextInt();
            for(int i = 1; i <= T; i++)
            {
                int n = cin.nextInt();
                Arrays.fill(lazy, -1);
                buildBinaryTree(1,n,1);
                int q = cin.nextInt();
                for(int j = 0; j < q; j++)
                {
                    int x = cin.nextInt();
                    int y = cin.nextInt();
                    int z = cin.nextInt();
                    update(x,y,z,1,n,1);
                }
                System.out.println("Case " + i + ": The total value of the hook is " + node[1]+".");
            }
        }
        static void update(int x,int y,int z,int L,int R,int num)
        {
            if(x <= L && R <= y)
            {
                node[num] = (R-L+1)*z;
                lazy[num] = z;
                return;
            }
            int mid = (L+R)>>1;
            pushDown(L,R,num);
            if(x <= mid)
            {
                update(x,y,z,L,mid,num<<1);
            }
            if(y > mid)
            {
                update(x,y,z,mid+1,R,num<<1|1);
            }
            node[num] = node[num<<1] + node[num<<1|1];
        }    
        static void pushDown(int L,int R,int num)
        {
            if(lazy[num] != -1)
            {
                int mid = (L+R)>>1;
                lazy[num<<1] = lazy[num];
                lazy[num<<1|1] = lazy[num];
                node[num<<1] = (mid-L+1)*lazy[num];
                node[num<<1|1] = (R-mid)*lazy[num];
                lazy[num] = -1;
            }
        }
        static void buildBinaryTree(int L,int R,int num)
        {
            if(L == R)
            {
                node[num] = 1;
                return;
            }
            int mid = (L+R)>>1;
            buildBinaryTree(L,mid,num<<1);
            buildBinaryTree(mid+1,R,num<<1|1);
            node[num] = node[num<<1]+node[num<<1|1];
        }
    }

    做这道题目时,我主要遇到的问题是:1.数组开得太小了,直接报错。2.lazy数组的用法,没有理解好!

    总之,这些都是模板题,做多点就容易上手了。

  • 相关阅读:
    LeetCode具体分析 :: Recover Binary Search Tree [Tree]
    [leetcode] Path Sum
    System、应用程序进程的Binder线程池和Handler消息循环
    R(二): http与R脚本通讯环境安装
    R(一): R基础知识
    Hive(五):hive与hbase整合
    Hive(六):HQL DDL
    Hive(四):c#通过odbc访问hive
    Hive(三):SQuirrel连接hive配置
    Hive(二):windows hive ODBC 安装
  • 原文地址:https://www.cnblogs.com/674001396long/p/10809779.html
Copyright © 2020-2023  润新知