现在的位置: 首页 > 综合 > 正文

HDU1698_Just a Hook

2015年06月17日 ⁄ 综合 ⁄ 共 2818字 ⁄ 字号 评论关闭

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17125    Accepted Submission(s): 8547

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.

 

题目大意:

有三种钩子,铜,银,金,价值分别为1,2,3,现有编号1~N个钩子,初始的时候都为铜钩子,之后进行多次操作。

操作可将区间[a,b]的钩子换成铜,银,金的。问:最后1~N个钩子的总价值是多少。

思路:

建立1~N的线段树,因为区间更新每次更新到叶子节点的话比较耗时,所以使用一个暂时标志,用来代表区间要改的值。这样就不必每次更新到叶子节点了。比如线段树区间为1~10,需要更新1~5,那么只需向下更新到区间1~5即可。如果需要更新1~4,则需要再往下走。

# include<stdio.h>
# include<iostream>
const int MAXN = 100010;

int sum[MAXN<<2],col[MAXN<<2];
void pushup(int root)
{
    sum[root] = sum[root<<1] + sum[root<<1|1];
}

void pushdown(int root,int mid)
{
    if(col[root])
    {
        col[root<<1] = col[root<<1|1] = col[root];
        sum[root<<1] = col[root]*(mid-(mid>>1));
        sum[root<<1|1] = col[root]*(mid>>1);
        col[root] = 0;
    }
}

void build(int root,int L,int R)
{
    col[root] = 0;
    sum[root] = 0;
    if(L == R)
    {
        sum[root] = 1;
        return;
    }
    int mid = (L+R)>>1;
    build(root<<1,L,mid);
    build(root<<1|1,mid+1,R);
    pushup(root);
}

void updata(int root,int L,int R,int s,int e,int c)
{
    if(s==L && e==R)
    {
        col[root] = c;
        sum[root] = c*(R-L+1);
        return;
    }
    pushdown(root,R-L+1);
    int mid = (L+R)>>1;
    if(e <= mid)
        updata(root<<1,L,mid,s,e,c);
    else if(s > mid)
        updata(root<<1|1,mid+1,R,s,e,c);
    else
    {
        updata(root<<1,L,mid,s,mid,c);
        updata(root<<1|1,mid+1,R,mid+1,e,c);
    }
    pushup(root);
}

int main()
{
    int T,kase = 1;
    scanf("%d", &T);
    while(T--)
    {
        int N,Q;
        scanf("%d%d", &N, &Q);
        build(1,1,N);
        while(Q--)
        {
            int s,e,c;
            scanf("%d%d%d",&s,&e,&c);
            updata(1,1,N,s,e,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",kase++,sum[1]);
    }
    return 0;
}

 

 

 

抱歉!评论已关闭.