zoukankan      html  css  js  c++  java
  • HDU-1698-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.

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define ll long long
     5 using namespace std;
     6 const int amn=4*1e5+5;
     7 struct t
     8 {
     9     ll l,r,sum,lazy,len;
    10 } tree[amn];
    11 ll num[amn];
    12 void push_up(int rt)
    13 {
    14     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    15 }
    16 void push_down(int rt,int l,int r)
    17 {
    18     if(tree[rt].lazy)
    19     {
    20         int mid=(l+r)/2;
    21         tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
    22         tree[rt<<1].sum=tree[rt<<1].lazy*(mid-l+1);///左区间长度
    23         tree[rt<<1|1].sum=tree[rt<<1|1].lazy*(r-(mid+1)+1);///右区间长度
    24         tree[rt].lazy=0;
    25     }
    26     return ;
    27 }
    28 void build(int rt,ll l,ll r)
    29 {
    30     tree[rt].l=l,tree[rt].r=r;
    31     tree[rt].len=r-l+1;
    32     tree[rt].lazy=0;
    33     if(tree[rt].l==tree[rt].r)
    34     {
    35         tree[rt].sum=num[l];
    36         return ;
    37     }
    38     ll mid=(l+r)/2;
    39     build(rt<<1,l,mid);
    40     build(rt<<1|1,mid+1,r);
    41     push_up(rt);
    42 }
    43 void updata(int rt,ll l, ll r,ll L,ll R,ll val)
    44 {
    45     int mid=(l+r)/2;///区间中点
    46     if(l>=L&&r<=R)
    47     {
    48         tree[rt].lazy=val;
    49         tree[rt].sum=tree[rt].lazy*tree[rt].len;
    50         return ;
    51     }
    52     push_down(rt,l,r);
    53     if(L<=mid)updata(rt<<1,l,mid,L,R,val);///若L<=mid,则搜左孩子,若R<mid,则搜右孩子
    54     if(mid<R)updata(rt<<1|1,mid+1,r,L,R,val);
    55     push_up(rt);
    56 }
    57 int main()
    58 {
    59     int t,n,q,x,y,z;
    60     cin>>t;
    61     for(int ca=1; ca<=t; ca++)
    62     {
    63         scanf("%d%d",&n,&q);
    64         for(int i=1; i<=n; i++)num[i]=1;
    65         if(q)
    66         {
    67             build(1,1,n);
    68             for(int i=1; i<=q; i++)
    69             {
    70                 scanf("%d%d%d",&x,&y,&z);
    71                 updata(1,1,n,x,y,z);
    72             }
    73             printf("Case %d: The total value of the hook is %lld.
    ",ca,tree[1].sum);
    74         }
    75         else
    76         {
    77             printf("Case %d: The total value of the hook is %lld.
    ",ca,0);
    78         }
    79     }
    80 }
  • 相关阅读:
    hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
    HDU 2147 kiki's game(博弈)
    C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭
    C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
    C++学习45 流成员函数put输出单个字符 cin输入流详解 get()函数读入一个字符
    C++学习44 格式化输出,C++输出格式控制
    C++学习43 输入输出有关的类和对象
    C++学习42 输入和输出的概念
    C++学习41 exception类
    C++学习40 抛出自己的异常
  • 原文地址:https://www.cnblogs.com/Railgun000/p/10526651.html
Copyright © 2011-2022 走看看