题目链接:点击打开链接

题意:

给定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. LSTM 时间序列数据的异常检测

    见 http://www.infoq.com/cn/articles/deep-learning-time-series-anomaly-detection 但是不够详细

  2. 14个优秀 JS 前端框架、库、工具及其使用时机

    这篇文章主要描述现今流行的一些 Javascript web 前端框架,库以及它们的适用场景. 新的 Javascript 库层出不穷,从而Web 社区愈发活跃.多样.在多方面快速发展.详细去描述每一 ...

  3. iOS 集成Protobuf,转换proto文件

    原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...

  4. 测试cnblog文章内部JS

    添加几个按钮 行内js 写法: <button onclick="javascript:alert('行内js')">行内js</button> 注意:al ...

  5. JAVA版本区块链钱包核心代码

    Block.java package com.ppblock.blockchain.core; import java.io.Serializable; /** * 区块 * @author yang ...

  6. dd---复制文件并对原文件的内容进行转换和格式化处理

    dd命令用于复制文件并对原文件的内容进行转换和格式化处理.dd命令功能很强大的,对于一些比较底层的问题,使用dd命令往往可以得到出人意料的效果.用的比较多的还是用dd来备份裸设备.但是不推荐,如果需要 ...

  7. caioj 1081 动态规划入门(非常规DP5:观光游览)

    这道题和前面的分组的题有点像 就是枚举最后一组的长度. 然后组数可以在第一层循环也可以在第二层循环 我自己的话就统一一下在第一层循环吧 然后这道题题意我一直没理解清楚,浪费了很多时间,写复杂了 同时初 ...

  8. ArcGIS api for javascript——显示地图属性

    描述 本例展示了如哦读取地图和图层的属性和返回信息给用户.本例中的四个按钮允许用户接收地图属性.每个按钮调用不同的函数. ·Get All Map Layers - 这个按钮调用getMapLayer ...

  9. 怎样查看电脑的IP地址

    在DOW窗体 :cmd->ipconfig 见截图:

  10. [Recompose] Stream a React Component from an Ajax Request with RxJS

    Loading data using RxJS is simple using Observable.ajax. This lesson shows you how to take the ajax ...