Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我用的树状数组怎么超时了啊?

Posted by wskiwwwx at 2009-09-24 13:07:56 on Problem 2155
import java.util.Scanner;

public class Main {

	private static int[][] matrix = new int[1001][1001];
	private static int n;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int testNum = sc.nextInt();
		for (int i = 0; i < testNum; ++i) {
			n = sc.nextInt();
			int t = sc.nextInt();
			while (t-- > 0) {
				String c = sc.next();
				if (c.equals("C")) {
					int x1 = sc.nextInt();
					int y1 = sc.nextInt();
					int x2 = sc.nextInt();
					int y2 = sc.nextInt();
					change(x1, y1, x2, y2);

				} else if (c.equals("Q")) {
					int x = sc.nextInt();
					int y = sc.nextInt();
					int v = getSum(x , y) & 1;
					//System.out.println(getSum(x , y));
					if(v == 0)
						System.out.println(0);
					if( v == 1)
						System.out.println(1);
				}
			}
			System.out.println();
		}
	}

	public static void change(int x1, int y1, int x2, int y2) {
		int y = y1;
		
		for (int i = x1; i <= x2; ++i) {
			change(i, y1, 1);
		}

		for (int i = x1; i <= x2; ++i) {
			change(i, y2+1 , -1);
		}
		// for (int i = x1; i <= x2; ++i) {
		// for (int j = y1; j <= y2; ++j) {
		//
		// if (matrix[i][j] == 0)
		// matrix[i][j] = 1;
		// else
		// matrix[i][j] = 0;
		// }
		// }
	}

	public static int lowbit(int i) {
		return i & -i;
	}

	public static void change(int i, int j, int value) {
		while (j <= n) {
			matrix[i][j] += value;
			j += lowbit(j);
		}
	}

	public static int getSum(int i, int j) {
		int sum = 0;
		while (j > 0) {
			sum += matrix[i][j];
			j -= lowbit(j);
		}
		return sum;
	}

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator