Description
They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i)
Now Sonam ask queries in which she gives rank of the student (x) and you have to tell marks obtained by that student
Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1.
Constraints:1<=T<=50,1<=N<=105,1<=Q<=105,1<= l(i) < r(i) <=1018,1<=x<=1018
Input
The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains two space separated values N and Q denoting the no of groups and number of queries asked respectively. The next line contains N group of two integers separated by space which shows lowest marks in group i ie l(i) and highest marks in group i ie r(i) such that if i < j then r(i) < l(j). The next lines contain Q space separated integers x, denoting rank of student.
Output
For each query output marks obtain by student whose rank is x(1<=x<=10^18).
Sample Input 1
1
3 3
1 10 12 20 22 30
5 15 25
Sample Output 1
5 16 27
人话说一下题意
- 每个人的成绩是唯一的,且是连续的
- 成绩拥有很多个区间,比如示例给的,1到10,然后11分的人没有,从12分到20分都有人得
- 题目问的是:那么rank为5的同学是谁,因为1-10rank的分数是1,2,3,4,5。那么显然是5。如果问15,因为没有11分的同学,所以自然是16
思路
二分查找,其实很简单,没什么好说的,二分查找的变式而已
参考链接
Code
package org.alphacat.second;
import java.util.Scanner;
public class Searching3 {
public static long getMark(long[] low, long[] arrLen, long x) {
int l = 0, r = low.length - 1;
//二分方式寻找x所处的区间
while (l < r) {
int mid = l + ((r - l) >> 1);
if (arrLen[mid] < x) {
l = mid + 1;
} else {
r = mid;
}
}
//x所处区间前的所有区间长度和
long preLen = l == 0 ? 0 : arrLen[l - 1];
return low[l] + x - preLen - 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int q = scanner.nextInt();
//记录各区间的左端点
long[] low = new long[n];
//记录各区间的右端点
long[] high = new long[n];
//记录各区间的长度
long[] arrLen = new long[n];
long[] query = new long[q];
long len = 0;
for (int j = 0; j < n; j++) {
low[j] = scanner.nextLong();
high[j] = scanner.nextLong();
len += high[j] - low[j] + 1;
arrLen[j] = len;
}
for (int j = 0; j < q; j++) {
query[j] = scanner.nextLong();
}
String res = problem(low, arrLen, query, q);
System.out.println(res);
}
}
private static String problem(long[] low, long[] arrLen, long[] query, int q) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < q - 1; j++) {
sb.append(getMark(low, arrLen, query[j]));
sb.append(" ");
}
sb.append(getMark(low, arrLen, query[q - 1]));
return sb.toString();
}
}