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

    Just a Hook

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


    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.
     
    Source

    成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候 

     

    代码:

     

     1 #include<cstring>
     2 #include<cstdio>
     3 const int maxn=100005;
     4 struct node
     5 {
     6     int lef,rig,sum;
     7     int cnt;
     8     int mid(){
     9       return lef+(rig-lef>>1);
    10     }
    11 };
    12 
    13 node sac[maxn*3];
    14 
    15 void Build(int left,int right,int pos)
    16 {
    17   sac[pos]=(node){left,right,1};
    18   if(left==right)return ;
    19   int mid=sac[pos].mid();
    20   Build(left,mid,pos<<1);
    21   Build(mid+1,right,pos<<1|1);
    22   sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
    23 }
    24 void Update(int left,int right,int pos,int val)
    25 {
    26    if(left<=sac[pos].lef&&sac[pos].rig<=right){
    27       sac[pos].sum=val*(sac[pos].rig-sac[pos].lef+1);
    28       sac[pos].cnt=val;
    29       return ;
    30     }
    31     if(sac[pos].cnt!=0){ //向下更新一次
    32       sac[pos<<1].sum=sac[pos].cnt*(sac[pos<<1].rig-sac[pos<<1].lef+1);
    33       sac[pos<<1|1].sum=sac[pos].cnt*(sac[pos<<1|1].rig-sac[pos<<1|1].lef+1);
    34       sac[pos<<1|1].cnt=sac[pos<<1].cnt=sac[pos].cnt;
    35       sac[pos].cnt=0;
    36     }
    37     int mid=sac[pos].mid();
    38     if(mid>=left)
    39       Update(left,right,pos<<1,val);
    40     if(mid<right)
    41       Update(left,right,pos<<1|1,val);
    42    sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
    43 }
    44 int main()
    45 {
    46     int test,n,Q;
    47     int a,b,c;
    48     scanf("%d",&test);
    49     for(int i=1;i<=test;i++){
    50       scanf("%d%d",&n,&Q);
    51       Build(1,n,1);
    52       while(Q--)
    53       {
    54         scanf("%d%d%d",&a,&b,&c);
    55         Update(a,b,1,c);
    56       }
    57       printf("Case %d: The total value of the hook is %d.
    ",i,sac[1].sum);
    58     }
    59 }
    View Code

     

     

  • 相关阅读:
    Spring 09 : AOP实例
    Spring08 AOP概念
    Spring 07 : 动态代理
    Spring06 Spring+Junit
    Spring05 : 基于注解的IOC
    Spring03 : 依赖注入
    jupyter修改python核(使用不同的python虚拟环境)
    线性代数的本质——引入几何视角
    图像的去雾与加雾
    从MATLAB看一个IDE应该具有的素质
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3898538.html
Copyright © 2011-2022 走看看