树状数组套权值线段树

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> #define E exit(0) 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 = 5e4 + ;
const int Out = ; struct Node {int opt, l, r, k;} Ask[N];
int n, m;
int Num[N << ], totNum, A[N], Length; int Root[N];
int W[N * ], Lson[N * ], Rson[N * ]; int Add_root[N], Cut_root[N];
int Hjt_; namespace Seg { void Insert(int &rt, int l, int r, int x) {
if(!rt) rt = ++ Hjt_;
W[rt] ++;
if(l == r) return ;
int mid = (l + r) >> ;
if(x <= mid) Insert(Lson[rt], l, mid, x);
else Insert(Rson[rt], mid + , r, x);
} int Getrank(int l, int r, int k, int opt) {
if(l == r) {
if(opt == ) return ;
int s = ;
for(int i = ; i <= Add_root[]; i ++) s += W[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) s -= W[Cut_root[i]];
return s;
}
int mid = (l + r) >> ;
if(k <= mid) {
for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
return Getrank(l, mid, k, opt);
} else {
int sum = ;
for(int i = ; i <= Add_root[]; i ++) {
sum += W[Lson[Add_root[i]]]; Add_root[i] = Rson[Add_root[i]];
}
for(int i = ; i <= Cut_root[]; i ++) {
sum -= W[Lson[Cut_root[i]]]; Cut_root[i] = Rson[Cut_root[i]];
}
int ret = sum + Getrank(mid + , r, k, opt);
return ret;
}
} int Getnum(int l, int r, int k) {
if(l == r) return l;
int mid = (l + r) >> ;
int sum = ;
for(int i = ; i <= Add_root[]; i ++) sum += W[Lson[Add_root[i]]];
for(int i = ; i <= Cut_root[]; i ++) sum -= W[Lson[Cut_root[i]]];
if(sum >= k) {
for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
Getnum(l, mid, k);
} else {
for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Rson[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Rson[Cut_root[i]];
Getnum(mid + , r, k - sum);
}
} void Poi_G(int l, int r, int x, int val) {
for(int i = ; i <= Cut_root[]; i ++) W[Cut_root[i]] += val;
if(l == r) return ;
int mid = (l + r) >> ;
if(x <= mid) {
for(int i = ; i <= Cut_root[]; i ++) {
if(Lson[Cut_root[i]] == ) Lson[Cut_root[i]] = ++ Hjt_;
Cut_root[i] = Lson[Cut_root[i]];
}
Poi_G(l, mid, x, val);
} else {
for(int i = ; i <= Cut_root[]; i ++) {
if(Rson[Cut_root[i]] == ) Rson[Cut_root[i]] = ++ Hjt_;
Cut_root[i] = Rson[Cut_root[i]];
}
Poi_G(mid + , r, x, val);
}
} int Asknum(int l, int r, int k) {
if(l == r) {
int sum = ;
for(int i = ; i <= Add_root[]; i ++) sum += W[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) sum -= W[Cut_root[i]];
k -= sum;
if(k > ) return -;
return l;
}
int sum = ;
for(int i = ; i <= Add_root[]; i ++) sum += W[Lson[Add_root[i]]];
for(int i = ; i <= Cut_root[]; i ++) sum -= W[Lson[Cut_root[i]]];
int mid = (l + r) >> ;
if(k <= sum) {
for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
Asknum(l, mid, k);
} else {
for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Rson[Add_root[i]];
for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Rson[Cut_root[i]];
Asknum(mid + , r, k - sum);
}
}
} namespace Bit { inline int Lowbit(int x) {return (x & (-x));} void Add(int rt, int x, int val) {
for(int i = rt; i <= n; i += Lowbit(i)) {
Seg:: Insert(Root[i], , Length, x);
}
} int Getrank(int l, int r, int k) {
Add_root[] = Cut_root[] = ;
for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
k = lower_bound(Num + , Num + Length + , k) - Num;
return Seg:: Getrank(, Length, k, );
} int Getnum(int l, int r, int k) {
Add_root[] = Cut_root[] = ;
for(int i = r; i >= ; i -= Lowbit(i)) {
Add_root[++ Add_root[]] = Root[i];
}
for(int i = l - ; i >= ; i -= Lowbit(i)) {
Cut_root[++ Cut_root[]] = Root[i];
}
return Seg:: Getnum(, Length, k);
} void Poi_G(int x, int k) {
Cut_root[] = ;
for(int i = x; i <= n; i += Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
int a = lower_bound(Num + , Num + Length + , A[x]) - Num;
A[x] = k;
Seg:: Poi_G(, Length, a, -);
Cut_root[] = ;
for(int i = x; i <= n; i += Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
a = lower_bound(Num + , Num + Length + , k) - Num;
Seg:: Poi_G(, Length, a, );
} int Asknum(int l, int r, int k, int opt) {
k = lower_bound(Num + , Num + Length + , k) - Num;
int kk = k;
Add_root[] = Cut_root[] = ;
for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
k = Seg:: Getrank(, Length, k, );
Add_root[] = Cut_root[] = ;
for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
int k2 = Seg:: Getrank(, Length, kk, );
Add_root[] = Cut_root[] = ;
for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
if(opt == ) {
if(k == ) return -;
return Seg:: Asknum(, Length, k - );
}
else return Seg:: Asknum(, Length, k2 + );
}
} int main() {
n = read(), m = read();
for(int i = ; i <= n; i ++) Num[i] = read(), A[i] = Num[i];
totNum = n;
for(int i = ; i <= m; i ++) {
Ask[i].opt = read();
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[++ totNum] = Ask[i].k;
if(Ask[i].opt == || Ask[i].opt == ) {
Num[++ totNum] = Ask[i].k;
}
}
sort(Num + , Num + totNum + );
Length = unique(Num + , Num + totNum + ) - Num - ;
for(int i = ; i <= n; i ++) {
int a = lower_bound(Num + , Num + Length + , A[i]) - Num;
Bit:: Add(i, a, );
} #define opt Ask[i].opt
#define l Ask[i].l
#define r Ask[i].r
#define k Ask[i].k for(int i = ; i <= m; i ++) {
if(opt == ) {
cout << Bit:: Getrank(l, r, k) << "\n";
} else if(opt == ) {
int ans = Bit:: Getnum(l, r, k);
cout << Num[ans] << "\n";
} else if(opt == ) {
Bit:: Poi_G(l, k);
} else if(opt == ) {
int ans = Bit:: Asknum(l, r, k, );
if(ans == -) cout << "-2147483647" << "\n";
else cout << Num[ans] << "\n";
} else {
int ans = Bit:: Asknum(l, r, k, );
if(ans == -) cout << "" << "\n";
else cout << Num[ans] << "\n";
}
}
return ;
}

luogu 3380的更多相关文章

  1. bzoj 3196 && luogu 3380 JoyOI 1730 二逼平衡树 (线段树套Treap)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 题面; 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Se ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  5. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  6. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  7. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

  8. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  9. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

随机推荐

  1. pandas之时间重采样笔记

    周期由高频率转向低频率称为降采样:例如5分钟股票交易数据转换为日交易数据 相反,周期也可以由低频转向高频称为升采样 其他重采样:例如每周三(W-WED)转换为每周五(W-FRI) import pan ...

  2. Java中final与C++中const的关系

    Java中的final有三种主要用法: (1)修饰变量: final变量是不可改变的,但它的值可以在运行时刻初始化,也可以在编译时刻初始化,甚至可以放在构造函数中初始化,而不必在声明的时候初始化,所以 ...

  3. ZooKeeper学习笔记(四)——shell客户端命令操作

    ZooKeeper客户端命令行操作 启动服务端 [simon@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start 查看状态信息 Using confi ...

  4. Task 开始 停止

    注意点:需要将每个线程的 MemoryCacheManager 保存,这里我保存在缓存中,需要取消时根据缓存key值取出 MemoryCacheManager //开始Task1 private vo ...

  5. 线程一(lock)

    对于线程同步操作最简单的一种方式就是使用 lock 关键字,通过 lock 关键字能保证加锁的线程只有在执行完成后才能执行其他线程.   lock 的语法形式如下. lock(object) {    ...

  6. js中的BOM和DOM常用事件方法

    笔记: window对象 ● window.innerHeight - 浏览器窗口的内部高度 ● window.innerWidth - 浏览器窗口的内部宽度 ● window.open() - 打开 ...

  7. java读取文件的几种方式性能比较

    //普通输入流读取文件内容 public static long checksumInputStream(Path filename) { try(InputStream in= Files.newI ...

  8. MACOS 安装mysqlclient 的 Library not loaded错误

    报错场景 >>> import MySQLdb Traceback (most recent call last): File "<stdin>", ...

  9. [Jenkins][centos]1 持续集成 之 配置VNC,部署Jenkins

    痛点:上一篇的AWS部署的VNC不知为啥挂了,死活连不上,因此改申请京东云做部署Jenkins 预计阅读时间:20分钟 更新软件,安装桌面 yum -y update yum -y groupinst ...

  10. kvm虚拟化环境的搭建

    首先搭建kvm的虚拟化环境,我选择的环境是在vmvare上的Centos 7的虚拟机,在该环境上搭建kvm的虚拟化环境 1:安装虚拟机(该过程自行安装) 2:操作系统环境的设置 (1)修改内核模式为兼 ...