Just a hook

Just a Hook
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.

題意大概是說有三種不同類別的hook,價值分別爲1,2,3,開始所有的N個物品的價值都爲1,接下來有Q次操作,每次操作都是將第X個到第Y個物品置換成價值爲Z(1,2,3)的物品,問Q次操作以後物品的總價值爲多少。每次操作都是區間置換,標記不能疊加,由於求的是所有物品的總價值,所以結果就是所建線段樹第一層父親結點的值,即  tree[1].sum  。大概的思路就是將要改變的值作爲標記值(mark),比如要變爲價值爲3就將標記的值更改爲3...以此類推,不改變的標記爲0。

#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"

using namespace std;

#define lson id<<1
#define rson id<<1|1
#define   MAX   100005

int N;

struct TREE
{
    int l,r,mark,sum;
}tree[MAX<<2];

void pushup(int id)
{
    tree[id].sum = tree[lson].sum + tree[rson].sum;
}

void pushdown(int id)
{
    tree[lson].mark = tree[id].mark; //標記不能疊加
    tree[rson].mark = tree[id].mark;
    tree[lson].sum = tree[id].mark * (tree[lson].r - tree[lson].l + 1);
    tree[rson].sum = tree[id].mark * (tree[rson].r - tree[rson].l + 1);
    tree[id].mark = 0;
}

void build(int id,int l,int r)
{
    tree[id].l = l;
    tree[id].r = r;
    tree[id].mark = 0; //初始化爲都未標記
    if(l == r)
    {
        tree[id].sum = 1; //開始價值都爲1
    }
    else
    {
        int mid = (l+r) / 2;
        build(lson,l,mid);
        build(rson,mid+1,r);
        pushup(id);
    }
}

void update(int id,int l,int r,int val)
{
    if(tree[id].l > r || tree[id].r < l) //區間外
    {
        return ;
    }
    if(tree[id].l >= l && tree[id].r <= r) //區間內直接求和
    {
        tree[id].mark = val;
        tree[id].sum = (tree[id].r - tree[id].l + 1) * val;
        return ;
    }
    if(tree[id].mark)
    {
        pushdown(id);
    }
    update(lson,l,r,val);
    update(rson,l,r,val);
    pushup(id);
}

int main()
{
    int T;
    while(~scanf("%d",&T))
    {
        for(int cas = 1;cas <= T;cas++)
        {
            int Q;
            scanf("%d%d",&N,&Q);
            build(1,1,N);
            int x,y,z;
            while(Q--)
            {
                scanf("%d%d%d",&x,&y,&z);
                update(1,x,y,z);
            }
            printf("Case %d: The total value of the hook is %d.\n",cas,tree[1].sum);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章