题意:

一开始有一个空序列,然后有下面四种操作:

  • 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 主席树的更多相关文章

  1. hdu 3727 Jewel (可持久化线段树+bit)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3727 题意: 对一段序列进行四种操作: Insert x :在序列尾部插入一个x: Query_1 s ...

  2. HDU3727 - Jewel(主席树)

    题目大意 对一个序列进行以下四种操作: 1.Insert x 在序列尾部插入x 2.Query_1 s t k 查询区间[s,t]的第k小 3.Query_2 x 查询x的在序列中排名 4.Query ...

  3. Sequence II HDU - 5919(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  4. To the moon HDU - 4348 (主席树,区间修改)

    Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...

  5. Super Mario HDU - 4417 (主席树)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  6. HDU 4348(主席树 标记永久化)

    题面一看就是裸的数据结构题,而且一看就知道是主席树... 一共四种操作:1:把区间[l, r]的数都加上d,并且更新时间.2:查询当前时间的区间和.3:查询历史时间的区间和.4:时光倒流到某个时间. ...

  7. K-th occurrence HDU - 6704 (SA, 主席树)

    大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ...

  8. HDU 3727 Jewel 可持久化线段树

    Jewel Problem Description   Jimmy wants to make a special necklace for his girlfriend. He bought man ...

  9. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. RHEL6.4 安装 highpoint RocketRAID 2720 阵列卡驱动

    step1:下载驱动程序. http://www.highpoint-tech.com/USA_new/series_rr272x_configuration.htm step2:上传驱动程序至服务器 ...

  2. P3290 寻找第K大数

    描述 寻找第K大数 N个小朋友在一起做游戏.每个小朋友在自己的硬纸板上写一个数,然后同时举起来.接着,小y老师提一个问题,看哪个小朋友先抢答出来.问题是:在这N个数中,第K大的是哪个数?请你编程完成. ...

  3. BFC(块级 格式化上下文)的理解

    本文转载(https://segmentfault.com/a/1190000013647777) 一.BFC的概念 1.规范解释 块格式化上下文(Block Formatting Context,B ...

  4. CentOS 7安装Docker服务详细过程

    ---恢复内容开始--- Docker 简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...

  5. redis 知识归档

    中文版redis命令 http://www.redis.net.cn/order/    redis例子 https://github.com/ServiceStack/ServiceStack.Ex ...

  6. IT部门域事件与业务分析

    IT event--->system--->IT dept |--------------->IT dept |--------------->system 域事件分类: 直接 ...

  7. python+selenium之验证码的处理

    对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码.验证码的类型很多,有字母数字的,有汉字的.甚至还有需要用户输入一道算术题的答案的.对于系统来说,使用验证码可以有效地防止采用机器猜测方 ...

  8. 使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm

    Helm之于Kubernetes好比yum之于Red Hat Enterprise Linux,或者apt-get之于Ubuntu. Helm是由helm CLI和Tiller组成,是典型的Clien ...

  9. Context 使用不当造成内存泄露

    问题: Activity中的context被传递给了一个生命周期长过activity的对象(通常为静态单实例变量),导致activity不能正常被销毁. 示例:Activity 调用 ChatMgr ...

  10. vue入坑教程(二)在vue项目中如何导入element以及sass

    在项目中导入element以及sass.stylus等方便开发的工具以及UI框架 (1)如何在vue项目中导入elementUI框架 elementUI是饿了么团队开发出来基于vue的前端UI框架,其 ...