• Just a Hook


    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.

    InputThe 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.
    OutputFor 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 <string>
    #include <map>
    #include <cstdio>
    using namespace std;
    int tree[400001];
    int lazy[400001];
    int n,m;
    void build(int l,int r,int t)
    {
        lazy[t] = 1;//初值为铜
        tree[t] = r - l + 1;//初始化区间和
        if(l == r)return;
        int mid = (l + r) >> 1;
        build(l,mid,t << 1);
        build(mid + 1,r,t << 1 | 1);
    }
    void update(int L,int R,int val,int l,int r,int t)
    {
        if(L <= l && R >= r)///区间在要求改变的范围内改变区间总和,并改变对应的lazy为val,不继续深入并返回
        {
            lazy[t] = val;
            tree[t] = (r - l + 1) * val;
            return;
        }
        int mid = (l + r) >> 1;
        if(lazy[t])///区间不被要求的范围所包含,需要向下更新。
        {
            lazy[t << 1] = lazy[t << 1 | 1] = lazy[t];
            tree[t << 1] = lazy[t] * (mid - l + 1);//区间和进行分割向下分配
            tree[t << 1 | 1] = lazy[t] * (r - mid);//区间和进行分割向下分配
            lazy[t] = 0;//lazy归零
        }
        if(R <= mid)update(L,R,val,l,mid,t << 1);
        else if(L > mid)update(L,R,val,mid + 1,r,t << 1 | 1);
        else
        {
            update(L,mid,val,l,mid,t << 1);
            update(mid + 1,R,val,mid + 1,r,t << 1 | 1);
        }
        tree[t] = tree[t << 1] + tree[t << 1 | 1];///向上更新
    }
    int main()
    {
        int t,a,b,c;
        scanf("%d",&t);
        for(int i = 1;i <= t;i ++)
        {
            scanf("%d%d",&n,&m);//用scanf耗时少
            build(1,n,1);
            for(int j = 0;j < m;j ++)
            {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
            printf("Case %d: The total value of the hook is %d.
    ",i,tree[1]);
        }
    }
  • 相关阅读:
    Javascript函数的简单学习
    JAVA学习绘图颜色及其笔画属性设置字体显示文字
    JAVA学习路线图
    JAVA学习AWT绘图
    JAVA学习Swing章节按钮组件JButton的简单学习
    JAVA学习Swing章节JPanel和JScrollPane面板的简单学习
    JAVA学习Swing章节流布局管理器简单学习
    JAVA学习Swing绝对局部简单学习
    JAVA学习Swing章节标签JLabel中图标的使用
    JAVA学习中Swing部分JDialog对话框窗体的简单学习
  • 原文地址:https://www.cnblogs.com/8023spz/p/7787535.html
Copyright © 2020-2023  润新知