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 数学成绩优异 ...
随机推荐
- 【SCALA】3、模拟电路
Simulation package demo17 abstract class Simulation { type Action = () => Unit case class WorkIte ...
- Error while launching application Error: spawn ENOMEM 解决
当NodeJs PM2 无法启动应用 出现 Error while launching application Error: spawn ENOMEM 错误时 执行一下 pm2 update ...
- Consul作为SpringCloud配置中心
一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...
- ZooKeeper学习笔记(四)——shell客户端命令操作
ZooKeeper客户端命令行操作 启动服务端 [simon@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start 查看状态信息 Using confi ...
- 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)
原文:在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决 ...
- Linux下OpenSSL加密解密压缩文件(AES加密压缩文件)
OpenSSL是一个开源的用以实现SSL协议的产品,它主要包括了三个部分:密码算法库.应用程序.SSL协议库.Openssl实现了SSL协议所需要的大多数算法.下面介绍使用Openssl进行文件的对称 ...
- C#数字日期转成中文日期
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- ElementUI对话框(dialog)提取为子组件
需求:在页面的代码太多,想把弹窗代码提取为子组件,复用也方便. 这里涉及到弹窗el-dialog的一个属性show-close: show-close="false"是设置不显 ...
- pymysql操作mysql数据库
1.建库 import pymysql # 建库 try: conn=pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd ...
- vue多页面项目搭建(vue-cli 4.0)
1.创建vue项目 cmd命令执行 vue create app (app 自定义的项目名) 一般都会选择后者,自己配置一下自己需要的选项(空格为选中) 这是我个人需要的一些选项,路由Router.状 ...