user-0 提问: c语言蓝桥杯的操作格子我用了提示的线段树求解但是为什么运行还是超时呢？请大家帮帮忙！谢谢！

回复:
user: user-1
content: 我用JAVA改写也是超时，求解！

user: user-2
content: 我用java写的,算法思路几乎完全参考了楼上的楼上的博客 但还是超时了,求解啊import java.util.Scanner;   public class Main {  	public static void main(String[] args) { 		Scanner scanner = new Scanner(System.in); 		int n = scanner.nextInt(); 		int m = scanner.nextInt(); 		Tree tree = createTree(1, n); 		for (int i = 1; i <= n; i++) { 			insert(tree, scanner.nextInt(), i); 		} 		int order[][] = new int[m][3]; 		for (int i = 0; i < order.length; i++) { 			for (int j = 0; j < order[0].length; j++) { 				order[i][j] = scanner.nextInt(); 			} 		} 		for (int i = 0; i < order.length; i++) { 			switch (order[i][0]) { 			case 1: 				change(tree,order[i][1],order[i][2]); 				break; 			case 2: 				System.out.println(getsum(tree,order[i][1],order[i][2])); 				break; 			case 3: 				System.out.println(getmax(tree,order[i][1],order[i][2])); 				break;  			default: 				break; 			} 		}  	}   	private static int getsum(Tree tree, int i, int j) { 		if (tree.left==i&&tree.right==j) { 			return tree.sum; 		} 		int mid = (tree.left + tree.right) / 2; 		if (j<=mid) { 			return getsum(tree.lchild, i, j); 			 		} 		else if (i>mid) { 			return getsum(tree.rchild, i, j); 		} 		else { 			return getsum(tree.lchild, i, mid)+getsum(tree.rchild, mid+1, j); 		} 	}  	private static int getmax(Tree tree, int i, int j) { 		// TODO Auto-generated method stub 		if (tree.left==i&&tree.right==j) { 			return tree.max; 		} 		int mid = (tree.left + tree.right) / 2; 		if (j<=mid) { 			return getmax(tree.lchild, i, j); 			 		}else if (i>mid) { 			return getmax(tree.rchild, i, j); 		} 		else { 			return max(getmax(tree.lchild, i, mid),getmax(tree.rchild, mid+1, j)); 		} 	}  	private static void change(Tree tree, int i, int j) { 		// TODO Auto-generated method stub 		if (tree.left == tree.right ) { 			tree.max = j; 			tree.sum = j; 			return; 		} 		int mid = (tree.left + tree.right) / 2; 		if (i <= mid) { 			change(tree.lchild, i, j); 			 		} else { 			change(tree.rchild, i, j); 		} 		tree.max=max(tree.lchild.max,tree.rchild.max); 		tree.sum=tree.lchild.sum+tree.rchild.sum; 	}  	private static int max(int max, int max2) { 		// TODO Auto-generated method stub 		return max>max2?max:max2; 	}  	private static void insert(Tree tree, int a, int index) { 		tree.max = max(tree.max, a); 		tree.sum +=a; 		if (tree.left == tree.right) { 			 			return; 		} 		if (index <= ((tree.left + tree.right) / 2)) { 			insert(tree.lchild, a, index); 		} else { 			insert(tree.rchild, a, index); 		} 	}  	static Tree createTree(int min, int max) { 		Tree tree = new Tree(); 		tree.left = min; 		tree.right = max; 		tree.max = 0; 		tree.sum = 0; 		if (min == max) { 			return tree; 		} 		int mid = (min + max) / 2;  		tree.lchild = createTree(min, mid); 		tree.rchild = createTree(mid + 1, max); 		return tree; 	}  }  class Tree { 	int max, sum; 	int left, right; 	Tree lchild, rchild; }

user: user-0
content: 太感谢了，我对比了一下和我的有些不同，主要是创建时我就一边赋值了，还有就是求最大指时与你的有点不同，其他的基本类似，我想问一下我的程序超时在哪里？可以帮看看吗？

user: user-3
content: 这是我写的。 蓝桥杯：操作格子（线段树）http://blog.csdn.net/u013055228/article/details/18965325

user: user-4
content: http://blog.sbw.so/Article/index/title/蓝桥杯%20算法训练%20操作格子%20使用线段树解法.html

user: user-0
content: 连接打不开，你说的什么呢