本文主要是介绍Super Mario —— 求小于等于k值的个数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
要注意细节呀,就把all写成n就一直错。
这个也想上到题目一样,不过query改了一下,因为是从1到all存的,所以可以直接和mid比较
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int n,m;
int ls[maxn*20],rs[maxn*20],sum[maxn*20],rt[maxn*20];
int tot,a[maxn],b[maxn];
void build(int l,int r,int &root)
{root=++tot;sum[root]=0;if(l==r)return ;int mid=l+r>>1;build(l,mid,ls[root]);build(mid+1,r,rs[root]);
}
void update(int l,int r,int &root,int last,int val)
{root=++tot;ls[root]=ls[last];rs[root]=rs[last];sum[root]=sum[last]+1;if(l==r)return ;int mid=l+r>>1;if(mid>=val)update(l,mid,ls[root],ls[last],val);elseupdate(mid+1,r,rs[root],rs[last],val);
}
int query(int l,int r,int ql,int qr,int num)
{if(l==r)return sum[qr]-sum[ql];int mid=l+r>>1;if(num<=mid)return query(l,mid,ls[ql],ls[qr],num);else{int ans=sum[ls[qr]]-sum[ls[ql]];ans+=query(mid+1,r,rs[ql],rs[qr],num);return ans;}
}
int main()
{int t;scanf("%d",&t);int cas=0;while(t--){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);int all=unique(b+1,b+1+n)-(b+1);tot=0;build(1,all,rt[0]);for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+all,a[i])-b;for(int i=1;i<=n;i++)update(1,all,rt[i],rt[i-1],a[i]);int l,r,k;printf("Case %d:\n",++cas);for(int i=1;i<=m;i++){scanf("%d%d%d",&l,&r,&k);l++,r++;int ans=upper_bound(b+1,b+1+all,k)-b;ans--;if(ans==0)printf("0\n");elseprintf("%d\n",query(1,all,rt[l-1],rt[r],ans));}}return 0;
}
这篇关于Super Mario —— 求小于等于k值的个数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!