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

http://acm.hdu.edu.cn/showproblem.php?

pid=5372

Segment Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1284    Accepted Submission(s): 375

Problem Description

Lillian is a clever girl so that she has lots of fans and often receives gifts from her fans.

One day Lillian gets some segments from her fans Lawson with lengths of 1,2,3... and she intends to display them by adding them to a number line.At the i-th add operation,she will put the segment with length of i on the number line.Every time she put the segment
on the line,she will count how many entire segments on that segment.During the operation ,she may delete some segments on the line.(Segments are mutually independent)

Input

There are multiple test cases.

The first line of each case contains a integer n — the number of operations(1<=n<=2?105,∑n<=7?105)

Next n lines contain the descriptions of the operatons,one operation per line.Each operation contains two integers a , b.

if a is 0,it means add operation that Lilian put a segment on the position b(|b|<109)
of the line.

(For the i-th add operation,she will put the segment on [b,b+i] of the line, with length of i.)

if a is 1,it means delete operation that Lilian will delete the segment which was added at the b-th add operation.

Output

For i-th case,the first line output the test case number.

Then for each add operation,ouput how many entire segments on the segment which Lillian newly adds.

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

Hint

For the second case in the sample:

At the first add operation,Lillian adds a segment [1,2] on the line.

At the second add operation,Lillian adds a segment [0,2] on the line.

At the delete operation,Lillian deletes a segment which added at the first add operation.

At the third add operation,Lillian adds a segment [1,4] on the line.

At the fourth add operation,Lillian adds a segment [0,4] on the line

Author

UESTC

Source

2015 Multi-University Training Contest 7

题意:

每次插入一个线段。或删除一个已存在的线段,每次插入后输出当前插入的线段能完整覆盖存在的几条线段。

题解:对于新插入的线段。查询有多少个线段左端点大于等于该线段的左端点。

再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。

时间复杂度nlogn

一共就4种情况。画绘图应该能发现。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
typedef pair<int, int> pii;
typedef long long ll;
const int N = 450007;
struct Tree {
	int c[N], maxn;
	void init(int n) { maxn = n; for (int i = 0; i <= n; i++)c[i] = 0; }
	int lowbit(int x) { return x&-x; }
	int sum(int x) {
		int ans = 0;
		while (x)ans += c[x], x -= lowbit(x);
		return ans;
	}
	void update(int pos, int val) {
		while (pos <= maxn)c[pos] += val, pos += lowbit(pos);
	}
}A, B;
int n;
set<pii> s;
int op[N], l[N], r[N];
pii a[N];
vector<int>G;
int main() {
	int cas = 0;
	while (cin>>n) {
		G.clear();
		int top = 0;
		for (int i = 1; i <= n; i++) {
			rd(op[i]), rd(l[i]);
			if (op[i] == 0)
			{
				G.push_back(l[i]);
				r[i] = l[i] + (++top);
				G.push_back(r[i]);
			}
		}
		printf("Case #%d:\n", ++cas);
		sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end());
		top = 0;
		for (int i = 1; i <= n; i++)
			if (op[i] == 0) {
				l[i] = lower_bound(G.begin(), G.end(), l[i]) - G.begin() + 1;
				r[i] = lower_bound(G.begin(), G.end(), r[i]) - G.begin() + 1;
				a[++top] = { l[i], r[i] };
			}
		A.init(G.size()); B.init(G.size());
		int all = 0;
		for (int i = 1; i <= n; i++)
		{
			if (op[i] == 0)
			{
				int ans = B.sum(r[i]);
				ans -= A.sum(l[i]-1);
				pt(ans); putchar('\n');
				A.update(l[i], 1);
				B.update(r[i], 1);
				all++;
			}
			else {
				A.update(a[l[i]].first, -1);
				B.update(a[l[i]].second, -1);
				all--;
			}
		}
	}
	return 0;
}
时间: 2024-12-21 12:04:45

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

每次输出有几条线段能完全覆盖大于自己和hdu5372相反 树状数组或线段树 poj 2481 Cows

http://poj.org/problem?id=2481 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14762   Accepted: 4886 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one

HDU 5372 Segment Game(线段树+离散化)

题意: 有两种操作: 1. 插入一个线段 2. 删除一个已存在的线段 每次插入后输出当前插入的线段能完整覆盖存在的几条线段. 解析: 线段树上面维护的是两个值,左端点的和,右端点的和 每次插入一条区间[L, R]就, 先询问 [0, R] 的右端点个数 lsum 再询问[L, INF]的左端点的个数 rsum tot表示:当前线段还有几条 那么题目要求的是整条线段的个数就是:lsum+rsum?tot (这里用到了容斥的思想) 再用线段树(或者树状数组)单点增加左节点的个数,和右节点的个数 删除

POJ 2481 树状数组 区间覆盖(POJ2352 Stars 的变形题)(线段化点)

0)学会将题目情景转化为自己熟悉的结构或模型. 题目大意: 每个奶牛有自己的一个区间,求每个奶牛的区间所覆盖的子区间个数(注意,真子集,相等的不算),按照输入的顺序输出. 转化: 要学会将题目情景转化为自己熟悉的模型或结构上.把每个区间的左端x值作为点的x坐标,右端x值作为点的y坐标,就可以把所有区间转化为一个二维坐标图上的点集,而此时每个点左上方的点(同Stars那道题目一样不包括自身)的个数,就是每个区间所覆盖的子区间的个数(对应题目要求,这里或许可以再变形). 同POJ2481 Stars

树状数组与线段树

一:树状数组 树状数组是对一个数组改变某个元素和求和比较实用的数据结构.两中操作都是O(logn). 需求:有时候我们需要频繁地求数组的前k项和或者求数组从小标i到j的和,这样每次最坏情况下的时间复杂度就会为O(N),这样效率太低了.而树状数组主要就是为了解决这样一个问题.树状数组在求和及修改都可以在O(lgN)时间内完成. 树状数组需要额外维护一个数组,我们设为C[N],原数组为A[N], 其中每个元素C[i]表示A[i-2^k+1]到A[i]的和,这里k是i在二进制时末尾0的个数.注意通过位

HDOJ 5338 ZZX and Permutations 线段树+树状数组

[题意]: 给一个排列加上表示循环的括号,问如何让1到n的对应的字典序最大. 从1开始贪心每个数字可以往三个地方走,右边第一个,跳转到左边的某一个,和自己构成循环 对于走到右边第一个的情况,只要判断右边的那个有没有被占据就可以了,如果可以和右边的互换,那么需要在线段树中将右边的数置为0 跳转到左边的某一个,一个数如果跳转到左边的某一个则说明左边的那个是括号开头这个数是括号结尾,用一个线段树查询区间里的最大值,由于括号间是不能重叠的,所以需要用树状数组二分一下左边界.如果这个数是要跳转到左边的某个

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai

cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】

http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 陆伯言军陷八卦阵之中,分明只是一条直路,却怎的也走不到尽头.阵中尽是石堆,以某一石堆为参考,无论向走还是向右,总是会回到出发的石堆,最后幸得一黄姓老翁带路才得脱出. 陆伯言逃离八卦阵后,来到山顶观察此

北大ACM暑期培训(1)——线段树,树状数组

本文出自:http://blog.csdn.net/svitter 今天ACM暑期实训开始了,今天讲述的内容是: 7.14  数据结构(一): 线段树,树状数组,二维线段树. 线段树:invertal tree (称为区间树更加合适) 作用:快速区间查询,用于解决区间统计的有关问题. 重点:同层节点不重叠. 每层最多有两个终止节点. 更新和进行区间分解的时间复杂度均为log(n); 方法:调用会多次使用递归更新插入查询: 空间:开空间的时候,一般情况下开4n大小,2*2log[n] - 1 <=

POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Description BackgroundRaymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just b