zoukankan      html  css  js  c++  java
  • HDU 1698 Just a Hook

    Just a Hook

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


    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.
     

    Source
     

    Recommend
    wangye
     
    思路:本来很简单的线段树,但是写了一个这样的线段树,结果超时了
    超时代码:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int time_number;
    int n,m;
    int search_ans;
    int begin,end,value;
    int the_last_value;
    struct Node
    {
        int leftboundary,rightboundary;
        Node *leftchild,*rightchild;
        int value;
    };
    Node* buildtree(int begin,int end)
    {
        Node* p = new Node;
        p -> leftboundary = begin;
        p -> rightboundary = end;
        p -> value = 1;
        if(begin == end)
             return p;
        p -> leftchild = buildtree(begin,(begin + end) / 2);
        p -> rightchild = buildtree((begin + end) / 2 + 1,end);
        return p;
    }
    void insert(Node *T,int begin,int end,int value)
    {
        if(begin >= T -> leftboundary && begin <= T -> rightboundary
           || end >= T -> leftboundary && end <= T -> rightboundary)
        {
            T -> value = -1;
        }
        if(begin <= T -> leftboundary && end >= T -> rightboundary)
        {
            T -> value = value;
        }
        if(T -> leftboundary == T -> rightboundary)
            return ;
        if(begin <= ((T -> leftboundary + T -> rightboundary) / 2))
            insert(T -> leftchild,begin,end,value);
        if(end > ((T -> leftboundary + T -> rightboundary) / 2))
            insert(T -> rightchild,begin,end,value);
    }
    int search(Node* T,int begin,int end)
    {
        if(begin <= T -> leftboundary && end >= T -> rightboundary && T -> value != -1)
        {
            search_ans += (T -> rightboundary - T -> leftboundary + 1) * T -> value;
            return search_ans;
        }
        if(begin <= (T -> leftboundary + T -> rightboundary) / 2)
            search(T -> leftchild,begin,end);
        if(end > (T -> leftboundary + T -> rightboundary) / 2)
            search(T -> rightchild,begin,end);
        return search_ans;
    }
    int main()
    {
        scanf("%d",&time_number);
        for(int k = 1;k <= time_number;k ++)
        {
            scanf("%d",&n);
            scanf("%d",&m);
            Node *root = buildtree(1,n);
            while(m --)
            {
                scanf("%d%d%d",&begin,&end,&value);
                insert(root,begin,end,value);
            }
            search_ans = 0;
            the_last_value = search(root,1,n);
            printf("Case %d: The total value of the hook is ",k);
            printf("%d
    ",the_last_value);
        }
        return 0;
    }

    后来看别人的才发现这个很不错的思维

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int t;
    int n,m;
    int map[100010];
    int operation[100010][3];
    int the_last_sum;
    int the_min_sum;
    int main()
    {
        scanf("%d",&t);
        for(int k = 1;k <= t;k ++)
        {
            memset(map,1,sizeof(map));
            memset(operation,0,sizeof(operation));
            scanf("%d",&n);
            scanf("%d",&m);
            for(int i = 1;i <= m;i ++)
            {
                scanf("%d%d%d",&operation[i][0],&operation[i][1],&operation[i][2]);
            }
            the_last_sum = 0;
            for(int i = 1;i <= n;i ++)
            {
                the_min_sum = 1;
                for(int j = m;j >= 0;j --)
                {
                    if(operation[j][0] <= i && operation[j][1] >= i)
                    {
                        the_min_sum = operation[j][2];
                        break ;
                    }
                }
                the_last_sum += the_min_sum;
            }
            printf("Case %d: The total value of the hook is ",k);
            printf("%d.
    ",the_last_sum);
        }
        return 0;
    }
  • 相关阅读:
    Html语言基础
    acm练习(四)
    acm练习(三)
    acm练习(二)
    acm练习(一)
    android自定义控件属性
    android ViewGroup getChildDrawingOrder与 isChildrenDrawingOrderEnabled()
    java 用Arrays.binarySearch解读 快速定位数字范围
    android极光推送初步了解...
    GridView与ListView冲突
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3354458.html
Copyright © 2011-2022 走看看