Jewel

Problem Description
 
Jimmy wants to make a special necklace for his girlfriend. He bought many beads with various sizes, and no two beads are with the same size. Jimmy can't remember all the details about the beads, for the necklace is so long. So he turns to you for help.

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)

 

Input

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.
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)

 
Output
 
Output 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.

 

题意:

  

这个是需要离线离散化的,题目中 231 意思是2^31.................

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 2e5++, mod = 1e9+, inf = 0x3fffffff; struct Query{int x,s,t,k;}Q[M];
int l[N*],r[N*],v[N*],san[N],cnt,num,root[N],sz,c,n;
LL ans1,ans2,ans3;
char ch[M][];
void init() {
cnt = ;
root[] = ;
sz = ;
num = ;
ans1 = ;
ans2 = ;
ans3 = ;
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(v,,sizeof(v));
} void update(int &k,int ll,int rr,int x) {
++sz;
l[sz] = l[k];
r[sz] = r[k];
v[sz] = v[k] + ;
k = sz;
if(ll == rr) return ;
if(x <= mid) update(l[k],ll,mid,x);
else update(r[k],mid+,rr,x);
}
int query_1(int x,int y,int ll,int rr,int k) {
if(ll == rr) return ll;
int ret = v[l[y]] - v[l[x]];
if(ret >= k) return query_1(l[x],l[y],ll,mid,k);
else return query_1(r[x],r[y],mid+,rr,k - ret);
}
int query_3(int &k,int ll,int rr,int x) {
if(ll == rr) return ll;
int ret = v[l[k]];
if(ret >= x) return query_3(l[k],ll,mid,x);
else return query_3(r[k],mid+,rr,x-ret);
}
int query_2(int &k,int ll,int rr,int x) {
if(ll == rr) return ;
if(x <= mid) return query_2(l[k],ll,mid,x);
else return v[l[k]] + query_2(r[k],mid+,rr,x);
}
int haxi(int x) {return lower_bound(san+,san+c+,x) - san;}
int main() {
int cas = ;
while(scanf("%d",&n)!=EOF) {
init();
for(int i = ; i <= n; ++i) {
scanf("%s",ch[i]);
if(ch[i][] == 'I') {
scanf("%d",&Q[i].x);
san[++cnt] = Q[i].x;
} else if(ch[i][] == 'Q') {
if(ch[i][] == '') {
scanf("%d%d%d",&Q[i].s,&Q[i].t,&Q[i].k);
} else if(ch[i][] == ''){
scanf("%d",&Q[i].x);
san[++cnt] = Q[i].x;
} else if(ch[i][] == '') {
scanf("%d",&Q[i].k);
}
}
}
sort(san+,san+cnt+);
c = unique(san+,san+cnt+) - san - ;
for(int i = ; i <= n; ++i) {
if(ch[i][] == 'I') {
int fx = haxi(Q[i].x);
update(root[num] = root[num-],,c,fx);
++num;
} else {
if(ch[i][] == '') {
ans1 += san[query_1(root[Q[i].s-],root[Q[i].t],,c,Q[i].k)];
} else if(ch[i][] == '') {
int fx = haxi(Q[i].x);
ans2 += query_2(root[num-],,c,fx);
} else if(ch[i][] == '') {
ans3 += san[query_3(root[num-],,c,Q[i].k)];
}
}
}
printf("Case %d:\n",cas++);
printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
}
return ;
}

HDU 3727 Jewel 可持久化线段树的更多相关文章

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

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

  2. HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)

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

  4. HDU 5820 (可持久化线段树)

    Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...

  5. HDU 5919 Sequence II(可持久化线段树)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...

  6. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  7. HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. HDU 4866 Shooting(持久化线段树)

    view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解 #include <iostream> #include <cstdio> #include & ...

  9. HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和

    To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

随机推荐

  1. C#之枚举类型

    参考: http://www.cnblogs.com/an-wl/archive/2011/04/14/2015815.html 惯例先上MSDN: https://msdn.microsoft.co ...

  2. java web 学习 --第十一天(Java三级考试)

    第十天的学习内容:http://www.cnblogs.com/tobecrazy/p/3473954.html Servlet理论知识: 1.servlet 生成class位置 tomcat编译后生 ...

  3. MVP设计模式的实现

    MVP:界面与业务逻辑分离在Winform中的应用 MVP,Model-View-Presenter的缩写. 在MSDN上,下载了一个示例,http://www.microsoft.com/china ...

  4. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  5. iOS 中CoreData的简单使用

    原文链接:http://www.jianshu.com/p/4411f507dd9f 介绍:本文介绍的CoreData不在AppDelegate中创建,在程序中新建工程使用,即创建本地数据库,缓存数据 ...

  6. 修改VS2010生成的dll文件中的内容

    我的电脑是64为的操作系统,所以先找到下面的路径 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin 找到这个文件:ildasm.exe,如 ...

  7. php安全编程: register_globals的安全性

    register_globals?本身并非安全风险.但是,它为跟踪用户输入和确保应用程序安全增加了难度.为什么会这样? 因为如果打开 register_globals,在全局名称空间和 $_GET.$ ...

  8. storyboard pushViewController 的时候,新的界面黑屏

    storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...

  9. [Linux] vim的高亮查找操作

    reference :  http://blog.chinaunix.net/uid-20732478-id-763411.html 使用了VIM这么久,却一直无法牢记一些基本的操作指令.今天查找一个 ...

  10. CSS居中布局总结

    居中布局 <div class="parent"> <div class="child">demo</div> </d ...