成段更新(更新为一直特定的值 HDU 1698)

http://blog.csdn.net/shiqi_614/article/details/8228102

 

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public class Main {

    int t, n, q, a, b, c;
    Tree tree[];
    
    void build(int pos, int lt, int rt) {
        tree[pos] = new Tree();
        tree[pos].lt = lt;
        tree[pos].rt = rt;
        tree[pos].val = 1;
        if (lt == rt) return;
        int mid = (lt + rt) >> 1;
        build(pos << 1, lt, mid);
        build(pos << 1 | 1, mid + 1, rt);
    }
    
    void update(int pos, int lt, int rt, int val) {
        if (lt == tree[pos].lt && rt == tree[pos].rt) {
            tree[pos].val = val;
            return;
        }
        if (tree[pos].val > 0) {
            tree[pos << 1].val = tree[pos << 1 | 1].val = tree[pos].val;
            tree[pos].val = -1;
        }
        int mid = (tree[pos].lt + tree[pos].rt) >> 1;
        if (rt <= mid) update(pos << 1, lt, rt, val);
        else if (lt > mid) update(pos << 1 | 1, lt, rt, val);
        else {
            update(pos << 1, lt, mid, val);
            update(pos << 1 | 1, mid + 1, rt, val);
        }
    }
    
    int query(int pos) {
        if (tree[pos].val > 0) {
            return (tree[pos].rt - tree[pos].lt + 1) * tree[pos].val;
        }
        return query(pos << 1) + query(pos << 1 | 1);
    }
    
    void run() throws Exception {
        t = cin.nextInt();
        for (int Case = 1; Case <= t; Case++) {
            n = cin.nextInt();
            tree = new Tree[n * 4 + 1];
            build(1, 1, n);
            q = cin.nextInt();
            while (q-- > 0) {
                a = cin.nextInt();
                b = cin.nextInt();
                c = cin.nextInt();
                update(1, a, b, c);
            }
            System.out.println("Case " + Case + ": The total value of the hook is " + query(1) + ".");
        }
    }   
    
    public static void main(String[] args) throws Exception {    
//        InputStreamReader is = new InputStreamReader(new FileInputStream("E:/input.txt"));
//        BufferedReader cin = new BufferedReader(is);
        
        inputStream = System.in;
        cin = new InputReader(inputStream);
        
        new Main().run();
//        out.close();          
    }
    
    static InputStream inputStream;
    static InputReader cin;
    
//    InputStreamReader is = new InputStreamReader(System.in);
//    BufferedReader cin = new BufferedReader(is);
    
//    static InputStream inputStream = System.in;
//    static InputReader cin = new InputReader(inputStream);    
    
//    static OutputStream outputStream = System.out;
//    static OutputWriter out = new OutputWriter(outputStream);
    
//    Scanner cin = new Scanner(new BufferedInputStream(System.in));
    
}

class Tree {
    int lt, rt, val;
}

class InputReader { 
    private InputStream stream; 
    private byte[] buf = new byte[1024]; 
    private int curChar; 
    private int numChars; 

    public InputReader(InputStream stream) { 
        this.stream = stream; 
    } 

    public int read() { 
        if (numChars == -1) 
            return -1; 
            //throw new InputMismatchException(); 
        if (curChar >= numChars) { 
            curChar = 0; 
            try { 
                numChars = stream.read(buf); 
            } catch (IOException e) { 
                throw new InputMismatchException(); 
            } 
            if (numChars <= 0) 
                return -1; 
        } 
        return buf[curChar++]; 
    } 

    public int nextInt() { 
        int c = read(); 
        if (c == -1) 
            return -1; 
        while (isSpaceChar(c)) 
            c = read(); 
        int sgn = 1; 
        if (c == '-') { 
            sgn = -1; 
            c = read(); 
        } 
        int res = 0; 
        do { 
            if (c < '0' || c > '9') 
                throw new InputMismatchException(); 
            res *= 10; 
            res += c - '0'; 
            c = read(); 
        } while (!isSpaceChar(c)); 
        return res * sgn; 
    } 
    
    public long nextLong() {
        int c = read(); 
        if (c == -1) 
            return -1; 
        while (isSpaceChar(c)) 
            c = read(); 
        int sgn = 1; 
        if (c == '-') { 
            sgn = -1; 
            c = read(); 
        } 
        long res = 0; 
        do { 
            if (c < '0' || c > '9') 
                throw new InputMismatchException(); 
            res *= 10; 
            res += c - '0'; 
            c = read(); 
        } while (!isSpaceChar(c)); 
        return res * sgn;
    }
     
    public String next() {   
        StringBuilder str = new StringBuilder();   
        int ch;   
        while (isSpaceChar(ch = read()));   
        if (ch == -1)   
            return null;   
        do {   
            str.appendCodePoint(ch);   
        } while (!isSpaceChar(ch = read()));   
        return str.toString();   
    }  
     
    public static boolean isSpaceChar(int c) { 
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; 
    } 

    public char nextCharacter() { 
        int c = read(); 
        while (isSpaceChar(c)) 
            c = read(); 
        return (char) c; 
    }   
} 


class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void println(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

    }

posted on 2013-03-05 21:46  Sure_Yi  阅读(167)  评论(0编辑  收藏  举报

导航