HDU 5372 Segment Game 树状数组

链接

Segment Game

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

Total Submission(s): 273    Accepted Submission(s): 48

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<=,<=)

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|<)
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

Source

2015 Multi-University Training Contest 7

题意:

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

题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。时间复杂度nlogn

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

#pragma comment(linker, "/STACK:102400000,102400000")
#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;
}
/*
99
7
1 2 2 1 3 !1 2
/*
1
????

0
1 8 3 7 2
*/
时间: 2024-12-29 23:52:51

HDU 5372 Segment Game 树状数组的相关文章

hdu 5372 Segment Game(树状数组+离散化)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:有两种操作,输入a b,①a==0,插入第i条线段[b,b+i],输出[b,b+i]内有多少条完全包含于[b,b+i]的线段②a==1,删除插入的第b条线段. 分析:由于插入的线段长度是递增的,那么就不存在包含[b,b+i]的线段.那么完全包含于[b,b+i]的线段的数目=右端点小于等于b+i的线段的数目-左端点小于b的线段的数目.由于输入的数比较大,离散化一下就行了. 代码: #inclu

HDOJ 5372 Segment Game 树状数组+离散化

因为这题的线段长度是递增的....所以: 题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点. 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案.用两个树状数组搞定.时间复杂度nlog Segment Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 975    Accepted Submiss

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno