[Tsinghua OJ] Range Query (Range) problem, ojrange
[Problem description]
There are n points on the number axis. For any closed interval [a, B], calculate the points that fall into it.
[Input]
The first row contains two integers: the total number of points n and the number of queries m.
The second row contains n numbers, which are the coordinates of each point.
The following m rows contain two integers: The left and right boundary a and B of the query interval.
[Output]
For each query, the number of output points falling in the closed interval [a, B] is the number of points.
[Input example]
5 2
1 3 7 9 11
4 6
7 12
[Output example]
0
3
[Restrictions]
0 ≤ n, m ≤ 5x105
For the interval [a, B] of the next query, there is a ≤ B
Coordinate differences between different points
The coordinates of each point and the boundary a and B of the query interval are all non-negative integers up to 10 ^ 7.
Time: 2 s, memory: 256 MB
[Solution] Do not talk nonsense, paste the source code first:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define L 500005 5 6 int a[L]; 7 8 int compare(const void *a, const void *b) 9 {10 int *pa = (int*)a;11 int *pb = (int*)b;12 return (*pa) - (*pb); 13 }14 15 void swap(int &a, int &b)16 {17 int temp;18 temp = a;19 a = b;20 b = temp;21 }22 23 int find(int begin, int end, int ac)24 {25 int mid, left = begin, right = end;26 while (left <= right)27 {28 mid = left + ((right - left) >> 1);29 if (a[mid] >= ac) right = mid - 1;30 else left = mid + 1;31 }32 return left;33 }34 35 36 int main()37 {38 int n, m, i;39 scanf("%d %d\n", &n, &m);40 41 for (i = 0; i < n; i++)42 {43 scanf("%d", &a[i]);44 }45 46 //refer to http://www.cnblogs.com/CCBB/archive/2010/01/15/1648827.html47 qsort(a, n, sizeof(int), compare);48 49 for (i = 0; i < m; i++)50 {51 int l, r, ans, lf, rt;52 scanf("%d %d", &l, &r);53 54 //make sure l <= r55 if (l > r)56 {57 swap(l, r);58 }59 60 rt = find(0, n - 1, r);61 lf = find(0, n - 1, l);62 ans = rt - lf;63 if (a[rt] == r) ans++;64 if (ans < 0) ans = 0;65 66 printf("%d\n", ans);67 }68 }
The first thought is that this question must have been done before. It is very simple. When we see this data size, we are sure we have to use binary search. (If you want to maintain a linear array first on the Internet, then search by O (1) is not mixed)
In fact, binary search is not as simple as it looks. Especially when writing a specific query, many details and critical points must be carefully considered based on the actual situation.
In combination with the source code above, there are several points worth noting:
1) for usage of qsort, see http://www.cnblogs.com/ccbb/archive/2010/01/15/1648827.html. Tsinghua OJ does not support the algorithm library.
2) the last three lines of code (if (a[rt] == r) ans++;
In fact, binary search is used to adjust the answer based on the specific situation. The upper and lower bounds are equal to or not equal to the values in array. This is also consistent with the processing of several details in binary search.
3) This row in the binary search function:mid = left + ((right - left) >> 1);
. On the one hand, bitwise operations increase computing efficiency; on the other hand, bitwise operations are not directly used(left + right) >> 1
This prevents numbers from going out of bounds during the calculation process, and causes the array subscript To Go Out of bounds.
4) do not use recursion for binary search. First, improve efficiency; second, prevent stack overflow.
What is the acm online oj website of Tsinghua University? Can I log on out of school?
No, it's famous to go to ZOJ or POJ.
Tsinghua University has a website dedicated to programming. After registering, you can participate in programming. Then, you can hand in the website and get the corresponding points and ask what the website is,
Tsinghua didn't talk about the Online Judge. Peking University's OJ is good. The website is poj.org/
Zhejiang University: acm.zju.edu.cn/onlinejudge/
USACO: ace.delos.com/usaco
Uva.onlinejudge.org/
Hangdian acm.hdu.edu.cn/
Xiamen University: acm.xmu.edu.cn/JudgeOnline/
Xidian: xdoj. maybe. im/JudgeOnline/
Hope to help you.