Interval (Nanyang oj522) (tree-like array)

Source: Internet
Author: User

Interval time limit: -Ms | Memory Limit:65535KB Difficulty:4
Describe
There is n (1 <= n <= 100000) intervals [AI, bi] and m (1 <= m <= 100000) queries, -100000 <= ai <= bi &l t;= 100000 is integers. Each query contains an integer XI ( -100000 <= x <= 100000). For each query, you should answer how many intervals Convers XI.
Input
The first line of input was the number of the
test case.
For each test case,
Integers n m on the first line,
Then n lines, each line contains the integers AI, bi;
Then M. Lines, each line contains an integer XI.
Output
m lines, each line an integer, the number of intervals that covers XI.
Sample input
23 41 31 22 301231 30 0-101
Sample output
0232010
 
/* a tree-like array application, The number of occurrences of the statistic in the given interval. Tle cried, max size: 200002 is ac,200005 is tle!!! Note the negative number is converted to a positive range, or else it will be tle!!! */#include <stdio.h> #include <string.h> #define MAX 200002int a[max];int lowbit (int N) {return n& (-n);} void asd (int i,int M) {while (I<=max) {a[i]+=m;i+=lowbit (i);}} int sum (int j) {int sum=0;while (j>0) {sum+=a[j];j-=lowbit (j);} return sum;} int main () {int i,n,m,t,t,k,p;scanf ("%d", &t), while (t--) {memset (a,0,sizeof (a)); scanf ("%d%d", &m,&n); while (m--) {scanf ("%d%d", &k,&t), ASD (k+100001,1), ASD (t+100002,-1);} while (n--) {scanf ("%d", &p);p rintf ("%d\n", Sum (p+100001));}} return 0;} 


Interval (Nanyang oj522) (tree-like array)

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.