题目链接:点击打开链接

题意:

给定n长的序列。q个询问

以下n个数字给出序列

每一个询问[l, r] k ,输出该区间中第k大的数

先建一个n个节点的空树。然后每次从后往前新建一棵树,依附原来的空树建。询问就是在树上二分。

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std; const int N = 100010;
const int M = N * 30;
int n, q, tot;
int a[N];
int T[M], lson[M], rson[M], c[M];
vector<int>G; void input(){
G.clear();
for (int i = 1; i <= n; i++)scanf("%d", &a[i]), G.push_back(a[i]);
sort(G.begin(), G.end());
G.erase(unique(G.begin(), G.end()), G.end());
for (int i = 1; i <= n; i++)a[i] = lower_bound(G.begin(), G.end(), a[i]) - G.begin() + 1;
}
int build(int l, int r){
int root = tot++;
c[root] = 0;
if (l != r){
int mid = (l + r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid + 1, r);
}
return root;
}
int updata(int root, int pos, int val){
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = G.size();
while (l <= r){
int mid = (l + r) >> 1;
if (pos <= mid){
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid - 1;
}
else {
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int L, int R, int k){
int l = 1, r = G.size(), ans = l;
while (l <= r){
int mid = (l + r) >> 1;
if (c[lson[L]] - c[lson[R]] >= k){
ans = mid;
r = mid - 1;
L = lson[L];
R = lson[R];
}
else {
l = mid + 1;
k -= c[lson[L]] - c[lson[R]];
L = rson[L];
R = rson[R];
}
}
return ans;
}
int main(){
while (~scanf("%d%d", &n, &q)){
input();
tot = 0;
T[n + 1] = build(1, G.size());
for (int i = n; i; i--)
T[i] = updata(T[i + 1], a[i], 1);
while (q--){
int l, r, k; scanf("%d %d %d", &l, &r, &k);
printf("%d\n", G[query(T[l], T[r+1], k)- 1]);
}
}
return 0;
}

POJ 2104 K-th Number 静态主席树(裸的更多相关文章

  1. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  2. poj 2104 K-th Number(主席树,详细有用)

    poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...

  3. COGS 930. [河南省队2012] 找第k小的数 主席树

    主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 ...

  4. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  5. POJ 2104 K-th Number(主席树模板题)

    http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...

  6. POJ 2104 K-th Number(主席树——附讲解)

    Description You are working for Macrohard company in data structures department. After failing your ...

  7. poj 2104 K-th Number(主席树)

    Description You are working for Macrohard company in data structures department. After failing your ...

  8. poj 2104 静态主席树

    我的第一道主席树(静态). 先记下自己对主席树的理解: 主席树的作用是用于查询区间第k大的元素(初始化nlog(n),查询log(n)) 主席树=可持续线段树+前缀和思想 主席树实际上是n棵线段树(由 ...

  9. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

随机推荐

  1. 福建省赛--Problem E The Longest Straight(标记+二分)

    Problem E The Longest Straight Accept: 71    Submit: 293 Time Limit: 1000 mSec    Memory Limit : 327 ...

  2. HDFS的配额

  3. realm怎样支持hashmap

    realm不支持hashmap这种形式stackoverflow给出了解决方案http://stackoverflow.com/ques... class MyData extends RealmOb ...

  4. 谈谈Vim中实用又好记的一些命令

      本文的目的在于总结一些日常操作中比较实用.有规律的Vim命令,而不致于介绍一些基础的Vim知识,比如几种插入模式,hjkl移动命令,dd删除本行,p粘贴 等等,故对Vim基本知识不够熟悉的请参见其 ...

  5. 洛谷P2197 nim游戏模板

    Code: #include<iostream> using namespace std; int main(){ int t; cin>>t; while(t--){ int ...

  6. 洛谷2474 [SCOI2008] 天平 差分约束->枚举

    题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A 和B 放在天平的左边,需要另外选出两个砝码放在天平的右边.问:有多少种 ...

  7. 洛谷2850 【Usaco2006 Dec】虫洞Wormholes SPFA

    问题描述 John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N ...

  8. Python安装(一)

    Python的安装 打开python的官网 进入下载界面 选择下载 安装步骤如下所示: 安装完成进入到dos界面,输入python -V,如下图展示及成功 打开Python工具 1:: print() ...

  9. Opencv Mat的三种常用类型简介

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/47683127 本文主要介绍Opencv ...

  10. javaweb——登陆权限过滤器的编写

    http://blog.csdn.net/lzc4869/article/details/50935858