luogu 2617
动态区间 $k$ 大
主席树 + 树状数组
树状数组的每个点对应一颗线段树
首先将所有点加入数据结构
枚举 x
code: for(int i = x; i <= n; i += Lowbit(i)) Poi_G(root[i], 1, Length, k, val);
区间修改时
将所有的后缀树的相应位置 -1, 再 +1
主席树查询时
在计算区间和的时候
类似树状数组的查询
将用到的线段树的相应节点加或减
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 1e5 + ; struct Node {int opt, l, r, k;} Ask[N]; int n, m;
int A[N], Num[N << ], Length; int root[N], Lson[N * ], Rson[N * ], W[N * ];
int root_add[], root_cut[];
int jsadd, jscut; int Lowbit(int x) {return (x & (-x));} int Hjt; void Poi_G(int &rt, int l, int r, int k, int val) {
if(!rt) rt = ++ Hjt;
W[rt] += val;
if(l == r) return ;
int mid = (l + r) >> ;
if(k <= mid) Poi_G(Lson[rt], l, mid, k, val);
else Poi_G(Rson[rt], mid + , r, k, val);
} void Pre_Poi_G(int x, int val) {
int k = lower_bound(Num + , Num + Length + , A[x]) - Num;
for(int i = x; i <= n; i += Lowbit(i)) Poi_G(root[i], , Length, k, val);
} int Sec_A(int l, int r, int k) {
if(l == r) return l;
int sum = ;
for(int i = ; i <= jsadd; i ++) sum += W[Lson[root_add[i]]];
for(int i = ; i <= jscut; i ++) sum -= W[Lson[root_cut[i]]];
int mid = (l + r) >> ;
if(k <= sum) {
for(int i = ; i <= jsadd; i ++) root_add[i] = Lson[root_add[i]];
for(int i = ; i <= jscut; i ++) root_cut[i] = Lson[root_cut[i]];
return Sec_A(l, mid, k);
} else {
for(int i = ; i <= jsadd; i ++) root_add[i] = Rson[root_add[i]];
for(int i = ; i <= jscut; i ++) root_cut[i] = Rson[root_cut[i]];
return Sec_A(mid + , r, k - sum);
}
} int Pre_Sec_A(int l, int r, int k) {
memset(root_add, , sizeof root_add);
memset(root_add, , sizeof root_add);
jsadd = jscut = ;
for(int i = r; i; i -= Lowbit(i)) root_add[++ jsadd] = root[i];
for(int i = l - ; i; i -= Lowbit(i)) root_cut[++ jscut] = root[i];
return Sec_A(, Length, k);
} int main() {
n = read(), m = read();
for(int i = ; i <= n; i ++) A[i] = read(), Num[++ Length] = A[i];
for(int i = ; i <= m; i ++) {
char c[]; scanf("%s", c);
Ask[i].opt = (c[] == 'Q' ? : );
if(Ask[i].opt == ) Ask[i].l = read(), Ask[i].r = read(), Ask[i].k = read();
else {Ask[i].l = read(), Ask[i].k = read(), Num[++ Length] = Ask[i].k;}
}
sort(Num + , Num + Length + );
Length = unique(Num + , Num + Length + ) - Num - ;
for(int i = ; i <= n; i ++) Pre_Poi_G(i, );
for(int i = ; i <= m; i ++) {
if(Ask[i].opt == ) {
int Ans = Num[Pre_Sec_A(Ask[i].l, Ask[i].r, Ask[i].k)];
printf("%d\n", Ans);
} else {
Pre_Poi_G(Ask[i].l, -);
A[Ask[i].l] = Ask[i].k;
Pre_Poi_G(Ask[i].l, );
}
}
return ;
}
luogu 2617的更多相关文章
- Luogu Dynamic Ranking (带修改的主席树)
题目大意: 网址:https://www.luogu.org/problemnew/show/2617 给定一个序列a[1].a[2].....a[N],完成M个操作,操作有两种: [1]Q i j ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- YTU 2617: B C++时间类的运算符重载
2617: B C++时间类的运算符重载 时间限制: 1 Sec 内存限制: 128 MB 提交: 284 解决: 108 题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为 ...
- Luogu 考前模拟Round. 1
A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...
- luogu P2580 于是他错误的点名开始了
luogu P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...
- CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...
随机推荐
- AS3.0 位图(BMP)解析类
/** * *-----------------------------* * | *** BMP格式解析类 *** | * *-----------------------------* * * 编 ...
- ASP.net Web API综合示例
目录 概述 功能介绍 程序结构 服务器端介绍 客户端介绍 “契约” Web API设计规则 并行写入冲突与时间戳 身份验证详解 Web API验证规则 客户端MVVM简介 Web.Config 本DE ...
- Asp.Net Mvc Area二级域名
参考:https://blog.maartenballiauw.be/post/2009/05/20/aspnet-mvc-domain-routing.html 参考:https://www.cnb ...
- 2019年全国高校sql数据库
2019年全国高校名单sql数据库信息: 根据教育部发布的文件整理形成可用.sql文件,导入数据库即可使用. 包括全国高等学校共计2956所,其中:普通高等学校2688所(含独立学院257所),成人高 ...
- .net SHA-256 SHA-1
Framework 4.5 uses SHA-256 algorithm for the signature, and 4.0 uses SHA-1.
- html中标签的英文含义!
html中的标签缩写的英文是什么? 标签 英文全称 含义 ul unordered lists 无序列表 li list item 列表项目 ol ordered lists 有序列表 dl d ...
- PLSQL登录的时候Warning提示:Using a filter for all users can lead to poor performance!
转自: https://blog.csdn.net/athena2015/article/details/81811908
- Mac 下 visual studio code 编辑器 设置为中文
1,mac系统VScode设置中文 macOS 快捷键:command + shift + p 输入搜索 configure language 1.Ctrl+Shift+P 打开命令 2.搜索 ...
- SQL SERVER-孤立用户
--SQL SERVER用户管理分为两层,实例级的login和数据库级的用户 --login的SID和数据库用户的SID必须一样才行 --数据库搬迁login在重建时生成新的sid,与原来数据库的用户 ...
- django 配置文件settings.py 设置模板
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'dj ...