Description
Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
The first line contains n and m, indicates the number of dogs and the number of feedings.
The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.
Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.
You can assume that n<100001 and m<50001.
The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.
Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.
You can assume that n<100001 and m<50001.
Output
Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.
Sample Input
7 2 1 5 2 6 3 7 4 1 5 3 2 7 1
Sample Output
3 2
Source
POJ Monthly--2006.02.26,zgl & twb
最开始看到这个题,我想到了poj2823Sliding Window那个求滚动区间最值:线段树以及单调队列的做法,然而这两种情况只能求最值,区间第k小,联想大名鼎鼎的
poj2104&&hdu2665kth number【主席树入门题+讲解】
poj2104K-th Number【划分树入门题】
,很容易想到划分树以及他的替代品主席树==几乎就是裸的模板题,然而avl的做法暂时没想到,待我ak了dp 图论回来再说==#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100001;
struct Node
{
int ls,rs,cnt;
}tr[maxn*20];
int cur,rt[maxn];
void init()
{
cur=0;
}
inline void push_up(int o)
{
tr[o].cnt=tr[tr[o].ls].cnt+tr[tr[o].rs].cnt;
}
int build(int l,int r)
{
int k=cur++;
if(l==r)
{
tr[k].cnt=0;
return k;
}
int mid=(l+r)>>1;
tr[k].ls=build(l,mid);
tr[k].rs=build(mid+1,r);
push_up(k);
return k;
}
int update(int o,int l,int r,int pos,int val)
{
int k=cur++;
tr[k]=tr[o];
if(l==pos&&r==pos)
{
tr[k].cnt+=val;
return k;
}
int mid=(l+r)>>1;
if(pos<=mid) tr[k].ls=update(tr[o].ls,l,mid,pos,val);
else tr[k].rs=update(tr[o].rs,mid+1,r,pos,val);
push_up(k);
return k;
}
int query(int l,int r,int o,int v,int kth)
{
if(l==r) return l;
int mid=(l+r)>>1;
int res=tr[tr[v].ls].cnt-tr[tr[o].ls].cnt;
if(kth<=res) return query(l,mid,tr[o].ls,tr[v].ls,kth);
else return query(mid+1,r,tr[o].rs,tr[v].rs,kth-res);
}
int b[maxn],sortb[maxn];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
//freopen("cin.txt","r",stdin);
int n,m,T;
//scanf("%d",&T);
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
sortb[i]=b[i];
}
sort(sortb+1,sortb+1+n);
int cnt=1;
for(int i=2;i<=n;i++)
if(sortb[i]!=sortb[cnt]) sortb[++cnt]=sortb[i];
rt[0]=build(1,cnt);
for(int i=1;i<=n;i++)
{
int p=lower_bound(sortb+1,sortb+1+cnt,b[i])-sortb;
rt[i]=update(rt[i-1],1,cnt,p,1);
}
for(int i=0;i<m;i++)
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
int idx=query(1,cnt,rt[a-1],rt[b],k);
printf("%d\n",sortb[idx]);
}
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
int sorted[MAXN]; //对原集合中元素排序后的值
int val[30][MAXN]; //val记录第k层当前位置的值
int toleft[30][MAXN]; //记录元素所在区间当前位置前的元素进入到左子树的个数
int sum[30][MAXN]; //记录比当前元素小的元素的和
int n;
void build(int l, int r, int d) {
if (l == r) return ;
int mid = (l + r) >> 1;
int same = mid - l + 1;
for (int i=l; i<=r; i++)
if (val[d][i] < sorted[mid])
same--;
int lp = l, rp = mid+1;
for (int i=l; i<=r; i++) {
if (i == l) toleft[d][i] = 0;
else toleft[d][i] = toleft[d][i-1];
if (val[d][i] < sorted[mid]) {
toleft[d][i]++;
val[d+1][lp++] = val[d][i];
} else if (val[d][i] > sorted[mid])
val[d+1][rp++] = val[d][i];
else {
if (same) {
same--;
toleft[d][i]++;
val[d+1][lp++] = val[d][i];
} else val[d+1][rp++] = val[d][i];
}
}
build(l, mid, d+1);
build(mid+1, r, d+1);
}
int query(int a, int b, int k, int l, int r, int d) {
if (a == b) return val[d][a];
int mid = (l + r) >> 1;
int s, ss, sss;
if (a == l) {
s = toleft[d][b];
ss = 0;
} else {
s = toleft[d][b] - toleft[d][a-1];
ss = toleft[d][a-1];
}
if (s >= k) {
a = l + ss;
b = l + ss + s - 1;
return query(a, b, k, l, mid, d+1);
} else {
a = mid+1 + a - l - ss;
b = mid+1 + b - l - toleft[d][b];
return query(a, b, k-s, mid+1, r, d+1);
}
}
int main()
{
// freopen("cin.txt","r",stdin);
int n, m;
scanf("%d %d", &n, &m);
for (int i=1; i<=n; i++) {
scanf("%d", &sorted[i]);
val[0][i] = sorted[i];
}
sort(sorted+1, sorted+1+n);
build(1, n, 0);
int a, b, k;
while (m--) {
scanf("%d%d%d", &a, &b, &k);
printf("%d\n", query(a, b, k, 1, n, 0));
}
return 0;
}