线段树(HDU 1166)

建树: log(n)

修改:log(n)

询问:log(n)

 

处理区间线段的问题:http://chhaj5236.blog.163.com/blog/static/11288108120099128044145/

建树有n次插入操作,n*logN,一次查询要logN,m次就是m*logN;总共复杂度O(n+m)*logN

 

处理点的问题:http://princetonboy.ycool.com/post.2855602.html

 

以上是两种不同的线段树的建树的方法,根据不同情况来建树 

 

 HDU 1166

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

public classMain {

	static final int MAXN = 200005;
	
	int t, n, ar[];
	Stringstr;
	Treetree[];
	
	void build(int pos, int lt, int rt) {
		tree[pos] = new Tree();
		tree[pos].lt = lt; tree[pos].rt = rt;
		if (lt == rt) {
			tree[pos].value = ar[lt];
			return;
		}
		int mid = (lt + rt) >> 1;
		build(pos << 1, lt, mid);
		build(pos << 1 | 1, mid + 1, rt);
		tree[pos].value = tree[pos << 1].value + tree[pos << 1 | 1].value;
	}
	
	void update(int pos, int index, int value) {
		if (tree[pos].lt == tree[pos].rt) {
			tree[pos].value += value;
			return;
		}
		int mid = (tree[pos].lt + tree[pos].rt) >> 1;
		if (index <= mid) 
			update(pos << 1, index, value);
		else 
			update(pos << 1 | 1, index, value);
		tree[pos].value = tree[pos << 1].value + tree[pos << 1 | 1].value;
	}
	
	int query(int pos, int lt, int rt) {
		if (tree[pos].lt == lt && tree[pos].rt == rt)
			return tree[pos].value;
		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);
		return query(pos << 1, lt, mid) + query(pos << 1 | 1, mid + 1, rt);
	}
	
	void run() {
		t = cin.nextInt();
//		tree = new Tree[MAXN];
		for(int Case = 1; Case <= t; Case++) {
			n = cin.nextInt();
			ar = new int[n + 1];
			tree = new Tree[n * 4 + 1];
			for (int i = 1; i <= n; i++) 
				ar[i] = cin.nextInt();
			build(1, 1, n);
			out.println("Case " + Case + ":");
			while(true) {
				str = cin.next();
				if (str.equals("End")) break;
				int a = cin.nextInt(), b = cin.nextInt();
				if (str.equals("Query")) {
					out.println(query(1, a, b));
				} else if (str.equals("Add")) {
					update(1, a, b);
				} else if (str.equals("Sub")) {
					update(1, a, -b);
				}
			}
		}
	}  

	public static void main(String[] args) throws Exception {
		Mainsolved = new Main();
		solved.run();
		out.close();
	}
	
	static InputStreaminputStream = System.in;
	static OutputStreamoutputStream = System.out;
	static InputReadercin = new InputReader(inputStream);
	static OutputWriterout = new OutputWriter(outputStream);
	
//	public static void main(String[] args) {
//		Main solved = new Main();
//		solved.run();
//	}
//	
//	static InputStream inputStream = System.in;
//	static InputReader cin = new InputReader(inputStream);
	
//	Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
}

classTree {
	int lt, rt, value;
	Tree() {}
	Tree(int lt, int rt, int vaule) {
		this.lt = lt;
		this.rt = rt;
		this.value = value;
	}
}


classInputReader {

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

	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 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 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 boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	public static boolean isWhitespace(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; 
    }

	public interfaceSpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}

classOutputWriter {
	private final PrintWriterwriter;

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

	public OutputWriter(Writerwriter) {
		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-02-27 21:01  Sure_Yi  阅读(218)  评论(0编辑  收藏  举报

导航