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 数学成绩优异 ...
随机推荐
- 案例(2)-- 线程不安全对象(SimpleDateFormat)
问题描述: 1.系统偶发性抛出异常:java.lang.NumberFormatException: multiple points ,追溯源头抛出的类为:SimpleDateFormat 问题的定位 ...
- 服务篇:我的第一WebService应用
一.我的第一个Webservice应用 1.新建一个空项目 2.添加新项,加入asmx,并再浏览器浏览 3.添加一个aspx网页 4.右键引用→添加服务引用→高级→添加Web引用,输入再浏览器浏览的a ...
- C#使用任务并行库(TPL)
TPL(Task Parallel Library) 任务并行库 (TPL) 是 System.Threading和 System.Threading.Tasks 命名空间中的一组公共类型和 API. ...
- C#简单工厂案例
using System; namespace Application { class JianDanGongChang { static void Main(string[] args) { Fac ...
- Gogs搭建私有git代码仓库
前置环境: 数据库 -> mysql git -> 服务端和客户端版本必须>=1.8.3 ssh服务 -> 如果只使用http/https方式的话,服务端无需配置ssh. st ...
- Bigdecimal除法异常
1.异常信息摘要(详细请见文末): java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represe ...
- 《阿里巴巴 Java 开发规约》自动化检测插件安装及体验
2017 开春之际,有助于提高行业编码规范化水平的<阿里巴巴 Java 开发手册>首次面世.汇聚阿里集团近万名技术精英的经验知识,这套高含金量的手册一经公开,便引起业界普遍关注和学习. 历 ...
- laravel 中将一对多关联查询的结果去重处理
先交代下数据表结构 主表(订单表)order数据 ord_id order_sn 1 EX2019100123458 其中主键为order_id(订单id) 子表(门票表)order_item数据 o ...
- php json_encode()函数返回对象和数组问题
php json_encode() 函数格式化数据时会根据不同的数组类型格式化不同类型的json数据 索引数组时 <?php $arr = [1,2,3,4,5]; print_r(json_e ...
- Extjs 兼容IE8常见问题及解决方法
1. 在IE8中整个页面都打不开,一般情况是: 页面组件中最后一个属性出现了逗号 没有多余的逗号,就很有可能是组件中没有设置renderTo:Ext.getBody(); 2. 页面按钮颜色失效 自定 ...