Just a Hook-HDU1698(线段树求区间)

http://acm.hdu.edu.cn/showproblem.php?pid=1698

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.

题目大意:

有一根棍,现在要给他表面镀上一层金属,(本身这根棍是铜的)

铜的价值是1

银得价值是2

金的价值是3

求最后的价值是多少。

分析:

用一个变量e表示这一段节点价值

如果这一段节点有两种金属就让e=-1;

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 101000
#define Lson r<<1
#define Rson r<<1|1

struct node
{
    int L,R,e;
    bool is;
    int mid()
    {
        return (L+R)/2;
    }
    int len()
    {
        return R-L+1;
    }
}a[N*4];

void BuildTree(int r,int L,int R)
{
    a[r].L=L;
    a[r].R=R;
    a[r].e=1;
    if(L==R)
    {
        return;
    }
    BuildTree(Lson,L,a[r].mid());
    BuildTree(Rson,a[r].mid()+1,R);
}
int Qurry(int r)
{
    if(a[r].e!=-1)
        return a[r].len()*a[r].e;
    else
        return Qurry(Lson)+Qurry(Rson);
}
void Update(int r,int L,int R,int e)
{

    if(a[r].L==L && a[r].R==R)
    {
        a[r].e=e;
        return;
    }
    if(a[r].e!=-1)
    {
        a[Lson].e=a[Rson].e=a[r].e;
    }
    a[r].e=-1;
    if(R<=a[r].mid())
        Update(Lson,L,R,e);
    else if(L>a[r].mid())
        Update(Rson,L,R,e);
    else
    {
        Update(Lson,L,a[r].mid(),e);
        Update(Rson,a[r].mid()+1,R,e);
    }
}
int main()
{
    int T,n,m,i,t,x,y,e;
    scanf("%d",&T);
    t=1;
    while(T--)
    {
        scanf("%d",&n);
        BuildTree(1,1,n);
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&e);
            Update(1,x,y,e);
        }
        printf("Case %d: The total value of the hook is %d.\n",t++,Qurry(1));
    }
    return 0;
}
时间: 2024-12-26 06:13:15

Just a Hook-HDU1698(线段树求区间)的相关文章

2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-th question is whether P remains balanced after pai and pbi  swapped. Note that questions ar

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 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

HDU6447 YJJ&#39;s Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 ?原题目描述在最下面. ?1e5个点,问从(0,0)走到(1e9,1e9)的最大收益. ?当你从(u-1,v-1)走到(u,v)时,你可以获得点(u,v)的权值. Solution: ?十分详细了. ?直接线段树区间最值.当然也可以树状数组,不能st表. ?\(dp[i] = max(query\_max(0,dp[i]-1,1)+val[i] ,

杭电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

LightOJ Array Queries 1082【线段树求区间最值】

1082 - Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index

【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps

https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意考虑一个月内装满多个房间和装满所有房间后不用再购买的情况. 代码: #include <iostream> #include <cstdio> #include <algorithm> const int maxn = 100005; const int INF = 0x3

poj 3264 线段树 求区间最大最小值

Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous rang

hdu1166 敌兵布阵(线段树 求区间和 更新点)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 59427    Accepted Submission(s): 25098 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又開始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

nyoj1185 最大最小值 (线段树求区间最大值和最小值)

对于不懂线段树的,先看为这篇文章理解下.点击打开链接 这道题普通方法 ,TLE. 最大最小值 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给出N个整数,执行M次询问. 对于每次询问,首先输入三个整数C.L.R: 如果C等于1,输出第L个数到第R个数之间的最小值: 如果C等于2,输出第L个数到第R个数之间的最大值: 如果C等于3,输出第L个数到第R个数之间的最小值与最大值的和. (包括第L个数和第R个数). 输入 首先输入一个整数T(T≤100),表示有T组数据.