POJ 2761 Feed the dogs

时间:2023-03-09 16:47:17
POJ 2761 Feed the dogs

主席树,区间第$k$大。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int a[maxn],b[maxn],root[maxn],n,m,sz;
struct Node {int l,r,sum;} s[maxn*]; int get(int x)
{
int L=,R=n,res;
while(L<=R)
{
int mid=(L+R)/;
if(b[mid]>x) R=mid-;
else if(b[mid]<x) L=mid+;
else res=mid,R=mid-;
}
return res;
} void update(int l,int r,int &x,int y,int p)
{
sz++; s[sz]=s[y]; s[sz].sum++; x=sz;
if(l==r) return;
int mid=(l+r)/;
if(p<=mid) update(l,mid,s[sz].l,s[y].l,p);
else update(mid+,r,s[sz].r,s[y].r,p);
} int quary(int l,int r,int x,int y,int p)
{
if(l==r) return l;
int mid=(l+r)/;
int tmp=s[s[y].l].sum-s[s[x].l].sum;
if(tmp>=p) return quary(l,mid,s[x].l,s[y].l,p);
else return quary(mid+,r,s[x].r,s[y].r,p-tmp);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n); for(int i=;i<=n;i++) a[i]=get(a[i]);
for(int i=;i<=n;i++) update(,n,root[i],root[i-],a[i]);
for(int i=;i<=m;i++)
{
int L,R,k; scanf("%d%d%d",&L,&R,&k);
printf("%d\n",b[quary(,n,root[L-],root[R],k)]);
}
return ;
}