树状数组这个真心想了好久,还是没想出来 %%% 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. express中ejs模板引擎

    1.在 app.js 中通过以下两个语句设置了 引擎类型 和页面模板的位置: app.set('views', __dirname + '/views'); app.set('view engine' ...

  2. HDU2697+DP

    dp[i][j]:从前i个中挑出某些且cost不超过j的最大val. dp[i][j]:应该有1到i-1的dp[k][j-?]来更新!! /* DP dp[i][j]:从前i个中挑出某些且cost不超 ...

  3. [Ruby on Rails系列]1、开发环境准备:Vmware和Linux的安装

    Ruby on Rails是一个采用Ruby语言的遵循MVC模式的Web开发框架.使用RoR会得到更加快速爽快的Web开发体验.相比于Java EE,该框架使Web开发的速度和效率变得更加轻快和敏捷. ...

  4. 一周一话题之四(JavaScript、Dom、jQuery全面复习总结<Dom篇>)

    -->目录导航 一. 初探Dom 1. Dom介绍 二. Dom基础 1. window顶级对象 2. body.document对象事件 3. 通用的HTML元素的事件 4. 冒泡事件 5. ...

  5. DVB系统中PCR的生成和PCR校正

    http://blog.csdn.net/chenliangming/article/details/3616720 引自<广播电视信息>2008年1月 从数字电视前端系统功能上来讲,传统 ...

  6. Java经典书籍

    Java Web开发教程---孙霞JSP应用开发详解(第三版)---刘晓华.张健.周慧贞Spring in Action---Craig Walls精通Struts基于MVC的Java Web设计与开 ...

  7. 解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)

    根据 src/corelib/global.h template <typename T>class QGlobalStatic{public: T *pointer; inline QG ...

  8. Python之数据结构篇

    简介: 数据结构是可以处理一些数据的结构,或者说,他们是用来存储一组相关数据的.在python中有三种内建的数据结构,分别是列表.元组合字典.我们将会学习如何使用它们是编程变得简单. 列表 list是 ...

  9. Redpine的Lite-Fi解决方案获Wi-Fi CERTIFIED认证

    应用微电路公司(AMCC)和Redpine Signals日前共同宣布,已合作开发出新一代基于Power Architecture的嵌入式Wi-Fi连接性解决方案,目前双方已经在AMCC的PowerP ...

  10. 【HDOJ】4056 Draw a Mess

    这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...