13 Shandong Province Boring counting (offline tree array or chairman tree + dichotomy or partition tree + two points)

Source: Internet
Author: User
Tags cas

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

2224:boring countingtime limit:3 Sec Memory limit:128 MB Description

In this problem given a number sequence P consisting of N integer and Pi was the ith element in the sequence. Now your task is to answer a list of queries, for each query, please tell us among [L, R], how many Pi is not less than a a nd not greater than B (l<= i <= R). In other words, your task is to count the number of pi (L <= i <= R, A <= Pi <= B).

Input

The first line there are an integer T (1 < T <=), indicates the number of test cases.
For each case, the first line contains numbers n and m (1 <= N, M <= 50000), the size of sequence P, the number of queries. The second line contains N numbers pi (1 <= pi <= 10^9), the number sequence P. Then there is M lines, each line contains four number L, R, A, B (1 <= L, R <= N, 1 <= A, B <= 10^9)

Output

For each case, an at first output of line ' case #c: ', C is the case number start from 1. Then for each query, output a line contains the answer.

Sample Input
113 56 9 5 2 3 6 8 7 3 2 5 1 41 13 1 101 13 3 63 6 3 62 8 2 81 9 1 9
Sample Output
Case #1:137369
HINT Source

2013 ACM College Student Program Design contest in Shandong Province

Test instructions: The number of static intervals [l,r] between [A, b]

This problem is actually an offline tree-like array of water problems, it is estimated that because I do not have the idea of offline, the general offline are unexpected.

Fortunately, the Chairman tree was able to do this, and it took only 20 minutes to 1 a.

The value of the binary K, and then judge the use of the K-large to determine the number of a or a, respectively, is the largest. and then casually.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6#include <cstdlib>7#include <vector>8 using namespacestd;9 #defineW (i) T[I].WTen #defineLS (i) t[i].ls One #defineRS (i) t[i].rs A #defineMAXN 100010 - intP[MAXN],A[MAXN],B[MAXN],ROOT[MAXN]; - structnode{ the     intls,rs,w; -Node () {ls=rs=w=0;} -}t[maxn* -]; - inttot=0; + voidInsert (int&i,intLintRintx) { -t[++tot]=T[i]; +I=tot; AW (i) + +; at     if(L==R)return; -     intMid= (l+r) >>1; -     if(x<=mid) Insert (LS (i), l,mid,x); -     ElseInsert (RS (i), mid+1, r,x); - } - intQueryintLxintRxintLintRintk) { in     if(L==R)returnl; -     intRET=W (LS (RX))-W (LS (lx)); to     intMid= (l+r) >>1; +     if(ret>=k)returnquery (LS (lx), LS (RX), l,mid,k); -     Else returnQuery (RS (LX), RS (RX), mid+1, r,k-ret); the } * BOOLcmpintIintj) { $     returna[i]<A[j];Panax Notoginseng } - intMain () the { +Ios::sync_with_stdio (false); Atot=0; the     intn,m; +     intT; -     intcas=1; $scanf"%d",&t); $      while(t--){ -scanf"%d%d",&n,&m); -          for(intI=1; i<=n;i++){ thescanf"%d", A +i); -p[i]=i;Wuyi         } thetot=0; -root[0]=0; WuSort (p+1, p+n+1, CMP); -          for(intI=1; i<=n;i++) b[p[i]]=i; About          for(intI=1; i<=n;i++){ $root[i]=root[i-1]; -Insert (Root[i],1, N,b[i]); -         } -printf"Case #%d:\n", cas++); A          while(m--){ +             intl,r,x,y; thescanf"%d%d%d%d",&l,&r,&x,&y); -             intlx=1, rx=r-l+1; $             intfx=-1; the             intans=0; the             intFlag =0; the             inttmpx; the              while(lx<=Rx) { -                 intMid = (LX+RX) >>1; inTmpx=a[p[query (root[l-1],root[r],1, N,mid)]; the                 if(tmpx<=y) { thefx=mid; AboutLx=mid+1; the}Elserx=mid-1; the             } the             if(fx==-1) flag=1; +             Elseans+=FX; -lx=1, rx=r-l+1; thefx=-1;Bayi              while(lx<=Rx) { the                 intMid = (LX+RX) >>1; theTmpx=a[p[query (root[l-1],root[r],1, N,mid)]; -                 if(tmpx>=x) { -fx=mid; therx=mid-1; the}ElseLx=mid+1; the             } the             if(fx==-1) flag=1; -             Elseans=ans-fx+1; the             if(flag) ans=0; theprintf"%d\n", ans); the         }94     } the     return 0; the}

13 Shandong Province Boring counting (offline tree array or chairman tree + dichotomy or partition tree + two points)

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.