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

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

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

时间: 2024-10-14 02:48:26

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

HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

poj 2777 线段树的区间更新

Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Chosen Problem Solving and Program design as an optional course, you are required to solve al

hdu 1556:Color the ball(线段树,区间更新,经典题)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7941    Accepted Submission(s): 4070 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电

hdu 1698:Just a Hook(线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15129    Accepted Submission(s): 7506 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing f

HDU 1556 Color the ball(线段树:区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和线段树都可以,拿这道题来入门一下线段树的区间更新. 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn = 1000

CodeForces 52C Circular RMQ(区间循环线段树,区间更新,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are given circular array a0,?a1,?...,?an?-?1. There are two types of operations with it: inc(lf,?rg,?v) - this operation increases each element on the segm

杭电1698--Just a Hook(线段树, 区间更新)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树, 区间更新, 用到了Lazy思想.利用已更新区间来减少未更新区间用时.(自己的理解, 应该是对的) #include <cstdio>#include <cstring>#include <iostream>using namespace std;int Node[100100<<2], n, m, lazy[100100<<2];void

FZU Problem 2171 防守阵地 II (线段树,区间更新)

 Problem 2171 防守阵地 II Accept: 143    Submit: 565Time Limit: 3000 mSec    Memory Limit : 32768 KB  Problem Description 部队中总共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,指挥部将选择M个士兵依次进入指定地点进行防守任务,获得的参考指数即为M个士兵的能力之和.随着时间的推移,指挥部将下达Q个指令来替换M个进行防守的士兵们,每个参加完防守