13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

时间:2023-03-08 20:44:54

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

2224: Boring Counting

Time Limit: 3 Sec  Memory Limit: 128 MB

Description

In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you 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 and 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

In the first line there is an integer T (1 < T <= 50), indicates the number of test cases. 
       For each case, the first line contains two 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 are M lines, each line contains four number L, R, A, B(1 <= L, R <= n, 1 <= A, B <= 10^9)

Output

For each case, at first output a line ‘Case #c:’, c is the case number start from 1. Then for each query output a line contains the answer.

Sample Input

1
13 5
6 9 5 2 3 6 8 7 3 2 5 1 4
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9

Sample Output

Case #1:
13
7
3
6
9

HINT

Source

题意:求静态的区间[l,r]介于[A,B]的数的个数

这题其实是一道离线树状数组的水题,估计是因为我并不是很具备离线的思维吧,一般的离线都想不到。

好在主席树也能够完成这个操作,并且也只花了20分钟不到就1A了。

二分k的值,然后判断利用第k大来判断出A,B分别是第几大。然后随便搞。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
#define w(i) T[i].w
#define ls(i) T[i].ls
#define rs(i) T[i].rs
#define MAXN 100010
int p[MAXN],a[MAXN],b[MAXN],root[MAXN];
struct node{
int ls,rs,w;
node(){ls=rs=w=;}
}T[MAXN*];
int tot=;
void Insert(int &i,int l,int r,int x){
T[++tot]=T[i];
i=tot;
w(i)++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid)Insert(ls(i),l,mid,x);
else Insert(rs(i),mid+,r,x);
}
int query(int lx,int rx,int l,int r,int k){
if(l==r)return l;
int ret=w(ls(rx))-w(ls(lx));
int mid=(l+r)>>;
if(ret>=k)return query(ls(lx),ls(rx),l,mid,k);
else return query(rs(lx),rs(rx),mid+,r,k-ret);
}
bool cmp(int i,int j){
return a[i]<a[j];
}
int main()
{
ios::sync_with_stdio(false);
tot=;
int n,m;
int t;
int cas=;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",a+i);
p[i]=i;
}
tot=;
root[]=;
sort(p+,p+n+,cmp);
for(int i=;i<=n;i++)b[p[i]]=i;
for(int i=;i<=n;i++){
root[i]=root[i-];
Insert(root[i],,n,b[i]);
}
printf("Case #%d:\n",cas++);
while(m--){
int l,r,x,y;
scanf("%d%d%d%d",&l,&r,&x,&y);
int lx=,rx=r-l+;
int fx=-;
int ans=;
int flag =;
int tmpx;
while(lx<=rx){
int mid = (lx+rx)>>;
tmpx=a[p[query(root[l-],root[r],,n,mid)]];
if(tmpx<=y){
fx=mid;
lx=mid+;
}else rx=mid-;
}
if(fx==-)flag=;
else ans+=fx;
lx=,rx=r-l+;
fx=-;
while(lx<=rx){
int mid = (lx+rx)>>;
tmpx=a[p[query(root[l-],root[r],,n,mid)]];
if(tmpx>=x){
fx=mid;
rx=mid-;
}else lx=mid+;
}
if(fx==-)flag=;
else ans=ans-fx+;
if(flag)ans=;
printf("%d\n",ans);
}
}
return ;
}