Boring Counting
Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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).
输入
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)
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)
输出
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.
示例输入
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
示例输出
Case #1: 13 7 3 6 9
提示
来源
2013年山东省第四届ACM大学生程序设计竞赛
示例程序
这题描述是求给定区间(s,t)求比a大且比b小的数
转换求a和b在区间的大小在相减就行我用划分树做的1280ms听说用主席树更快
ACcode:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100100
using namespace std;
int tree[20][maxn];
int sorted[maxn];
int toleft[20][maxn];
void build(int l,int r,int d){
if(l == r) return;
int mid = (l + r) >> 1;
int same = mid - l + 1;
for(int i = l; i <= r; i++)
if(tree[d][i] < sorted[mid])
same --;
int lpos = l,rpos = mid + 1;
for(int i = l; i <= r; i++){
if(tree[d][i] < sorted[mid]){
toleft[d][i] = toleft[d][i - 1] + 1;
tree[d + 1][lpos++] = tree[d][i];
}
else if(tree[d][i] == sorted[mid] && same > 0){
same --;
toleft[d][i] = toleft[d][i - 1] + 1;
tree[d + 1][lpos++] = tree[d][i];
}
else{
toleft[d][i] = toleft[d][i - 1];
tree[d + 1][rpos++] = tree[d][i];
}
}
build(l,mid,d + 1);
build(mid + 1,r,d + 1);
}
int query(int L,int R,int l,int r,int dep,int k){
if(l==r)return tree[dep][l];
int mid=(L+R)>>1;
int cnt=toleft[dep][r]-toleft[dep][l-1];
if(cnt>=k){
int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
int newr=newl+cnt-1;
return query(L,mid,newl,newr,dep+1,k);
}
else {
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+1,R,newl,newr,dep+1,k-cnt);
}
}
int main(){
int loop,n,m,cnt=1;
scanf("%d",&loop);
while(loop--){
memset(tree,0,sizeof(tree));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&tree[0][i]);
sorted[i]=tree[0][i];
}
sort(sorted+1,sorted+1+n);
build(1,n,0);
int s,t,a,b;
printf("Case #%d:\n",cnt++);
while(m--){
scanf("%d%d%d%d",&s,&t,&a,&b);
int low,big,l,r;
l=1;r=t-s+2;
while(l<r){
int mid=(l+r)>>1;
int tmp=query(1,n,s,t,0,mid);
if(a>tmp)
l=mid+1;
else
r=mid;
}
low=l;
l=1;r=t-s+2;
while(l<r){
int mid=(l+r)>>1;
int tmp=query(1,n,s,t,0,mid);
if(b>=tmp)
l=mid+1;
else
r=mid;
}
big=l;
cout<<big-low<<'\12';
}
}
}
/*
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
*/