题意:有两种操作:(1)插入线段,第i次插入的线段左边界为Li,长度为i (2)删除线段,删除第x次插入的线段。每次插入线段之前询问有多少条线段被它覆盖。

思路:由于插入的线段长度是递增的,所以第i次插入的线段的长度比以前插入的所有线段都要长,从以前插入的线段里面任取一条,考虑其与当前线段的位置关系,主要有以下三种:

图中,下方表示当前线段。注意到,只有被当前线段覆盖的线段,它的左右边界和当前线段的左右边界的相对位置是不同的。也就是说,查询有多少个线段的右端点小于等于该线段右端点,再查询有多少条线段左端点小于该线段的左端点, 两者之差就是答案。因为不符合要求的线段同时进入了前者和后者,而符合要求的答案只进入了前者。

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//#pragma comment(linker, "/STACK:1024000000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} #if 0
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const double EPS = 1e-8;
#endif /* -------------------------------------------------------------------------------- */ const int maxn = 4e5 + ; int u[maxn], v[maxn], w[maxn], tot;
pii rem[maxn]; class TA {
int C[maxn], n;
inline int lowbit(int x) { return x & -x; }
public:
void init(int n) {
this->n = n;
for (int i = ; i <= n; i ++) C[i] = ;
}
void update(int p, int x) {
while (p <= n) {
C[p] += x;
p += lowbit(p);
}
}
int query(int p) {
int ans = ;
while (p) {
ans += C[p];
p -= lowbit(p);
}
return ans;
}
};
TA lta, rta; inline int find(int x) {
return lower_bound(w, w + tot, x) - w + ;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int cas = , n;
while (cin >> n) {
tot = ;
int len = ;
for (int i = ; i < n; i ++) {
scanf("%d%d", u + i, v + i);
if (u[i] == ) {
rem[len] = mp(v[i], v[i] + len);
w[tot ++] = v[i];
w[tot ++] = v[i] + len ++;
}
}
sort(w, w + tot);
tot = unique(w, w + tot) - w;
lta.init(tot);
rta.init(tot);
printf("Case #%d:\n", ++ cas);
len = ;
for (int i = ; i < n; i ++) {
if(u[i] == ) {
pii buf = rem[v[i]];
int lid = find(buf.X), rid = find(buf.Y);
lta.update(lid, - );
rta.update(rid, - );
}
else {
int lid = find(v[i]), rid = find(v[i] + len ++);
printf("%d\n", rta.query(rid) - lta.query(lid - ));
lta.update(lid, );
rta.update(rid, );
}
}
}
return ;
}

[hdu5372 Segment Game]树状数组的更多相关文章

  1. HDU 5372 Segment Game (树状数组)

    题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数. 题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端 ...

  2. 当前插入的线段能完整覆盖存在的几条线段 树状数组 HDU 5372 Segment Game

    http://acm.hdu.edu.cn/showproblem.php? pid=5372 Segment Game Time Limit: 3000/1500 MS (Java/Others)  ...

  3. 2015 多校联赛 ——HDU5372(树状数组)

    Sample Input 3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0   Sample Output Case #1: 0 0 0 Case #2: 0 1 0 2 有0, ...

  4. POJ3928Ping pong[树状数组 仿逆序对]

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 Descrip ...

  5. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  6. POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2691   Accepted: 996 Descript ...

  7. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

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

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

  9. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

随机推荐

  1. Rank of Tetris 杭电 拓扑排序加并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  2. Xshell 中文提示乱码

    1.Alt+P 打开配置对话框,点击终端->编码,选择Unicode(utf-8)编码

  3. 详解 HashMap

    本篇博文的知识点,在我们的日常生活中,应用十分广阔.比如:每个学生,都有自己的对应的学号.每一个公民,都有自己的身份证号- - 相信看到这里,有的同学基本上已经猜到了这个类的主要用途.那么,话不多说, ...

  4. testNG groups 分组测试

    testNG的分组通过xml文件<groups>标签和@Test(group="组名")来实现分组 xml中关于分组的详细介绍,通过groups 定义一个组,通过< ...

  5. [Yii2] 快速套模板,加载JS,CSS(HTML标签<base>)

    刚开始学,弄了好久,不知道怎么加载JS.CSS,以及怎么不加载YII2自带的模板!其实真的好简单! 补: 其实是我垃圾,YII2默认访问路径是WEB,所以把style文件放到web下就能加载! 首先把 ...

  6. redis: Set集合类型(五)

    Set里面的值是不能重复的 Set设置值(头部):sadd myset hello Set获取值:smembers myset 检查Set是否包含某个元素:sismember myset hello ...

  7. 硬盘性能测试工具之bonnie++

    bonnie++ 官方站点 先写内存的两倍,内存较大时比较耗时.适合简单的测试场景. # bonnie++ -u root 写测试 读测试 Version 1.97 ------Sequential ...

  8. 认证与授权】Spring Security系列之认证流程解析

    上面我们一起开始了Spring Security的初体验,并通过简单的配置甚至零配置就可以完成一个简单的认证流程.可能我们都有很大的疑惑,这中间到底发生了什么,为什么简单的配置就可以完成一个认证流程啊 ...

  9. 不停机还能替换代码?6年的 Java程序员表示不可思议

    相信很多人都有这样一种感受,自己写的代码在开发.测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,而线上调试代码又很麻烦,让人头疼得很.不过, 阿里巴巴出了一款名叫Arth ...

  10. 天池Docker学习赛笔记

    容器的基本概念 什么是容器? 容器就是一个视图隔离.资源可限制.独立文件系统的进程集合.所谓"视图隔离"就是能够看到部分进程以及具有独立的主机名等:控制资源使用率则是可以对于内存大 ...