Just a hook

来源:互联网 发布:小苍的淘宝店铺 编辑:程序博客网 时间:2024/05/21 02:21
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

11021 5 25 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   100005int 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;}


0 0