K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 52348   Accepted: 17985
Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion
 
 
思路:
  可持久化线段树模板;
 
来,上代码:
  结构体数组版(ac):

#include <cstdio>
#include <algorithm> using namespace std; struct TreeNode {
int l,r,dis;
}; class PersistentLineSegmentTree {
private:
int n,m,if_z,tot,num[],num_[],size; char Cget; TreeNode root[],node[*]; inline void read_int(int &now_)
{
now_=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now_=now_*+Cget-'';
Cget=getchar();
}
now_*=if_z;
} public:
PersistentLineSegmentTree()
{
read_int(n),read_int(m);
for(int i=;i<=n;i++)
{
read_int(num[i]);
num_[i]=num[i];
}
sort(num+,num+n+);
size=unique(num+,num+n+)-num-;
Build(,size,root[]);
for(int i=;i<=n;i++)
{
num_[i]=lower_bound(num+,num+size+,num_[i])-num;
Down(,size,root[i-],root[i],num_[i]);
}
int lit,rit,k;
for(int i=;i<=m;i++)
{
read_int(lit),read_int(rit),read_int(k);
printf("%d\n",num[Query(,size,root[lit-],root[rit],k)]);
}
} inline void Up(TreeNode &now)
{
now.dis=node[now.l].dis+node[now.r].dis;
} void Build(int l,int r,TreeNode &now)
{
if(l==r) return ;
int mid=(l+r)>>;
now.l=++tot;
Build(l,mid,node[now.l]);
now.r=++tot;
Build(mid+,r,node[now.r]);
} void Down(int l,int r,TreeNode &pre,TreeNode &now,int to)
{
if(l==r)
{
now.dis++;
return ;
}
int mid=(l+r)>>;
if(to>mid)
{
now.l=pre.l;
now.r=++tot;
Down(mid+,r,node[pre.r],node[now.r],to);
}
else
{
now.r=pre.r;
now.l=++tot;
Down(l,mid,node[pre.l],node[now.l],to);
}
Up(now);
} int Query(int l,int r,TreeNode &pre,TreeNode &suc,int K)
{
if(l==r)
{
return l;
}
int mid=(l+r)>>;
int dis=node[suc.l].dis-node[pre.l].dis;
if(K>dis) return Query(mid+,r,node[pre.r],node[suc.r],K-dis);
else return Query(l,mid,node[pre.l],node[suc.l],K);
}
}; class PersistentLineSegmentTree tree; int main()
{
return ;
}

指针版(正确但超时):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; struct TreeNode {
int l,r,mid,dis; TreeNode *left,*right;
}; class PersistentLineSegmentTree {
private:
int n,m,if_z,num[],num_[],size; char Cget; TreeNode *root[],*null; inline void read_int(int &now_)
{
now_=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now_=now_*+Cget-'';
Cget=getchar();
}
now_*=if_z;
} public:
PersistentLineSegmentTree()
{
null=new TreeNode;
null->left=null;
null->right=null;
root[]=null;
read_int(n),read_int(m);
for(int i=;i<=n;i++)
{
read_int(num[i]);
num_[i]=num[i];
root[i]=null;
}
sort(num+,num+n+);
size=unique(num+,num+n+)-num-;
Build(root[],,size);
for(int i=;i<=n;i++)
{
num_[i]=lower_bound(num+,num+size+,num_[i])-num;
Down(root[i-],root[i],num_[i]);
}
int lit,rit,k;
for(int i=;i<=m;i++)
{
read_int(lit),read_int(rit),read_int(k);
printf("%d\n",num[Query(root[lit-],root[rit],k)]);
}
} void Build(TreeNode *&now,int l,int r)
{
if(now==null)
{
now=new TreeNode;
now->l=l;
now->r=r;
now->dis=;
now->mid=(l+r)>>;
now->left=null;
now->right=null;
}
if(l==r) return ;
Build(now->left,l,now->mid);
Build(now->right,now->mid+,r);
} inline void Up(TreeNode *&now)
{
now->dis=now->left->dis+now->right->dis;
} void Down(TreeNode *&pre,TreeNode *&now,int to)
{
now=new TreeNode;
now->l=pre->l;
now->r=pre->r;
now->mid=pre->mid;
now->left=null;
now->right=null;
if(now->l==now->r)
{
now->dis=pre->dis+;
return ;
}
if(to>now->mid)
{
now->left=pre->left;
Down(pre->right,now->right,to);
}
else
{
now->right=pre->right;
Down(pre->left,now->left,to);
}
Up(now);
} int Query(TreeNode *&pre,TreeNode *&now,int K)
{
if(now->l==now->r) return now->l;
int dis=now->left->dis-pre->left->dis;
if(dis<K) return Query(pre->right,now->right,K-dis);
else return Query(pre->left,now->left,K);
}
};
class PersistentLineSegmentTree tree; int main()
{
return ;
}

AC日记——K-th Number poj 2104的更多相关文章

  1. K-th Number POJ - 2104

    K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...

  2. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

  3. 主席树 【权值线段树】 && 例题K-th Number POJ - 2104

    一.主席树与权值线段树区别 主席树是由许多权值线段树构成,单独的权值线段树只能解决寻找整个区间第k大/小值问题(什么叫整个区间,比如你对区间[1,8]建立一颗对应权值线段树,那么你不能询问区间[2,5 ...

  4. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

  5. AC日记——Destroying The Graph poj 2125

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8356   Accepted: 2 ...

  6. HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)

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

  7. AC日记——青蛙的约会 poj 1061

    青蛙的约会 POJ - 1061   思路: 扩展欧几里得: 设青蛙们要跳k步,我们可以得出式子 m*k+a≡n*k+b(mod l) 式子变形得到 m*k+a-n*k-b=t*l (m-n)*k-t ...

  8. hdu 2665 Kth number (poj 2104 K-th Number) 划分树

    划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...

  9. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

随机推荐

  1. CF 497 div 2 B

    B. Turn the Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. poj 3258 跳房子问题 最大化最小值

    题意:奶牛跳房子,从n块石头中移除M块,使得间距最小的最大值?思路:“转换” 从N块中选择n-m块使得两两之间的间距尽可能大 c(d) 是间距最大的满足条件,即第一块 放在 xi的位置 下一块就要放在 ...

  3. python实现分布式进程

    今天用python实现分布式,基于python2.7,注意:在linux下执行测试通过,在windows测试失败.# -*- coding: utf-8 -*-__author__ = 'dell'i ...

  4. Diycode开源项目 Glide图片加载分析

    1.使用Glide前的准备 1.1.首先要build.gradle中添加   github原地址点击我. 参考博客:Glide-开始! 参考博客:android图片加载库Glide的使用介绍. 参考博 ...

  5. P2615 神奇的幻方

    P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...

  6. 【面试】一篇文章帮你彻底搞清楚“I/O多路复用”和“异步I/O”的前世今生

    曾经的VIP服务 在网络的初期,网民很少,服务器完全无压力,那时的技术也没有现在先进,通常用一个线程来全程跟踪处理一个请求.因为这样最简单. 其实代码实现大家都知道,就是服务器上有个ServerSoc ...

  7. 单例模式【python】

    在python中,如需让一个类只能创建一个实例对象,怎么能才能做到呢? 思路:1.通过同一个类创建的不同对象,都让他们指向同一个方向.   2.让个类只能创建唯一的实例对象. 方法:用到 _ _new ...

  8. Android输入法弹出时覆盖输入框问题

    本文来自网易云社区 作者:孙有军 当一个activity中含有输入框时,我们点击输入框,会弹出输入法界面,整个界面的变化效果与manifest中对应设置的android:windowSoftInput ...

  9. 申请社交平台appkey详细教程

    申请社交平台appkey详细教程 大部分app都需要实现分享到微信.微博等社交平台的功能,但是在各个平台上申请appkey是一件很繁琐的事情.现在来分享一个申请社交平台appkey详细教程,在开发过程 ...

  10. 新博客 http://kunyashaw.com/

    感谢博客园. 请关注我的新博客: http://kunyashaw.com/