传送门:CodeForces-1262D
思路分析
将数组按权值和ID排序
题意就可以转化为取前$k$个数,在原数组上第$pos$个数是哪个
将询问按k从小到大排序,前k个一定是一样的
将前k个数的ID放进树状数组里,二分查找到第一个前缀和等于$pos$的位置就是答案
还有一个黑科技,用红黑树维护Kth值
样例输入
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
样例输出
20
10
20
10
20
10
AC代码
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
int a[N],bit[N],ans[N];
rbtree Tree;
struct date {
int x,id;
} t[N];
struct node {
int k,pos,id;
} q[N];
void add(int x) {
for(int i=x; i<N; i+=lowbit(i)) bit[i]+=1;
}
int query(int x) {
int sum=0;
for(int i=x; i; i-=lowbit(i)) sum+=bit[i];
return sum;
}
int cmp1(date x,date y) {
if(x.x==y.x) return x.id<y.id;
else return x.x>y.x;
}
int cmp2(node x,node y) {
return x.k<y.k;
}
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int n;
read(n);
for(int i=1; i<=n; i++) {
int x;
read(x);
a[i]=x;
t[i].x=x;
t[i].id=i;
}
int m;
read(m);
for(int i=1; i<=m; i++) {
read(q[i].k,q[i].pos);
q[i].id=i;
}
sort(t+1,t+1+n,cmp1);
sort(q+1,q+1+m,cmp2);
int now=1;
for(int i=1; i<=m; i++) {
//名次树
while(now<=q[i].k) Tree.insert(t[now++].id);
int res=*Tree.find_by_order(q[i].pos-1);
ans[q[i].id]=a[res];
//二分树状数组
while(now<=q[i].k) add(t[now++].id);
int l=1,r=n,res=0;
while(r>=l){
int mid=l+r>>1;
if(query(mid)>=q[i].pos) res=mid,r=mid-1;
else l=mid+1;
}
ans[q[i].id]=a[res];
}
for(int i=1; i<=m; i++) cout<<ans[i]<<endl;
return 0;
}