POJ 2104 && POJ 2761 (静态区间第k大,主席树)
查询区间第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大,主席树)的更多相关文章
- POJ 2104 K-th Number (区间第k大)
题意:给定一个序列A,接下来又m个询问,每个询问输出A[L,R]中的第K大.(保证第k大存在) 思路: 我想拿来练习“可持久化线段树”的,搜到这个比较巧的算法也可以解决这个问题,叫“归并树?.大概的思 ...
- HDU2665 求区间第K大 主席树
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- HDU3473--Minimum Sum(静态区间第k大)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- 主席树(静态区间第k大)
前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...
- 可持久化线段树(主席树)——静态区间第k大
主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...
- 计蒜客 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 ...
随机推荐
- 20155225 2006-2007-2 《Java程序设计》第四周学习总结
20155225 2006-2007-2 <Java程序设计>第四周学习总结 教材学习内容总结 对"是一种"语法测试几次之后,总结一句:满足"是一种" ...
- Nginx 虚拟主机 VirtualHost 配置
Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...
- Elastic-Job开发指南
开发指南 代码开发 作业类型 目前提供3种作业类型,分别是Simple,DataFlow和Script. DataFlow类型用于处理数据流,它又提供2种作业类型,分别是ThroughputDataF ...
- java 基础类库之 SysFun
/** * 将对象转为字符串:为空,则返回空字符串:而不是"null" * @param pObj 为空,则返回空字符串 * @return */ public static St ...
- EcOS安装
从ubuntu 拷贝到 centos cd /media ls cd ./sf_EcOS 这个目录就是共享目录,名字可能不一样 cp -r studio.zip /home/ 1. 查看版本 cent ...
- ECSHOP中 {insert name='ads' id=$ads_id num=$ads_num}含义
<div class="smallban"> <ul> <!-- TemplateBeginEditabl ...
- TradingView 初识
如引用 TradingView 库,需引入库中 3 个文件(所需库为 github 私有库,需申请) <script type="text/javascript" src=& ...
- UVA - 11995 - I Can Guess the Data Structure! STL 模拟
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- SHOW SLAVE STATUS解读
*************************** 1. row *************************** Slave_IO_State: ...
- Python 中的语句
上一节已经了解到了Python中的基本数据类型和一些基本的操作,本节就大致讲一下关于Python中的语句的相关问题. 我们熟悉的print()语句可能是这样的.print('YJK923') or p ...