码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-05-04 21:24:27      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:define   cut   images   meta   sample   nod   represent   and   red   

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数组的用法,没有理解好!

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

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

标签:define   cut   images   meta   sample   nod   represent   and   red   

原文地址:https://www.cnblogs.com/674001396long/p/10809779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!