傳送門
文章目錄
題意:
思路:
比較明顯的是我們需要將序列從大到小排序,讓后取前kkk個數,再從中選擇第pospospos小的位置輸出當前位置的數即可。
一開始想用setsetset維護,但是setsetset沒有找第kkk小的函數,所以就用主席樹切了。但是題解給了一個黑科技,來記錄一下。
比較全的博客
這個有找第kkk小的函數,所以就變成了一個碼量很小的題了。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std
;
using namespace __gnu_pbds
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=1000010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int n
,m
;
int c
[N
];
PII a
[N
];
int ans
[N
];
struct Node {int k
,pos
,id
;bool operator < (const Node
&W
) const {if(k
!=W
.k
) return k
<W
.k
;else return pos
<W
.pos
;}
}q
[N
];tree
<int, null_type
, less
<int>, rb_tree_tag
, tree_order_statistics_node_update
> pos
;int main()
{
scanf("%d",&n
);for(int i
=1;i
<=n
;i
++) scanf("%d",&a
[i
].X
),a
[i
].X
=-a
[i
].X
,a
[i
].Y
=i
,c
[i
]=-a
[i
].X
;sort(a
+1,a
+1+n
);scanf("%d",&m
);for(int i
=1;i
<=m
;i
++) scanf("%d%d",&q
[i
].k
,&q
[i
].pos
),q
[i
].id
=i
;sort(q
+1,q
+1+m
);int now
=0;for(int i
=1;i
<=m
;i
++) {while(now
<q
[i
].k
) pos
.insert(a
[++now
].Y
);ans
[q
[i
].id
]=c
[*pos
.find_by_order(q
[i
].pos
-1)];}for(int i
=1;i
<=m
;i
++) printf("%d\n",ans
[i
]);return 0;
}
總結
以上是生活随笔為你收集整理的Codeforces Round #602 (Div. 2) D2. Optimal Subsequences stl 黑科技的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。