题目链接

因为这道题没有删除修改之类的,所以很多人会用离散化之后的线段树来做,但是实际上(可能是我懒得去做离散化这个操作了),然后就是直接写可持久化线段树,区间的长度就是int的从最小到最大的长度,然后记录的是size,我们最后直接返回的是对应的位置即可。


 #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + , _UP = , _DOWN = -;
int N, M, root[maxN], tot, siz[ * maxN], lc[ * maxN], rc[ * maxN];
inline void insert(int &rt, int old, ll l, ll r, int qx)
{
rt = ++tot;
siz[rt] = siz[old] + ; lc[rt] = lc[old]; rc[rt] = rc[old];
if(l == r) return;
ll mid = HalF;
if(qx <= mid) insert(lc[rt], lc[old], l, mid, qx);
else insert(rc[rt], rc[old], mid + , r, qx);
}
inline ll query(int now, int old, ll l, ll r, int k)
{
if(l == r) return l;
ll mid = HalF;
if(k <= siz[lc[now]] - siz[lc[old]]) return query(lc[now], lc[old], l, mid, k);
else return query(rc[now], rc[old], mid + , r, k - siz[lc[now]] + siz[lc[old]]);
}
inline void init()
{
for(int i=; i<=tot; i++) root[i] = ;
tot = ;
}
int main()
{
while(scanf("%d%d", &N, &M) != EOF)
{
init();
for(int i=, val; i<=N; i++)
{
scanf("%d", &val);
insert(root[i], root[i - ], _DOWN, _UP, val);
}
int l, r, k;
while(M--)
{
scanf("%d%d%d", &l, &r, &k);
printf("%lld\n", query(root[r], root[l-], _DOWN, _UP, k));
}
}
return ;
}
05-29 00:26