[luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)
BZOJ上是权限题,洛谷赞啊。
求区间 K 大数很简单。
但是如果修改某个数的话,那么就得把这个数及后面所建的主席树都更新一遍 nlogn,显然不行。
所以可以在外面套一个树状数组来优化,树状数组的每一个节点都表示某个区间的主席树。
所以可以通过树状数组来求前缀和主席树。
具体实现看代码。
——代码
#include <cstdio>
#include <iostream>
#include <algorithm> const int MAXN = ;
int n, m, tot, cnt, size, t1, t2;
int a[MAXN], b[MAXN], x[MAXN], y[MAXN], z[MAXN], q1[MAXN], q2[MAXN], root[MAXN], ls[MAXN * ], rs[MAXN * ], sum[MAXN * ];
char s[MAXN]; inline int read()
{
int x = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar());
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x;
} inline void insert(int &now, int l, int r, int x, int v)
{
if(!now) now = ++cnt;
sum[now] += v;
if(l == r) return;
int mid = (l + r) >> ;
if(x <= mid) insert(ls[now], l, mid, x, v);
else insert(rs[now], mid + , r, x, v);
} inline int query(int l, int r, int x)
{
if(l == r) return l;
int i, mid = (l + r) >> , ans = ;
for(i = ; i <= t1; i++) ans -= sum[ls[q1[i]]];
for(i = ; i <= t2; i++) ans += sum[ls[q2[i]]];
if(x <= ans)
{
for(i = ; i <= t1; i++) q1[i] = ls[q1[i]];
for(i = ; i <= t2; i++) q2[i] = ls[q2[i]];
return query(l, mid, x);
}
else
{
for(i = ; i <= t1; i++) q1[i] = rs[q1[i]];
for(i = ; i <= t2; i++) q2[i] = rs[q2[i]];
return query(mid + , r, x - ans);
}
} int main()
{
int i, j, l, r;
n = read();
m = read();
for(i = ; i <= n; i++) a[i] = read(), b[++tot] = a[i];
for(i = ; i <= m; i++)
{
for(s[i] = getchar(); s[i] != 'Q' && s[i] != 'C'; s[i] = getchar());
if(s[i] == 'C') x[i] = read(), z[i] = read(), b[++tot] = z[i];
else x[i] = read(), y[i] = read(), z[i] = read();
} std::sort(b + , b + tot + );
size = std::unique(b + , b + tot + ) - (b + );
for(i = ; i <= n; i++) a[i] = std::lower_bound(b + , b + size + , a[i]) - b;
for(i = ; i <= m; i++)
if(s[i] == 'C')
z[i] = std::lower_bound(b + , b + size + , z[i]) - b; for(i = ; i <= n; i++)
for(j = i; j <= size; j += j & -j)
insert(root[j], , size, a[i], ); for(i = ; i <= m; i++)
if(s[i] == 'Q')
{
t1 = t2 = ;
for(j = x[i] - ; j; j -= j & -j) q1[++t1] = root[j];
for(j = y[i]; j; j -= j & -j) q2[++t2] = root[j];
printf("%d\n", b[query(, size, z[i])]);
}
else
{
for(j = x[i]; j <= size; j += j & -j) insert(root[j], , size, a[x[i]], -);
a[x[i]] = z[i];
for(j = x[i]; j <= size; j += j & -j) insert(root[j], , size, z[i], );
}
return ;
}
[luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)的更多相关文章
- P2617 Dynamic Rankings(树状数组套主席树)
P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- 【Luogu】P2617Dynamic Ranking(树状数组套主席树)
题目链接 树状数组套主席树有点难懂qwq 不好理解 树状数组套主席树的直观理解应该是:树状数组的每一个节点是一棵主席树. 普通区间修改我们是创建1个线段树,树状数组套主席树的时候我们就创建log个线段 ...
- LUOGU P2617 Dynamic Rankings(树状数组套主席树)
传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树
[题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...
- BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树
BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...
- [COGS257]动态排名系统 树状数组套主席树
257. 动态排名系统 时间限制:5 s 内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...
- BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...
- 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】
题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...
随机推荐
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- rhel7安装oracle 11gR2,所需的依赖包
binutils-2.23.52.0.1-30.el7.x86_64 compat-libstdc++-33-3.2.3-61.x86_64compat-libstdc++-33-3.2.3-61.i ...
- .net简单的fileupload控件上传
前台代码: <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID ...
- VM virtualBox设置无缝全屏
设置之前:
- JVM最多能创建多少个线程: unable to create new native thread
转载自:http://www.rigongyizu.com/jvm-max-threads/ 有应用报出这样的异常“java.lang.OutOfMemoryError: unable to crea ...
- 高阶函数与接口混入和java匿名类
高阶函数与接口混入和java匿名类. 高阶函数中的组件(参量)函数相当于面向对象中的混入(接口)类. public abstract class Bird { private String name; ...
- redis-3.0.1 sentinel 主从高可用 详细配置
最近项目上线部署,要求redis作高可用,由于redis cluster还不是特别成熟,就选择了redis sentinel做高可用.redis本身有replication,实现主从备份.结合sent ...
- com口操作excel
_Application app; //Excel应用程序接口 Workbooks books; //工作薄集合 _Workbook book; //工作薄 Work ...
- 08CSS边框边距
CSS边框边距 边框样式——border-style border-top-style border-bottom-style border-left-style border-right-style ...
- js模拟支付宝发送短信验证码&&&&短信倒计时
html <div class="pwdContent"> <div class="pwdBox"></div> <d ...