Tsinghua Academy Range

Source: Internet
Author: User

Descriptioin

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

Input

The first line contains 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 both 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 21 3 7 9 114 67 12

Output

03
Restrictions

0 <= N, M <= 5 * 10^5

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

Points in S is distinct from each of the other.

Coordinates of each point as well as the query interval boundaries A and B is non-negative integers not greater than 10^7 .

Time:2 sec

memory:256 MB

This topic, good early contact before, when the data structure just learn, what also do not understand, this year again to brush this set of questions, feel okay

The topic of [A, b] all the number of elements, binary search, to find the nether, to a strict lower bound for a, to the B it is not strictly the lower bound, done.

This problem does not need two points, can only take part of the score.

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 Const intMax_size = 5E5 +Ten;6 intNum[max_size];7 intA[max_size];8 9 voidQuick_sort (intS[],intLintR)Ten { One     if(L <R) A     { -         inti = l, j = r, x =S[l]; -          while(I <j) the         { -              while(I < J && S[j] >=x) -j--; -             if(I <j) +s[i++] =S[j]; -  +              while(I < J && S[i] <x) Ai++; at             if(I <j) -s[j--] =S[i]; -         } -S[i] =x; -Quick_sort (S, l, I-1); -Quick_sort (S, i+1, R); in     } - } to  + ///two minutes to check the next session - intBsearchlowerbound (intArry[],intLowintHighinttarget) the { *     if(High < Low | | target <=Arry[low]) $         return-1;Panax Notoginseng     intMid = (Low + high +1) /2; -      while(Low <High ) the     { +         if(Arry[mid] <target) ALow =mid; the         Else +High = mid-1; -Mid = (Low + high +1)/2; $     } $     returnmid; - } -  the intBsearchlowerbound_1 (intArry[],intLowintHighinttarget) - {Wuyi     if(High < Low | | Target <Arry[low]) the         return-1; -     intMid = (Low + high +1) /2; Wu      while(Low <High ) -     { About         if(Arry[mid] <=target) $Low =mid; -         Else -High = mid-1; -Mid = (Low + high +1)/2; A     } +     returnmid; the } -  $ intMain () the { the     intN, M; the     intx, y, ans; the      while(~SCANF ("%d%d", &n, &m)) -     { in          for(inti =0; I < n; i++) the         { thescanf"%d", num+i); About         } the         //quick_sort subscript starting from 0 theQuick_sort (NUM,0, N-1); the  +          for(inti =0; I < m; i + +) -         { thescanf"%d%d", &x, &y);Bayi  theans = bsearchlowerbound_1 (num,0, N-1, y)-bsearchlowerbound (NUM,0, N-1, x); the  -printf"%d\n", ans); -         } the     } the     return 0; the}
View Code

Tsinghua Academy Range

Related Article

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.