比赛 |
ctime蒟蒻生日赛 |
评测结果 |
WWWWWWTTTTWWWWWWWWWWWWTTTTTWTW |
题目名称 |
K小数 |
最终得分 |
0 |
用户昵称 |
Samle |
运行时间 |
19.093 s |
代码语言 |
C++ |
内存使用 |
2.24 MiB |
提交时间 |
2017-10-17 21:38:33 |
显示代码纯文本
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define R register
#define N 100005
#define inf 707406378
#define mod 1000000007
inline void in(int &x) {
static int ch; static bool flag;
for(flag = false,ch = getchar();ch < '0'||ch > '9';ch = getchar()) flag |= ch == '-';
for(x = 0;isdigit(ch);ch = getchar()) x = (x<<1) + (x<<3) + ch - '0';
x = flag ? -x : x;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
int n,m;
int a[N],b[N],c[N];
int size,part[N];
struct node{int l,r,k,id,ans;}q[5005];
inline bool cmp1(node x,node y){
if(part[x.l]!=part[y.l])return part[x.l]<part[y.l];
if(part[x.r]!=part[y.r])return part[x.r]<part[y.r];
return x.k<y.k;
}
inline bool cmp2(node x,node y){return x.id<y.id;}
int f[N];
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int d){
for(R int i=x;x<=n;x+=lowbit(x))f[x]+=d;
}
inline int query(int x){
R int sum=0;
for(R int i=x;i;i-=lowbit(x))sum+=f[x];
return sum;
}
int l=1,r;
inline int erfen(int k){
R int mid,ll=1,rr=n;
while(ll<=rr){
mid=ll+rr>>1;
if(query(mid)>=k)rr=mid-1;
else ll=mid+1;
}
return ll;
}
inline void update(int pos,int val){
add(c[pos],val);
}
inline int dy(){
freopen("kthnumber.in","r",stdin);
freopen("kthnumber.out","w",stdout);
in(n),in(m);
size=sqrt(n);
for(R int i=1;i<=n;++i){
in(a[i]),b[i]=a[i];
part[i]=(i-1)/size+1;
}
sort(b+1,b+1+n);
for(R int i=1;i<=n;++i)c[i]=lower_bound(b+1,b+1+n,a[i])-b;
for(R int i=1;i<=n;++i)b[c[i]]=a[i];
for(R int i=1;i<=m;++i){
in(q[i].l),in(q[i].r),in(q[i].k);
q[i].id=i;
}
sort(q+1,q+1+m,cmp1);
/* cout<<"a ";for(int i=1;i<=n;++i)cout<<a[i]<<' ';cout<<endl;
cout<<"b ";for(int i=1;i<=n;++i)cout<<b[i]<<' ';cout<<endl;
cout<<"c ";for(int i=1;i<=n;++i)cout<<c[i]<<' ';cout<<endl;
cout<<"part ";for(int i=1;i<=n;++i)cout<<part[i]<<' ';cout<<endl;
cout<<"f ";for(int i=1;i<=n;++i)cout<<f[i]<<' ';cout<<endl;
cout<<"q is "<<endl;
for(int i=1;i<=m;++i)cout<<q[i].l<<' '<<q[i].r<<' '<<q[i].k<<' '<<q[i].id<<' '<<q[i].ans<<endl;*/
for(R int i=1;i<=m;++i){
while(l<q[i].l)update(l++,-1);
while(l>q[i].l)update(--l,1);
while(r<q[i].r)update(++r,1);
while(r>q[i].r)update(r--,-1);
// cout<<"will come to get the id"<<endl;
R int id=erfen(q[i].k);
// id++;
// cout<<"get the is "<<id<<endl;
q[i].ans=b[id];
}
sort(q+1,q+1+m,cmp2);
for(R int i=1;i<=m;++i,putchar('\n'))write(q[i].ans);
exit(0);
}
int QAQ = dy();
int main(){;}