您的位置:首页 > 其它

HDU 4417 Super Mario 划分树+二分

2016-09-11 17:12 411 查看
Super Mario
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4939    Accepted Submission(s): 2262

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

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5877 5876 5875 5873 5872


来源: http://acm.hdu.edu.cn/showproblem.php?pid=4417 #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <set>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define MAXN 120000
int Sorted[MAXN];
int Tree[30][MAXN];
int Toleft[30][MAXN];
void Build(int Lef,int Rig,int dep)
{
if(Lef == Rig) return;
int Mid = (Lef+Rig)>>1;
int Same = Mid - Lef + 1;

for(int i=Lef;i<=Rig;i++)
if(Tree[dep][i]<Sorted[Mid]) Same--;
int Lpos = Lef;
int Rpos = Mid+1;
for(int i=Lef;i<=Rig;i++)
{
if(Tree[dep][i]<Sorted[Mid])
Tree[dep+1][Lpos++] = Tree[dep][i];
else if(Tree[dep][i]==Sorted[Mid] && Same>0)
{
Tree[dep+1][Lpos++] = Tree[dep][i];
Same--;
}
else Tree[dep+1][Rpos++] = Tree[dep][i];
Toleft[dep][i] = Toleft[dep][Lef-1]+Lpos-Lef;
}
Build(Lef,Mid,dep+1);
Build(Mid+1,Rig,dep+1);
}

int Query(int Lef,int Rig,int l,int r,int dep,int K)
{
if(l==r) return Tree[dep][l];

int Mid = (Lef+Rig)>>1;
int Cnt = Toleft[dep][r] - Toleft[dep][l-1];
if(Cnt>=K)
{
int newL = Lef + Toleft[dep][l-1] - Toleft[dep][Lef-1];
int newR = newL + Cnt -1;
return Query(Lef,Mid,newL,newR,dep+1,K);
}
else
{
int newR = r + Toleft[dep][Rig] - Toleft[dep][r];
int newL = newR - (r-l-Cnt);
return Query(Mid+1,Rig,newL,newR,dep+1,K-Cnt);
}
}
int main()
{
//    freopen("F:\\test.txt","r",stdin);
//    freopen("F:\\tsst.txt","w",stdout);
int T;scanf("%d",&T);
for(int t=1,N,M;t<=T;t++)
{
printf("Case %d:\n",t);
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+N+1);
Build(1,N,0);
for(int i=1;i<=M;i++)
{
//printf("i == %d\n",i);
int a,b,H;
scanf("%d %d %d",&a,&b,&H);
int L = 1,R=b-a+1;
while(L<=R)
{
// printf("L %d,R %d\n",L,R);
int Mid = (R+L)>>1;
int Ans = Query(1,N,a+1,b+1,0,Mid);
if(Ans>H) R = Mid-1;
else L = Mid+1;
}
printf("%d\n",L-1);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: