题面

luogu

题解

2019年AC的第一道题~~

函数名命名为rank竟然会ce

我写的是树状数组套值域线段树(动态开点)

操作1:询问\(k\)在\([l-r]\)这段区间有多少数比它小,再加\(1\)

操作2:前缀和思想得到\([l-r]\)区间的线段树,然后类似平衡树找第\(k\)大

操作3:直接修改

操作4/5:操作1+操作2

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register const int N = 50010; using namespace std; inline int gi() {
RG int x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-'0', c = getchar();
return f ? -x : x;
} int tot, A[N<<1], a[N], cnt, n;
int t1, t2, tmp1[N], tmp2[N]; struct question {
int op, l, r, k;
}q[N]; struct node {
int ls, rs, v;
}t[N<<7];
int rt[N];
#define lowbit(x) (x&(-x))
void Update(int &now, int l, int r, int pos, int k) {
if (!now) now = ++cnt;
t[now].v += k;
if (l == r) return ;
int mid = (l + r) >> 1;
if (pos <= mid) Update(t[now].ls, l, mid, pos, k);
else Update(t[now].rs, mid+1, r, pos, k);
}
void update(int x, int k) {
for (int i = x; i <= n; i += lowbit(i)) Update(rt[i], 1, tot, a[x], k);
}
int query(int now, int l, int r, int pos) {
if (l == r) return t[now].v;
int mid = (l + r) >> 1;
if (pos <= mid) return query(t[now].ls, l, mid, pos);
return t[t[now].ls].v+query(t[now].rs, mid+1, r, pos);
} int Rank(int l, int r, int k) {
if (l > r) return 0;
l--;
int s = 0;
for (int i = r; i; i -= lowbit(i)) s += query(rt[i], 1, tot, k);
for (int i = l; i; i -= lowbit(i)) s -= query(rt[i], 1, tot, k);
return s;
} int Kth(int l, int r, int k) {
if (l == r) return l;
int mid = (l + r) >> 1, s = 0;
for (int i = 1; i <= t1; i++) s += t[t[tmp1[i]].ls].v;
for (int i = 1; i <= t2; i++) s -= t[t[tmp2[i]].ls].v;
if (s >= k) {
for (int i = 1; i <= t1; i++) tmp1[i] = t[tmp1[i]].ls;
for (int i = 1; i <= t2; i++) tmp2[i] = t[tmp2[i]].ls;
return Kth(l, mid, k);
}
else {
for (int i = 1; i <= t1; i++) tmp1[i] = t[tmp1[i]].rs;
for (int i = 1; i <= t2; i++) tmp2[i] = t[tmp2[i]].rs;
return Kth(mid+1, r, k-s);
}
} int kth(int l, int r, int k) {
l--; t1 = t2 = 0;
for (int i = r; i; i -= lowbit(i)) tmp1[++t1] = rt[i];
for (int i = l; i; i -= lowbit(i)) tmp2[++t2] = rt[i];
return A[Kth(1, tot, k)];
} int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
n = gi();
int m = gi();
for (int i = 1; i <= n; i++) A[++tot] = a[i] = gi();
for (int i = 1; i <= m; i++) {
q[i].op = gi();
if (q[i].op != 3) {
q[i].l = gi(); q[i].r = gi(); q[i].k = gi();
if (q[i].op != 2) A[++tot] = q[i].k;
}
else {
q[i].l = q[i].r = gi();
A[++tot] = q[i].k = gi();
}
}
sort(A+1, A+1+tot);
tot = unique(A+1, A+1+tot) - A - 1;
for (int i = 1; i <= n; i++) a[i] = lower_bound(A+1, A+1+tot, a[i])-A;
for (int i = 1; i <= n; i++) update(i, 1);
for (int i = 1; i <= m; i++)
if (q[i].op != 2)
q[i].k = lower_bound(A+1, A+1+tot, q[i].k)-A;
for (int i = 1; i <= m; i++) {
if (q[i].op == 1)
printf("%d\n", Rank(q[i].l, q[i].r, q[i].k-1)+1);
else if (q[i].op == 2) printf("%d\n", kth(q[i].l, q[i].r, q[i].k));
else if (q[i].op == 3) {
update(q[i].l, -1);
a[q[i].l] = q[i].k;
update(q[i].l, 1);
}
else if (q[i].op == 4) {
int g = Rank(q[i].l, q[i].r, q[i].k-1);
if (!g) puts("-2147483647");
else printf("%d\n", kth(q[i].l, q[i].r, g));
}
else {
int g = Rank(q[i].l, q[i].r, q[i].k);
if (g == q[i].r-q[i].l+1) puts("2147483647");
else printf("%d\n", kth(q[i].l, q[i].r, g+1));
}
}
return 0;
}

洛谷 P3380 【模板】二逼平衡树(树套树)的更多相关文章

  1. BZOJ3196 & 洛谷3380:二逼平衡树——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3196 https://www.luogu.org/problemnew/show/P3380 (题 ...

  2. bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1807  Solved: 772[Submit][Stat ...

  3. bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description ...

  4. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...

  5. BZOJ3196 二逼平衡树 【线段树套平衡树】

    题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...

  6. BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay

    传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...

  7. 洛谷.3835.[模板]可持久化平衡树(fhq treap)

    题目链接 对每次Merge(),Split()时产生的节点都复制一份(其实和主席树一样).时间空间复杂度都为O(qlogq).(应该更大些 因为rand()?内存真的爆炸..) 对于无修改的操作实际上 ...

  8. bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】

    四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...

  9. [BZOJ3196] [Tyvj1730] 二逼平衡树(线段树 套 Splay)

    传送门 至少BZOJ过了,其他的直接弃. 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的 ...

  10. 洛谷P3380 【模板】二逼平衡树(树套树)(线段树+树状数组)

    P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...

随机推荐

  1. Android selector中的item的顺序

    在selector中,要将默认状态的item放在最后面,因为一旦前面的item满足匹配条件,后面的item就不会去匹配.因此,把默认状态的item放在前面的话,后面的item没有执行的机会

  2. Springboot21 整合redis、利用redis实现消息队列

    1 前提准备 1.1 创建一个springboot项目 技巧01:本博文基于springboot2.0创建 1.2 安装redis 1.2.1 linux版本 参考博文 1.2.2 windows版本 ...

  3. Python代码注释

    1.单行注释使用# # Code 2.多行注释,成对使用'''或""",三个单撇号或三个双引号 “”” Code “”” 3.多行快捷注释 1).增加注释 选中待注释的多 ...

  4. Windows Cmder

    一.简介 作为一个程序员,即使是在windows工作环境,cmd也是我们必不可少的使用工具.cmder 是为 Windows 提供的一个便携式控制台仿真器,用来替代windows的cmd,使用非常简单 ...

  5. Luogu 3957 [NOIP2017]普及组 跳房子

    写了好久,感觉自己好菜,唉…… 首先发现这个$g$的取值具有单调性,可以想到二分答案,然后考虑用$dp$来检验,这样子可以写出朴素的转移方程: 设$f_i$表示以$i$结尾的最大价值,那么有$f_i ...

  6. Luogu 3616 富金森林公园

    刚看到此题的时候:sb分块??? Rorshach dalao甩手一句看题 于是回去看题……果然是题读错了…… [思路] 对权值离散化后(要先读入所有输入里的权值一起离散化……所以一共有4e4个数据( ...

  7. ShopNc登录验证码

  8. Django框架 之 logging配置

    Django框架 之 logging配置 logging配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

  9. input 框提示信息

    给input添加提示信息,只需添加 “placeholder”的class,将提示信息放在value中, 其中“placeholder”的名字是随便取的,不是H5的“placeholder”属性 例子 ...

  10. Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial【摘】

    Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial We learned how to integrate Spring ...