HDU 1698 Just a Hook (裸区间更新线段树)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvlinfeng970/article/details/80291296

注意lazy里面要存的是新的节点的值还是新节点没有往下更新的值,每次要灵活使用。

import java.io.*;
import java.util.*;
import java.math.*;
import java.io.PrintWriter;
import java.util.Arrays;

public class Main{
    static InputReader input = new InputReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int[] tree = new int[100000<<2|1];
    static int[] lazy = new int[100000<<2|1];
    public static void main(String[] args){
        int count = input.nextInt();
        for(int i=0;i<count;i++){
            Arrays.fill(tree,0);
            Arrays.fill(lazy,0);
            int n = input.nextInt();
            int q = input.nextInt();
            BuildTree(1,n,1);
            for(int j=0;j<q;j++){
                Update(1,n,input.nextInt(),input.nextInt(),1,input.nextInt());
            }
            out.println("Case "+(i+1)+": The total value of the hook is "+tree[1]+".");
        }
        out.close();
    }
    static void BuildTree(int l,int r,int root){
        if(l==r){
            tree[root] = 1;
            return;
        }
        int m = (l+r)>>1;
        BuildTree(l,m,root<<1);
        BuildTree(m+1,r,root<<1|1);
        PushUp(root);
    }
    static void PushUp(int root){
        tree[root] = tree[root<<1]+tree[root<<1|1];
    }
    static void Update(int l,int r,int L,int R,int root,int value){
        if(L<=l&&R>=r){
            tree[root] = (r-l+1)*value;
            if(l!=r)lazy[root] = value;
            return;
        }
        int m = (l+r)>>1;
        if(L<=m){
            if(lazy[root<<1]>0){
                PushDown(l,m,root<<1);
            }
            Update(l,m,L,R,root<<1,value);
        }
        if(R>m){
            if(lazy[root<<1|1]>0){
                PushDown(m+1,r,root<<1|1);
            }
            Update(m+1,r,L,R,root<<1|1,value);
        }
        PushUp(root);
    }
    static void PushDown(int l,int r,int root){
        int m = (l+r)>>1;
        tree[root<<1] = lazy[root]*(m-l+1);
        if(m-l>0){
            lazy[root<<1] = lazy[root];
        }
        tree[root<<1|1] = lazy[root]*(r-m);
        if(r-m>1){
            lazy[root<<1|1] = lazy[root];
        }
        lazy[root] = 0;
    }
    static class InputReader
{
    BufferedReader buf;
    StringTokenizer tok;
    InputReader()
    {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext()
    {
        while(tok == null || !tok.hasMoreElements()) 
        {
            try
            {
                tok = new StringTokenizer(buf.readLine());
            } 
            catch(Exception e) 
            {
                return false;
            }
        }
        return true;
    }
    String next()
    {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt()
    {
        return Integer.parseInt(next());
    }
    long nextLong()
    {
        return Long.parseLong(next());
    }
    double nextDouble()
    {
        return Double.parseDouble(next());
    }
    BigInteger nextBigInteger()
    {
        return new BigInteger(next());
    }
    BigDecimal nextBigDecimal()
    {
        return new BigDecimal(next());
    }
}
    
}

猜你喜欢

转载自blog.csdn.net/lvlinfeng970/article/details/80291296