Just a Hook,HDU1698 线段树的区间更新。

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1698
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.

Just a Hook,HDU1698 线段树的区间更新。_第1张图片

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.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,a,b)for(int i=a;i<=b;i++)
#define rep1(i,a,b)for(int i=a;i<b;i++)
using namespace std;
const int maxn=1e5;
int arr[maxn];
int tree[maxn*4];
int lazy[maxn*4];
int t,n,m,a,b,c;
void pushup(int root){
    tree[root]=tree[2*root]+tree[2*root+1];
}
void pushdown(int root,int l,int r){
    if(!lazy[root])return;
    int mid=(l+r)/2;
    lazy[root*2]=lazy[root];
    lazy[root*2+1]=lazy[root];
    tree[root*2]=lazy[root]*(mid-l+1);
    tree[root*2+1]=lazy[root]*(r-mid);
    lazy[root]=0;
}
void build(int root ,int l,int r){
    int mid=(l+r)/2;
    lazy[root]=0;
    if(l==r){
        tree[root]=1;
        return ;
    }
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    pushup(root);
}
void update(int root,int l,int r,int findl,int findr,int val){
    if(findr<l||findl>r)return;
    if(findl<=l&&findr>=r)
    {
        tree[root]=(r-l+1)*val;
        lazy[root]=val;
        return;
    }
    pushdown(root,l,r);
    int mid=(l+r)/2;
    if(findl<=mid)update(root*2,l,mid,findl,findr,val);
    if(findr>mid)update(root*2+1,mid+1,r,findl,findr,val);
    pushup(root);
}
int main(){
    int k=1;
    scanf("%d",&t);
      rep(i,1,t){
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            update(1,1,n,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",k++,tree[1]);
    }
    return 0;
}

你可能感兴趣的:(线段树)