成段更新(上色 POJ 2777)

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

public classMain {

	int L, T, O, A, B, C;
	Tree tree[];
	
	int countBit(int x){
		int out;
		for(out=0;x > 0;x>>=1)
			out+=x&1;
		return out;
	}
	
	void build(int pos, int lt, int rt) {
		tree[pos] = new Tree();
		tree[pos].lt = lt; tree[pos].rt = rt;
		tree[pos].color = 1; tree[pos].flag = true;
		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 (tree[pos].lt == lt && tree[pos].rt == rt) {
			tree[pos].color = 1 << val - 1;
			tree[pos].flag = true;
			return;
		}
		if (tree[pos].flag) {
			tree[pos << 1].color = tree[pos].color;
			tree[pos << 1 | 1].color = tree[pos].color;
			tree[pos << 1].flag = tree[pos << 1 | 1].flag = true;
			tree[pos].flag = false;
		}
		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);
		}
		tree[pos].color = tree[pos << 1].color | tree[pos << 1 | 1].color;
	} 

	int query(int pos, int lt, int rt) {
		if (tree[pos].lt == lt && tree[pos].rt == rt) {
			return tree[pos].color;
		}
		if (tree[pos].flag) return tree[pos].color;
		int mid = (tree[pos].lt + tree[pos].rt) >> 1;
		if (rt <= mid) return query(pos << 1, lt, rt);
		else if (lt > mid) return query(pos << 1 | 1, lt, rt);
		else return (query(pos << 1, lt, mid) | query(pos << 1 | 1, mid + 1, rt));
	}
	
	
	void run() throws Exception {
		String str[] = cin.readLine().split(" ");
		L = Integer.valueOf(str[0]);
		T = Integer.valueOf(str[1]);
		O = Integer.valueOf(str[2]);
		tree = new Tree[L * 4];
		build(1, 1, L);
		while (O-- > 0) {
			str = cin.readLine().split(" ");
			A = Integer.valueOf(str[1]);
			B = Integer.valueOf(str[2]);
			if (A > B) {
				int t = A;
				A = B;
				B = t;
			}
			if (str[0].charAt(0) == 'C') {
				C = Integer.valueOf(str[3]);
				update(1, A, B, C);
			} else if (str[0].charAt(0) == 'P') {
				System.out.println(countBit(query(1, A, B)));
			}
		}
	}   
	
	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));
	
}

classTree {
	int lt, rt, color;
	boolean flag;
}

classInputReader { 
    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; 
    }   
} 


classOutputWriter {
	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-06 00:12  Sure_Yi  阅读(162)  评论(0编辑  收藏  举报

导航