http://acm.hdu.edu.cn/showproblem.php?pid=4417
Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
Sample Input
1
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
题目大意:
n
n
n个数,
m
m
m个询问,询问
[
L
,
R
]
[L,R]
[L,R]内小于等于
v
v
v的数的个数。
思路:主席树。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#define pr pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,nn,m,tot;
int rt[maxn],a[maxn],b[maxn];
struct node
{
int ls,rs,sum;
}tree[maxn*40];
inline void Insert(int num,int &x,int l,int r)
{
tree[++tot]=tree[x];x=tot;
++tree[x].sum;
if(l==r) return;
int mid=(l+r)>>1;
if(num<=mid)
Insert(num,tree[x].ls,l,mid);
else
Insert(num,tree[x].rs,mid+1,r);
}
int querynum(int i,int j,int l,int r,int x)//[i,j]区间内小于等于x的数的个数 传入参数 rt[i-1] rt[j] 1 n x
{
if(x>=b[r])
return tree[j].sum-tree[i].sum;
else if(x<b[l])
return 0;
int res=0;
int mid=l+r>>1;
if(x<=b[mid])
res+=querynum(tree[i].ls,tree[j].ls,l,mid,x);
else
{
res+=tree[tree[j].ls].sum-tree[tree[i].ls].sum;
res+=querynum(tree[i].rs,tree[j].rs,mid+1,r,x);
}
return res;
}
inline void prework()
{
tree[0].ls=tree[0].rs=tree[0].sum=0;
rt[0]=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
nn=unique(b+1,b+1+n)-b-1;
tot=0;
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(b+1,b+1+nn,a[i])-b;
rt[i]=rt[i-1];
Insert(a[i],rt[i],1,nn);
}
}
inline void mainwork()
{
int l,r,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&l,&r,&v);
++l,++r;
printf("%d\n",querynum(rt[l-1],rt[r],1,nn,v));
}
}
int main()
{
int t,times=0;
scanf("%d",&t);
while(t--)
{
prework();
printf("Case %d:\n",++times);
mainwork();
}
return 0;
}