成段更新(加上一个值 POJ 3468)

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

 

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

public classMain {

	int n, q, ar[], 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].add = 0;
		if (lt == rt) {
			tree[pos].sum = ar[lt];
			return;
		}
		int mid = (lt + rt) >> 1;
		build(pos << 1, lt, mid);
		build(pos << 1 | 1, mid + 1, rt);
		tree[pos].sum = tree[pos << 1].sum + tree[pos << 1 | 1].sum;
	}
	// 重点 向下更新 (update(), query() 中都要用到)
	void Down(int pos) {
		if (tree[pos].add == 0) return;
		tree[pos << 1].add += tree[pos].add;
		tree[pos << 1 | 1].add += tree[pos].add;
		tree[pos << 1].sum += tree[pos].add * (tree[pos << 1].rt - tree[pos << 1].lt + 1);
		tree[pos << 1 | 1].sum += tree[pos].add * (tree[pos << 1 | 1].rt - tree[pos << 1 | 1].lt + 1);
		tree[pos].add = 0;
	}
	
	void update(int pos, int lt, int rt, long add) {
		if (lt == tree[pos].lt && rt == tree[pos].rt) {
			tree[pos].add += add;
			tree[pos].sum += (tree[pos].rt - tree[pos].lt + 1) * add;
			return;
		}
		Down(pos);
		int mid = (tree[pos].lt + tree[pos].rt) >> 1;
		if (rt <= mid) update(pos << 1, lt, rt, add);
		else if (lt > mid) update(pos << 1 | 1, lt, rt, add);
		else {
			update(pos << 1, lt, mid, add);
			update(pos << 1 | 1, mid + 1, rt, add);
		}
		tree[pos].sum = tree[pos << 1].sum + tree[pos << 1 | 1].sum;
	}
	
	long query(int pos, int lt, int rt) {
		if (lt == tree[pos].lt && rt == tree[pos].rt) {
			return tree[pos].sum;
		}
		Down(pos);
		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);
		}
	}
	
	String str;
		
	void run() throws Exception {
		n = cin.nextInt();
		q = cin.nextInt();
		ar = new int[n + 1];
		for (int i = 1; i <= n; i++) ar[i] = cin.nextInt();
		tree = new Tree[n * 4 + 1];
		build(1, 1, n);
		while (q-- > 0) {
			str = cin.next();
			a = cin.nextInt();
			b = cin.nextInt();
			if (a > b) {
				int t = a;
				a = b;
				b = t;
			}
			if (str.charAt(0) == 'Q') {
				System.out.println(query(1, a, b));
			} else if (str.charAt(0) == 'C') {
				c = cin.nextInt();
				update(1, a, b, c);
			}
		}
	}   
	
	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;
	long add, sum;
}

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:08  Sure_Yi  阅读(166)  评论(0编辑  收藏  举报

导航