HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3313 Accepted Submission(s): 1548
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.
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.)
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
Case 1:
4
0
0
3
1
2
0
1
5
1
pid=5362" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5362
pid=5361" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5361
5360pid=5359" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5359
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int tree[30][100100],toleft[30][100100];
int sorted[100100];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void build(int l,int r,int dep)
{
if(l==r)
return;
int mid=(l+r)>>1;
int same=mid-l+1;
int i;
int lpos=l;
int rpos=mid+1;
for(i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid])
same--;
}
for(i=l;i<=r;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][l-1]+lpos-l;
}
build(l,mid,dep+1);
build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
if(l==r)
{
return tree[dep][l];
}
int mid=(L+R)>>1;
int cnt=toleft[dep][r]-toleft[dep][l-1];
if(cnt>=k)
{
int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
int newr=newl+cnt-1;
return query(L,mid,newl,newr,dep+1,k);
}
else
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+1,R,newl,newr,dep+1,k-cnt);
}
}
int main()
{
int t,c=0;
scanf("%d",&t);
while(t--)
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&tree[0][i]);
sorted[i]=tree[0][i];
}
qsort(sorted+1,n,sizeof(sorted[1]),cmp);
build(1,n,0);
printf("Case %d:\n",++c);
while(m--)
{
int a,b,h;
scanf("%d%d%d",&a,&b,&h);
a++;
b++;
int l=1,r=(b-a)+1;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
int temp=query(1,n,a,b,0,mid);
if(temp<=h)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
}
}
}
HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)的更多相关文章
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- [csu/coj 1080]划分树求区间前k大数和
题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...
- Super Mario HDU - 4417 (主席树询问区间比k小的个数)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)
题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
随机推荐
- 【转】jvm收集器
HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. Serial(串行GC)收集器 Serial收集 ...
- [Testing][API][soapUI] 測試API 的軟體工具紀錄
soapUI 測試API 的軟體工具紀錄 http://files.cnblogs.com/vincentmylee/soapUIScript%E9%9C%80%E8%A6%81%E8%B3%87%E ...
- Java学习1_一些基础1——16.5.4
每个java程序中都必须有一个main方法,格式为: public class ClassName { public static void main(String[] args) { program ...
- [HNOI2006]最短母串 (AC自动机+状压)
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...
- 设置CAD显示窗体
AcDbViewTableRecord view; AcGePoint3d max = acdbHostApplicationServices()->workingDatabase()-> ...
- vue-quill-editor + element-ui upload实现富文本图片上传
代码贴上 <template> <div class="quill-editor-example"> <div class="box&quo ...
- 打造完美的ImageLoader——LruCache+DiskLruCache
做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...
- [luogu4127 AHOI2009] 同类分布 (数位dp)
传送门 Solution 裸数位dp,空间存不下只能枚举数字具体是什么 注意memset最好为-1,不要是0,有很多状态答案为0 Code //By Menteur_Hxy #include < ...
- Nginx+Tomcat简单负载均衡
Nginx,Apache安装完成 复制Tomcat: tomcat-8080 tomcat-8081 启动Tomcat8080: cd /usr/local/tomcat-8080/bin ...
- 窥探原理:实现一个简单的前端代码打包器 Roid
roid roid 是一个极其简单的打包软件,使用 node.js 开发而成,看完本文,你可以实现一个非常简单的,但是又有实际用途的前端代码打包工具. 如果不想看教程,直接看代码的(全部注释):点击地 ...