HDU 5372 Segment Game (树状数组)
题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数。
题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。时间复杂度nlogn
由于坐标范围很大,需要离散。
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstdarg>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define root 1, 1, n
#define lc (k << 1)
#define rc (k << 1 | 1)
#define middle ((L + R) >> 1)
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) //typedef __int64 LL;
//typedef long long LL;
typedef pair<int, int> Pair;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-10;
//LL MOD = 1000000007; struct Operator {
int type, lb;//所有操作,lb表示左边界
}op[MAXN];
int c1[MAXN], c2[MAXN], n;//两个树状数组
int h1[MAXN], h2[MAXN], L[MAXN];//用于hash,L[i]表示第i次询问的左边界 int lowbit(int x) { return x & (-x); } void update(int *c, int n, int k, int v) {
while(k <= n) {
c[k] += v;
k += lowbit(k);
}
} int query(int *c, int k) {
int ans = ;
while(k > ) {
ans += c[k];
k -= lowbit(k);
}
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
FIN;
// FOUT;
#endif
int cas = ;
while(~scanf("%d", &n)) {
mem0(c1); mem0(c2);
int cnt = , sz1 = , sz2 = ;
rep (i, , n) {
scanf("%d %d", &op[i].type, &op[i].lb);
if(op[i].type == ) {
cnt ++;
h1[sz1++] = op[i].lb;
h2[sz2++] = op[i].lb + cnt;
L[cnt] = op[i].lb;
}
} sort(h1, h1 + sz1); sz1 = unique(h1, h1 + sz1) - h1;
sort(h2, h2 + sz2); sz2 = unique(h2, h2 + sz2) - h2;
printf("Case #%d:\n", ++cas); cnt = ;
rep (i, , n) {
if( !op[i].type ) { //(cnt_seg - query(c1, lb - 1)) - (cnt_seg - query(c2, rb)) = q2(rb) - q1(lb - 1)
++cnt;
int lb = lower_bound(h1, h1 + sz1, op[i].lb) - h1 + ;
int rb = lower_bound(h2, h2 + sz2, op[i].lb + cnt) - h2 + ;
printf("%d\n", query(c2, rb) - query(c1, lb - ));
update(c1, sz1, lb, );
update(c2, sz2, rb, );
}
else {
update(c1, sz1, lower_bound(h1, h1 + sz1, L[op[i].lb]) - h1 + , -);
update(c2, sz2, lower_bound(h2, h2 + sz2, L[op[i].lb] + op[i].lb) - h2 + , -);
}
}
}
return ;
}
HDU 5372 Segment Game (树状数组)的更多相关文章
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 5101 Select --离散化+树状数组
题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...
随机推荐
- English trip -- MC(情景课)3 D
xu言: have a nice weekend... sentences How many people are there in you family? they are 3 people in ...
- Spring Boot 启动 Struts 报冲突
错误信息如下: Caused by: com.opensymphony.xwork2.config.ConfigurationException: Bean type class com.opensy ...
- linux中tomcat内存溢出PermGen space
1.若是部署时候,一个tomcat下面项目越少越好,单独为一个项目配置tomcat(在客户给你充足的端口的情况下) 2.在维护的时候,若一个tomcat下放多个项目的话,这时候可以把所有jar包放在t ...
- UVA-10020 Minimal coverage(贪心)
题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间. 题目分析:简单的区间覆盖问题.可以按这样一种策略进行下去:在所有区间起点.长度有序的前提下,对于当前起点 ...
- java回收算法
两个最基本的java回收算法:复制算法和标记清理算法 复制算法:两个区域A和B,初始对象在A,继续存活的对象被转移到B.此为新生代最常用的算法 ...
- SVN 创建分支
TortoiseSVN Client创建分支:
- UVALive 5984
题目链接:Save the Students! #include<stdio.h> #include<string.h> #include<iostream> #i ...
- jsonp 遍历文档
遍历文档 将html解析成一个Document后,就可以使用类似Dom的方法进行操作 File input = new File("/tmp/input.html"); Docum ...
- 关于 lerp();
value lerp(value s, value a, value b ); 该函数返回的值为:a + s * (b - a) ,是一个处于 [a, b] 之间的值. 当s=0, 该函数返回a :当 ...
- Linux内核参数优化记录
//fs.file-max 最大打开文件数 //fs.nr_open=20480000 单个进程允许打开的文件句柄上限 //信号量及共享内存,可以使用ipcs -l来获取 //kernel.sem 信 ...