HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁
思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的。
具体可以看q学姐的B站视频
代码:
#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int n, q, tot;
int a[maxn], root[maxn];
vector<int> st;
int getId(int x){
return lower_bound(st.begin(), st.end(),x) - st.begin() + ;
}
struct node{
int lson, rson;
int sum;
}T[maxn * ];
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(m >= pos)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
int query(int l, int r, int pre, int now, int k){
if(l == r) return l;
int m = (l + r) >> ;
int sum = T[T[now].lson].sum - T[T[pre].lson].sum;
if(sum >= k)
return query(l, m, T[pre].lson, T[now].lson, k);
else
return query(m + , r, T[pre].rson, T[now].rson, k - sum);
}
int main(){
int t;
scanf("%d", &t);
while(t--){
tot = ;
scanf("%d%d", &n, &q);
st.clear();
for(int i = ; i <= n; i++)
scanf("%d", &a[i]), st.push_back(a[i]);
sort(st.begin(), st.end());
st.erase(unique(st.begin(), st.end()), st.end()); for(int i = ; i <= n; i++)
update(, n, root[i], root[i - ], , getId(a[i]));
while(q--){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", st[query(, n, root[l - ], root[r], k) - ]);
}
}
return ;
}
HDU 2665 Kth number(主席树静态区间第K大)题解的更多相关文章
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- POJ2104-- K-th Number(主席树静态区间第k大)
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- hdu 2665 Kth number 主席树
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- HDU3727--Jewel (主席树 静态区间第k大)
Jewel Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- [poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...
- K-th Number 线段树的区间第K大
http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...
- poj 2104 主席树(区间第k大)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44940 Accepted: 14946 Ca ...
随机推荐
- iview 表单非空验证
rules: { title: [ {required: true, message: '请填写栏目名称', trigger: 'blur'} ], desc: [ {required: true, ...
- layui 将后台传过来的值等价替换
<th lay-data="{field:'opentime',width:'12%' , sort: true, align:'center',templet:'#roleTpl'} ...
- [ionic3.x开发记录]ios下页面过渡效果不出现的小坑
如果内容没有被<ion-content></ion-content>或者<ion-header></ion-header>标签包裹,页面过渡的时候是没有 ...
- jeecg中excel导出字段判空处理
我们清楚,jeecg 导出 excel 采用的是 easypoi,不知道是否遇到过这种情况: 我们以一个实体属性为例: @Excel(name="问题分类",dicCode=&qu ...
- abap test seam 和 TEST-INJECTION
TEST-SEAM 和 TEST-INJECTION 一块儿使用 可以模拟出调用方法的return,exporting,chaning值. 例如: 1: 假设有一个类zcl_demo_input,该 ...
- NodeMan架构
在上一篇文章中我们介绍了NodeMan的基本概念,介绍了这是一套利用NodeJs框架来管理Ubuntu服务器的解决方案,接下来我们会继续介绍关于这样一套解决方案更多细节的内容. 后台: 使用node作 ...
- c# 设置开机启动
private static RegistryKey _rlocal = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Wi ...
- windows----------如何禁用PC端微信的开机启动
1.开始菜单--->运行--->输入msconfig 2.如下图,然后点击启动 3.打开任务管理器 4.右键wechat,然后禁用.
- [BZ4923][Lydsy1706月赛]K小值查询
K小值查询 题面 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. Input ...
- extjs +String2 +Spring 下的分页 以及返回json格式错误的问题
首先,分页 很简单. 1前台extjs数据源, var shipMgrStore = Ext.create('Ext.data.Store', { model: 'App.ShipMgr.model ...