清華OJ-范圍查詢


題目鏈接:https://dsa.cs.tsinghua.edu.cn/oj/course.shtml?courseid=58

Descriptioin

Let S be a set of n integral points on the x-axis. For each given interval [a, b], you are asked to count the points lying inside.

Input

The first line contains two integers: n (size of S) and m (the number of queries).

The second line enumerates all the n points in S.

Each of the following m lines consists of two integers a and b and defines an query interval [a, b].

Output

The number of points in S lying inside each of the m query intervals.

Example

Input

5 2
1 3 7 9 11
4 6
7 12

Output

0
3

Restrictions

0 <= n, m <= 5 * 10^5

For each query interval [a, b], it is guaranteed that a <= b.

Points in S are distinct from each other.

Coordinates of each point as well as the query interval boundaries a and b are non-negative integers not greater than 10^7.

Time: 2 sec

Memory: 256 MB

描述

數軸上有n個點,對於任一閉區間 [a, b],試計算落在其內的點數。

輸入

第一行包括兩個整數:點的總數n,查詢的次數m。

第二行包含n個數,為各個點的坐標。

以下m行,各包含兩個整數:查詢區間的左、右邊界a和b。

輸出

對每次查詢,輸出落在閉區間[a, b]內點的個數。

樣例

見英文題面

限制

0 ≤ n, m ≤ 5×105

對於每次查詢的區間[a, b],都有a ≤ b

各點的坐標互異

各點的坐標、查詢區間的邊界a、b,均為不超過10^7的非負整數

時間:2 sec

內存:256 MB

————————————————————————————————————————————————————————————————————————————————————

實在是花了不少時間來做,仍然沒有過!

以下是得50分的代碼

//范圍查詢(Range)
#include <iostream>
#include <malloc.h>
using namespace std;
int n = 0;
int query(int q[],int a,int b);

int main(){
    int pn , qn , *p , x , y ;  //pn是點的個數,qn是查詢的次數
    int curpoint = 0;
    cin>>pn>>qn;
    n = pn;
    p=(int*) malloc (sizeof(int) * pn);
    for(int i = 0 ; i < pn; i++)
    {    
        cin>>curpoint;
        p[i] = curpoint;
    }
    
    for(int j = 1; j <= qn; j++)
    {
        cin>>x>>y;
        cout<<query(p,x,y)<<endl;
    }
    free(p);
    return 0;
}
 
int query(int q[],int a,int b){ 
//查詢在范圍內的點,返回點的個數
    int count = 0 , i , j ;
    for(int i = 0 ; i <= n ; i++)
        if(q[i] >= a && q[i] <= b) count++;
    
    return count;
}

今天修改並測試過了二分查詢,但提交后判分只得15分:

 

//范圍查詢(Range)
#include <iostream>
#include <malloc.h>
using namespace std;
int n = 0;
int query(int q[],int a,int b); 
int BinerySearch(int *q,int x,int y,int v);  //二分查找
int main(){
    int pn , qn , *p , x , y;  //pn是點的個數,qn是查詢的次數
    int curpoint = 0;
    cin>>pn>>qn;
    n = pn;
    p=(int*) malloc (sizeof(int) * pn);
    for(int i = 0 ; i < pn; i++)//輸入點的數值 
    {    
        cin>>curpoint;
        p[i] = curpoint;
    }
    //范圍的查詢
    for(int j = 1; j <= qn; j++)
    {
        cin>>x>>y;
        cout<<query(p,x,y)<<endl;
    }
    free(p);
    return 0;
}

int BinerySearch(int *q,int lo,int hi,int v){
    //二分查找 用於查詢v這個數值在數組中應該是什么位置 
    if (v >= q[hi]) return hi;
    if (v <= q[lo]) return lo;
    while (lo <= hi)
    {
        int mid = lo + (hi - lo)/2;
        if ( q[mid] == v || (v > q[mid] && v < q[mid+1])) return mid;
        else if (q[mid] > v) hi = mid;
        else if(v > q[mid]) lo = mid + 1;
        
    }
    return -1;
}

int query(int q[],int a,int b){ 
//查詢在范圍內的點,返回點的個數
    int count = 0 , i , j ;
    i = BinerySearch(q,0,n-1,a);
    j = BinerySearch(q,0,n-1,b);
    for(i; i <= j ; i++)
        if(q[i] >= a && q[i] <= b) count++;
    return count;
}

看來還需要提升自己,多聽鄧老師的課,提高算法的認識。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM