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 ...
随机推荐
- Kendo MVVM 数据绑定(五) Events
Kendo MVVM 数据绑定(五) Events 本篇和 Kendo MVVM 数据绑定(三) Click 类似,为事件绑定的一般形式.Events 绑定支持将 ViewModel 的方法绑定到 D ...
- js获取当前的年月日时分秒周期
function timeNow(){ var date = new Date(); this.year = date.getFullYear(); this.month = date.getMont ...
- js读取excel数据后的时间格式转换
使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ...
- cpp 计算程序运行时间的两种方法
1. #include <time.h> time_t begin_t = clock(); // to do time_t finish_t = clock(); cout<< ...
- ubuntu下安装ffmpeg扩展
可通过PPA进行安装 sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get ...
- UVALive 5031 Graph and Queries (Treap)
删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...
- CodeForces 52C Circular RMQ (线段树)
线段树区间更新维护最小值...记得下放标记... 如果线段树上的一个完整区间被修改,那么最小值和最大值增加相应的值后不变, 会改变是因为一部分改变而另外一部分没有改变所以维护一下就好. 询问的时候也要 ...
- CF Gym 100187B A Lot of Joy (古典概型)
题意:给两个一样的只含有26个小写字母的字符串,然后两个分别做一下排列,问如果对应位置的字母相等那么就愉悦值就加一,问愉悦值的期望是多少? 题解:只考虑两个序列相对的位置,那么就相当于固定一个位置,另 ...
- Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- C# 控制台应用程序输出颜色字体[更正版]
首先感谢院子里的“yanxinchen”,之前的方法是通过c#调用系统api实现的,相比之下我的有点画蛇添足了,哈哈. 最佳解决方案的代码: static void Main(string[] arg ...