Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 11808 | Accepted: 3409 |
Description
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
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
Sample Input
7 2 1 5 2 6 3 7 4 1 5 3 2 7 1
Sample Output
3 2
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <queue>
#define N 100013
using namespace std;
struct node
{
int l,r,s,key;
void init()
{
l=r=key=0;
s=1;
}
};
node sbt[N];
int nu;
void right_rotate(int &t)
{
int k=sbt[t].l;
sbt[t].l=sbt[k].r;
sbt[k].r=t;
sbt[k].s=sbt[t].s;
sbt[t].s=sbt[sbt[t].l].s+sbt[sbt[t].r].s+1;
t=k;
}
void left_rotate(int &t)
{
int k=sbt[t].r;
sbt[t].r=sbt[k].l;
sbt[k].l=t; //开始这里错了
sbt[k].s=sbt[t].s;
sbt[t].s=sbt[sbt[t].l].s+sbt[sbt[t].r].s+1;
t=k;
}
void Maintain(int &t,bool flag)
{
if(flag==false)
{
if(sbt[sbt[sbt[t].l].l].s>sbt[sbt[t].r].s)
right_rotate(t);
else if(sbt[sbt[sbt[t].l].r].s>sbt[sbt[t].r].s)
left_rotate(sbt[t].l),right_rotate(t);
else return ;
}
else
{
if(sbt[sbt[sbt[t].r].r].s>sbt[sbt[t].l].s)
left_rotate(t);
else if(sbt[sbt[sbt[t].r].l].s>sbt[sbt[t].l].s)
right_rotate(sbt[t].r),left_rotate(t);//该死的复制粘贴、这里又出了问题
else return ;
}
Maintain(sbt[t].l,false);
Maintain(sbt[t].r,true);
Maintain(t,false);
Maintain(t,true);
}
void insert(int &t,int &v)
{
if(t==0)
{
t=++nu;
sbt[t].init();
sbt[t].key=v;
return ;
}
sbt[t].s++;
if(v<=sbt[t].key)
insert(sbt[t].l,v);
else
insert(sbt[t].r,v);
Maintain(t,v>sbt[t].key);
return ;
}
int del(int &t,int v)
{
if(t==0) return 0;//开始没写这个、出现负数了都、、
sbt[t].s--;
if(v==sbt[t].key||(v>sbt[t].key&&!sbt[t].r)||(v<sbt[t].key&&!sbt[t].l))
{
if(!sbt[t].l||!sbt[t].r)//把问题都搞定后、这里少了个 '!',然后华丽丽的WA了
{ int k=sbt[t].key;
t=sbt[t].r+sbt[t].l;
return k;
}
else
{
return sbt[t].key=del(sbt[t].l,v+1);
}
}
if(v>sbt[t].key)
return del(sbt[t].r,v);
return del(sbt[t].l,v);
}
int getkth(int &t,int k)
{
if(k==sbt[sbt[t].l].s+1)
return sbt[t].key;
if(k<sbt[sbt[t].l].s+1)
return getkth(sbt[t].l,k);
return getkth(sbt[t].r,k-sbt[sbt[t].l].s-1);
}
struct inteval
{
int i,j,k;
int index,record;
};
inteval sor_t[50003];
bool cmp1(const inteval&a,const inteval&b)
{
return a.i<b.i;
}
bool cmp2(const inteval&a,const inteval&b)
{
return a.index<b.index;
}
int re[N];
int main()
{
nu=0;
int root=0;
int n,m,i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%d",&re[i]);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&sor_t[i].i,&sor_t[i].j,&sor_t[i].k);
sor_t[i].index=i;
}
sort(sor_t,sor_t+m,cmp1);
for(i=sor_t[0].i;i<=sor_t[0].j;i++)
insert(root,re[i]);
sor_t[0].record=getkth(root,sor_t[0].k);
for(i=1;i<m;i++)
{
if(sor_t[i].i>sor_t[i-1].j)
{
for(j=sor_t[i-1].i;j<=sor_t[i-1].j;j++)
del(root,re[j]);
for(j=sor_t[i].i;j<=sor_t[i].j;j++)
insert(root,re[j]);
sor_t[i].record=getkth(root,sor_t[i].k);
}
else
{
for(j=sor_t[i-1].i;j<sor_t[i].i;j++)//万恶的复制粘贴呀
del(root,re[j]);
for(j=sor_t[i-1].j+1;j<=sor_t[i].j;j++)
insert(root,re[j]);
sor_t[i].record=getkth(root,sor_t[i].k);
}
}
sort(sor_t,sor_t+m,cmp2);
for(i=0;i<m;i++)
printf("%d\n",sor_t[i].record);
return 0;
}