2015 Multi-University Training Contest 7 D(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,也就是说只有两种情况(见下图),那么我们将插入的线段左端点和右端点分别放入两个树状数组,那么查询左端点大于等于该线段的左端点的线段数-右端点大于该线段右端点的线段数即为答案。还要记得离散化。

 

 来自:https://blog.csdn.net/winddreams/article/details/47445171

代码:

#define maxn 200010
struct node
{
    int k , s , e ;
}p[maxn] , que[maxn] ;

int n , m , cnt ;
int c[maxn<<1][2] ;
int a[maxn<<1] ;
int search1(int x)
{
    int low = 1 , mid , high = m ;
    while( low <= high )
    {
        mid = (low + high) >> 1 ;
        if( a[mid] == x ) return mid ;
        if( a[mid] < x ) low = mid + 1 ;
        else high = mid - 1 ;
    }
}
int lowbit(int x)
{
    return x & -x ;
}
void add(int i,int j,int k)
{
    while( i )
    {
        c[i][j] += k ;
        i -= lowbit(i) ;
    }
}
int sum(int i,int j)
{
    int ans = 0 ;
    while( i <= m )
    {
        ans += c[i][j] ;
        i += lowbit(i) ;
    }
    return ans ;
}
int main()
{
    int step = 0 ;
    int i , j ;
    while( scanf("%d", &n) != EOF )
    {
        cnt = m = 1 ;
        for(i = 1 ; i <= n ; i++) 
        { 
            scanf("%d %d", &p[i].k, &p[i].s) ;
            if( !p[i].k ) 
            { 
                a[m++] = p[i].s ;
                a[m++] = p[i].e = p[i].s+cnt ;
                cnt++ ;
            } 
        } 
        sort(a+1,a+m) ;
        m = unique(a+1,a+m) - a ;
        for(i = 1 ; i <= n ; i++)
        {
            if( p[i].k ) continue ;
            p[i].s = search1(p[i].s) ;
            p[i].e = search1(p[i].e) ;
        }
        memset(c,0,sizeof(c)) ;
        cnt = 1 ;
        printf("Case #%d:\n",++step) ;
        for(i=1;i<=n;i++)
        {
            if( p[i].k )
            {
                add(que[ p[i].s ].s,0,-1 ) ;
                add(que[ p[i].s ].e,1,-1 ) ;
            }
            else
            {
                que[cnt++]=p[i];
                printf("%d\n",sum(p[i].s,0)-sum(p[i].e+1,1) ) ;
                add(p[i].s,0,1) ;
                add(p[i].e,1,1) ;
            }
        }
    }
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值