zoukankan      html  css  js  c++  java
  • kuangbin专题七 HDU1698 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.


    将一段区间设置为同一个数,然后区间求和。自己敲得时候,还是发现了一点细节问题。这个和修改有区别,pushdown的左右孩子的sum是= 而不是 += 可以理解
    还有最重要的一点!!! 以后的lazy还是不要直接写了,应该先判断一下,就因为这个,找了好长时间bug


    还有一个问题是 区间求和时的 Pushdown和Pushup 不是必要的???不是太懂。



      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <math.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <string>
      7 #include <vector>
      8 #include <set>
      9 #include <map>
     10 #include <queue>
     11 #include <algorithm>
     12 #include <sstream>
     13 #include <stack>
     14 using namespace std;
     15 #define FO freopen("in.txt","r",stdin);
     16 #define rep(i,a,n) for (int i=a;i<n;i++)
     17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
     18 #define pb push_back
     19 #define mp make_pair
     20 #define all(x) (x).begin(),(x).end()
     21 #define fi first
     22 #define se second
     23 #define SZ(x) ((int)(x).size())
     24 #define debug(x) cout << "&&" << x << "&&" << endl;
     25 #define lowbit(x) (x&-x)
     26 #define mem(a,b) memset(a, b, sizeof(a));
     27 typedef vector<int> VI;
     28 typedef long long ll;
     29 typedef pair<int,int> PII;
     30 const ll mod=1000000007;
     31 const int inf = 0x3f3f3f3f;
     32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
     33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
     34 //head
     35 
     36 const int maxn=100010;
     37 int sum[maxn<<2],lazy[maxn<<2],_,n,q,l,r,val;
     38 
     39 void Pushup(int rt) {
     40     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     41 }
     42 
     43 void Pushdown(int rt,int x) {
     44     if(lazy[rt]) {//!!!
     45         lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
     46         sum[rt<<1]=(x-(x>>1))*lazy[rt];//!!! =
     47         sum[rt<<1|1]=(x>>1)*lazy[rt];
     48         lazy[rt]=0;
     49     }
     50 }
     51 
     52 void build(int rt,int L,int R) {
     53     lazy[rt]=0;
     54     if(L==R) {
     55         sum[rt]=1;
     56         return;
     57     }
     58     int mid=(L+R)>>1;
     59     build(rt<<1,L,mid);
     60     build(rt<<1|1,mid+1,R);
     61     Pushup(rt);
     62 }
     63 
     64 void Updata(int rt,int L,int R,int l,int r) {
     65     if(L>=l&&R<=r) {//核心
     66         lazy[rt]=val;
     67         sum[rt]=(R-L+1)*val;
     68         return;
     69     }
     70     Pushdown(rt,R-L+1);
     71     int mid=(L+R)>>1;
     72     if(l<=mid) Updata(rt<<1,L,mid,l,r);
     73     if(r>mid) Updata(rt<<1|1,mid+1,R,l,r);
     74     Pushup(rt);
     75 }
     76 
     77 int Query(int rt,int L,int R,int l,int r) {
     78     if(L>=l&&R<=r) return sum[rt];
     79     Pushdown(rt,R-L+1);
     80     int ans=0;
     81     int mid=(L+R)>>1;
     82     if(l<=mid) ans+=Query(rt<<1,L,mid,l,r);
     83     if(r>mid) ans+=Query(rt<<1|1,mid+1,R,l,r);
     84     Pushup(rt);
     85     return ans;
     86 
     87 }
     88 
     89 int main() {
     90     int cur=1;
     91     for(scanf("%d",&_);_;_--) {
     92         scanf("%d",&n);
     93         build(1,1,n);
     94         scanf("%d",&q);
     95         while(q--) {
     96             scanf("%d%d%d",&l,&r,&val);
     97             Updata(1,1,n,l,r);
     98         }
     99         printf("Case %d: The total value of the hook is %d.
    ",cur++,Query(1,1,n,1,n));
    100     }
    101 }



  • 相关阅读:
    C++ 类初始化的顺序
    Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
    Find out all the elements in A and B such that the A[i]B[j]=C[k]
    顺时针打印数组,美丽版
    Given a string S, find the longest palindromic substring in S.
    很多机器,每台机器上都有很一部分数据,如何输出分布在所有机器上的所有数据的median
    C++'s mutable and conceptual constness
    上20阶楼梯,可以一次迈1,2,3步,请问有多少种上法?
    Count smaller elements on right side in an array.
    一排房子,连续填色,成本最低的问题
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9862235.html
Copyright © 2011-2022 走看看