hdu 1698 Just a Hook (线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

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


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.

hdu 1698 Just a Hook (线段树)_第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.
 

Source
2008 “Sunline Cup” National Invitational Contest
 

Recommend
wangye   |   We have carefully selected several similar problems for you:  1542 1255 1828 3397 2871 

题目大意:给一列数,初始值均为1,改变某个区间的值,最后输出给出的这列数的和。
测试数据:先输入一个t表示有几组测试数据,再输入一个l表示这列数的长度,接着输出一个n表示接下去有几个操作,然后输入【a,b】这段区间所有的数改成c这个数。最后求得这列数该笔后的总价值。

解题思路:需要注意的是更新树和查找树这两个函数,因为数据比较大,不能更新到最底下,采用几个判断(在代码中有详细的注释)来进行节省时间。

详见代码。
#include <iostream>
#include <cstdio>

using namespace std;

struct node
{
    int l,r;
    int color;
} s[100000*4+10];

void InitTree(int l,int r,int k)
{
    s[k].l=l;
    s[k].r=r;
    s[k].color=1;
    int mid=(l+r)/2;
    if (l==r)
        return ;
    InitTree(l,mid,2*k);
    InitTree(mid+1,r,2*k+1);
}

void UpdataTree(int l,int r,int c,int k)
{
    if (s[k].l==l&&s[k].r==r)
    {
        s[k].color=c;
        return ;
    }
    if (s[k].color==c)
        return;
    if (s[k].color!=-1)//color=-1表示多个颜色
    {
        s[k*2].color=s[k].color;
        s[k*2+1].color=s[k].color;
        s[k].color=-1;
    }
    int mid=(s[k].l+s[k].r)/2;
    if (l>mid)//右边
        UpdataTree(l,r,c,2*k+1);
    else if (r<=mid)
        UpdataTree(l,r,c,2*k);
    else
    {
        UpdataTree(mid+1,r,c,2*k+1);
        UpdataTree(l,mid,c,2*k);
    }
}

int SearchTree(int k)//返回第k段区间的价值
{
    if (s[k].color!=-1)//只是单种颜色就直接计算价值
        return ((s[k].r-s[k].l)+1)*s[k].color;
    return SearchTree(2*k)+SearchTree(2*k+1);//如果不是就通过下一层的返回值进行计算
}

int main()
{
    int t;
    int flag=1;
    int a,b,c;//a,b表示区间;c表示颜色
    scanf("%d",&t);
    while (t--)
    {
        int l;//钩子的长度
        int n;
        scanf("%d%d",&l,&n);
        InitTree(1,l,1);
        for (int i=1; i<=n; i++)
        {
            scanf ("%d%d%d",&a,&b,&c);
            UpdataTree(a,b,c,1);
        }
        int ans=SearchTree(1);
        printf ("Case %d: The total value of the hook is %d.\n",flag++,ans);
    }
    return 0;
}



 

你可能感兴趣的:(算法,C语言)