思路:裸的划分树

#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. Spring dependency checking with @Required Annotation

    Spring's dependency checking in bean configuration file is used to make sure all properties of a cer ...

  2. Codeforces 622B The Time 【水题】

    B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  3. [iOS基础控件 - 6.12.4] NavigationController vs TabBarController

    A.属性 1. Item NavigationController: navigationItem (不需要创建) title rightBarButtonItems/ rightBarButtonI ...

  4. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

  5. 使用 DllImport 属性

    本主题说明 DllImport 属性的常见用法.第一节讨论使用 DllImport 从托管应用程序调用本机代码的优点.第二节集中讨论封送处理和 DllImport 属性的各个方面. 从托管应用程序调用 ...

  6. android4.0访问不能网络解决方法

    @SuppressLint("NewApi")protected void onCreate(Bundle savedInstanceState) {StrictMode.setT ...

  7. SQL中DATE和DATETIME类型不能直接作比较

    如题,今天纠结了一天的问题. 在存储过程中定义了两个datetime类型的时间,然后把这个两个时间作为where条件中一个date字段between的两个时间段,结果无论如何都不执行... 就像  u ...

  8. apache win openssl

    Rubayat Hasan Software Development, Music, Web Design, life, thoughts…   Home Portfolio Projects Con ...

  9. PostgreSQL的 initdb 源代码分析之四

    继续分析: if (pwprompt && pwfilename) { fprintf(stderr, _("%s: password prompt and password ...

  10. android常见错误-E/AndroidRuntime(13678): java.lang.NoClassDefFoundError:

    08-01 11:09:39.791: E/AndroidRuntime(13678): FATAL EXCEPTION: main 08-01 11:09:39.791: E/AndroidRunt ...