题意:求区间第k小,节点可修改

思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高。那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出权值线段树前缀和,相减就是区间前缀和。而且我维护也只要改logn棵树就好了。具体看JQ博客

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5e4 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int a[maxn], root[maxn], tot;
int sroot[maxn]; //树状数组的头
int use[maxn]; //保存树状数组的头
int n, m, L, R;
vector<int> vv;
struct node{
int lson, rson;
int sum;
}T[maxn * ];
struct Que{
int order;
int l, r, k;
}q[maxn];
void init(){
memset(T, , sizeof(T));
memset(sroot, , sizeof(root));
tot = ;
vv.clear();
}
int lowbit(int x){
return x&(-x);
}
int getId(int x){
return lower_bound(vv.begin(), vv.end(),x) - vv.begin() + ;
}
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
int add(int x, int v, int pos){
for(int i = x; i <= n; i += lowbit(i)){
update(, vv.size(), sroot[i], sroot[i], v, pos);
}
}
int SUM(int pos){
int ret = ;
for(int i = pos; i > ; i -= lowbit(i))
ret += T[T[use[i]].lson].sum;
return ret;
} int query(int l, int r, int now, int pre, int k){
if(l == r) return l;
int sum = SUM(R) - SUM(L) + T[T[now].lson].sum - T[T[pre].lson].sum;
int m = (l + r) >> ;
if(sum >= k){
for(int i = R; i > ; i -= lowbit(i)) use[i] = T[use[i]].lson;
for(int i = L; i > ; i -= lowbit(i)) use[i] = T[use[i]].lson;
return query(l, m, T[now].lson, T[pre].lson, k);
}
else{
for(int i = R; i > ; i -= lowbit(i)) use[i] = T[use[i]].rson;
for(int i = L; i > ; i -= lowbit(i)) use[i] = T[use[i]].rson;
return query(m + , r, T[now].rson, T[pre].rson, k - sum);
}
} int main(){
int T;
scanf("%d", &T);
while(T--){
init();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
vv.push_back(a[i]);
}
for(int i = ; i <= m; i++){
char o[];
scanf("%s", o);
if(o[] == 'Q'){
q[i].order = ;
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
}
else{
q[i].order = ;
scanf("%d%d", &q[i].l, &q[i].k);
vv.push_back(q[i].k);
}
} sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end()); for(int i = ; i <= n; i++){
update(, vv.size(), root[i], root[i - ], , getId(a[i]));
}
for(int i = ; i <= m; i++){
if(q[i].order == ){
L = q[i].l - , R = q[i].r;
for(int j = R; j > ; j -= lowbit(j)) use[j] = sroot[j];
for(int j = L; j > ; j -= lowbit(j)) use[j] = sroot[j];
printf("%d\n", vv[query(, vv.size(), root[R], root[L], q[i].k) - ]);
}
else{
add(q[i].l, -, getId(a[q[i].l]));
a[q[i].l] = q[i].k;
add(q[i].l, , getId(a[q[i].l]));
}
}
}
return ;
}

ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解的更多相关文章

  1. P2617 Dynamic Rankings(树状数组套主席树)

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

  2. BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树

    [题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...

  3. LUOGU P2617 Dynamic Rankings(树状数组套主席树)

    传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...

  4. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

  5. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

  6. [COGS257]动态排名系统 树状数组套主席树

    257. 动态排名系统 时间限制:5 s   内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...

  7. BZOJ 2141 排队(树状数组套主席树)

    解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...

  8. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  9. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. Word图片、表格添加题注

    1.首先为图片.表格添加题注: 2.通过交叉引用,如图**和图片.表的编号相关联起来:

  2. jmeter 之调试

    目前知道的调试方法有两种:debug sample .http mirror server debug sample  debug sample 的用户界面如下: 如果选择ture则表示打印对应的数据 ...

  3. @RequestParam @PathVariable

    1.Request参数 在访问各种各样网站时,经常会发现网站的URL的最后一部分形如:?xxxx=yyyy&zzzz=wwww.这就是HTTP协议中的Request参数,它有什么用呢?先来看一 ...

  4. 《图解HTTP》读书笔记(七:通信数据转发程序-代理/网关/隧道)

    HTTP通信时,除客户端和服务器以外,还有一些用于通信数据转发的应用程序,例如代理.网关和隧道,它们可以配合服务器工作.这些服务器和应用程序可以将请求转发给通信线路上的下一站服务器,并且能接收从那台服 ...

  5. Python3学习之路~9.2 操作系统发展史介绍、进程与线程区别、线程语法、join、守护线程

    一 操作系统发展史介绍 参考链接:http://www.cnblogs.com/alex3714/articles/5230609.html 二 进程与线程 进程: 对各种资源管理的集合 就可以称为进 ...

  6. 【心得】-NO.114.面试.1 -【To HR And Interviewer】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  7. 20175211 2017-2018-2 《Java程序设计》第六周学习记录

    目录 7.1 内部类 7.2 匿名类 7.3 异常类 断言 参考资料 <Java 2实用教程>第七章 内部类和异常类 7.1 内部类 内部类的外嵌类的成员变量在内部类中依然有效,内部类中的 ...

  8. 白话skynet第二篇:skynet的通信调试pack和sprotol

    今天来说说Skynet客户端和服务端网络通信的基础部分. Skynet当前版本.lua是skynet自带的5.3版本. 根据示例,我们可以知道.通信的步骤如下. 客户端按大小端打包成二进制. sock ...

  9. php----------php安装xhprof扩展和简单使用

    1.下载源码包 https://github.com/longxinH/xhprof  (wget https://github.com/longxinH/xhprof/archive/master. ...

  10. map-有序 multimap-可重复 unordered_map-无序

    #include <iostream> #include <vector> #include <map> #include <unordered_map> ...