Optimal Subsequences(主席树)
题意:
给定一个序列,长度为n(<=2e5),有m(<=2e5)个询问,每个询问包含k和pos,要从原序列中选出一个长度为k的子序列,要求是这个序列的和是所有长度为k的序列中最大的,且是字典序最小的,要求输出子序列中第pos个元素。
思路:
显然要是长度为k的序列中和最大,必须要优先选大的数。显然必须全部选的数不需要考虑位置,部分选的数(必定是最小的数)肯定优先选择坐标小的,因此可以将所有数按数值从大到小排,然后坐标从小到达排,前k的数就是每次要选的数。而第pos个元素就是排好的数前k个中坐标值第pos小的,用主席树维护坐标值就好了。
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const int Log=40;
int num[maxn*Log],lson[maxn*Log],rson[maxn*Log];
int root[maxn];
int tot;
int build(int l,int r){
int pos=++tot;
if(l<r){
int mid=(l+r)>>1;
lson[pos]=build(l,mid);
rson[pos]=build(mid+1,r);
}
return pos;
}
int update(int rt,int l,int r,int p){
int pos=++tot;
num[pos]=num[rt]+1;
lson[pos]=lson[rt];
rson[pos]=rson[rt];
if(l<r){
int mid=(l+r)>>1;
if(p<=mid) lson[pos]=update(lson[rt],l,mid,p);
else rson[pos]=update(rson[rt],mid+1,r,p);
}
return pos;
}
int query(int Old,int New,int l,int r,int k){
if(l==r)return l;
int mid=(l+r)>>1;
int x=num[lson[New]]-num[lson[Old]];
if(x>=k) return query(lson[Old],lson[New],l,mid,k);
else return query(rson[Old],rson[New],mid+1,r,k-x);
}
struct node{
int v,pos;
}N[maxn];
bool cmp(node a,node b){
return a.v==b.v?a.pos<b.pos:a.v>b.v;
}
int a[maxn];
int main(){
int n,q;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&N[i].v);
N[i].pos=i;
a[i]=N[i].v;
}
sort(N+1,N+1+n,cmp);
for(int i=1;i<=n;i++)
root[i]=update(root[i-1],1,n,N[i].pos);
root[0] = build(1, n);
cin>>q;
while(q--){
int k,pos;scanf("%d%d",&k,&pos);
int p=query(root[0],root[k],1,n,pos);
printf("%d\n",a[p]);
}
}
Optimal Subsequences(主席树)的更多相关文章
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- D2. Optimal Subsequences (Hard Version) 主席树
题目链接:https://codeforces.com/contest/1262/problem/D2 将数组按大到小排序(相同大小的按下标由小到大排序),依次将排序后的每个数在原数组中的位置放入主席 ...
- CodeForces - 597C:Subsequences (主席树+DP)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- CF-Technocup3 D Optimal Subsequences
D Optimal Subsequences http://codeforces.com/contest/1227/problem/D2 显然,每次求的k一定是这个序列从大到小排序后前k大的元素. 考 ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
E. Sign on Fence Bizon the Champion has recently finished painting his wood fence. The fence consi ...
- CodeForces960F:Pathwalks (主席树+DP)
You are given a directed graph with n nodes and m edges, with all edges having a certain weight. The ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心
D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...
- bzoj3207--Hash+主席树
题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
随机推荐
- 《剑指offer》面试题20 顺时针打印矩阵 Java版
我的方法:遇到这种题最好在纸上画一画打印路线.我利用了4个标志left.top.right.bottom,表示当前需要打印的左界.上届.右界和下界,换句话说这些界线之外的已经打印了,如此一来判断结束的 ...
- Balanced Lineup poj3264 线段树
Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...
- P1455 搭配购买 (并查集+01背包)
[题目描述] 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵 ...
- 深入理解js闭包【写的通俗易懂,很值的阅读】
详细内容在下面这个链接里面: https://www.cnblogs.com/uedt/archive/2010/10/28/1863389.html 能写出这样的文章,定是大佬!
- 06-File-文件
文件 长久保存信息的一种数据信息集合 常用操作 打开关闭(文件一旦打开,需要关闭操作) 读写内容 查找 open函数 open函数负责打开文件,带有很多参数 第一个参数: 必须有,文件的路径和名称 m ...
- 从后台看python--为什么说python是慢的
python越来越作为一种科学技术研究的语言越来越流行,可是我们经常听到一个问题,python是慢的.那么我们从后台分析一下,为什么python是慢的. python是一种动态类型,解释型语言,它的值 ...
- python 中PIL.Image和OpenCV图像格式相互转换
PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane ...
- linux为 rsync 添加开机启动
[root@rsync-server-1 /]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local [root@rsync ...
- RocketMQ集群部署安装
RcoketMQ:[ 1.低延时:在高压下,1毫秒内超过99.6%的反应延迟. 2.面向金融:具有跟踪和审计功能的高可用性. 3.行业可持续发展:保证了万亿级的消息容量. 4.厂商中立:一个新的开放的 ...
- K8S进入容器方法
前言 k8s如何进入一个pod里有多个容器的方法 参考地址 https://blog.csdn.net/aa1215018028/article/details/81205691 方法1 kubect ...