#include #include #include #include using namespace std; //1-indexed #include template struct BIT{ int n; vectorbit; BIT(int n_=0):n(n_),bit(n_+1){} T sum(int i) { T ans=0; for(;i>0;i-=i&-i)ans+=bit[i]; return ans; } void add(int i,T a) { if(i==0)return; for(;i<=n;i+=i&-i)bit[i]+=a; } int lower_bound(T k)//k<=sum(ret) { if(k<=0)return 0; int ret=0,i=1; while((i<<1)<=n)i<<=1; for(;i;i>>=1) if(ret+i<=n&&bit[ret+i] >Q; vector >Ai; map,int>mid; main() { cin>>N; for(int i=0;i>A[i]; for(int i=0;iL(Q.size(),-1e9-1),R(Q.size(),1e9); sort(Ai.begin(),Ai.end()); for(int c=0;c<35;c++) { vector >M; for(int i=0;iP(N); for(int i=0;i<=Ai.size();i++) { long T=i=(r-l+1)/2; if(flag)R[I]=M[id].first; else L[I]=M[id].first; id++; } if(iL(N/K),R(N/K); for(int i=0;i+K<=N;i+=K) { L[i/K]=mid[make_pair(i,i+K)]; R[i/K]=mid[make_pair(N-i-K,N-i)]; } for(int i=1;i