import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arr[] = new int[9]; //因为从1开始 注意点!!!所以要加1 while (true) { // 树状数组更新操作!!! 附上测试代码 int n1 = sc.nextInt(); System.out.println(sum(n1, arr) - sum(n1 - 1, arr)); } } public static void ImpVal(int arr[]) { Scanner sc = new Scanner(System.in); for (int i = 1; i <= 8; i++) { // 导入一个值更新对应的树状数组 一步建立在前一步的基础上更新 int x = sc.nextInt(); int v = sc.nextInt(); while (x <= 8) { arr[x] += v; x += (-x & x); } for (int temp = 1; temp <= 8; temp++) { System.out.print(arr[temp] + " "); } System.out.println(); } } public static int sum(int x, int arr[]) { // 查询索引位置val 而那个arr[]保存的事自己对应区域的总值 int temp = 0; while (x >= 1) { temp += arr[x]; x -= (x & -x); } return temp; } }