查询区间第K大,而且没有修改。

使用划分树是可以做的。

作为主席树的入门题,感觉太神奇了,Orz

 /* ***********************************************
Author :kuangbin
Created Time :2013-9-4 20:13:20
File Name :POJ2104.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
const int M = MAXN * ;
int n,q,m,tot;
int a[MAXN], t[MAXN];
int T[M], lson[M], rson[M], c[M]; void Init_hash()
{
for(int i = ; i <= n;i++)
t[i] = a[i];
sort(t+,t++n);
m = unique(t+,t++n)-t-;
}
int build(int l,int r)
{
int root = tot++;
c[root] = ;
if(l != r)
{
int mid = (l+r)>>;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
}
int hash(int x)
{
return lower_bound(t+,t++m,x) - t;
}
int update(int root,int pos,int val)
{
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = , r = m;
while(l < r)
{
int mid = (l+r)>>;
if(pos <= mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int left_root,int right_root,int k)
{
int l = , r = m;
while( l < r)
{
int mid = (l+r)>>;
if(c[lson[left_root]]-c[lson[right_root]] >= k )
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + ;
k -= c[lson[left_root]] - c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&q) == )
{
tot = ;
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
Init_hash();
T[n+] = build(,m);
for(int i = n;i ;i--)
{
int pos = hash(a[i]);
T[i] = update(T[i+],pos,);
}
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",t[query(T[l],T[r+],k)]);
}
}
return ;
}

POJ 2104 && POJ 2761 (静态区间第k大,主席树)的更多相关文章

  1. POJ 2104 K-th Number (区间第k大)

    题意:给定一个序列A,接下来又m个询问,每个询问输出A[L,R]中的第K大.(保证第k大存在) 思路: 我想拿来练习“可持久化线段树”的,搜到这个比较巧的算法也可以解决这个问题,叫“归并树?.大概的思 ...

  2. HDU2665 求区间第K大 主席树

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...

  3. POJ-2104-K-th Number(区间第K大+主席树模板题)

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

  4. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  5. HDU3473--Minimum Sum(静态区间第k大)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

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

  7. 主席树(静态区间第k大)

    前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...

  8. 可持久化线段树(主席树)——静态区间第k大

    主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. VirtualBox上安装CentOS-7(Minimal)

    Windows 10家庭中文版,VirtualBox 5.2.12,CentOS 7(Minimal版), 因为听到大家在谈论CentOS,阿里云上也有CentOS,CentOS还是Red Hat出品 ...

  2. python基础-类的封装

    封装:类中封装了公有属性和方法,对象封装了私有属性的值 class F1: def __init__(self,n): self.N=n print('F') class F2: def __init ...

  3. (二) Log4j 配置详解

    第一节: rootLogger 根配置 Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 指代 把指定级 ...

  4. 20155309 《Java程序设计》实验三(Java面向对象程序设计)实验报告

    一.实验内容及步骤 (一)编码标准 在IDEA中使用工具(Code->Reformate Code)把代码重新格式化. (二)在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后, ...

  5. Knockout.Js官网学习Demo(使用VS2012或者VS2013均可打开)

    https://pan.baidu.com/s/1gf9JZ8n#list/path=%2F

  6. Abstract Factory 抽象工厂

    意图 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 适用性 一个系统要独立于它的产品的创建.组合和表示时 结构 参与者 AbstractFactory:声明一个创建抽象产品对象 ...

  7. JS 如何准确获取当前页面URL网址信息

    在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是一些获取url信息的小总结. 下面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs. ...

  8. 【Java】 用PriorityQueue实现最大最小堆

    PriorityQueue(优先队列),一个基于优先级堆的无界优先级队列. 实际上是一个堆(不指定Comparator时默认为最小堆),通过传入自定义的Comparator函数可以实现大顶堆. Pri ...

  9. Oracle win32_11gR2_client.zip

    先将下载下来的ZIP文件解压,并运行setup.exe文件. 第一步:选择管理员(0MB)(A),然后点击下一步 第二步:选择语言,点击下一步 第三步:选择安装的路径,然后点击下一步 第四步:执行到第 ...

  10. jQuery事件和动画

    1.toggle事件 <!DOCTYPE html>   <html>   <head lang="en">   <meta charse ...