Jewel

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985    Accepted Submission(s): 247

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

貌似很水的一道题,,主席树裸题,不知道为什么现场就几个队了这个题。。对于查询某个数的排名,我是用树状数组搞的。。和主席树没有联系
注意HDU上的题目有一点问题,,不是231  而是2的31次方

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5+;
int lson[maxn*],rson[*maxn],c[maxn*];
int tree[maxn],tot,idx,maxv;
int build (int l,int r)
{
int root = tot++;
c[root] = ;
if (l != r)
{
int mid =(l + r) >> ;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
}
int update(int root,int pos,int val)
{
int newroot = tot++;
int tmp = newroot;
int l = ,r = maxv;
c[newroot] = c[root] + val;
while (l < r)
{
int mid = (l + r) >> ;
if (pos <= mid)
{
rson[newroot] = rson[root];
root = lson[root];
lson[newroot] = tot++;
newroot = lson[newroot];
r = mid;
}
else
{
lson[newroot] = lson[root];
root = rson[root];
rson[newroot] = tot++;
newroot = rson[newroot];
l = mid + ;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int left,int right,int k)
{
int l_root = tree[left-];
int r_root = tree[right];
int l = , r = maxv;
while (l < r)
{
int mid = (l + r) >> ;
if (c[lson[r_root]] - c[lson[l_root]] >= k)
{
r = mid;
l_root = lson[l_root];
r_root = lson[r_root];
}
else
{
l = mid + ;
k -= c[lson[r_root]] - c[lson[l_root]];
l_root = rson[l_root];
r_root = rson[r_root];
}
}
return l;
}
int query(int root,int l,int r,int k)
{
if (l == r)
return l;
int mid = (l + r) >> ;
if (k <= mid)
return query(lson[root],l,mid,k);
else
return c[lson[root]] + query(rson[root],mid+,r,k);
}
ll bit_arr[maxn];
inline int lowbit (int x)
{
return x & -x;
}
void ADD(int x)
{
while (x < maxn)
{
bit_arr[x]++;
x += lowbit (x);
}
}
ll get_sum(int x)
{
ll res = ;
while (x)
{
res += bit_arr[x];
x -= lowbit (x);
}
return res;
}
struct
{
int x,y,k,flag;
}Q[maxn];
ll vec[maxn];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n,cas = ;
while (~scanf ("%d",&n))
{
memset(bit_arr,,sizeof(bit_arr));
printf("Case %d:\n",cas++);
tot = idx = ;
for (int i = ; i < n; i++)
{
char op[];
scanf ("%s",op);
if (strcmp(op,"Insert") == )
{
int x;
scanf ("%d",&x);
Q[i].flag = ;
Q[i].x = x;
vec[idx++] = x;
}
if (strcmp(op,"Query_1") == )
{
int u,v,k;
scanf ("%d%d%d",&u,&v,&k);
Q[i].flag = ;
Q[i].x = u,Q[i].y = v,Q[i].k = k;
}
if (strcmp(op,"Query_2") == )
{
int x;
scanf ("%d",&x);
Q[i].flag = ;
Q[i].x = x;
}
if (strcmp(op,"Query_3") == )
{
int k;
scanf ("%d",&k);
Q[i].flag = ;
Q[i].x = k;
}
}
sort(vec,vec+idx);
idx = unique(vec,vec+idx) - vec;
maxv = idx ;
tree[] = build(,maxv);
int now = ;
ll ans1 = , ans2 = , ans3 = ;
for (int i = ; i < n; i++)
{
if (Q[i].flag == )
{
int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + ;
tree[now] = update(tree[now-],tmp,);
ADD(tmp);
now++;
}
if (Q[i].flag == )
{
ans1 += vec[query(Q[i].x,Q[i].y,Q[i].k)-];
}
if (Q[i].flag == )
{
int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + ;
ans2 += get_sum(tmp);
}
if (Q[i].flag == )
{
ans3 += vec[query(,now-,Q[i].x)-];
}
}
printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
}
return ;
}

HDU3727--Jewel (主席树 静态区间第k大)的更多相关文章

  1. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  2. HDU 2665 Kth number(主席树静态区间第K大)题解

    题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...

  3. POJ2104-- K-th Number(主席树静态区间第k大)

    [转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...

  4. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

  5. poj 2104 主席树(区间第k大)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44940   Accepted: 14946 Ca ...

  6. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  7. 主席树入门(区间第k大)

    主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...

  8. 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)

    题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...

  9. poj2761静态区间第k大

    例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...

随机推荐

  1. 生成唯一的id(转)

    很多朋友都利用md5()来生成唯一的编号,但是md5()有几个缺点:1.无序,导致数据库中排序性能下降.2.太长,需要更多的存储空间.其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid ...

  2. shell 脚本实现的守护进程

    转自:http://blog.csdn.net/cybertan/article/details/3235722 转自:http://blog.sina.com.cn/s/blog_4c451e0e0 ...

  3. 一个好用的Python备份mysql的脚本

    前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...

  4. Python 常量与变量

    先在lib文件夹中定义一个模块 class _const(object): class ConstError(TypeError):pass def __setattr__(self, name, v ...

  5. CSS权威指南学习笔记系列(1)CSS和文档

    题外话:HTML是一种结构化语言,而CSS是它的补充:这是一种样式语言.CSS是前端三板斧之一,因此学习CSS很重要.而我还是菜鸟,所以需要加强学习CSS.这个是我学习CSS权威指南的笔记,如有不对, ...

  6. CSS实现垂直居中的常用方法

    在前端开发过程中,盒子居中是常常用到的.其中 ,居中又可以分为水平居中和垂直居中.水平居中是比较容易的,直接设置元素的margin: 0 auto就可以实现.但是垂直居中相对来说是比较复杂一些的.下面 ...

  7. elasticsearch中的概念简述

    Near Realtime(NRT) Elasticsearch接近实时.从为一个文档建立索引到可被搜索,正常情况下有1秒延迟. Cluster 一个集群有一个唯一的名字,默认是"elast ...

  8. Web Api学习一

    接触WebApi读的第一篇文章: ASP.NET Web API(一):使用初探,GET和POST数据 实践过程中,用的Fiddler模拟Post请求时收到的对象总是为空null 解决:将文章中的内容 ...

  9. Regex.Escape

    C# 字符串变量str 的值为"a\nb"如果直接输出显示的话,就成了:ab需要输出显示为:a\nb问,怎么办?千万别告诉我定义: str=@"a\nb",因为 ...

  10. Jquery插件 easyUI属性汇总

    属性分为CSS片段和JS片段. CSS类定义:1.div easyui-window        生成一个window窗口样式.      属性如下:                   1)mod ...