• Just a Hook


     

    Just a Hook

    题目大意:原来有N个铜棍, 一个人有种能力可以把一个区间的棍变成铜,银或者金的,价值分别是1,2,3, 最后求出总价值,没啥好说的,赤裸裸的线段树; 

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 8   Accepted Submission(s) : 3
    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. [center][img]../../../data/images/C116-1010-1.JPG[/img][/center] 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.
     

    #include<iostream>

    #include<cstdio>
    using namespace std;
    #define maxn 100000
    struct node
    {
        int L, R, nature;
        int mid()
        {
            return (L+R) / 2;
        }
    };
    node a[maxn*4+10];
    int value;
    void BuildTree(int r, int L, int R);
    void Insert(int r ,int L, int R, int nature);
    void Query(int r);
    int main()
    {
        int ncase, k=1;
        scanf("%d", &ncase);
        while(ncase--)
        {
            int N, Q;
            scanf("%d%d", &N, &Q);
            BuildTree(1, 1, N);
            for(int i=0; i<Q; i++)
            {
                int X, Y, Z;
                scanf("%d%d%d", &X, &Y, &Z);
                Insert(1, X, Y, Z);
            }
            value = 0;
            Query(1);
            printf("Case %d: The total value of the hook is %d. ", k++, value);
        }
        return 0;
    }
    void BuildTree(int r, int L, int R)
    {
        a[r].L = L, a[r].R = R, a[r].nature = 1;
        if(L == R)return ;
        BuildTree(r*2, L, a[r].mid());
        BuildTree(r*2+1, a[r].mid()+1, R);
    }
    void Insert(int r ,int L, int R, int nature)
    {
        if(a[r].nature == nature)return ;
        if(a[r].L == L && a[r].R == R)
        {
            a[r].nature = nature;
            return;
        }
        if(a[r].nature)
            a[r*2].nature = a[r*2+1].nature = a[r].nature;
        a[r].nature = 0;
        if(R <= a[r].mid())
            Insert(r*2, L, R, nature);
        else if(L > a[r].mid())
            Insert(r*2+1, L, R, nature);
        else
        {
            Insert(r*2, L, a[r].mid(), nature);
            Insert(r*2+1, a[r].mid()+1, R, nature);
        }
    }
    void Query(int r)
    {
        if(a[r].nature)
        {
            value += (a[r].R - a[r].L + 1) * a[r].nature;
            return ;
        }
        Query(r*2);
        Query(r*2+1);
    }
  • 相关阅读:
    【VS开发】【电子电路技术】VPX技术介绍
    【VS开发】【电子电路技术】VPX技术介绍
    【VS开发】【DSP开发】浅谈Linux PCI设备驱动(二)
    【VS开发】【DSP开发】浅谈Linux PCI设备驱动(二)
    【VS开发】【DSP开发】浅谈Linux PCI设备驱动(一)
    【VS开发】【DSP开发】浅谈Linux PCI设备驱动(一)
    【Linux开发】彻底释放Linux线程的资源
    【Linux开发】彻底释放Linux线程的资源
    【VS开发】C++线程安全
    【VS开发】C++线程安全
  • 原文地址:https://www.cnblogs.com/liuxin13/p/3872696.html
Copyright © 2020-2023  润新知