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, 1≤n,m≤2⋅105. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.
You are given a sequence of integers a=[a1,a2,…,an] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
[11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
[40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1≤k≤n) is given, then the subsequence is called optimal if:
it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b1,b2,…,bk] is lexicographically smaller than the sequence c=[c1,c2,…,ck] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1≤t≤k) such that b1=c1, b2=c2, ..., bt−1=ct−1 and at the same time bt<ct. For example:
[10,20,20] lexicographically less than [10,21,1],
[7,99,99] is lexicographically less than [10,21,1],
[10,21,0] is lexicographically less than [10,21,1].
You are given a sequence of a=[a1,a2,…,an] and m requests, each consisting of two numbers kj and posj (1≤k≤n, 1≤posj≤kj). For each query, print the value that is in the index posj of the optimal subsequence of the given sequence a for k=kj.
For example, if n=4, a=[10,20,30,20], kj=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request kj=2, posj=1 is the number 20, and the answer to the request kj=2, posj=2 is the number 30.
Input
The first line contains an integer n (1≤n≤2⋅105) — the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a1,a2,…,an (1≤ai≤109).
The third line contains an integer m (1≤m≤2⋅105) — the number of requests.
The following m lines contain pairs of integers kj and posj (1≤k≤n, 1≤posj≤kj) — the requests.
Output
Print m integers r1,r2,…,rm (1≤rj≤109) one per line: answers to the requests in the order they appear in the input. The value of rj should be equal to the value contained in the position posj of the optimal subsequence for k=kj.
Examples
input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
output
20
10
20
10
20
10
input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
for k=1: [20],
for k=2: [10,20],
for k=3: [10,20,10].
题意
给你n个数,定义长度为k的理想序列为当前k个数和最大的子序列,且这个子序列的字典序要最小。
然后现在给你q个询问,每次问你长度为ki的理想序列的第pos个数是什么
题解
理想序列的构成,显然是贪心的,每次放最大的字典序最小的数进去。
我们将询问离线之后,难点就变成如何求第k个数是多少,实际上这个就是典型的离线求第k大的题目。。。做法非常多,我才用的是树状数组的二分,这个复杂度是logn^2的,线段树上2分是logn的,这个我就懒得写了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int a[maxn],index[maxn],ans[maxn],sum[maxn];
int n;
int lowbit(int x){
return x&(-x);
}
void update(int x,int val){
while(x <= n){
sum[x] += val;
x += lowbit(x);
}
}
int query(int x){
int s=0;
while(x>0){
s += sum[x];
x -= lowbit(x);
}
return s;
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>a[i];
index[i]=0;
}
set<pair<int,int> >S;
for(int i=1;i<=n;i++){
S.insert(make_pair(-a[i],i));
}
int m;scanf("%d",&m);
vector<pair<pair<int,int>,int>>Q;
for(int i=0;i<m;i++){
int x,y;scanf("%d%d",&x,&y);
Q.push_back(make_pair(make_pair(x,y),i));
}
sort(Q.begin(),Q.end());
int now = 0;
for(int i=0;i<Q.size();i++){
while(now<Q[i].first.first){
now=now+1;
pair<int,int> tmp = *S.begin();
update(tmp.second,1);
index[tmp.second]=1;
S.erase(tmp);
}
int pos = Q[i].first.second;
int l=1,r=n,Ans=n;
while(l<=r){
int mid=(l+r)/2;
if(query(mid)>=pos){
Ans=mid;
r=mid-1;
}else{
l=mid+1;
}
}
ans[Q[i].second]=a[Ans];
}
for(int i=0;i<m;i++){
cout<<ans[i]<<endl;
}
}
int main(){
solve();
}
Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心的更多相关文章
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3
A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学
F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和
E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造
C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心
B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题
A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box
#include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem
//只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...
随机推荐
- JDK环境变量配置遇见的错误以及解决办法
cmd中输入java -version错误信息: An error has occurred while processing the shared archive file.Unable to un ...
- oracle 查询两个字段值相同的记录
select A.* from tb_mend_enrol A, (select A.Typeid, A.address from tb_mend_enrol A group by A.Typeid, ...
- CodeForces-1265E(期望)
题意 有1~n镜子,每个镜子说你漂亮的概率是pi/100,如果第i个回答你漂亮那么就一直问到第n个说漂亮为止,否则重新从1开始问,一天只问一个镜子,问直到镜子n说你漂亮的期望天数. 思路 设Ei为问到 ...
- [阅读笔记]EfficientDet
EfficientDet 文章阅读 Google的网络结构不错,总是会考虑计算性能的问题,从mobilenet v1到mobile net v2.这篇文章主要对近来的FPN结构进行了改进,实现了一种效 ...
- 浅谈python中selenium库调动webdriver驱动浏览器的实现原理
最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...
- 设置fiddler抓取安卓手机的包
1.在手机端设置代理,IP地址为fiddler所在电脑IP,端口默认8888 2.在fiddler上打开工具-设置-连接,勾选允许远程计算机连接,点击确定 3.安装证:手机浏览器输入 http://( ...
- 压测 swoole_websocket_server 性能
概述 这是关于 Swoole 入门学习的第十篇文章:压测 swoole_websocket_server 性能. 第九篇:Swoole Redis 连接池的实现 第八篇:Swoole MySQL 连接 ...
- jenkins构建,拉取不到最新版本代码,报clock of the subversion server appears to be out of sync
一.问题描述 今天遇到个问题,我这边提交了代码后,一般会马上去jenkins上点一下,构建到开发环境上. 但是发现修改没生效,后来发现,提交的版本假设是3250,但是jenkins构建使用的版本为32 ...
- (转)RocketMQ工作原理
原文:https://blog.csdn.net/lyly4413/article/details/80838716 1.消息中间件的发展: 第一代以ActiveMQ为代表,遵循JMS(java消息服 ...
- 在标准实体特殊消息上注册插件及Dynamics CRM 2015中计算字段的使用
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复157或者20151005可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 前面的 插件系列博客教程 讲述了 ...