区间第k大裸题,开始的错误代码竟然A了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
//#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("
----------
")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 200007;
int treeIndex;
int sum[N<<5], T[N], L[N<<5], R[N<<5];
inline void Build(int &rt, int l, int r){
rt = ++treeIndex;
if(l == r) return;
int mid = (l + r) >> 1;
Build(L[rt], l, mid), Build(R[rt], mid + 1, r);
}
inline void Updata(int &root, int rt, int l, int r, int x){
root = ++treeIndex;
L[root] = L[rt], R[root] = R[rt];
sum[root] = sum[rt] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(x <= mid)
Updata(L[root], L[rt], l, mid, x);
else
Updata(R[root], R[rt], mid + 1, r, x);
}
inline int Query(int u, int v, int l, int r, int K){
if(l == r) return l;
int mid = (l + r) >> 1, x = sum[L[v]] - sum[L[u]];
if(x >= K)
return Query(L[u], L[v], l, mid, K);
else
return Query(R[u], R[v], mid + 1, r, K - x);
}
int a[N], b[N];
int main(){
//FileOpen();
int n, Ques;
io >> n >> Ques;
R(i,1,n){
io >> a[i];
b[i] = a[i];
}
sort(b + 1, b + n + 1);
int m = unique(b + 1, b + n + 1) - b - 1;
Build(T[0], 1, m);
R(i,1,n){
int pos = lower_bound(b + 1, b + m + 1, a[i]) - b;
Updata(T[i], T[i - 1], 1, m, pos);
}
while(Ques--){
int l, r, K;
io >> l >> r >> K;
printf("%d
", b[Query(T[l-1], T[r], 1, m, K)]);
}
return 0;
}