为什么这里二分写int mid = (l + (r - l)) >> 1;就会TLE 而改为int mid = (l + r) >> 1;就AC(换成 / 也是一样的) 不懂
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int N = 300010;
int[] a = new int[N];
int[] s = new int[N];
List<Integer> alls = new ArrayList<>();
List<Pair> add = new ArrayList<>();
List<Pair> query = new ArrayList<>();
for(int i = 0;i < n; i++){
int x = sc.nextInt();
int c = sc.nextInt();
add.add(new Pair(x, c));
alls.add(x);
}
for(int i = 0; i < m; i ++){
int l = sc.nextInt();
int r = sc.nextInt();
query.add(new Pair(l, r));
alls.add(l);
alls.add(r);
}
Collections.sort(alls);
int unique = unique(alls);
alls = alls.subList(0, unique);
for(Pair item : add){
int index = find(item.first, alls);
a[index] = item.second;
}
for(int i = 1; i <= alls.size(); i++) s[i] = s[i-1] + a[i];
for(Pair item : query){
int l = find(item.first, alls);
int r = find(item.second,alls);
System.out.println(s[r] - s[l-1]);
}
}
public static int unique(List<Integer> list) {
// 定义一个变量j,用于记录不重复元素的索引位置
int j = 0;
// 循环遍历列表,直到列表的最后一个元素
for (int i = 0; i <= list.size() - 1; i++) {
// 如果当前元素是列表的第一个元素,或者当前元素与前一个元素不同
if (i == 0 || list.get(i) != list.get(i - 1)) {
// 将当前元素复制到索引j的位置
list.set(j, list.get(i));
// 更新j的值,指向下一个不重复元素的索引位置
j++;
}
}
// 返回不重复元素的数量
return j;
}
public static int find(int x, List<Integer> list){
int l = 0;
int r = list.size() - 1;
while(l < r){
int mid = (l + r) >> 1;
if(list.get(mid) >= x) r = mid;
else l = mid + 1;
}
return l + 1;
}
static class Pair{
int first;
int second;
public Pair(int x, int c){
this.first = x;
this.second = c;
}
}
}