题意:

给定一个序列,长度为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(主席树)的更多相关文章

  1. codeforces 1262D Optimal Subsequences 主席树询问第k小

    题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...

  2. D2. Optimal Subsequences (Hard Version) 主席树

    题目链接:https://codeforces.com/contest/1262/problem/D2 将数组按大到小排序(相同大小的按下标由小到大排序),依次将排序后的每个数在原数组中的位置放入主席 ...

  3. CodeForces - 597C:Subsequences (主席树+DP)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  4. CF-Technocup3 D Optimal Subsequences

    D Optimal Subsequences http://codeforces.com/contest/1227/problem/D2 显然,每次求的k一定是这个序列从大到小排序后前k大的元素. 考 ...

  5. 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 ...

  6. CodeForces960F:Pathwalks (主席树+DP)

    You are given a directed graph with n nodes and m edges, with all edges having a certain weight. The ...

  7. 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, ...

  8. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  9. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

随机推荐

  1. P2835 刻录光盘 (tarjan缩点)

    [题目描述] 现在假设总共有N个营员(2<=N<=200),每个营员的编号为1~N.LHC给每个人发了一张调查表,让每个营员填上自己愿意让哪些人到他那儿拷贝资料.当然,如果A愿意把资料拷贝 ...

  2. Python学习第四十天函数的装饰器用法

    在软件开发的过程中,要遵循软件的一些原则封装的,不改变原有的代码的基础增加一些需求,python提供了装饰器来扩展函数功能,下面说说函数装饰器用法 def debug(func):      def ...

  3. 让鼠标滚轮在WINDOWS系统中也和MAC一样滚动(WIN系统鼠标滚轮反向)

    用习惯MAC系统偶尔要用用WINDOWS系统,或者由于各种原因总之时不时需要由MAC转向WIN系统或同时需要两个平台机器上倒腾来倒腾去的同学,一般来说最痛苦的就是鼠标滚轮方向不一致. 基本上我们知道苹 ...

  4. webpack打包发布项目

    1.打包 :npm run build 2.发布: 1)使用静态服务器工具包 npm install -g serve serve dist 访问:http://www.localhost.5000 ...

  5. node中使用log4js4.x版本记录

    const log4js = require('log4js'); log4js.configure({ appenders: { logFile: { //文件形式打印日志 type: " ...

  6. Nginx安装与配置-Centos7

    Nginx是一款高性能免费开源网页服务器,也可用于反向代理和负载均衡服务器.该软件由伊戈尔·赛索耶夫于2004年发布,2019年3月11日,Nginx被F5 Networks以6.7亿美元收购.201 ...

  7. 使用BaGet来管理内部Nuget包

    有的时候,我们想要制作一些nuget包,供自己内部调用,不想公开出去,那么就需要一个能够集中管理nuget包的服务了,今天我来给大家介绍一款轻便好用的包管理服务-----BaGet 下载并部署BaGe ...

  8. flask之创建项目

    一:安装flask workon flask_project 进入虚拟后安装flask pip install flask==0.10.1 安装指定的版本 进入虚拟环境的目录,查看创建的所有的虚拟环境 ...

  9. pandas.DataFrame.drop_duplicates 用法说明

    DataFrame.drop_duplicates(subset=None, keep='first', inplace=False) subset考虑重复发生在哪一列,默认考虑所有列,就是在任何一列 ...

  10. OSS重磅推出OSS Select——使用SQL选取文件的内容

    对象存储OSS(Object Storage Service)具有海量.可靠.安全.高性能.低成本的特点.OSS提供标准.低频.归档类型,覆盖多种数据从热到冷的存储需求,单个文件的大小从1字节到48. ...