题意是指第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 (树状数组)的更多相关文章

  1. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

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

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

  3. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

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

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

  5. hdu 5775 Bubble Sort 树状数组

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

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

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

  7. HDU 3854 Glorious Array(树状数组)

    题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...

  8. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. HDU 5101 Select --离散化+树状数组

    题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...

随机推荐

  1. LeetCode--066--加一

    问题描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. ...

  2. Number Clicker CodeForces - 995E (中途相遇)

    链接 大意: 给定模数$p$, 假设当前在$x$, 则可以走到$x+1$, $x+p-1$, $x^{p-2}$ (mod p), 求任意一条从u到v不超过200步的路径 官方题解给了两个做法, 一个 ...

  3. ubuntu软件(查看文件差异)

    你可以在ubuntu系统自带的软件--->ubuntu软件中心输入:meld diff 就可以安装.

  4. iOS UI-三种简单的动画设置

    一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 ...

  5. js数组的方法小结

    js中数组是一种非常常用数据结构,而且很容易模拟其他的一些数据结构,比如栈和队列.数组的原型Array.prototype内置了很多方法,下面就来小小总结一下这些方法. 检测数组就不用多说了,使用EC ...

  6. linux thtree level page tables

    To translate a virtual address into a physical one, the CPU must take the contents of each level fie ...

  7. POJ 3352 Road Construction 双联通分量 难度:1

    http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...

  8. HttpWebRequest HttpClient

    HttpWebRequest HttpClient 简单封装使用,支持https HttpWebRequest using System; using System.Collections.Gener ...

  9. INTEL NUC下一代计算机 怎么进bios,进不去bios

    手上有个 英特尔® NUC 套件 DE3815TYKHE 玩具一样,200大洋收来的,配置不高,配了个4G内存,加上以前的2.5笔记本硬盘. 放家里当个小服务器玩吧,挺省电的. BIOS 开启fast ...

  10. 在junit中添加fail--有test失败即build Failed

    项目使用jenkins做持续集成,ant来构建,发现在跑junit单元测试的时候,如果有test case失败了,ci的状态是黄色的unstable,而不是红色的failed,看起来很不爽.个人觉得b ...