先打上代码以后更新解释

 #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 ;
}

主席树套树状数组 动态区间第k小的更多相关文章

  1. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  2. 【POJ2104】【整体二分+树状数组】区间第k大

    Description You are working for Macrohard company in data structures department. After failing your ...

  3. G - KiKi's K-Number(树状数组求区间第k大)

    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...

  4. Dynamic Rankings || 动态/静态区间第k小(主席树)

    JYF大佬说,一星期要写很多篇博客才会有人看 但是我做题没有那么快啊QwQ Part1 写在前面 区间第K小问题一直是主席树经典题=w=今天的重点是动态区间第K小问题.静态问题要求查询一个区间内的第k ...

  5. 主席树初步学习笔记(可持久化数组?静态区间第k大?)

    我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...

  6. 主席树总结(经典区间第k小问题)(主席树,线段树)

    接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...

  7. 区间第K小——可持久化线段树模板

    概念 可持久化线段树又叫主席树,之所以叫主席树是因为这东西是fotile主席创建出来的. 可持久化数据结构思想,就是保留整个操作的历史,即,对一个线段树进行操作之后,保留访问操作前的线段树的能力. 最 ...

  8. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...

  9. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

随机推荐

  1. js实现键盘操作对div的移动或改变-------Day43

    <爸爸去哪儿>的第二季据说要开播了额,有点小期待,不知道这一季的小宝贝们会有多萌,还会甜到心底吧, 哈哈,还记得那个风一样的女子呢,不知道她如今怎样了. 言归正传,继续今天的记录,实际上在 ...

  2. [Javascrip] Logging Timing Data to the Console

    Learn to use console.time with console.timeEnd to get accurate timings of operations in javascript. ...

  3. linux下查看磁盘空间

    如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h  文件系统              容量 已用 可用 已用% 挂载点  /dev/sda2   ...

  4. 根据IP地址获取IP的详细信息

    <?php header('Content-Type:text/html; charset=utf-8'); function ip_data() { $ip = GetIP(); $url = ...

  5. [转] linux系统文件流、文件描述符与进程间关系详解

    http://blog.sina.com.cn/s/blog_67b74aea01018ycx.html linux(unix)进程与文件的关系错综复杂,本教程试图详细的阐述这个问题. 包括:     ...

  6. iOS 短信验证码倒计时按钮的实现

    验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的小伙伴们可以参考一下: 实现思路: 创建按钮,添加点击方法: 用NSTimer定时器, ...

  7. Android 连接 SQL Server (jtds方式)——上

    本文将介绍开发Android程序,连接SQL Server,通过第三方包jtds的方式. 如果你有同样的需求,请跟着做一遍,博主将以最详细的方式,进行介绍. 首先说明,Java.Android连接SQ ...

  8. dede织梦后台页面及功能修改及精简操作方法

    先让我们来看看都有哪些页面控制着后台的功能和显示.下方为系统默认的后台界面图,为了便于下面的说明我对各个部分进行了一些标示.共A.B.C.D.E五个区域. 常用:A区域[顶部LOGO行]对应文件:/d ...

  9. My.Ioc 代码示例——Lifetime 和 ILifetimeScope

    很多 Ioc 框架在创建对象的过程中,都会采取某种方式来缓存/复用/释放已构建的对象.在 My.Ioc 中,这个目的是通过 Lifetime/ILifetimeScope 来实现的.其中,Lifeti ...

  10. React 点击删除列表中对应项(React 获取DOM中自定义属性)

    点击删除按钮,删除列表中对应项本来是React比较基础的应用,可是应用情况变得复杂了以后,我还真想了一会儿才搞定. 简化一下应用场景:点击新增按钮,增加一条输入框,点击输入框旁边的按钮,删除该输入框( ...