码迷,mamicode.com
首页 > 其他好文 > 详细

清华学堂 Range

时间:2015-03-28 11:33:50      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

 

这道题目,好早以前就接触过,那时候数据结构刚学,什么也不懂,今年又来刷这套题了,感觉还好

题目求 [a, b] 内所有的元素个数,二分搜索,求下界,对a求它严格的下界, 对b求它不严格的下界,搞定。

这道题目不用二分,只能拿一部分分数。

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 const int MAX_SIZE = 5E5 + 10;
 6 int num[MAX_SIZE];
 7 int a[MAX_SIZE];
 8 
 9 void quick_sort(int s[], int l, int r)
10 {
11     if(l < r)
12     {
13         int i = l, j = r, x = s[l];
14         while(i < j)
15         {
16             while(i < j && s[j] >= x)
17                 j--;
18             if(i < j)
19                 s[i++] = s[j];
20 
21             while(i < j && s[i] < x)
22                 i++;
23             if(i < j)
24                 s[j--] = s[i];
25         }
26         s[i] = x;
27         quick_sort(s, l, i-1);
28         quick_sort(s, i+1, r);
29     }
30 }
31 
32 ///二分查下届
33 int BSearchLowerBound(int arry[], int low, int high, int target)
34 {
35     if(high < low || target <= arry[low])
36         return -1;
37     int mid = (low + high + 1) / 2;
38     while(low < high)
39     {
40         if(arry[mid] < target)
41             low = mid;
42         else
43             high = mid - 1;
44         mid = (low + high + 1)/2;
45     }
46     return mid;
47 }
48 
49 int BSearchLowerBound_1(int arry[], int low, int high, int target)
50 {
51     if(high < low || target < arry[low])
52         return -1;
53     int mid = (low + high + 1) / 2;
54     while(low < high)
55     {
56         if(arry[mid] <= target)
57             low = mid;
58         else
59             high = mid - 1;
60         mid = (low + high + 1)/2;
61     }
62     return mid;
63 }
64 
65 int main()
66 {
67     int n, m;
68     int x, y, ans;
69     while(~scanf("%d %d", &n, &m))
70     {
71         for(int i = 0; i < n; i++)
72         {
73             scanf("%d", num+i);
74         }
75         //quick_sort 下标从 0 开始
76         quick_sort(num, 0, n-1);
77 
78         for(int i = 0; i < m; i ++)
79         {
80             scanf("%d %d", &x, &y);
81 
82             ans = BSearchLowerBound_1(num, 0, n-1, y) - BSearchLowerBound(num, 0, n-1, x);
83 
84             printf("%d\n", ans);
85         }
86     }
87     return 0;
88 }
View Code

 

清华学堂 Range

标签:

原文地址:http://www.cnblogs.com/ya-cpp/p/4373919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!