zoukankan      html  css  js  c++  java
  • HDU1698 Just a Hook(成段更新)【模板】

    题目链接
    Just a Hook

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

    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<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<ctime>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    #define pi 3.14159265358979
    #define mod 1000000007
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100010;
    
    int n, sum;
    
    struct node
    {
        int l, r, n;
    }a[maxn << 2];
    
    void build(int l, int r, int rt)
    {
        a[rt].l = l;
        a[rt].r = r;
        a[rt].n = 1;
        if (l != r)
        {
            int m = (l + r) >> 1;
            build(lson);
            build(rson);
        }
    }
    
    void update(int p, int l, int r, int rt)
    {
        if (a[rt].n == p) return;   //相同则无需修改
        if (a[rt].l == l&&a[rt].r == r)
        {
            a[rt].n = p;
            return;
        }
        if (a[rt].n != -1)  //如果不是杂色
        {
            a[2 * rt].n = a[2 * rt | 1].n = a[rt].n;
            a[rt].n = -1;   //节点与修改不同,必定杂色
        }
        int m = (a[rt].l + a[rt].r) >> 1;
        if (l > m)
        {
            update(p, l, r, rt << 1 | 1);
        }
        else if (r <= m)
        {
            update(p, l, r, rt << 1);
        }
        else
        {
            update(p, lson);
            update(p, rson);
        }
    }
    
    int find(int rt)        
    {
        if (a[rt].n != -1)     //如果不是杂色直接算
        {
            return a[rt].n*(a[rt].r - a[rt].l + 1);
        }
        else
        {
            return find(rt << 1) + find(rt << 1 | 1);
        }
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        for (int i = 1; i <= t; i++)
        {
            int n, k;
            scanf("%d%d", &n, &k);
            build(1, n, 1);
            while (k--)
            {
                int l, r, m;
                scanf("%d%d%d", &l, &r, &m);
                update(m, l, r, 1);
            }
            printf("Case %d: The total value of the hook is %d.
    ", i, find(1));
        }
        return 0;
    }
    
    
    Fighting~
  • 相关阅读:
    验证guid()类型值的函数
    jquery时期到计时插件
    最简单快速的Apache二级域名实现方法
    线程和线程的常用方法
    Mobile WEB前端研发流程
    HTML5标莶使用初级技巧
    前端开发中常见的HTML5标签乱用案例
    iPad应用的10大用户体验设计准则
    移动平台3G手机网站前端开发布局技巧汇总
    分享HTML 5的参考手册,演讲稿,电子书和教程
  • 原文地址:https://www.cnblogs.com/Archger/p/12774795.html
Copyright © 2011-2022 走看看