Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7503 Accepted Submission(s): 3223
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.)
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
题意:求区间权值在1~k的个数
分析;主席树入门,求第K大值一样
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
//#define ll long long
using namespace std;
const int N=100010 ;
int n,m,len,tot;
int root[N],a[N];
int ls[N*30],rs[N*30],s[N*30];
vector<int> vec;
int Hash(int k){
int f=lower_bound(vec.begin(),vec.end(),k)-vec.begin();
if(vec[f]!=k)return f;
return f+1;
}
void update(int k,int pre,int &now,int l,int r){
now=++tot;
ls[now]=ls[pre];
rs[now]=rs[pre];
s[now]=s[pre]+1;
if(l==r)return;
int m=(l+r)>>1;
if(k<=m)update(k,ls[pre],ls[now],l,m);
else update(k,rs[pre],rs[now],m+1,r);
}
int query(int pre,int now,int L,int R,int l,int r){
if(L<=l && r<=R){
return s[now]-s[pre];
}
int m=(l+r)>>1,s=0;
if(L<=m) s+=query(ls[pre],ls[now],L,R,l,m);
if(R>m) s+=query(rs[pre],rs[now],L,R,m+1,r);
return s;
}
int main()
{
int T,cur=1;cin>>T;
while(T--){
scanf("%d%d",&n,&m);
tot=0;
mem(s,0);
vec.clear();
for(int i=1;i<=n;i++){
scanf("%d",a+i);
vec.pb(a[i]);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
len=vec.size();
for(int i=1;i<=n;i++){
update(Hash(a[i]),root[i-1],root[i],1,len);
}
printf("Case %d:\n",cur++);
while(m--){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
cout<<query(root[l],root[r+1],1,Hash(k),1,len)<<endl;
}
}
return 0;
}