2012 ACM/ICPC Asia Regional Hangzhou Online ,HDU4417-Super Mario

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
 
题意就是给你一个区间,一个数,问你这个区间里比这个数小的数有多少个,思路是划分树+二分枚举第k大的数

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>

using namespace std;

const int maxn=100010;

struct node
{
int num[maxn];
int val[maxn];
}tree[30];

int sorted[maxn];

void build(int ind,int l,int r)
{
if(l == r)
return ;
int mid=(l+r)>>1;
int isame=mid-l+1;
for(int i=l;i<=r;i++)
if(sorted[mid] > tree[ind].val[i])
isame--;
int ln=l;
int rn=mid+1;
for(int i = l;i<=r;i++)
{
if(i == l)
tree[ind].num[i]=0;
else
tree[ind].num[i]=tree[ind].num[i-1];
if(tree[ind].val[i] < sorted[mid])
{
tree[ind].num[i]++;
tree[ind+1].val[ln++]=tree[ind].val[i];
}
else if(tree[ind].val[i] > sorted[mid])
tree[ind+1].val[rn++]=tree[ind].val[i];
else
{
if(isame)
{
isame--;
tree[ind].num[i]++;
tree[ind+1].val[ln++]=tree[ind].val[i];
}
else
tree[ind+1].val[rn++]=tree[ind].val[i];
}
}
build(ind+1,l,mid);
build(ind+1,mid+1,r);
}

int query(int ind,int l,int r,int a,int b,int k)
{
if(a == b)
return tree[ind].val[a];
int s0,s1;
int mid=(l+r)>>1;
if(a == l)
{
s0=0;
s1=tree[ind].num[b];
}
else
{
s0=tree[ind].num[a-1];
s1=tree[ind].num[b]-s0;
}
if(s1 >= k)
{
a=l+s0;
b=l+s0+s1-1;
return query(ind+1,l,mid,a,b,k);
}
else
{
a=mid-l+1+a-s0;
b=mid-l+1+b-s0-s1;
return query(ind+1,mid+1,r,a,b,k-s1);
}
}

int main()
{
int t;
scanf("%d",&t);
int p=1;
while(t--)
{
int n, m, x, y;
int k;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&sorted[i]);
tree[0].val[i]=sorted[i];
}
sort(sorted+1,sorted+n+1);
build(0,1,n);
printf("Case %d:\n",p++);
while(m--)
{
scanf("%d%d%d",&x,&y,&k);
x++;
y++;
int l=1,r=(y-x+1),mid,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(query(0,1,n,x,y,mid) <= k)//第mid大元素小于k
{
l=mid+1;
ans=mid;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值