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 ...
随机推荐
- springmvc的DispatcherServlet源码——doDispatch方法解析
DispatcherServlet的doDispatch方法主要用作职责调度工作,本身主要用于控制流程,主要职责如下: 1.文件上传解析,如果请求类型是multipart将通过MultipartRes ...
- 【翻译转载】【官方教程】Asp.Net MVC4入门指南(6):验证编辑方法和编辑视图
本节中,您将开始修改为电影控制器所新加的操作方法和视图.然后,您将添加一个自定义的搜索页. 在浏览器地址栏里追加/Movies, 浏览到Movies页面.并进入编辑(Edit)页面. Edit(编辑) ...
- QrenCode : linux命令行下生成二维码图片
原文链接:http://wowubuntu.com/qrencode.html # 作者:riku/ / 本文采用CC BY-NC-SA 2.5协议授权,转载请注明本文链接. 对于二维码大家应该并不陌 ...
- MySQL如何找出未提交事务信息
前阵子,我写了一篇博客"ORACLE中能否找到未提交事务的SQL语句", 那么在MySQL数据库中,我们能否找出未提交事务执行的SQL语句或未提交事务的相关信息呢? 实验验证了一下 ...
- python 之网页解析器
一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...
- javaSe-反射1
package com.java.chap07.sec01; public class Student { private String name; private Integer age; //创建 ...
- java面试题(杨晓峰)---第八讲谈谈Vector,ArrayList,LinkedList有何区别?
Vector,ArrayList,LinkedList均为线性的数据结构,但是从现实方式与应用场景中又存在差别. 1 底层实现方式 ArrayList内部数组来实现,LinkedList内部采用双向链 ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.web.socket.server.standard.ServerEndpointExporter
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/web/socket/ ...
- [神经网络]一步一步使用Mobile-Net完成视觉识别(五)
1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第五篇,讲解如何调用测试训练结果. 上一篇中我们输出了训练的模型,这一篇中我们通过调用训练好的模型来完 ...
- Bootstrap历练实例:嵌套的媒体对象
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...