HDU 5372 Segment Game

本文介绍了一种处理线段添加与删除操作的算法,通过离散化和树状数组实现,用以统计每一步操作后线段上完整覆盖的段落数量。输入包括多次操作指令,支持添加与删除指定长度的线段,并输出每次添加操作后被完全覆盖的段落数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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<=2105,n<=7105)

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
离散化+树状数组
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int low(int x) { return x&-x; }
const int maxn = 400005;
int n, m, x[maxn], y[maxn], z[maxn], a[maxn], tot, f[2][maxn], tt = 0, u, v;

void add(int x, int y, int z)
{
	for (int i = y; i <= m; i += low(i)) f[x][i] += z;
}

int get(int x, int y)
{
	int ans = 0;
	for (int i = y; i; i -= low(i)) ans += f[x][i];
	return ans;
}

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		tot = 0;
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &z[i], &x[i]);
			if (!z[i]) {
				a[++tot] = x[i];
				y[i] = x[i] + (++tot) / 2;
				a[tot] = y[i];
			}
			else
			{
				y[i] = a[x[i] + x[i]];
				x[i] = a[x[i] + x[i] - 1];
			}
		}
		sort(a + 1, a + tot + 1);
		m = unique(a + 1, a + tot + 1) - a;
		printf("Case #%d:\n", ++tt);
		for (int i = 1; i <= n; i++)
		{
			u = lower_bound(a + 1, a + m, x[i]) - a;
			v = lower_bound(a + 1, a + m, y[i]) - a;	
			if (!z[i]) printf("%d\n", get(1, v) - get(0, u - 1));
			add(0, u, z[i] ? -1 : 1);
			add(1, v, z[i] ? -1 : 1);
		}
	} 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值