主席树套树状数组 动态区间第k小
先打上代码以后更新解释
#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小的更多相关文章
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- 【POJ2104】【整体二分+树状数组】区间第k大
Description You are working for Macrohard company in data structures department. After failing your ...
- 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 ...
- Dynamic Rankings || 动态/静态区间第k小(主席树)
JYF大佬说,一星期要写很多篇博客才会有人看 但是我做题没有那么快啊QwQ Part1 写在前面 区间第K小问题一直是主席树经典题=w=今天的重点是动态区间第K小问题.静态问题要求查询一个区间内的第k ...
- 主席树初步学习笔记(可持久化数组?静态区间第k大?)
我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...
- 主席树总结(经典区间第k小问题)(主席树,线段树)
接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...
- 区间第K小——可持久化线段树模板
概念 可持久化线段树又叫主席树,之所以叫主席树是因为这东西是fotile主席创建出来的. 可持久化数据结构思想,就是保留整个操作的历史,即,对一个线段树进行操作之后,保留访问操作前的线段树的能力. 最 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
随机推荐
- HDU1700:Points on Cycle
Problem Description There is a cycle with its center on the origin. Now give you a point on the cycl ...
- 基于redis 内存数据库简单使用
在ecplise中使用内存数据的客端户,前提要准备要下载两个jar包 commons-pool2-2.0.jar jedis-2.4.2.jar 前提准备做好了,那我们就开启redis的服务,打开一个 ...
- Weibo SSO认证 和初次请求数据
在进行SSO请求之前 我们要先去新浪微博的开放平台http://open.weibo.com/进行创建应用.以便得到appKey 和AppSecret. 点击创建应用 .进行资料填写 在这里 App ...
- Android(java)学习笔记223:上下文的区分
1.两种上下文: (1)Activity.this 界面的上下文 (2)getApplicationContext() 整 ...
- python栈的实现(入栈,出栈)
#coding=utf-8 class Stack(): def __init__(st,size):#栈的初始化 st.stack=[]; st.size=size; st.top=-1 def p ...
- C#学习第六天
今天的内容主要是参数数组,C#允许函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中最后一个参数,成为参数数组. 参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它 ...
- ASP.NET-FineUI开发实践-16(二)
实现那还差点,在事件参数里我传了一个boolall选中状态参数,这个参数由前台给的,RowSelect 传的是index 行号,就是改这,通过$符号来分开的, if (commandArgs.Leng ...
- JS 点击事件失效
有时候,会发现js失效 ,代码如果是如下的时候 <input type="button" onclick="change()"value="変更 ...
- ASP.net中网站访问量统计方法
仅共学习 一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime) ...
- (转)SQL Server中使用convert进行日期转换
原文链接:http://www.cnblogs.com/weiqt/articles/1826847.html SQL Server中使用convert进行日期转换 一般存入数据库中的时间格式为yyy ...