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. Android 自定义View (三) 圆环交替 等待效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24500107 一个朋友今天有这么个需求(下图),我觉得那自定义View来做还是很 ...

  2. Android 自定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...

  3. sass笔记-3|Sass基础语法之样式复用和保持简洁

    上一篇详述了Sass如何嵌套.导入和注释这3个基本方式来保持条理性和可读性,这一篇更进一步地阐述sass保持样式复用和简洁的方式--混合器和选择器继承--这两种方式都能复用样式,使用它们也不难,但一定 ...

  4. (转)js获取url参数值

    明天有空编辑下 今天做项目遇到js取得url地址问号后面的参数,找了下面的,用着非常好,项目是选项卡样式的,也就是一点击二级分类,底下的同样名字的背景变红(选项卡倍选中) http://www.cnb ...

  5. Repeater的ItemDataBound 事件中e.Item.DataItem 的数据类型

    1.使用DataSet和DataTable绑定数据源时 DataRowView view = (DataRowView)e.Item.DataItem; 2.DataReader绑定数据源时 Syst ...

  6. TextView drawablePadding没有效果

    1.当TextView 设置宽度设置为match_parent的时候 TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远 2.当TextVi ...

  7. JUnit报initializationError的解决方法

    在新搭建的环境上测试时,一个模块发现错误: java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing 一看就是缺少Class.多方查找,发现 ...

  8. 手动安装svn到eclipse

    今天为了装个svn搞得我焦头烂额~ 1.下载site-1.10.10.zip.(http://download.csdn.net/download/codepython/9082533) 2.在ecl ...

  9. THINK PHP U的用法

    public function index(){ //$db=new \Think\Model(); //$db=M('msg'); //$result=$db->query("sel ...

  10. view ondraw

    窗口发生重绘时会被应用程序的窗口框架给调用 要使输出的东西始终能在窗口中看到 就可以使用该函数  窗口从到有的时候就会产生WM_PAINT消息,让窗口发生重绘 这是程序就会执行到ONDRAW函数处 所 ...