HDU3727--Jewel (主席树 静态区间第k大)
Jewel
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985 Accepted Submission(s): 247
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)
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)
#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大)的更多相关文章
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
- POJ2104-- K-th Number(主席树静态区间第k大)
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...
- [poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...
- poj 2104 主席树(区间第k大)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44940 Accepted: 14946 Ca ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- 主席树入门(区间第k大)
主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...
- 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)
题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- poj2761静态区间第k大
例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...
随机推荐
- JSP具体篇——out
out对象 out对象用于在web浏览器上输出信息,而且管理应用server上的输出缓冲区.在使用out对象输出数据时.能够对数据缓冲区进行操作.及时清除缓冲区中残留的数据.为其它输出让出缓冲空间. ...
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- VC++中操作XMLWin32实例
摘要:VC++中操作XML XML在Win32程序方面应该没有在Web方面应用得多,很多Win32程序也只是用XML来存存配置信息而已,而且没有足够的好处的话还不如用ini.VC++里操作XML有两个 ...
- Android系统更改状态栏字体颜色
随着时代的发展,Android的状态栏都不是乌黑一片了,在Android4.4之后我们可以修改状态栏的颜色或者让我们自己的View延伸到状态栏下面.我们可以进行更多的定制化了,然而有的时候我们使用的是 ...
- Java多线程——线程的生命周期和状态控制
一.线程的生命周期 线程状态转换图: 1.新建状态 用new关键字和Thread类或其子类建立一个线程对象后,该线程对象就处于新生状态.处于新生状态的线程有自己的内存空间,通过调用start方法进入就 ...
- css 实现文字过长变成省略号(包含单行的and多行的)
单行的比较简单 但是必须条件同时满足 <DIV STYLE="width: 120px; height: 50px; border: 1px solid blue;overflow: ...
- MFC 堆栈溢出 test dword ptr [eax],eax ; probe page.
今天调试程序的时候,发现一个奇怪的问题,之前调试都没问题的,今早加了一点东西,就出现错误,跳到调试位置,如下4行红色部分 ; Find next lower page and probe cs20: ...
- 一些SQL语句的问题
1.getdate()函数问题 go create table table_1( id int primary key identity, name ) not null, daytime datet ...
- 测试RegExp对象的属性
//测试RegExp对象的属性function testRegExpProperty(){ var regexp = /abc/; //regexp.ignoreCase = true; //无效 c ...
- LRU缓存算法 - C++版
LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法. 实现思路: hashtable + 双向链表 时间复杂度: 插入,查找,删除:O(1) 空间使用 ...