1. 程式人生 > >HDU 1698 Just a Hook (線段樹的區間更新)

HDU 1698 Just a Hook (線段樹的區間更新)

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,代表1-n內所有點值都為1,給q個運算元,x,y,z,表示把x,y內所有值改為z,q個操作完了求總區間和,區間更新基本題,練習入門lazy tag的好題

程式碼:

#include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
struct node
{
    int l,r;
    int tag;//上一次更新的值
    int v;//當前區間的總和
}t[100005*4];
void Build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].tag=0;
    if(l==r)
    {
        t[k].v=1;
        return;
    }
    int mid=(l+r)/2;
    Build(l,mid,k*2);
    Build(mid+1,r,k*2+1);
    t[k].v=t[k*2].v+t[k*2+1].v;
}
void pushdown(int k)//將上次沒有傳遞的狀態傳遞下去
{
    if(t[k].tag!=0)
    {
        t[k*2].tag=t[k*2+1].tag=t[k].tag;//傳給子標籤
        t[k*2].v=(t[k*2].r-t[k*2].l+1)*t[k].tag;//賦值
        t[k*2+1].v=(t[k*2+1].r-t[k*2+1].l+1)*t[k].tag;
        t[k].tag=0;//傳完了置為0
    }
}
void update(int l,int r,int v,int k)
{
    if(l==t[k].l&&r==t[k].r)//找到了該區間,賦值,標記,返回
    {
        t[k].v=(r-l+1)*v;
        t[k].tag=v;
        return;
    }
    pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        update(l,r,v,k*2);
    else if(l>mid)
        update(l,r,v,k*2+1);
    else
    {
        update(l,mid,v,k*2);
        update(mid+1,r,v,k*2+1);
    }
    t[k].v=t[k*2].v+t[k*2+1].v;//向上傳遞更新值
}
int main()
{
    int i,j,n,test,m,x,y,v;
    scanf("%d",&test);
    for(i=1;i<=test;i++)
    {
        scanf("%d",&n);
        Build(1,n,1);
        scanf("%d",&m);
        for(j=0;j<m;j++)
        {
            scanf("%d%d%d",&x,&y,&v);
            update(x,y,v,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,t[1].v);
    }
    return 0;
}