HDU-3727 Jewel
Initially, there is no bead at all, that is, there is an empty chain. Jimmy always sticks the new bead to the right of the chain, to make the chain longer and longer. We number the leftmost bead as Position 1, and the bead to its right as Position 2, and so on. Jimmy usually asks questions about the beads' positions, size ranks and actual sizes. Specifically speaking, there are 4 kinds of operations you should process:
Insert x
Put a bead with size x to the right of the chain (0 < x < 231, and x is different from all the sizes of beads currently in the chain)
Query_1 s t k
Query the k-th smallest bead between position s and t, inclusive. You can assume 1 <= s <= t <= L, (L is the length of the current chain), and 1 <= k <= min (100, t-s+1)
Query_2 x
Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)
InputThere are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above.
You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.
There are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above.
You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)
OutputOutput 4 lines for each test case. The first line is "Case T:", where T is the id of the case. The next 3 lines indicate the sum of results for Query_1, Query_2 and Query_3, respectively.
Sample Input
10
Insert 1
Insert 4
Insert 2
Insert 5
Insert 6
Query_1 1 5 5
Query_1 2 3 2
Query_2 4
Query_3 3
Query_3 1
Sample Output
Case 1:
10
3
5
Hint
The answers for the 5 queries are 6, 4, 3, 4, 1, respectively.
题解:可持续化线段树的模板题
AC代码为:
/*
有四种操作
1、在序列最优插入一个数字(该数字从没出现过)
2、询问序列内某区间第k小值
3、询问当前序列内数字x是第几小的(x一定在序列中)
4、询问当前序列内第k小的值
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int tot,rt[N];
char s[10];
vector<int> v;
struct data
{
int id;
int l,r,x;
}data[N*2];
inline int getid(int x) {return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;}
void input(int n)
{
char s[10];
int x;
for(int i=0;i<n;i++)
{
scanf("%s",s);
if(s[0]=='I')
{
scanf("%d",&x);
data[i].id = 0;
data[i].x = x;
v.push_back(x);
}
else if(s[6]=='1')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
data[i].id = 1;
data[i].l = a, data[i].r = b, data[i].x = c;
}
else if(s[6]=='2')
{
scanf("%d",&x);
data[i].id = 2;
data[i].x = x;
}
else
{
scanf("%d",&x);
data[i].id = 3;
data[i].x = x;
}
}
}
struct node
{
int l,r,sum;
}tree[N*24];
inline void build(int l,int r,int &x) //1~v.size()
{
x = ++tot;
tree[x].sum = 0;
if(l==r) return;
int m = (l+r) >> 1;
build(l,m,tree[x].l);
build(m+1,r,tree[x].r);
}
inline void update(int l,int r,int &x,int y,int k)
{
tree[++tot] = tree[y], tree[tot].sum++,x=tot;
if(l==r) return;
int m = (l+r) >> 1;
if(k<=m) update(l,m,tree[x].l,tree[y].l,k);
else update(m+1,r,tree[x].r,tree[y].r,k);
}
inline int query1(int l,int r,int y,int x,int k) //查找区间第k小的数
{
if(l==r) return l;
int mid = (l+r) >> 1;
int sum = tree[tree[x].l].sum - tree[tree[y].l].sum;
if(k<=sum) return query1(l,mid,tree[y].l,tree[x].l,k);
else return query1(mid+1,r,tree[y].r,tree[x].r,k-sum);
}
inline int query2(int l,int r,int x,int k) //在当前序列中,输出X是第几小的数。
{
if(l==r) return 1;
int mid = (l+r) >> 1;
if(k<=mid) return query2(l,mid,tree[x].l,k);
else
{
int sum = tree[tree[x].l].sum;
return sum += query2(mid+1,r,tree[x].r,k);
}
}
inline int query3(int l,int r,int x,int k) //找到当前序列中第X小的数是几
{
if(l==r) return l;
int mid = (l+r) >> 1;
int sum = tree[tree[x].l].sum;
if(sum>=k) return query3(l,mid,tree[x].l,k);
else return query3(mid+1,r,tree[x].r,k-sum);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
int cas = 1;
while(~scanf("%d",&n))
{
v.clear();
tot = 0;
LL ans1 = 0, ans2 = 0, ans3 = 0;
input(n);
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int cnt = v.size();
build(1,cnt,rt[0]);
int now = 1;
for(int i=0;i<n;i++)
{
if(data[i].id==0)
{
update(1,cnt,rt[now],rt[now-1],getid(data[i].x));
now++;
}
else if(data[i].id==1)
{
int l = data[i].l, r = data[i].r, x = data[i].x;
ans1 += v[query1(1,cnt,rt[l-1],rt[r],x)-1];
}
else if(data[i].id==2) ans2+=query2(1,cnt,rt[now-1],getid(data[i].x));
else ans3+=v[query3(1,cnt,rt[now-1],data[i].x)-1];
}
printf("Case %d:\n%I64d\n%I64d\n%I64d\n",cas++,ans1,ans2,ans3);
}
return 0;
}
HDU-3727 Jewel的更多相关文章
- hdu 3727 Jewel (可持久化线段树+bit)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3727 题意: 对一段序列进行四种操作: Insert x :在序列尾部插入一个x: Query_1 s ...
- HDU 3727 Jewel 可持久化线段树
Jewel Problem Description Jimmy wants to make a special necklace for his girlfriend. He bought man ...
- HDU 3727 Jewel 主席树
题意: 一开始有一个空序列,然后有下面四种操作: Insert x在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同. Query_1 s t k查询区间\([s,t]\)中第\ ...
- 【HDOJ】3727 Jewel
静态区间第K大值.主席树和划分树都可解. /* 3727 */ #include <iostream> #include <sstream> #include <stri ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- 玩转VSCode-完整构建VSCode开发调试环境
随着VSCode的不断完善和强大,是时候将部分开发迁移到VS Code中了. 目前使用VS2019开发.NET Core应用,一直有一个想法,在VS Code中复刻VS的开发环境,同时迁移到VS Co ...
- 微信小程序api封装(promise)
顺带这是我平时公司切换改变网络环境 直接上代码,我相信就可以懂了, //app.js function fetchApi(url, type, params, method) { return new ...
- 本地Git连接GitLab(服务器)远程仓库
1.简介 远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如github.gitlab等.为了不公开自己项目代码,可以在自己的服务器上搭建自己的项目仓库,最常见的是搭建GitLa ...
- 领扣(LeetCode)最大连续1的个数 个人题解
给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...
- gitbook怎么操作
首先我先说一下什么是GitBook,它和Git没半毛钱关系,定义如下: GitBook 是一个基于 Node.js 的命令行工具,支持 Markdown 和 AsciiDoc 两种语法格式,可以输出 ...
- Unix, Linux以及NT内核和它们各自衍生的系统关系图
- Code Runner for VS Code 突破 1000 万下载量!支持运行超过 40 种语言
记得三年多前,韩老师那时还在写 PHP(是的,没错!在微软写 PHP),同时需要写 Python 和 Node.js .所以在那时,支持多种语言的 VS Code 已经是笔者的主力编辑器了.唯一不足的 ...
- Javascript ----函数表达和形参实参
1.函数是对象,函数名实际上是函数对象的指针 1.函数声明方式 (函数声明提前) function sum(num1,num2){return num1+num2;} 2.函数表达式 var sums ...
- 使用Java窗口程序执行输入的任何cmd命令
利用Java窗口程序来执行用输入的任何命令 实现效果: Java桌面窗口,输入框.按钮,当输入框被输入命令的时候,点击按钮执行命令! 实现代码 package com.remote.remote.ag ...
- cglib测试例子和源码详解
目录 简介 为什么会有动态代理? 常见的动态代理有哪些? 什么是cglib 使用例子 需求 工程环境 主要步骤 创建项目 引入依赖 编写被代理类 编写MethodInterceptor接口实现类 编写 ...