先打上代码以后更新解释

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define RAP(i, n, s) for(int i = n; i >= s; i --)
#define LOW for(; x; x -= x & (-x))
using namespace std;
const int maxn = + ;
const int Maxn = ;
const int maxnode = * maxn;
int s[maxnode], ls[maxnode], rs[maxnode], A[maxn], root[maxn], Ln, Rn, L[maxn], R[maxn], c[maxn];
int n, Q, ms = ;
void update(int x, int& y, int L, int R, int pos, int v){
s[y = ++ ms] = s[x] + v;
if(L == R) return ;
int M = L + R >> ;
ls[y] = ls[x]; rs[y] = rs[x];
if(pos <= M) update(ls[x], ls[y], L, M, pos, v);
else update(rs[x], rs[y], M + , R, pos, v);
return ;
}
void update(int x, int v){
for(int i = x; i <= Maxn; i += i & (-i)) update(c[i], c[i], , Maxn, A[x], -); A[x] = v;
for(int i = x; i <= Maxn; i += i & (-i)) update(c[i], c[i], , Maxn, A[x], ); return ;
}
void init(int x, int tp){
if(!tp) { L[++ Ln] = root[x]; LOW if(c[x]) L[++ Ln] = c[x]; }
else { R[++ Rn] = root[x]; LOW if(c[x]) R[++ Rn] = c[x]; }
return ;
}
int query(int ql, int qr, int k){
Ln = Rn = ; init(qr, ); init(ql - , );//0是左
int ll = , rr = Maxn;
while(ll < rr){
int Lsum = , Rsum = , M = ll + rr >> ;
REP(i, , Ln) Lsum += s[ls[L[i]]];
REP(i, , Rn) Rsum += s[ls[R[i]]];
int kth = Rsum - Lsum;
if(kth >= k){//往左找
REP(i, , Ln) L[i] = ls[L[i]];
REP(i, , Rn) R[i] = ls[R[i]];
rr = M;
}
else{//往右找
REP(i, , Ln) L[i] = rs[L[i]];
REP(i, , Rn) R[i] = rs[R[i]];
ll = M + ; k -= kth; //看好了二分! 别忘了还要减 Σ( ° △ °|||)︴
}
}
return ll;
}
inline void read(int &x){
x = ; int sig = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') sig = -; ch = getchar(); }
while(isdigit(ch)) x = * x + ch - '', ch = getchar();
x *= sig; return ;
}
inline void write(int x){
if(x == ) { putchar(''); return ; }
if(x < ) putchar('-'), x = -x;
int len = , buf[];
while(x) buf[len ++] = x % , x /= ;
RAP(i, len - , ) putchar(buf[i] + ''); return ;
}
void init(){
read(n); read(Q);
REP(i, , n) read(A[i]);
REP(i, , n) update(root[i - ], root[i], , Maxn, A[i], );
return ;
}
void work(){
int tp, ql, qr, k, x, v;
while(Q --){
read(tp);
if(tp) read(ql), read(qr), read(k), write(query(ql, qr, k) - ), putchar('\n');// 减一
else read(x), read(v), update(x, v);
}
return ;
}
void print(){ return ;
}
int main(){
init();
work();
print();
return ;
}

逗比版:

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define t node[d]
#define cnt countn[d]
#define repnode(d) for(int i=1;i<=countn[d];i++)
using namespace std;
const int maxn=+,maxnode=+,Max=;
int ls[maxnode],rs[maxnode],s[maxnode],root[maxn],A[maxn],c[maxn],n,m,ms=,countn[],node[][maxn],sum[];
void update(int x,int& y,int L,int R,int pos,int d){
s[y=++ms]=s[x]+d;
if(L==R) return;
ls[y]=ls[x];rs[y]=rs[x];
int M=L+R>>;
if(pos<=M) update(ls[x],ls[y],L,M,pos,d);
else update(rs[x],rs[y],M+,R,pos,d);
return;
}
void update(int x,int v){
for(int i=x;i<=Max;i+=i&-i) update(c[i],c[i],,Max,A[x],-);A[x]=v;
for(int i=x;i<=Max;i+=i&-i) update(c[i],c[i],,Max,A[x],);return;
}
void init(int x,int d){
t[++cnt]=root[x];
for(;x;x-=x&-x) if(c[x]) t[++cnt]=c[x];
return;
}
int query(int ql,int qr,int k){
countn[]=countn[]=;
init(qr,);init(ql-,);
int L=,R=Max,d;
while(L<R){
sum[]=sum[]=;
int M=L+R>>;
repnode(d=) sum[d]+=s[ls[t[i]]];
repnode(d=) sum[d]+=s[ls[t[i]]];
int kth=sum[]-sum[];
if(k<=kth){
repnode(d=) t[i]=ls[t[i]];
repnode(d=) t[i]=ls[t[i]];
R=M;
}
else{
repnode(d=) t[i]=rs[t[i]];
repnode(d=) t[i]=rs[t[i]];
L=M+;k-=kth;
}
} return L;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-;ch=getchar();}
while(isdigit(ch)) x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;} if(x<) putchar('-'),x=-x;
int len=,buf[]; while(x) buf[len++]=x%,x/=;
for(int i=len-;i>=;i--) putchar(buf[i]+'');return;
}
void init(){
n=read();m=read();
for(int i=;i<=n;i++) A[i]=read();
for(int i=;i<=n;i++) update(root[i-],root[i],,Max,A[i],);
return;
}
void work(){
int tp,i,j,k,x,v;
while(m--){
tp=read();
if(tp){
i=read();j=read();k=read();
write(query(i,j,k)-);putchar('\n');
}
else{
x=read();v=read();
update(x,v);
}
}
return;
}
void print(){
return;
}
int main(){
init();work();print();return ;
}

最新文章

  1. 关于spring boot jar包与war包的问题
  2. ArcGIS Server开发实践之【Search Widget工具查询本地地图服务】
  3. fabric批量操作远程操作主机的练习
  4. 解决错误: java.lang.NoClassDefFoundError: antlr/RecognitionException
  5. BZOJ 1927 星际竞速(最小费用最大流)
  6. javascript原生获取元素的方法对比
  7. bzoj3774
  8. MySQL UNION 与 UNION ALL 语法与用法
  9. careercup-中等难度 17.11
  10. [Effective C++ --006]若不能使用编译器自动生成的函数,就该明确拒绝
  11. html+css3实现网页时钟
  12. c# Unicode字符串的解码
  13. 【iOS】Swift扩展extension和协议protocol
  14. 隐马尔科夫模型HMM(四)维特比算法解码隐藏状态序列
  15. MySQL数据库简识
  16. 关于VB里判断逻辑的说明
  17. Error: Cannot find module &#39;babel-runtime/regenerator&#39;
  18. 安全概念:DMZ(非军事化区,隔离区)
  19. sersync+rsync=实时异步备份
  20. PHP之旅 php数据类型

热门文章

  1. Eclipse导入Gradle时报错:SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable
  2. java 数据库两种连接方法
  3. 多线程(NSThread、NSOperation、GCD)编程浅谈
  4. navicat导入mysql数据库sql时报错或数据不完全问题
  5. 《Android开发艺术探索》读书笔记 (6) 第6章 Android的Drawable
  6. js异步的理解---千呼万唤始出来啊!
  7. querystring,parse和stringify相互转换
  8. Linux Stu
  9. 关于Core Data的一些整理(三)
  10. JS屏蔽右键菜单,复制,粘帖xxxxx........