HDU 5372 (树状数组 离散化)

本文介绍了一种基于线段操作的游戏算法,通过增加和删除线段来统计被完全覆盖的线段数量。利用树状数组进行高效计算,适用于处理大量线段操作的问题。

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

Segment Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1688    Accepted Submission(s): 523


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
 


题意:n个操作:

1 x:在[x,x+i]增加一条线段;0 x:删除第x次增加操作的线段.

这个增加线段操作的i表示第i个增加操作,(坑)。每次增加完一条线段输出有多少条

线段包含在他之内。

因为增加的线段长度递增,用树状数组统计下它之外的线段就行了。每次更新增加线段

的右端点和左端点,统计增加线段右端点右方有多少左端点左方有多少。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
#define maxn 211111

int n;
struct node {
	int op;
	int l, r;
}p[maxn];
vector <int> num;
map <int, int> gg;
int cnt;
int add[maxn][2];

void init () {//离散化
	num.clear ();
	for (int i = 1; i <= n; i++) {
		num.push_back (p[i].l);
		num.push_back (p[i].r);
	}
	sort (num.begin (), num.end ());
	cnt = 0;
	int Max = num.size ();
	gg.clear ();
	for (int i = 0; i < Max; i++) {
		if (!i || num[i] != num[i-1]) {
			gg[num[i]] = ++cnt;
		}
	}
	for (int i = 1; i <= n; i++) {
		p[i].l = gg[p[i].l];
		p[i].r = gg[p[i].r];
	}
}

int l[maxn*2], r[maxn*2];

int lowbit (int x) {
	return x&(-x);
}

void add_r (int pos, int num) {
	for (int i = pos; i > 0; i -= lowbit (i)) {
		r[i] += num;
	}
}

int sum_r (int pos) {
	int ans = 0;
	for (int i = pos; i <= cnt; i += lowbit (i)) {
		ans += r[i];
	}
	return ans;
}

void add_l (int pos, int num) {
	for (int i = pos; i <= cnt; i += lowbit (i)) {
		l[i] += num;
	}
}

int sum_l (int pos) {
	int ans = 0;
	for (int i = pos; i > 0; i -= lowbit (i)) {
		ans += l[i];
	}
	return ans;
}

void solve () {
	int tot = 0;
	memset (l, 0, sizeof l);
	memset (r, 0, sizeof r);
	for (int i = 1; i <= n; i++) {
		if (p[i].op) {
			add_r (p[i].r, -1);
			add_l (p[i].l, -1);
			tot--;
		}
		else {
			int sub = sum_r (p[i].r+1) + sum_l (p[i].l-1);
			printf ("%d\n", tot-sub);
			add_r (p[i].r, 1), add_l (p[i].l, 1);
			tot++;
		}
	}
}

int main () {
	int kase = 0;
	while (scanf ("%d", &n) == 1) {
		printf ("Case #%d:\n", ++kase);
		int add_cnt = 1;
		for (int i = 1; i <= n; i++) {
			int u;
			scanf ("%d%d", &p[i].op, &u);
			if (p[i].op) {
				p[i].l = add[u][0], p[i].r = add[u][1];
			}
			else {
				p[i].l = u, p[i].r = u+add_cnt;
				add[add_cnt][0] = p[i].l;
				add[add_cnt][1] = p[i].r;
				add_cnt++;
			}
		}
		init ();
		solve ();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值