树状数组这个真心想了好久,还是没想出来 %%% 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. hdu 4336 Card Collector 容斥原理

    读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...

  2. hdu 3927 Math Geek

    纯数论题,不解释!!!! 代码如下: #include<stdio.h> int main(){ ,m; scanf("%d",&t); while(t--){ ...

  3. SRM588

    250: 有n首歌每首歌有duration和tone,连续唱m首歌会消耗每首歌的duration以及相邻两首歌的tone的差的绝对值的和,给个T,问说在T时间内最对能唱多少歌. 将歌按tone排序后发 ...

  4. SGU128 Snake

    SGU128,题意是给定N个点,问说能不能形成一个闭环G,要求G经过每个点,且在每个点处都有90度的转角,且不能出现自交. 没想出来,通过这提供的思路,由于每个点处都需要90度的转弯,因此每个点处必然 ...

  5. 【HDOJ】4251 The Famous ICPC Team Again

    划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...

  6. 编程概念--使用async和await的异步编程

    Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...

  7. hdu4639Hehe

    http://acm.hdu.edu.cn/showproblem.php?pid=4639 统计连续he的数量恰为斐波序列  不同块进行相乘 #include <iostream> #i ...

  8. Struts1和Struts2都有什么区别?

    总的来说,Struts1 的 Action 是单例模式,因此开发者必须保证它是线程安全的或是同步的,因为Struts 1中每个Action仅有一个实例来处理所有的请求.     但是在用Struts  ...

  9. 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  10. LeetCode Excel Sheet Column Title (输出excel表的列名称)

    题意:给一个数字n,输出excel表的列名称. 思路:其实观察可知道,是个26进制的标记而已.那就模拟一下,每次计算一位时就先左移1位,再进行计算. class Solution { public: ...