题意:有两种操作:(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. 落谷 P1734 最大约数和

    题目描述 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 输入格式 输入一个正整数S. 输出格式 输出最大的约数之和. 输入输出样例 输入 #1复制 11 输出 #1复制 ...

  2. mybatis源码配置文件解析之三:解析typeAliases标签

    在前边的博客在分析了mybatis解析settings标签,<mybatis源码配置文件解析之二:解析settings标签>.下面来看解析typeAliases标签的过程. 一.概述 在m ...

  3. 容易忽略的CSS3属性

    flex布局模型 1. flex-direction: row |  row-reverse | column | column-reverse flex-direction 属性决定主轴的方向(即项 ...

  4. 推荐一个小而美的Python代码格式化工具

    代码可读性是评判代码质量的标准之一,有一个衡量代码质量的标准是 Martin 提出的 “WFT” 定律,即每分钟爆出 “WTF” 的次数.你在读别人代码或者做 Code Review 的时候有没有 “ ...

  5. java算法-二分法查找实现

    什么是二分法查找 首先,使用二分法查找的前提是:被查找的数组已排好序 具体实现: 假如有一组数为3,12,24,36,55,68,75,88要查给定的值24.可设三个变量front,mid,end分别 ...

  6. 轻量级mysql安装教程-避免采坑

    1:安装包获取 安装了很多次,来来回回踩,踩出了一片光明大道,简单好用. 百度网盘获取:链接:https://pan.baidu.com/s/13frFBTODaMeADZOHj5KdJQ 提取码:a ...

  7. 7、窗口函数 & Windows 的 Operator demo

    代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 一.窗口函数 在定义了窗口分配器之后,我们需要为每一个窗口明确的指定计算逻辑,这个就是窗口函数要做的事情,当 ...

  8. 【mybatis annotation】数据层框架应用--Mybatis(二) 基于注解实现数据的CRUD

    使用MyBatis框架进行持久层开发 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBa ...

  9. jstat命令查看JVM 的GC状态

    转载于   https://www.cnblogs.com/alter888/p/10407952.html jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: jstat ...

  10. GitHub 被指审查内容,著名“换脸”开源项目 deepfake 遭限制访问

    开发四年只会写业务代码,分布式高并发都不会还做程序员? >>>   昨天 Hacker News 上一条关于 deepfake 开源项目的帖子(https://news.ycombi ...