super mario
Time limit: 2000/1000 MS (java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8448 Accepted Submission(s): 3568
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
这题用划分树做,再用二分法查找。
#include<bits/stdc++.h>
using namespace std;
int n,m,tree[20][100001],toleft[20][100001],t,sortnum[100001];
void dfs(int l,int r,int depth)
{
if(l != r)
{
int same = 0,mid = (l + r) / 2 + 1;
for(int i = l;i < mid;++i)
if(sortnum[i] == sortnum[mid])
++same;
int temp = sortnum[mid],num = 0,lpos = l,rpos = mid;
for(int i = l;i <= r;++i)
if(tree[depth][i] < temp)
{
toleft[depth][i] = ++num;
tree[depth + 1][lpos++] = tree[depth][i];
}
else if(tree[depth][i] > temp ||(tree[depth][i] == temp && !same))
{
toleft[depth][i] = num;
tree[depth + 1][rpos++] = tree[depth][i];
}
else
{
toleft[depth][i] = ++num;
tree[depth + 1][lpos++] = tree[depth][i];
--same;
}
dfs(l,mid - 1,depth + 1);
dfs(mid,r,depth + 1);
}
}
int findk(int L,int R,int l,int r,int k,int depth)
{
if(l == r)
return tree[depth][l];
int temp,temp1 = 0,mid = (L + R) / 2 + 1;
if(l > L)
temp1 = toleft[depth][l - 1];
temp = toleft[depth][r] - temp1;
if(temp < k)
{
int temp2 = mid + l - L - temp1;
int temp3 = temp2 + r - l - temp;
return findk(mid,R,temp2,temp3,k - temp,depth + 1);
}
return findk(L,mid - 1,L + temp1,L + toleft[depth][r] - 1,k,depth + 1);
}
int findh(int l,int r,int h,int depth)
{
int ans = 0;
int left = l,right = r;
while(left <= right)
{
int mid = (right + left) / 2;
int temp = findk(0,n - 1,l,r,mid - l + 1,0);
if(temp <= h)
{
ans += (mid - left + 1);
left = mid + 1;
}
else
right = mid - 1;
}
return ans;
}
int main()
{
scanf("%d",&t);
int casei = 0;
while(t)
{
++casei;
scanf("%d",&n);
scanf("%d",&m);
for(int i = 0;i < n;++i)
{
scanf("%d",&tree[0][i]);
sortnum[i] = tree[0][i];
}
sort(sortnum,sortnum + n);
dfs(0,n - 1,0);
printf("Case %d:\n",casei);
while(m)
{
int l,r,k;
scanf("%d",&l);
scanf("%d",&r);
scanf("%d",&k);
int ans = findh(l,r,k,0);
printf("%d\n",ans);
--m;
}
--t;
}
return 0;
}
相关阅读
Root 方法: 通过fastboot 刷入指定的recovery.img, 替换了系统原生的recovery, 进入recovery,刷入root相关文件,以达到root目的
RTX2080Super值得买吗 RTX2080Super显卡详细图解评测
RTX2080Super显卡怎么样?性能如何?值得入手吗?今天给大家带来RTX2080Super显卡详细图解评测,希望对大家有所帮助。RTX2080Super显卡详
简介supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、
很多朋友喜欢用QQ音乐听歌,想要开启QQ音乐中的super sound音效,该怎么开启呢?下面我们就来看看详细的图文教程,详细如下文。1)点开QQ音
supermap iobjects学习——三维通视分析,可视域分析(2)
三维可视域分析是在场景的模型数据表面,相对于某个观察点,基于一定的水平视角、垂直视角以及指定的范围半径,分析该区域内所有通视点