OJ多个连续区间查找第n个位置的元素

Searching_3
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).

题目描述
给定n个连续区间,查第n个位置的元素
元素。

思路:
找到小于关键字key的区间最大左边界,
计算每个区间的长度 length
key+左边界-1

例如
【1,10】 【12,20】 【22,30】
length 分别是 10 9 9
key=25
key>10 key=key-10=15
key>9 key=key-9=6
key<9 res=key-1+length=6-1+22=27

import java.util.Scanner;

/**
 * 1
 * 3 3
 * 1 10 12 20 22 30
 * 5 15 25
 * Sample Output 1
 *
 * 5 16 27
 */
public class Main {
    static Scanner scanner=new Scanner(System.in);
    static  int group_num;
    static  int q_num;
    static  int[] group;
    static  int[] q_arr;
    static  int[] res;
    public  static void main(String [] args){
        int T=scanner.nextInt();
        while(T-->0){
            group_num=scanner.nextInt();
            q_num=scanner.nextInt();
            group=new int[group_num*2];
            q_arr=new int[q_num];
            res=new int[q_num];
            for(int i=0;i<group_num*2;i++){
                group[i]=scanner.nextInt();
            }
            for(int i=0;i<q_num;i++){
                q_arr[i]=scanner.nextInt();
            }
            query();
            for(int i=0;i<res.length;i++){
                System.out.print(res[i]);
                if(i!=res.length-1) System.out.print(" ");
            }
            System.out.print("\n");
        }


    }

    private static void query() {
        for(int i=0;i<q_num;i++){
            int num=q_arr[i];
            for(int j=0;j<group_num*2-1;j=j+2){
                int length=group[j+1]-group[j]+1;
                if(num>length) num-=length;
                else {
                    res[i]=num-1+group[j];
                    break;
                }
            }

        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值