树状数组(HDU 1166)

树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组a[1..n],// 从1开始
用lowbit函数维护了一个树的结构

  用lowbit函数维护了一个树的结构

那么查询a[1]+...+a[n]的时间是log级别的,而且是一个在线的数据结构,
支持随时修改某个元素的值,复杂度也为log级别。
 
例:HDU 1166
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public classMain {

	int t, n, a[], c[];
	String str;
	
	int lowbit(int x) {
		return x & (-x);
	}
	
	void add(int pos, int val) {
		while (pos <= n) {
			c[pos] += val;
			pos += lowbit(pos);
		}
	}
	
	int sum(int pos) {
		int s = 0;
		while (pos > 0) {
			s += c[pos];
			pos -= lowbit(pos);
		}
		return s;
	}
	
	void run() {
		t = cin.nextInt();
		for (int Case = 1; Case <= t; Case++) {
			n = cin.nextInt();
			a = new int[n + 1];
			c = new int[n + 1];
			for (int i = 1; i <= n; i++) {
				a[i] = cin.nextInt();
				add(i, a[i]);
			}
			System.out.println("Case " + Case + ":");
			while (true) {
				str = cin.next();
				if (str.equals("End")) break;
				int a = cin.nextInt(), b = cin.nextInt();
				if (str.equals("Add")) {
					add(a, b);
				} else if (str.equals("Sub")) {
					add(a, -b);
				} else if (str.equals("Query")) {
					System.out.println(sum(b) - sum(a - 1));
				}
			}
		}
	}  

	public static void main(String[] args) {
		Mainsolved = new Main();
		solved.run();
	}
	
	static InputStreaminputStream = System.in;
	static InputReadercin = new InputReader(inputStream);
	
//	Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
}


classInputReader { 

    private InputStreamstream; 
    private byte[] buf = new byte[1024]; 
    private int curChar; 
    private int numChars; 

    public InputReader(InputStreamstream) { 
        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 (IOExceptione) { 
                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 Stringnext() {   
        StringBuilderstr = 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; 
    } 
    
} 
 
 

posted on 2013-02-27 19:08  Sure_Yi  阅读(184)  评论(0编辑  收藏  举报

导航