HDU 5372 Segment Game(树状数组)——多校练习7

来源:互联网 发布:淘宝的淘金币怎么赚 编辑:程序博客网 时间:2024/05/23 12:11

Segment Game

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


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
30 00 30 150 10 01 10 10 0
 

Sample Output
Case #1:000Case #2:0102
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
 
/*********************************************************************/

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

放上出题人的解题报告


之所以说只要查询左端点大于等于该线段的左端点的线段数-右端点大于该线段右端点的线段数即为答案,是因为一条线段[l0,r0]完全覆盖另一条线段[l1,r1]当且仅当l0<=l1&&r1<=r0,即左端点大于等于当前线段的左端点,右端点小于等于当前线段的右端点


解题思路:这题思路挺简单,用两个树状数组或者线段树进行单点更新(推荐树状数组,写起来相对方便一些),然后求得区间内包含的线段即可,但有两个注意点:

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

     2.这里的删减操作是删除第i个增加操作的线段,增加操作也是增加一条长为当前第i个增加操作的长度是i,注意是增加操作,不是总的操作!

有疑问欢迎提出,谢谢

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 200005;const int inf = 1000000000;const int mod = 1000000007;int a[2*N],b[2*N],l[N],r[N],x[N],y[N],d[2*N];int lowbit(int t) //计算c[t]展开的项数{    return t&(-t);}int Sum(int n,int *c) //求前n项的和.{    int sum=0;    while(n>0)    {        sum+=c[n];        n-=lowbit(n);    }    return sum;}void update(int i,int *c,int t){    while(i<=d[0])    {        if(t)            c[i]++;        else            c[i]--;        i+=lowbit(i);    }}int main(){    int i,j,k=1,t,n,s;    while(~scanf("%d",&n))    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        for(t=s=i=1;i<=n;i++)        {            scanf("%d%d",&x[i],&y[i]);            if(!x[i])            {                d[s++]=y[i];                d[s++]=y[i]+t;                t++;            }        }        sort(d+1,d+s+1);//将数组d排序        d[0]=unique(d+1,d+s+1)-(d+1);//去除数组d中相邻的重复元素        printf("Case #%d:\n",k++);        for(i=j=1;j<=n;j++)        {            if(x[j])            {                update(l[y[j]],a,0);                update(r[y[j]],b,0);            }            else            {                l[i]=lower_bound(d+1,d+d[0]+1,y[j])-d;                //姑且可以叫做数据压缩吧,因为只要考虑大小关系,所以1 5 8完全可以记为1 2 3,减小树状数组的整体大小                r[i]=lower_bound(d+1,d+d[0]+1,y[j]+i)-d;                printf("%d\n",Sum(r[i],b)-Sum(l[i]-1,a));                update(l[i],a,1);                update(r[i],b,1);                i++;            }        }    }    return 0;}
菜鸟成长记

0 0
原创粉丝点击