hdu5372 Segment Game

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

题意:给你n个操作,每次增加线段或者删除第i个增加操作中增加的线段,问你每次增加操作中,所增加的线段会覆盖多少条完整的线段。

这题思路挺简单,用树状数组或者线段树进行单点更新,然后求得区间内包含的线段即可,但有两个注意点:

1.每次增加线段都比之前的所有线段长,所以求区间内线段数的时候不用考虑横穿整个区间,只需考虑全部在区间内或者部分在区间内,部分在区间外的线段。

2.这里的删减操作是删除第i个增加操作的线段,增加操作也是增加一条长为当前第i个增加操作的长度是i,注意是增加操作,不是总的操作!这里wa了10多次。。。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 200050
struct node{
	int l,r,f;
}a[maxn];
int b1[2*maxn],b2[2*maxn],pos[2*maxn],caozuo[2*maxn];
int lowbit(int x){
	return x&(-x);
}

void update1(int pos,int num){
	while(pos<=2*maxn){
		b1[pos]+=num;pos+=lowbit(pos);
	}
}

int getsum1(int pos){
	int num=0;
	while(pos>0){
		num+=b1[pos];pos-=lowbit(pos);
	}
	return num;
}

void update2(int pos,int num){
	while(pos<=2*maxn){
		b2[pos]+=num;pos+=lowbit(pos);
	}
}

int getsum2(int pos){
	int num=0;
	while(pos>0){
		num+=b2[pos];pos-=lowbit(pos);
	}
	return num;
}

int main()
{
	int n,m,i,j,d,tot,s1,s2,t1,t2,num1=0,caozuo1;
	while(scanf("%d",&n)!=EOF)
	{
		tot=0;caozuo1=0;
		//memset(caozuo,0,sizeof(caozuo));
		//memset(pos,0,sizeof(pos));
		for(i=1;i<=n;i++){
			scanf("%d%d",&a[i].f,&d);
			if(a[i].f==0){
				caozuo1++;caozuo[caozuo1]=i;
				//caozuo++;a[i].idx=caozuo;
				a[i].l=d;a[i].r=d+caozuo1;
				tot++;pos[tot]=a[i].l;
				tot++;pos[tot]=a[i].r;
			}
			else if(a[i].f==1){
				d=caozuo[d];
				a[i].l=a[d].l;a[i].r=a[d].r;
			}
		}
		num1++;
		printf("Case #%d:\n",num1);
		memset(b1,0,sizeof(b1));
		memset(b2,0,sizeof(b2));
		sort(pos+1,pos+1+tot);
		m=1;
		for(i=2;i<=tot;i++){
			if(pos[i]!=pos[m]){
				m++;pos[m]=pos[i];
			}
		}
		for(i=1;i<=n;i++){
			if(a[i].f==0){
				t1=lower_bound(pos+1,pos+m+1,a[i].l)-pos;
				t2=lower_bound(pos+1,pos+m+1,a[i].r)-pos;
				s1=getsum1(t2)-getsum1(t1-1);
				s2=getsum2(t2);
				printf("%d\n",s1-s2);
				update1(t1,1);
				update2(t1,1);
				update2(t2,-1);
			}
			else if(a[i].f==1){
				t1=lower_bound(pos+1,pos+m+1,a[i].l)-pos;
				t2=lower_bound(pos+1,pos+m+1,a[i].r)-pos;
				update1(t1,-1);
				update2(t1,-1);
				update2(t2,1);
			}
		}
	}
	return 0;
}
/*
5
0 3
0 4
0 1
0 4
0 -1
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 19:04:56

hdu5372 Segment Game的相关文章

[2015hdu多校联赛补题]hdu5372 Segment Game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 x:询问[x, x+i](i为该操作的编号)区间内有多少个完整的线段,并加入线段[x, x+i](线段直接重叠不影响) 1操作 1 x:删除0操作中插入的编号为x的线段,(不影响其他线段,不会重复删除同一线段,删除的线段一定是已经插入的) 解:题目有一个重要的条件:后面插入的线段一定比前面的长.那么

HDU5372——树状数组——Segment Game

http://acm.hdu.edu.cn/showproblem.php?pid=5372 /* 要求有多少线段被现在加进去的线段完全覆盖,所求即左端点比当前加进去的线段大的减去右端点比当前加进去的线段大的,就是覆盖的线段树 用两个树状数组来更新左端点和右端点的值 跟求逆序数那道题目一样,进行排序,二分得到现在排序之后的位置,因为前面加进去的能更新后面的 */ /************************************************ * Author :Powatr

hdu5372(2015多校7)--Segment Game(树状数组)

题目链接:点击打开链接 题目大意:存在一个横轴,有n此操作,0代表在横轴上新增加一条边,1代表删除1条边,其中0 x代表在从x位置开始增加一条边,当第i次加边时,边的长度为i,1 x代表删除第x次加的边.问每当新加入一条边是,这条边能完整的包含几条边. 为什么当时没做这个题,,,已经泪奔,,, 问的是新加的那条边能覆盖多少条边,统计已加入的边的左端点大于或等于新边左端点的个数x,统计已加入的边的右端点大于新边的右端点的个数y.那么新编能覆盖的边也就是x-y,,,,, 用树状数组分别维护一左端点的

2017浙江省赛 E - Seven Segment Display ZOJ - 3962

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix

UNDO segment深入解析

Undo Segment深入解析   在undo自动管理时,设置了undo_retention以后,undo块就存在四种状态. Active:表示正在使用该undo的事务还没有提交或回滚.Inactive:表示该undo上没有活动的事务,该状态的undo可以被其他事务覆盖.Expired:表示该undo持续inactive的时间超过undo_retention所指定的时间.Freed:表示该undo块内容是空的,从来没有被使用过. Undo Retention      After a tran

segment

1.segmentedControlStyle 设置segment的显示样式. typedef NS_ENUM(NSInteger, UISegmentedControlStyle) { UISegmentedControlStylePlain,     // large plain 系统默认平板样式 segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; UISegmentedControlStyleBord

Segment Advisor

Segment Advisor通过分析和检查AWR中关于segments的使用和增长统计信息,以及采样分析segment中的数据,找出哪些segments有可以回收的空间. Segment Advisor运行在维护窗口,以自动维护任务运行. Segment Advisor会给出以下类型的建议: 1.如果Segment Advisor检测到一个对象有大量的空闲空间,会建议进行在线segment收缩:如果segment不支持在线收缩,比如表所在的表空间没有使用ASSM,会建议进行在线重定义. 2.如

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

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