HDU 3727 Jewel 主席树
题意:
一开始有一个空序列,然后有下面四种操作:
Insert x
在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同。Query_1 s t k
查询区间\([s,t]\)中第\(k\)小的元素。Query_2 x
查询元素\(x\)的名次Query_3 k
查询整个区间的第\(k\)小值
分析:
首先离线一下所有查询,然后离散化,剩下的都是很经典的主席树操作。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 10;
const int maxq = 35000 * 3 + 10;
const int maxnode = maxn << 5;
const int maxcmd = maxn + maxq;
int n;
char cmd[15];
int type[maxcmd], a[maxcmd], b[maxcmd], c[maxcmd];
int x[maxn], tot;
int sz, root[maxn];
int lch[maxnode], rch[maxnode], sum[maxnode];
int update(int pre, int L, int R, int pos) {
int rt = ++sz;
sum[rt] = sum[pre] + 1;
if(L < R) {
int M = (L + R) / 2;
if(pos <= M) { rch[rt] = rch[pre]; lch[rt] = update(lch[pre], L, M, pos); }
else { lch[rt] = lch[pre]; rch[rt] = update(rch[pre], M+1, R, pos); }
}
return rt;
}
int query(int l, int r, int L, int R, int k) {
if(L == R) return L;
int num = sum[lch[r]] - sum[lch[l]];
int M = (L + R) / 2;
if(num >= k) return query(lch[l], lch[r], L, M, k);
else return query(rch[l], rch[r], M+1, R, k - num);
}
int Rank(int rt, int L, int R, int x) {
if(L == R) return sum[rt];
int M = (L + R) / 2;
if(x > M) return sum[lch[rt]] + Rank(rch[rt], M+1, R, x);
else return Rank(lch[rt], L, M, x);
}
int main()
{
int kase = 1;
while(scanf("%d", &n) == 1) {
tot = 0;
for(int i = 0; i < n; i++) {
scanf("%s", cmd);
scanf("%d", a + i);
if(!cmd[6]) { type[i] = 0; x[tot++] = a[i]; }
else type[i] = cmd[6] - '0';
if(type[i] == 1) scanf("%d%d", b + i, c + i);
}
sort(x, x + tot);
int cnt = 0;
sz = 0;
LL ans[3];
for(int i = 0; i < 3; i++) ans[i] = 0;
for(int i = 0; i < n; i++) {
if(type[i] == 0) {
cnt++;
int pos = lower_bound(x, x + tot, a[i]) - x + 1;
root[cnt] = update(root[cnt-1], 1, tot, pos);
} else if(type[i] == 1) {
int q = query(root[a[i]-1], root[b[i]], 1, tot, c[i]);
ans[0] += x[q - 1];
} else if(type[i] == 2) {
int pos = lower_bound(x, x + tot, a[i]) - x + 1;
ans[1] += Rank(root[cnt], 1, tot, pos);
} else {
int q = query(0, root[cnt], 1, tot, a[i]);
ans[2] += x[q - 1];
}
}
printf("Case %d:\n", kase++);
for(int i = 0; i < 3; i++) printf("%lld\n", ans[i]);
}
return 0;
}
HDU 3727 Jewel 主席树的更多相关文章
- hdu 3727 Jewel (可持久化线段树+bit)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3727 题意: 对一段序列进行四种操作: Insert x :在序列尾部插入一个x: Query_1 s ...
- HDU3727 - Jewel(主席树)
题目大意 对一个序列进行以下四种操作: 1.Insert x 在序列尾部插入x 2.Query_1 s t k 查询区间[s,t]的第k小 3.Query_2 x 查询x的在序列中排名 4.Query ...
- Sequence II HDU - 5919(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- To the moon HDU - 4348 (主席树,区间修改)
Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...
- Super Mario HDU - 4417 (主席树)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- HDU 4348(主席树 标记永久化)
题面一看就是裸的数据结构题,而且一看就知道是主席树... 一共四种操作:1:把区间[l, r]的数都加上d,并且更新时间.2:查询当前时间的区间和.3:查询历史时间的区间和.4:时光倒流到某个时间. ...
- K-th occurrence HDU - 6704 (SA, 主席树)
大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ...
- HDU 3727 Jewel 可持久化线段树
Jewel Problem Description Jimmy wants to make a special necklace for his girlfriend. He bought man ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- css设置文字超出部分显示省略号。。。
兼容IE/Firefox/Chrome display:block; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
- 文件操作函数及光标,tell,truncate
一丶文件基本操作 1.打开文件 open(文件名(路径),mode = "?",encoding = "字符集") 2.文件路径: 1.绝对路径,从磁盘的根目录 ...
- CDN加速服务
CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一般的CDN公共库都会包含全球所有最流行的开源JavaScrip ...
- Kendo MVVM 数据绑定(七) Invisible/Visible
Kendo MVVM 数据绑定(七) Invisible/Visible Invisible/Visible 绑定可以根据 ViewModel 的某个属性来显示/隐藏 DOM 元素.例如: <d ...
- ABAP数据类型
数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1 Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...
- Eucalyptus——EC2的开源实现(转载)
Eucalyptus[22]是加利福尼亚大学的 Daniel Nurmi 等人实现的,是一个用于实现云计算的开源软件基础设施.Eucalyptus 是 Amazon EC2 的一个开源实现,它与 EC ...
- ABAP vs Java, 蛙泳 vs 自由泳
去年7月定下的一年之内学会自由泳的目标终于实现了,特来还愿. ABAP和Java, 蛙泳和自由泳.前面的组合是Jerry用来挣钱养家的技术,后者是Jerry花了大量业余时间和金钱苦练的技能.或许有的朋 ...
- python_79_模块定义导入优化
''' 1.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件 (文件名:test.py,对应的模块名:test. import ...
- Word2vec资料
Word2vec 很好的资料 Word2Vec-知其然知其所以然 https://www.zybuluo.com/Dounm/note/591752 Word2Vec数学原理讲解 http://w ...
- python中yield的用法详解
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂 ...