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): 41150    Accepted Submission(s): 19840


 

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.

Source

2008 “Sunline Cup” National Invitational Contest

题意:棍子能够由金、银、铜三种金属组成,价值分别为3,2,1。 有一根长度为N棍子,初始时,每个单位都是铜。进行Q次操

作 ,每次将[a,b]区间内的金属替换为相应的金属,最后求棍子的总价值。

思路:对于区间的更新以及区间的查询,我首先想到的是线段树的做法。每一个区间维护的是该区间的总价值。

因为是区间的更新,所以可以用到延迟标记lazy,当更新到的区间范围已经完全被包含在需要更新的区间范围内时,我只将当前

的区间更新了,并且将当前区间的延迟标记lazy赋值为更新的金属的价值。只有查询或更新到更下层的区间时,才会把上层区间

的lazy取出来,把当前区间的值更新了,并且将当前区间的延迟标记lazy赋值为上层的lazy。最后查询1到N范围内的价值。

AC代码:

#include <cstdio>

using namespace std;
const int MAXN = 1e6;
int N;
typedef long long ll;

struct node{
    int l,r;
    ll sum,lazy;
    void update(ll x){
        sum=1ll*(r-l+1)*x;
        lazy=x;
    }
}tree[MAXN<<2];

void push_up(int x){
    tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}

void push_down(int x){
    ll lazyval=tree[x].lazy;
    if(lazyval>0){
        tree[x<<1].update(lazyval);
        tree[x<<1|1].update(lazyval);
        tree[x].lazy=0;
    }
}

void build(int x,int l,int r){
    tree[x].lazy=tree[x].sum=0;
    tree[x].l=l;tree[x].r=r;
    if(l==r){
        tree[x].sum=1;
        return;
    }
    int mid=(l+r)/2;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    push_up(x);
}

void update(int x,int l,int r,ll val){
    int L=tree[x].l,R=tree[x].r;
    if(l<=L && R<=r){
        tree[x].update(val);
        return ;
    }
    push_down(x);
    int mid=(L+R)/2;
    if(mid>=l)
        update(x<<1,l,r,val);
    if(mid<r)
        update(x<<1|1,l,r,val);
    push_up(x);
}

ll query(int x,int l,int r){
    int L=tree[x].l,R=tree[x].r;
    if(l<=L && R<=r)
        return tree[x].sum;
    push_down(x);
    int mid=(L+R)/2;
    ll ans=0;
    if(mid>=l)
        ans+=query(x<<1,l,r);
    if(mid<r)
        ans+=query(x<<1|1,l,r);
    return ans;
}

int main(){
    int T;scanf("%d",&T);
    int cae=0;
    while(T--){
        scanf("%d",&N);
        build(1,1,N);
        int M;scanf("%d",&M);
        while(M--){
            int l,r;
            ll val;
            scanf("%d%d%lld",&l,&r,&val);
            update(1,l,r,val);
        }
        printf("Case %d: The total value of the hook is %lld.\n",++cae,query(1,1,N));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/81214455