HUD-4417:Super Mario(二分+划分树)
2018-02-09 12:58
323 查看
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8172 Accepted Submission(s): 3449
[align=left]Problem Description[/align]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.
[align=left]Input[/align]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.)
[align=left]Output[/align]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.
[align=left]Sample Input[/align]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
[align=left]Sample Output[/align]Case 1:
4
0
0
3
1
2
0
1
5
1
思路:利用二分求出[L,R]中小等于H的数的最大排名。求排名可以用划分树。#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5;
int a[MAX],val[20][MAX],num[20][MAX];
void build(int k,int L,int R)
{
if(L==R)return;
int m=(L+R)/2,tot=m-L+1;
for(int i=L;i<=R;i++)tot-=(val[k][i]<a[m]);
int ln=L,rn=m+1;
for(int i=L;i<=R;i++)
{
if(i==L)num[k][i]=0;
else num[k][i]=num[k][i-1];
if(val[k][i]<a[m]||(val[k][i]==a[m]&&tot))
{
val[k+1][ln++]=val[k][i];
num[k][i]++;
if(a[m]==val[k][i])tot--;
}
else val[k+1][rn++]=val[k][i];
}
build(k+1,L,(L+R)/2);
build(k+1,(L+R)/2+1,R);
}
int ask(int k,int L,int R,int x,int y,int z)
{
if(L==R)return val[k][L];
int ly=(x==L?0:num[k][x-1]);
int tot=num[k][y]-ly;
if(tot>=z)return ask(k+1,L,(L+R)/2,L+ly,L+num[k][y]-1,z);
else
{
int st=(L+R)/2+1+x-L-ly;
return ask(k+1,(L+R)/2+1,R,st,st+y-x+1-tot-1,z-tot);
}
}
int main()
{
int T,cas=1;cin>>T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
val[0][i]=a[i];
}
sort(a+1,a+n+1);
build(0,1,n);
printf("Case %d:\n",cas++);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
int l=1,r=y-x+1,ans=0;
while(r>=l)
{
int m=(l+r)/2;
if(ask(0,1,n,x+1,y+1,m)<=z)
{
l=m+1;
ans=m;
}
else r=m-1;
}
printf("%d\n",ans);
}
}
return 0;
}
相关文章推荐
- HDU-4417-Super Mario(划分树+二分)
- HDOJ 4417 - Super Mario 划分树成熟模板(可解决相同元素)+二分
- HDU 4417 - Super Mario (求区间小于h的数 划分树 二分 线段树 树状数组)
- HDU 4417 Super Mario (划分树)(二分)
- hdu 4417 Super Mario 划分树+二分
- 【HDU】4417 Super Mario(划分树+二分)
- HDU 4417 Super Mario (树状数组+离线处理)(划分树+二分答案)
- HDU-4417 Super Mario,划分树+二分!
- HDU 4417 Super Mario(划分树+二分)
- HDU 4417 Super Mario(划分树+二分)
- HDU 4417 Super Mario(划分树+二分)
- HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
- hdu 4417 Super Mario(划分树)
- HDU 4417 Super Mario(划分树)
- HDU4417 Super Mario(划分树+二分)
- hdu 4417 Super Mario (主席树+二分)
- HDU 4417 划分树+二分
- HDU 4417 Super Mario(划分树)#by zh
- Hdu 4417 Super Mario(主席树+二分)
- 【划分树+二分】HDU 4417 Super Mario