Feed the dogs
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 21225 | Accepted: 6719 |
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
题目大意给出一串狗的值,每次有一个查询区间,查询该区间内第K个狗的值。
注意题目中给定的条件,每个区间都不完全包括,所以每次查询时删除原有的区间,加入新的区间即可;
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=100005;
int root,s;//root根节点
struct node
{
int l;//左
int r;//右
int val;//值
int ran;//随机的权值
int w;//同样的值的个数
int num;//当前节点下节点的个数
}tree[N];
struct query
{
int x,y,z;
int ans;
int id;
}in[N];
int cmp1(query a,query b)
{
return a.id<b.id;
}
int cmp(query a,query b)
{
return a.x<b.x;
}
void lturn(int &k)//左旋
{
int t=tree[k].r;
tree[k].r=tree[t].l;
tree[t].l=k;
tree[t].num=tree[k].num;
tree[k].num=tree[tree[k].l].num+tree[tree[k].r].num+tree[k].w;
k=t;
}
void rturn(int &k)//右旋
{
int t=tree[k].l;
tree[k].l=tree[t].r;
tree[t].r=k;
tree[t].num=tree[k].num;
tree[k].num=tree[tree[k].l].num+tree[tree[k].r].num+tree[k].w;
k=t;
}
void ins(int &k,int x)//插入
{
if(k==0)
{
s++;
k=s;
tree[k].val=x;
tree[k].ran=rand();
tree[k].num=1;
tree[k].w=1;
return;
}
tree[k].num++;
if(tree[k].val==x)
tree[k].w++;
else if(tree[k].val<x)
{
ins(tree[k].r,x);
if(tree[k].ran>tree[tree[k].r].ran)
lturn(k);
}
else
{
ins(tree[k].l,x);
if(tree[k].ran>tree[tree[k].l].ran)
rturn(k);
}
}
void del(int &k,int x)//删除
{
if(k==0)
return;
if(tree[k].val==x)
{
if(tree[k].w>1)
{
tree[k].w--;
tree[k].num--;
return ;
}
if(tree[k].l*tree[k].r==0)
{
k=tree[k].l+tree[k].r;
}
else if(tree[tree[k].l].ran<tree[tree[k].r].ran)
{
rturn(k);
del(k,x);
}
else
{
lturn(k);
del(k,x);
}
}
else if(x>tree[k].val)
{
tree[k].num--;
del(tree[k].r,x);
}
else
{
tree[k].num--;
del(tree[k].l,x);
}
}
int queryknum(int k,int x)//查询第k大的值,从小到大
{
if(k==0)
return 0;
if(x<=tree[tree[k].l].num)
return queryknum(tree[k].l,x);
else if(x>tree[tree[k].l].num+tree[k].w)
return queryknum(tree[k].r,x-tree[tree[k].l].num-tree[k].w);
else
return tree[k].val;
}
int main()
{
int n,m;
int data[N];
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&data[i]);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&in[i].x,&in[i].y,&in[i].z);
in[i].id=i;
}
sort(in+1,in+m+1,cmp);
root=0;
s=0;
for(int i=in[1].x;i<=in[1].y;i++)
{
ins(root,data[i]);
}
in[1].ans=queryknum(root,in[1].z);
for(int i=2;i<=m;i++)
{
if(in[i].x<=in[i-1].y)//删除多余的值插入新的值
{
for(int j=in[i-1].x;j<in[i].x;j++)
del(root,data[j]);
for(int j=in[i-1].y+1;j<=in[i].y;j++)
ins(root,data[j]);
}
if(in[i].x>in[i-1].y)
{
memset(tree,0,sizeof(tree));
for(int j=in[i].x;j<=in[j].y;j++)
ins(root,data[j]);
}
in[i].ans=queryknum(root,in[i].z);
}
sort(in+1,in+m+1,cmp1);
for(int i=1;i<=m;i++)
printf("%d\n",in[i].ans);
return 0;
}