DSA Range, dsarange

Source: Internet
Author: User

DSA Range, dsarange

I recently started school and reviewed the data structure and algorithms. I went to MOOC. This is a question on the Tsinghua oj platform.

Question: Range Query)

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

Code 1

The first solution is the code written based on the course last year:

/* VC ++ 6.0PA1-RangeAsk. cppTsinghua Data Structures & Algorithmsby zerolevaat 2014-10-19 */# include <stdio. h> # define _ OJ_const int MAX_SIZE = 500005; // 0 ≤ n, m ≤ 5 × 10 ^ 5, total number of points void MSort (int [], int, int ); // Merge and sort void Merge (int [], int); // Merge int BSearch (const int [], int, int, bool &); /* Half-lookup. If no element subscript is found,-1 is returned if the searched value is smaller than all elements, otherwise, the subscript */int main () {# ifndef _ OJ _ freopen ("input.txt", "r ", Stdin); // freopen ("output.txt", "w", stdout); # endif int m, n, I; int * point = new int [MAX_SIZE]; // allocate a large array scanf ("% d", & n, & m); for (I = 0; I <n; I ++) to the stack) {scanf ("% d", & point [I]); // enter all coordinates} MSort (point, 0, n); // sort, O (nlogn) while (m --) {int a, B; scanf ("% d", & a, & B); bool find_ B = false; bool find_a = false; int cnt = (B <point [0] | a> point [n-1])? 0: (BSearch (point, n, B, find_ B)-BSearch (point, n, a, find_a); // directly subtract, O (2 logn) cnt = find_a? Cnt + 1: cnt; // The maximum element subscript returned by BSearch is not greater than the queried value. // determine whether a is found, add 1 printf ("% d \ n", cnt);} # ifndef _ OJ _ fclose (stdin); // fclose (stdout ); # endif return 0;} void MSort (int a [], int lo, int hi) {if (hi-lo <2) {return ;} int mi = (lo + hi)> 1; MSort (a, lo, mi); MSort (a, mi, hi); Merge (a, lo, mi, hi); return;} void Merge (int a [], int lo, int mi, int hi) {int * A = a + lo; int I; int lb = mi-lo; int * B = new in T [lb]; // create a new B array to store the lo-mi element in array a for (I = 0; I <lb; I ++) {B [I] = A [I];} int lc = hi-mi; int * c = a + mi; // use the new pointer c to identify the int j, k; for (I = 0, j = 0, k = 0; (j <lb) & (k <lc); I ++) {// compare the size of elements in array B and array c, store the Small and Medium values in the original array a // until an array has been compared to A [I] = (B [j] <= c [k])? B [j ++]: c [k ++];} while (j <lb) // If c is compared first, the elements in the remaining array B are large, copy to the end of the original array {// If B is compared first, c is not necessarily copied to a, because the c is a [I ++] = B [j ++] of A;} delete [] B; return;} int BSearch (const int a [], int n, int x, bool & find) {int lo = 0, hi = n; int mi; while (lo 

It was written at that time, but the general idea was very clear: input, sorting, search, and computing. Here, sorting and search use Merge Sorting and binary search. For details, see the code and comment.

Code 2
# Include <stdio. h> # include <string. h> # define MAXN 500005 # define MAXNUM limit 005int n, m; int BSearch (const int sorted [], int x); // half-lookup, returns the subscript int main () {scanf ("% d", & n, & m) of the element not greater than the number to be searched ); int * sorted = new int [MAXN]; // stores the sorted number char * p = new char [MAXNUM]; memset (p, 0, sizeof (p) /sizeof (char); int max = 0; int I; for (I = 0; I <n; I ++) {int tmp; scanf ("% d ", & tmp); p [tmp] = 1; // if the number num is input, p [num] = 1; otherwise, p [num] = 0 max = (tmp <max )? Max: tmp;} // sort // from 0-max, use the sorted array to store the sorted numbers int j = 0; for (I = 0; I <= max; ++ I) {if (1 = p [I]) {sorted [j ++] = I ;}// due to coordinate differences, after sorting, j = n-1 while (m --) {int cnt = 0; int a, B; scanf ("% d", & a, & B ); int pA = BSearch (sorted, a); int pB = BSearch (sorted, B); cnt = pB-pA; cnt = (pA> = 0 & a = sorted [pA])? Cnt + 1: cnt; // determine whether a is found. If yes, add cnt to 1 // printf ("pA = % d & pB = % d \ n ", pA, pB); if (0 = n) {printf ("0 \ n"); continue;} printf ("% d \ n", cnt );} return 0;} int BSearch (const int sorted [], int x) {int lo = 0, hi = n; while (lo 

This is now written, and it is opportunistic. There is no Merge Sorting here, which is too troublesome to write. The interaction between coordinate points is used to check the code. OthersCode 1Same.

It mainly describes the major bugs encountered when writing this Code:

Code 3

Here is the code of xueba's roommate:

#include <stdio.h>#include <string.h>int flag[10000005];int cnt[10000005];int main(){    int i, n, q, num, a, b, Min = 10000000, Max = 0;    memset(flag, 0, sizeof(flag));    scanf("%d%d", &n, &q);    for (i = 0; i < n; i++)    {        scanf("%d", &num);        if (num < Min)        {            Min = num;        }        if (num > Max)        {            Max = num;        }        flag[num] = 1;    }    cnt[Min] = 1;    for (i = Min+1; i <= Max; i++)    {        cnt[i] = cnt[i-1] + (flag[i] ? 1 : 0);    }    while (q--)    {        scanf("%d%d", &a, &b);        if (a > Max || b < Min)        {            printf("0\n");            continue;        }        if (a < Min)        {            a = Min;        }        if (b > Max)        {            b = Max;        }        printf("%d\n", cnt[b] - cnt[a] + (flag[a] ? 1 : 0));    }    return 0;}

After reading it, I can't help myself .. Although there is no comment, it is easy to understand. I always feel that the derivation of the cnt array is like dynamic planning. Well, don't worry about my teasing discourse ~

OK! I will continue to review it today ~ Effort ~
PS: The Blue Bridge cup was postponed until March. I can only say that this kind of slag is too good ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.