Kth number

                                                Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                        Total Submission(s): 10682    Accepted Submission(s): 3268

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
Input
The first line is the number of the test cases.
For
each test case, the first line contain two integer n and m (n, m <=
100000), indicates the number of integers in the sequence and the number
of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
Output
For each test case, output m lines. Each line contains the kth big number.
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
Sample Output
2
Source
说实话  我不知道我该怎么去解释 因为这就是主席树的板子
无区间修改的主席树 不解释
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#define ll long long
#define LL unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int left,right;
int val;
}tree[N*];
int cnt;
int a[N],b[N];
int root[N];
int build(int l,int r){
int pos=++cnt;
tree[pos].val=;
if(l==r)return pos;
int mid=(l+r)>>;
tree[pos].left=build(l,mid);
tree[pos].right=build(mid+,r);
return pos;
}
void update(int pre,int &now,int x,int l,int r){
tree[++cnt]=tree[pre];
now=cnt;
tree[now].val++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid){
update(tree[pre].left,tree[now].left,x,l,mid);
}
else{
update(tree[pre].right,tree[now].right,x,mid+,r);
} }
int query(int L,int R,int k,int l,int r){
if(l==r) return l;
int mid=(l+r)>>;
int sum=tree[tree[R].left].val-tree[tree[L].left].val;
if(k<=sum){
return query(tree[L].left,tree[R].left,k,l,mid);
}
else{
return query(tree[L].right,tree[R].right,k-sum,mid+,r);
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(root,,sizeof(root));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int n,m;
scanf("%d%d",&n,&m);
cnt=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int tt = unique(b+,b++n)-b-;
root[0]=build(1,tt);
for(int i=;i<=n;i++) {
a[i]=lower_bound(b+,b++tt,a[i])-b;//二分找到a[i]的位置
update(root[i-],root[i],a[i],,tt);//root[i-1]表示上一个版本的线段树
}
for(int i=;i<=m;i++) {
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=query(root[l-],root[r],k,,tt);
printf("%d\n", b[ans]);
}
}
return ;
}

HDU 2665(主席树,无修改第k小)的更多相关文章

  1. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  2. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  3. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  4. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  5. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  6. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  7. BZOJ.2588.Count on a tree(主席树 静态树上第k小)

    题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...

  8. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  9. 主席树(区间第k小的数)

    题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...

随机推荐

  1. mongoDB 删除集合后,空间不释放的解决方法

    mongoDB 删除集合后,空间不释放,添加新集合,没有重新利用之前删除集合所空出来的空间,也就是数据库大小只增不减. 方法有: 1.导出导入 dump & restore 2.修复数据库 r ...

  2. html5——伸缩比例

    基本概念 1.父盒子设置了伸缩属性,子盒子设置伸缩比例 2.以上设置完之后子盒子会按照比例分布在父盒子中 3.当设置伸缩比例时默认会按照x轴方向分配,因为默认情况下伸缩布局主轴方向是x轴方向 4.设置 ...

  3. Linux内存压力测试-memtester工具

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  4. Ubuntu无线转有线教程

    本来想测试一下有线转无线的,奈何网卡不支持,所以就测试了一回无线转有线的测试!(真无聊,不过也算学习一下linux网桥的知识) ca0gu0@ub:~$ sudo brctl addbr br0 #添 ...

  5. Centos6.4 安装bind dns 服务器

    一.介绍 1)Centos6.4 64bit minimal 2) bind-9.8.2-0.30.rc1.el6_6.3.x86_64 二.安装 $ yum install -y bind bind ...

  6. UITableview 兼容IOS6 和IOS7的方法

    1. TableVIew向下拉44像素  添加Auto layout 2. Extended edge 选择Under top bars 2. 在Viewdidload中添加代码 if ([[UIDe ...

  7. include和require区别

    1.  include()执行的时候需要引用的文件每次都要进行读取评估; require()执行时需要引用的文件只处理一次(实际上执行时需要引用的文件内容替换了require()语句) 可以看出若有包 ...

  8. do{}while(0)

    有时会在源码中或在写代码时在宏定义中用到do...while(0). 采用这种方式进行宏定义, 主要是为了防止出现以下错误 : do{}while(0) 空的宏定义避免出现warnning. #def ...

  9. 洛谷——P1966 火柴排队&&P1774 最接近神的人_NOI导刊2010提高(02)

    P1966 火柴排队 这题贪心显然,即将两序列中第k大的数的位置保持一致,证明略: 树状数组求逆序对啦 浅谈树状数组求逆序对及离散化的几种方式及应用 方法:从前向后每次将数插入到bit(树状数组)中, ...

  10. PAT 1108 Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...