思路:裸的划分树

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#define Maxn 100010
#define lson(x) x<<1
#define rson(x) x<<1|1
#define mid ((tree[po].l+tree[po].r)>>1)
using namespace std;
int val[][Maxn],toLeft[][Maxn],sorted[Maxn];
struct Tree{
int l,r;
}tree[Maxn*];
void BuildTree(int l,int r,int dep,int po)
{
tree[po].l=l,tree[po].r=r;
if(l==r)
return ;
int same=mid-l+,i;
for(i=l;i<=r;i++)
if(val[dep][i]<sorted[mid])
same--;
int lpos=l,rpos=mid+;
for(i=l;i<=r;i++){
if(i==l)
toLeft[dep][i]=;
else
toLeft[dep][i]=toLeft[dep][i-];
if(val[dep][i]<sorted[mid])
toLeft[dep][i]++,val[dep+][lpos++]=val[dep][i];
else
if(val[dep][i]>sorted[mid])
val[dep+][rpos++]=val[dep][i];
else
if(same)
toLeft[dep][i]++,val[dep+][lpos++]=val[dep][i],same--;
else
val[dep+][rpos++]=val[dep][i];
}
BuildTree(l,mid,dep+,lson(po));
BuildTree(mid+,r,dep+,rson(po));
}
int query(int l,int r,int k,int dep,int po)
{
if(l==r)
return val[dep][l];
int vd,invd;
if(l==tree[po].l){
vd=toLeft[dep][r];
invd=;
}
else{
vd=toLeft[dep][r]-toLeft[dep][l-];
invd=toLeft[dep][l-];
}
if(vd>=k){
int newl=invd+tree[po].l;
int newr=invd+vd+tree[po].l-;
return query(newl,newr,k,dep+,lson(po));
}
else{
int ss=l-tree[po].l-invd;
int s=r-l+-vd;
int newl=mid+ss+;
int newr=mid+ss+s;
return query(newl,newr,k-vd,dep+,rson(po));
}
}
int main()
{
int n,m,i,j,l,r,Case=,t,x;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++){
scanf("%d",val[]+i);
sorted[i]=val[][i];
}
sort(sorted+,sorted++n);
BuildTree(,n,,);
printf("Case %d:\n",++Case);
scanf("%d",&m);
for(i=;i<=m;i++){
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r,(r-l+)/,,));
}
}
return ;
}

hdu 4251 划分树的更多相关文章

  1. HDU 4251 --- 主席树(划分树是正解)

    题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...

  2. HDU 4417 (划分树+区间小于k统计)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...

  3. hdu 2665 划分树

    思路:裸的划分树 #include<iostream> #include<algorithm> #include<cstring> #include<cstd ...

  4. hdu 4417 划分树

    思路:二分枚举区间第k大.用划分树查找是否符合要求的高度. #include<iostream> #include<algorithm> #include<cstdio& ...

  5. HDU 4417 划分树写法

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  6. HDU 4417 划分树+二分

    题意:有n个数.m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质.二分查找在区间[l,r]小于等于k的个数. 假设在区间第 i ...

  7. hdu 3473 划分树

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. hdu 2665 划分树模板题(可作为模板)

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Hdu 4251 区间中位数(划分树)

    题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/3276 ...

随机推荐

  1. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  2. Spring Object/XML mapping example

    In this tutorial, we will extend last Maven + Spring hello world example by adding JDBC support, to ...

  3. [Xcode使用 - 3] 复制Xcode5.1.1中的项目模板到Xcode6.1

         由于Xcode6中精简了许多的项目和文件模板,导致开发非常不方便,所以这里简单介绍了怎么复制旧版本Xcode中的模板到新的Xcode中      这里要复制的是项目模板Empty Appli ...

  4. (hzau)华中农业大学第四届程序设计大赛网络同步赛 G: Array C

    题目链接:http://acm.hzau.edu.cn/problem.php?id=18 题意是给你两个长度为n的数组,a数组相当于1到n的物品的数量,b数组相当于物品价值,而真正的价值表示是b[i ...

  5. [html]html常用代码

    上传文件表单属性 enctype="multipart/form-data" 单选(是否选中) checked="checked" 下拉列表(是否选中) sel ...

  6. Thinkphp框架----微信公众测试号开发(2)

    ---恢复内容开始--- 最近忙着投简历找工作.现在继续更 微信回复图文设置. 效果: 页面HTML需要一个form表单 简单效果: 接下来是数据库 字段:id title(标题) text(描述) ...

  7. Oracle数据库程序包全局变量的应用

    1 前言  在程序实现过程中,经常用遇到一些全局变量或常数.在程序开发过程中,往往会将该变量或常数存储于临时表或前台程序的全局变量中,由此带来运行效率降低<频繁读取临时表>或安全隐患< ...

  8. MEF 编程指南(八):过滤目录

    当使用子容器的时候,基于特定的标准(Specific Criteria)过滤目录是很必要的.比如,基于部件构造策略的过滤器是很常见的.下面的代码片段演示了如何构建的特殊途径(Particular Ap ...

  9. uistepper on ios versions prior to 5.0

    xcode5 打开运行就出现这个错误 uistepper on ios versions prior to 5.0 直接在General -->Deployment Info -->Dep ...

  10. 关于webapi 返回的类型的笔记

    经过测试发现使用IE浏览器返回的数据是json,而使用Firefox和Chrome返回的则为xml,经研究发现IE在发生http请求时请求头accpet节点相比Firefox和Chrome缺少&quo ...