zoukankan      html  css  js  c++  java
  • 【线段树】HUD1698:Just a Hook

    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.
     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <string.h>
     6 using namespace std;
     7 typedef long long ll;
     8 typedef unsigned long long ull;
     9 const int N = 1e5+50;;
    10 typedef pair<int,int>pii;
    11 typedef pair<ll,int>P;
    12 int n,m;
    13 int sum[N*4],lazy[N*4],a[N];
    14 void pushDown(int rt,int len_l,int len_r){
    15     if(lazy[rt]){
    16         sum[rt*2]=len_l*lazy[rt];
    17         sum[rt*2+1]=len_r*lazy[rt];
    18         lazy[rt*2]=lazy[rt];
    19         lazy[rt*2+1]=lazy[rt];
    20         lazy[rt]=0;
    21     }
    22 }
    23 void pushUp(int rt){
    24     sum[rt]=sum[rt*2] + sum[rt*2+1];
    25 }
    26 void build(int l,int r,int rt){
    27     lazy[rt]=0;
    28     if(l==r){
    29         sum[rt]=a[l];
    30         return;
    31     }
    32     int mid=(l+r)/2;
    33     build(l,mid,rt*2);
    34     build(mid+1,r,rt*2+1);
    35     pushUp(rt);
    36 }
    37 void update(int L,int R,int val,int l,int r,int rt){
    38     if(L<=l && r<=R){
    39         sum[rt]=val*(r-l+1);
    40         lazy[rt]=val;
    41         return;
    42     }
    43     int mid=(l+r)/2;
    44     pushDown(rt,mid-l+1,r-mid);
    45     if(L<=mid)update(L,R,val,l,mid,rt*2);
    46     if(R>mid) update(L,R,val,mid+1,r,rt*2+1);
    47     pushUp(rt);
    48 }
    49 int query(int L,int R,int l,int r,int rt){
    50     if(L<=l && r<=R){
    51         return sum[rt];
    52     }
    53     int mid=(l+r)/2;
    54     pushDown(rt,mid-l+1,r-mid);
    55     int ret=0;
    56     if (L <= mid) ret += query(L,R ,l,mid,rt*2);
    57     if (R > mid) ret += query(L,R,mid+1,r,rt*2+1);
    58     return ret;
    59 }
    60 int main() {
    61     int T;
    62     int l,r,val;
    63     cin>> T;
    64     for(int j=1;j<=T;j++)
    65     {
    66         scanf("%d",&n);
    67         for(int i=1;i<=n;i++)
    68         {
    69             a[i]=1;
    70         }
    71         build(1,n,1);
    72         scanf("%d",&m);
    73         while(m--)
    74         {
    75             scanf("%d%d%d",&l,&r,&val);
    76             update(l,r,val,1,n,1);
    77         }
    78         printf("Case %d: The total value of the hook is %d.
    ",j,query(1,n,1,n,1));
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    Centos 7环境下配置MySQL 5.7读写分离
    Centos 7环境下安装配置MySQL 5.7
    Hadoop 2.8集群安装及配置记录
    ASP.NET Core 1.1版本之Hello word
    Hadoop版Helloworld之wordcount运行示例
    SSH配置免密登陆设置汇总
    最小安装模式下Centos7.*网卡启动配置
    骚扰式管理
    项目团队之分工协作
    利用微软AntiXss Library过滤输出字符,防止XSS攻击
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/8376304.html
Copyright © 2011-2022 走看看