• HDU 1698 Just a Hook


    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.
    方法-:
    #include<stdio.h> #define N 100010 int A[N], num; struct node { int left, right, sum, val; //sum存放区间的总价值,val存放该区间单根棍子的价值 }no[4*N]; void Bulid(int left, int right, int root) { int mid; no[root].left = left; no[root].right = right; no[root].val = 1; //刚开始每根木棍的价值都是1 if (left == right) { no[root].sum = A[left]; return ; } mid = (left+right)/2; Bulid(left, mid, root*2); Bulid(mid+1, right, root*2+1); no[root].sum = no[root*2].sum + no[root*2+1].sum; } void Pushdown(int root) //向下更新 { no[root*2].sum = (no[root*2].right-no[root*2].left+1)*no[root].val; no[root*2].val = no[root].val; no[root*2+1].sum = (no[root*2+1].right-no[root*2+1].left+1)*no[root].val; no[root*2+1].val = no[root].val; no[root].val = 0; //更新完后根节点的值要初始化为0 } void Update(int left, int right, int root) { int mid; if (no[root].val == num) //如果想改变的值和本来的值相同,则不用继续 return ; if (no[root].left == left && no[root].right == right) { no[root].val = num; no[root].sum = (right-left+1)*no[root].val; //如果找到该区间则更新该区间的值 return ; } if (no[root].val > 0) //等于0时代表不需要更新 Pushdown(root); mid = (no[root].left+no[root].right)/2; if (right <= mid) Update(left, right, root*2); else if (left > mid) Update(left, right, root*2+1); else { Update(left, mid, root*2); Update(mid+1, right, root*2+1); } no[root].sum = no[root*2].sum + no[root*2+1].sum; //子节点更新完后更新根节点 } int main () { int T, n, m, L, R, k = 0, i; scanf("%d", &T); while (T--) { k++; scanf("%d", &n); for (i = 1; i <= n; i++) A[i] = 1; Bulid(1, n, 1); scanf("%d", &m); while (m--) { scanf("%d%d%d", &L, &R, &num); Update(L, R, 1); } printf("Case %d: The total value of the hook is %d. ", k, no[1].sum); } return 0; }
    方法二:建线段树时节点中将价值的种类保存下来,如果该区间价值都一样,则保存1/2/3,若不一样则保存-1
    #include<stdio.h>
    #define N 100010
    struct node
    {
        int left, right, val;
    }no[4*N];
    int num;
    void Bulid(int left, int right, int root)
    {
        int mid;
        no[root].left = left;
        no[root].right = right;
        no[root].val = 1;
        if (left == right) return ;
        mid = (left+right)/2;
        Bulid(left, mid, root*2);
        Bulid(mid+1, right, root*2+1);
    }
    void Update(int left, int right, int root)
    {
        int mid;
        if (no[root].val == num) return ;
        if (no[root].left == left && no[root].right == right)
        {
            no[root].val = num;
            return ;
        }
        if (no[root].val != -1)
        {
            no[root*2].val = no[root].val;
            no[root*2+1].val = no[root].val;
            no[root].val = -1; //更新完子节点后一定要初始化为-1!!!!
        } //如果该区间价值不是混合的,那么它的子节点也一定不是混合的
        mid = (no[root].left+no[root].right)/2;
        if (right <= mid) Update(left, right, root*2);
        else if (left > mid) Update(left, right, root*2+1);
        else
        {
            Update(left, mid, root*2);
            Update(mid+1, right, root*2+1);
        }
    }
    int Count(int root) //计算区间的总价值
    {
        if (no[root].val != -1) //若价值不是混合的,直接用区间长度乘以单价值即可
            return (no[root].right-no[root].left+1)*no[root].val;
        else //若混合,则求其两个子节点的和
            return Count(root*2) + Count(root*2+1);
    }
    int main ()
    {
        int T, n, m, L, R, k = 0;
        scanf("%d", &T);
        while (T--)
        {
            k++;
            scanf("%d", &n);
            Bulid(1, n, 1);
            scanf("%d", &m);
            while (m--)
            {
                scanf("%d%d%d", &L, &R, &num);
                Update(L, R, 1);
            }
            printf("Case %d: The total value of the hook is %d.
    ", k, Count(1));
        }
        return 0;
    }
  • 相关阅读:
    20140327工作室日志
    20140326学习工作室日志
    标准连接池实现
    JDBC
    监听器
    数据表多对多
    MATLAB 中几个颜色空间的坐标范围
    RabbitMQ(一)
    Web Service之Axis(二)
    Web Service之CXF(四)
  • 原文地址:https://www.cnblogs.com/syhandll/p/4692359.html
Copyright © 2020-2023  润新知