hdu 2665 划分树
思路:裸的划分树
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define Maxn 100010
#define inf 0x7fffffff
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
#define mid ((tree[po].l+tree[po].r)>>1)
using namespace std;
struct Tree{
int l,r;
}tree[Maxn*];
int sorted[Maxn];
int val[][Maxn],toLeft[][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=tree[po].l+invd;
int newr=tree[po].l+vd+invd-;
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,k,s,t,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
scanf("%d",&val[][i]);
sorted[i]=val[][i];
}
sort(sorted+,sorted++n);
BuildTree(,n,,);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&k);
printf("%d\n",query(s,t,k,,));
}
}
return ;
}
hdu 2665 划分树的更多相关文章
- hdu 2665 划分树模板题(可作为模板)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 (划分树+区间小于k统计)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...
- hdu 4251 划分树
思路:裸的划分树 #include<iostream> #include<algorithm> #include<cstdio> #include<cmath ...
- hdu 4417 划分树
思路:二分枚举区间第k大.用划分树查找是否符合要求的高度. #include<iostream> #include<algorithm> #include<cstdio& ...
- HDU 4417 划分树写法
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- HDU 4417 划分树+二分
题意:有n个数.m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质.二分查找在区间[l,r]小于等于k的个数. 假设在区间第 i ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- HDU 2665(主席树,无修改第k小)
Kth number Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 3473 划分树
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- 【转】并发编程之GCD
http://blog.xcodev.com/blog/2013/11/04/gcd-intro/ Dispatch Queue Dispatch Queue是一个任务执行队列,可以让你异步或同步地执 ...
- HDU 5754 Life Winner Bo (博弈)
Life Winner Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5754 Description Bo is a "Life W ...
- My97DatePicker的calendar.js的反混淆
eval(string)函数 <script> eval(function(p, a, c, k, e, d) { p = 'function p(){console.log(" ...
- zznu 1914 asd的甩锅计划
http://acm.zznu.edu.cn/problem.php?id=1914 asd的甩锅计划 时间限制: 1 Sec 内存限制: 128 MB提交: 114 解决: 10[提交][状态] ...
- Protobuf一键生成代码bat文件
最近在摆弄Unity的Socket,需要用到Protobuf,一般都会有多个协议文件,所以研究了下bat的批处理,下面给出批处理文件代码: @echo off ::协议文件路径, 最后不要跟“\”符号 ...
- ZendFramework2 与MongoDB的整合
从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还FQ ...
- iOS UIWebView加载时添加进度条
标注:此框架仅适合UIWebView 对iOS8后新出的WKWebView不适用,当然,你可以尝试修改框架里的几个代理方法. 框架是:NJKWebViewProgress 导入头文件 #import ...
- Spring3.0 AOP 详解
一.什么是 AOP. AOP(Aspect Orient Programming),也就是面向切面编程.可以这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度 ...
- win7常用快捷键
Win+1:打开/显示超级任务栏第一个图标代表的程序Win+2:打开/显示超级任务栏第二个图标代表的程序(3.4.……如此类推)Win+Tab:3D切换窗口,你要是按住不松口,则所有窗口会轮流翻转Wi ...
- pod install warning
warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777 解决方法: sudo chmod 775 /usr/l ...