码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu 1698(线段树 区间修改 区间查询)

时间:2017-10-07 14:48:49      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:cstring   ade   hook   for   修改   inpu   ever   nal   metal   

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. 

InputThe 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. 
OutputFor 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.


题意:有一个长度为n的钩子,钩子由金银铜三种棒子组成,所以问题来了:骚年,这是你的金棒子,还是你的银棒子,咳咳,扯远了,现在有个浪到飞起的男人,他能将一段的不管曾经是怎样的棒子全部改成自己想要的棒子.好的,现在已知金棒子价值为3,银为2,铜为1.
求修改完以后整个钩子的价值.

再多练几道线段树吧...不过这道题没强制在线,似乎可以搞些别的东东?当我没说....反正就打线段树吧...

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define N 100000
#define lson root<<1
#define rson root<<1|1
using namespace std;

int node[N<<2],lazy[N<<2];

void pushup(int root)
{
    node[root]=node[lson]+node[rson];
}

void pushdown(int root,int val)
{
    if(lazy[root]!=-1)
    {
        lazy[lson]=lazy[root];
        lazy[rson]=lazy[root];
        node[lson]=(val-(val>>1))*lazy[root];
        node[rson]=(val>>1)*lazy[root];
        lazy[root]=-1;
    }
}

void build(int l,int r,int root)
{
    lazy[root]=-1;
    node[root]=1;
    if(l==r)
    {
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(root);
}

void update(int left,int right,int val,int l,int r,int root)
{
    if(left<=l&&right>=r)
    {
        lazy[root]=val;
        node[root]=val*(r-l+1);
        return;
    }
    pushdown(root,r-l+1);
    int mid=(l+r)>>1;
    if(left<=mid)
    {
        update(left,right,val,l,mid,lson);
    }
    if(mid<right)
    {
        update(left,right,val,mid+1,r,rson);
    }    
    pushup(root);
}

int main()
{
    int ttt=0,t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        
        for(int i=1;i<=m;i++)
        {
            int ll,rr,ww;
            scanf("%d%d%d",&ll,&rr,&ww);
            update(ll,rr,ww,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",++ttt,node[1]);
    }
}

 

 

 

每天刷题,身体棒棒!

Hdu 1698(线段树 区间修改 区间查询)

标签:cstring   ade   hook   for   修改   inpu   ever   nal   metal   

原文地址:http://www.cnblogs.com/stxy-ferryman/p/7634508.html

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