zoukankan      html  css  js  c++  java
  • 1698-Just a Hook 线段树(区间替换)

    Just a Hook

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


    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.
    题意:n个数初始值为1,m次操作区间替换,输出总和
     1 #include<cstdio>
     2 #define lson l,m,rt<<1
     3 #define rson m+1,r,rt<<1|1
     4 const int MAXN = 100100;
     5 int sum[MAXN<<2];
     6 int col[MAXN<<2];
     7 int t,n,q;
     8 void putup(int rt)
     9 {
    10     sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    11 }
    12 void putdown(int rt,int m)
    13 {
    14     if (col[rt])
    15     {
    16         col[rt<<1] = col[rt<<1|1] = col[rt];
    17         sum[rt<<1] = (m-(m>>1))*col[rt];
    18         sum[rt<<1|1] = (m>>1)*col[rt];
    19         col[rt] = 0;
    20     }
    21 }
    22 void build(int l,int r,int rt)
    23 {
    24     col[rt] = 0;
    25     sum[rt] = 1;
    26     if (l==r) return ;
    27     int m = (l+r)>>1;
    28     build(lson);
    29     build(rson);
    30     putup(rt);
    31 }
    32 void update(int l,int r,int rt,int L,int R,int c)
    33 {
    34     if (L<=l && r<=R)
    35     {
    36         col[rt] = c;
    37         sum[rt] = c*(r-l+1);
    38         return ;
    39     }
    40     putdown(rt,r-l+1);
    41     int m = (l+r)>>1;
    42     if (L<=m) update(lson,L,R,c);
    43     if (R>m)  update(rson,L,R,c);
    44     putup(rt);
    45 }
    46 int main()
    47 {
    48     scanf("%d",&t);
    49     for (int i=1; i<=t; ++i)
    50     {
    51         scanf("%d%d",&n,&q);
    52         build(1,n,1);
    53         while (q--)
    54         {
    55             int x,y,z;
    56             scanf("%d%d%d",&x,&y,&z);
    57             update(1,n,1,x,y,z);
    58         }
    59         printf("Case %d: The total value of the hook is %d.
    ",i,sum[1]);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    A/B test
    镜像下载python包
    Jupyter 快捷键汇总
    牛顿法(Newton's method)VS梯度下降法(Gradient Descent)
    L-BFGS算法详解(逻辑回归的默认优化算法)
    np.bincount()频率统计函数
    异常值检验实战3_NBA球员表现稳定性分析
    异常值检测方法(Z-score,DBSCAN,孤立森林)
    浅谈压缩感知(十六):感知矩阵之RIP
    浅谈压缩感知(十五):感知矩阵之spark常数
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7054762.html
Copyright © 2011-2022 走看看