D2. Optimal Subsequences (Hard Version) 主席树
题目链接:https://codeforces.com/contest/1262/problem/D2
将数组按大到小排序(相同大小的按下标由小到大排序),依次将排序后的每个数在原数组中的位置放入主席树。
对于每个询问的k,pos
输出原数组中下标为query(T[0],T[k],1,len,pos)所对应的数字即可
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 200005
#define ll long long
int T[maxn*],L[maxn*],R[maxn*],sum[maxn*],tot;
ll b[maxn];
struct node{
int pos,num;
bool operator <(const node &w)const{
if(num==w.num)return pos<w.pos;
return num>w.num;
}
}a[maxn];
inline int update(int pre,int l,int r,int x)
{
int rt=++tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre]+;
if(l<r)
{
int mid=l+r>>;
if(x<=mid)L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+,r,x);
}
return rt;
}
inline int query(int u,int v,int l,int r,int k)
{
if(l>=r)return l;
int x=sum[L[v]]-sum[L[u]],mid=l+r>>;
if(x>=k)return query(L[u],L[v],l,mid,k);
else return query(R[u],R[v],mid+,r,k-x);
}
int main()
{
int n,m;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].pos=i;
}
sort(a+,a++n);
int len=n;
tot=;
for(int i=;i<=n;i++)
{
b[a[i].pos]=a[i].num;
T[i]=update(T[i-],,len,a[i].pos);
}
int k,p;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&k,&p);
printf("%d\n",b[query(T[],T[k],,len,p)]);
}
return ;
}
D2. Optimal Subsequences (Hard Version) 主席树的更多相关文章
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) - D2. Optimal Subsequences (Hard Version)(主席树)
题意:一共有$n$个数,$m$次询问,每次询问包括$k.pos$两个数,需要你从这$n$个数里面找出$k$个数,使得他们的总和最大,如果有多种情况,找出序号字典序最小的一组,然后输出这个序列中第$po ...
- 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, ...
- Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)
题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字. 题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个 ...
- Optimal Subsequences(主席树)
题意: 给定一个序列,长度为n(<=2e5),有m(<=2e5)个询问,每个询问包含k和pos,要从原序列中选出一个长度为k的子序列,要求是这个序列的和是所有长度为k的序列中最大的,且是字 ...
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- 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 ...
- HDU 5919 -- Sequence II (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
随机推荐
- 移动端遇到的bug (长期更新)
移动端遇到的bug border-radius和transform在一起的bug 当父级设置了border-radius+overflow:hidden的时候,圆角是可以包住子级的,这是个很常见的场景 ...
- H3C配置Hybrid端口
- js New一个函数和直接调用的区别
使用New是构造函数,不使用New是函数调用,同时this指向不同. 示例: function Test(name, age, job) { console.log(this); this.name ...
- IE显示 “Promise”未定义,vue项目兼容ie的两种方案
第一种方法: 直接在html中加入js链接: <script src = "https://cdn.polyfill.io/v2/polyfill.min.js">&l ...
- Linux 内核VLB 总线
另一个对 ISA 的扩展是 VESA Local Bus(VLB) 接口总线, 它扩展了 ISA 连接器, 通过 添加第 3 个知道长度的槽位. 一个设备可只插入这个额外的连接器(不用插入 2 个关联 ...
- 2019 ICPC 陕西西安邀请赛 D. Miku and Generals
传送门:https://nanti.jisuanke.com/t/39271 题意: 给你n个人,每个人有一个权值 a_i ,(a_i是可以被100整除的))现在需要你将n个人分成两组,有m个关系 ...
- 学习linux命令,看这篇2w多字的linux命令详解
用心分享,共同成长 没有什么比每天进步一点点更重要了 本文已收录到我的github:https://github.com/midou-tech/articles/tree/master/docs/li ...
- 【一起学源码-微服务】Nexflix Eureka 源码七:通过单元测试来Debug Eureka注册过程
前言 上一讲eureka client是如何注册的,一直跟到源码发送http请求为止,当时看eureka client注册时如此费尽,光是找一个regiter的地方就找了半天,那么client端发送了 ...
- Redis安装(单机及各类集群,阿里云)
Redis安装(单机及各类集群,阿里云) 前言 上周,我朋友突然悄悄咪咪地指着手机上的一篇博客说,这是你的博客吧.我看了一眼,是之前发布的<Rabbit安装(单机及集群,阿里云>.我朋友很 ...
- Selenium3+Python3环境搭建
安装python 官网直接下载当前最新版的python Python官网地址:https://www.python.org/ 百度搜索Python3.6安装步骤 Python安装步骤:自行百度,暂不提 ...