题意:

给定一个序列,长度为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. vue数据响应式的一些注意点

    有关对象属性值不触发视图更新的情况: Vue 不能检测到对象属性的添加或删除,由于 Vue 会在初始化实例时对属性执行 getter/setter 转化过程,所以属性必须在 data 对象上存在才能让 ...

  2. FFmpeg从入门到出家(FLV文件结构解析)

    FLV(FLASH VIDEO),是一种常用的文件封装格式,目前国内外大部分视频分享网站都是采用的这种格式.其标准定义为<Adobe Flash Video File Format Specif ...

  3. nativescript(angular2)——ListView组件

    NativeScript是一个不使用webview的情况下构建跨平台并且原生的iOS和Android应用.使用Angular.TypeScript或JavaScript来获得原生UI和性能体验,同时可 ...

  4. Spark集成Kafka实时流计算Java案例

    package com.test; import java.util.*; import org.apache.spark.SparkConf; import org.apache.spark.Tas ...

  5. Perl脚本通过Expect登陆多台设备批量执行命令并Log

    本例子尝试使用Perl脚本借助Expect模块实现如下目的: 登陆多台设备 设备登陆信息按如下格式存放于文件中. $ cat hosts.txt 192.168.30.7:node1:telnet:b ...

  6. CSS3选择器 ::selection选择器

    “::selection”伪元素是用来匹配突出显示的文本(用鼠标选择文本时的文本).浏览器默认情况下,用鼠标选择网页文本是以“深蓝的背景,白色的字体”显示的,效果如下图所示: 从上图中可以看出,用鼠标 ...

  7. thinkphp 系统变量

    一.查看可用变量 dump($_SERVER); 可以直接在html输出系统变量的值 <p>{$Think.server.HTTP_HOST}</p>. 二.环境变量 1.查看 ...

  8. LOJ2540「PKUWC2018」随机算法

    又是一道被咕了很久的题 貌似从WC2019之前咕到了现在 我们用f[i][s]表示现在最大独立集的大小为i 不可选集合为s 然后转移O(n)枚举加进来的点就比较简单啦 这个的复杂度是O(2^n*n^2 ...

  9. 00.斐波那契数列第n项

    # 斐波那契数列第n项 # 1 1 2 3 5 8 def fib(n): if n <= 2: return 1 else: return fib(n-2)+fib(n-1) def fib2 ...

  10. 对微信小程序的研究2

    .json 后缀的 JSON 配置文件 .wxml 后缀的 WXML 模板文件 .wxss 后缀的 WXSS 样式文件 .js 后缀的 JS 脚本逻辑文件 JSON 配置 我们可以看到在项目的根目录有 ...