树状数组这个真心想了好久,还是没想出来 %%% www.cppblog.com/Yuan/archive/2010/08/18/123871.html

树状数组求前缀和大于等于k的最大值,第一次看到这种方法,很神奇,就是没看懂= =

二分也是可以求的,不过感觉会慢一些……

思路就是把所有没有询问到的数压缩

例如如果n等于10 值询问到了 2, 7 大概是这样的

【1,2】【3,4,5,6,7】【8,9,10】

  1                2                          3

分成3块,最多分为q块,实现离散化。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
int op[N], a[N], b[N], p[N], no[N];
int n, q; struct BIT {
int arr[N];
int n;
int sum(int p) {
int ans = ;
while (p) {
ans += arr[p];
p -= lowbit(p);
}
return ans;
}
void add(int p, int v) {
while (p <= n) {
arr[p] += v;
p += lowbit(p);
}
}
int find(int k) { //在数组中找第一个大于等于k的位置
int pos = , cnt = ;
for (int i = ; i >= ; --i) {
pos += (<<i);
if (pos >= n || cnt + arr[pos] >= k) pos -= (<<i);
else cnt += arr[pos];
}
return pos+;
}
void init(int n) {
this->n = n;
memset(arr, , sizeof arr);
}
int lowbit(int x) {
return x&-x;
}
} bit; int main()
{
//freopen("in.txt", "r", stdin);
int T, cas = ;
scanf("%d", &T);
while (T--) {
printf("Case %d:\n", ++cas);
scanf("%d%d", &n, &q);
char ch[];
int idx = ;
for (int i = ; i <= q; ++i) {
scanf("%s%d", ch, &a[i]);
if (*ch == 'T') op[i] = ;
else if (*ch == 'Q') op[i] = ;
else op[i] = ;
if (op[i] < ) b[++idx] = a[i];
}
b[++idx] = n;
sort(b+, b++idx);
n = unique(b+, b++idx) - b - ;
bit.init(*q);
for (int i = ; i <= n; ++i) {
bit.add(q+i, b[i]-b[i-]);
no[q+i] = b[i]; // no[i] 数组i处的编号 原编号!!
p[i] = q+i; // p[i] 编号为i的位置
}
int top = q;
for (int i = ; i <= q; ++i) {
if (op[i] == ) {
int x = lower_bound(b+, b++n, a[i]) - b;
bit.add(p[x], -); // 要把x挪到顶端 p[x]位置的数字个数减少一个
no[p[x]]--; // x走了 剩下的是x-1
p[x] = top; // x的位置变成了top
bit.add(top, ); // top位置有一个数字x +1
no[top] = a[i];
top--;
} else if (op[i] == ) {
int x = lower_bound(b+, b++n, a[i]) - b;
printf("%d\n", bit.sum( p[ x ] ));
} else {
int pos = bit.find(a[i]);
int sp = bit.sum(pos);
if (sp == a[i]) printf("%d\n", no[pos]);
else printf("%d\n", no[pos]-(sp-a[i]));
}
}
}
return ;
}

Splay 再补……

HDU 3436--Queue-jumpers (树状数组 or Splay Tree)的更多相关文章

  1. HDU 1541.Stars-一维树状数组(详解)

    树状数组,学长很早之前讲过,最近才重视起来,enmmmm... 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据 ...

  2. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  3. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  4. hdu-5493 Queue(二分+树状数组)

    题目链接: Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. hdu 4267 多维树状数组

    题意:有一个序列 "1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i ...

  6. HDU 4777 Rabbit Kingdom 树状数组

    分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...

  7. HDU 6348 序列计数 (树状数组 + DP)

    序列计数 Time Limit: 4500/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  8. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  9. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

随机推荐

  1. python繁体中文到简体中文的转换

      处理中文字符串遇到了繁体和简体中文的转换,python版: 1.下载zh_wiki.py及langconv zh_wiki.py:https://github.com/skydark/nstool ...

  2. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  3. 批量扫描互联网无线路由设备telnet,并获取WIFI密码

    批量扫描互联网无线路由设备telnet,并获取WIFI密码 http://lcx.cc/?i=4513

  4. 宜昌IT软件资源汇总

    三峡云计算中心大厦 http://castd-ycci.com/ 三峡云计算中心http://www.sanxiacloud.com/index.htm 宜昌创业者(罗新:小卡片做成大产业 年销售额5 ...

  5. Android 自绘TextView解决提前换行问题,支持图文混排

    先看下效果图: 上面是MTextView,下面是默认的TextView. 一.原因 用最简单的全英文句子为例,如果有一个很长的单词,这一行剩余的空间显示不下了,那么规则就是不打断单词,而是把整个单词丢 ...

  6. border-radius的水平和竖直半径

    通常我们设置border-radius都只区分四个角的, 如border-radius: 1em 2em. 其实每个角的border-radius都由两部分组成, 水平半径和竖直半径. 要设置水平和竖 ...

  7. 统计 p-value 含义

    p-value是一种概率:在原假设为真的前提下,出现该样本或比该样本更极端的结果的概率之和. 例子: 我们假设 H0:出现正面的概率是1/2 扔硬币20次出现了14次正面.该样本的单边p-value计 ...

  8. JavaBean 内省API BeanUtils工具 泛型 xml xml约束

    1 什么是JavaBean?有何特征? 1)符合特定规则的类    2)JavaBean分二类:     a)侠义的JavaBean         .私有的字段(Field)         .对私 ...

  9. UML中关系图解

    转自http://blog.csdn.net/duran1986/article/details/5573415 最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有 ...

  10. awesome awesomeness

    Awesome Awesomeness A curated list of amazingly awesome awesomeness.Also available on: Awesome-Aweso ...