zoukankan      html  css  js  c++  java
  • (线段树)hdoj1698-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<bits/stdc++.h>
     3 #include <stack>
     4 #include <queue>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef unsigned long long ull;
    11 const int MAX=1e5+5;
    12 int T;
    13 int n;
    14 int q;
    15 struct node
    16 {
    17     int left,right,mid;
    18     int col;
    19     int sum;
    20     int lazy;
    21 }st[MAX*10];
    22 void init(int l,int r,int k)
    23 {
    24     st[k].left=l;
    25     st[k].right=r;
    26     st[k].mid=(l+r)/2;
    27     st[k].col=1;
    28     st[k].sum=r-l;
    29     st[k].lazy=0;
    30     if(l+1==r)
    31         return;
    32     init(l,(l+r)/2,2*k);
    33     init((l+r)/2,r,2*k+1);
    34 }
    35 void pushdown(int k)
    36 {
    37     if(st[k].lazy!=0)
    38     {
    39         st[2*k].lazy=st[2*k+1].lazy=st[k].lazy;
    40         st[k].lazy=0;
    41         st[2*k].sum=(st[2*k].right-st[2*k].left)*st[2*k].lazy;
    42         st[2*k+1].sum=(st[2*k+1].right-st[2*k+1].left)*st[2*k+1].lazy;
    43     }
    44 }
    45 void update(int ptl,int ptr,int chl,int chr,int col,int k)
    46 {
    47     if(ptl>=chl&&ptr<=chr)
    48     {
    49         st[k].sum=(ptr-ptl)*col;
    50         st[k].lazy=col;
    51         return;
    52     }
    53     if(ptl>=chr||ptr<=chl)
    54         return;
    55     pushdown(k);
    56     update(ptl,(ptl+ptr)/2,chl,chr,col,2*k);
    57     update((ptl+ptr)/2,ptr,chl,chr,col,2*k+1);
    58     st[k].sum=st[2*k].sum+st[2*k+1].sum;
    59     return;
    60 }
    61 int main()
    62 {
    63     scanf("%d",&T);
    64     for(int i=1;i<=T;i++)
    65     {
    66         int zuo,you,co;
    67         scanf("%d",&n);
    68         init(1,n+1,1);
    69         scanf("%d",&q);
    70         while(q--)
    71         {
    72             scanf("%d %d %d",&zuo,&you,&co);
    73             update(1,n+1,zuo,you+1,co,1);
    74         }
    75         printf("Case %d: The total value of the hook is %d.
    ",i,st[1].sum);
    76     }
    77 }
    
    
  • 相关阅读:
    mac鼠标滚动方向自然问题
    Android学习笔记(9):使用XML文件和Java代码控制UI界面
    新版Eclipse找不到Java EE Module Dependencies选项
    JAR包放在WEB-INF/lib子目录报ClassNotFoundException解决方案
    Eclipse中將Java项目转变为Java Web项目
    java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceE解决方法
    MyBatis数据持久化(九)动态sql
    MyBatis数据持久化(八)sql复用
    MyBatis数据持久化(七)多表连接查询
    JQuery实现的弹窗效果
  • 原文地址:https://www.cnblogs.com/quintessence/p/6410950.html
Copyright © 2011-2022 走看看