HDU 5372 Segment Game 树状数组

13 篇文章 0 订阅
5 篇文章 0 订阅

Segment Game

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


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对ab, 若第i次操作中, 若a=0则表示把线段(b, b+i)加入,输出加入线段会覆盖到的完整的线段的数目;若a=1则表示把第b次加入的线段删除
思路:用两个树状数组记录第i条线段的L,R, 离散化, 加入线段时只要计算R值比Ri小的数目 - L值比Li小的数目即为结果
#include <stdio.h>
#include <map>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 300000
vector<int>seg;
map<int, int>p;
int op[maxn], L[maxn], R[maxn], lc[maxn], rc[maxn];
void add(int *a, int i, int x)
{
    while(i <= maxn)
    {
        a[i] += x;
        i += i & -i;
    }
}
int sum(int *a, int i)
{
    int s = 0;
    while(i > 0)
    {
        s += a[i];
        i -= i & -i;
    }
    return s;
}
int main()
{
    int n, a, b, i, cnt, k;
    k = 1;
    while(scanf("%d", &n) != EOF)
    {
        p.clear();
        seg.clear();
        memset(lc, 0, sizeof(lc));
        memset(rc, 0, sizeof(rc));
        for(i = 0;i < n;i++)
        {
            scanf("%d %d", &a, &b);
            if(a == 0)
            {
                op[i] = 1;
                L[i] = b;
                R[i] = b + seg.size() + 1;
                seg.push_back(i);
                p[L[i]] = p[R[i]] = 0;
            }
            else
            {
                op[i] = -1;
                b = seg[b - 1];
                L[i] = L[b];
                R[i] = R[b];
                p[L[i]] = p[R[i]] = 0;
            }
        }

        map <int, int>::iterator it = p.begin();
        cnt = 1;
        while(it != p.end())
        {
            it->second = cnt++;
            it++;
        }
        for(i = 0;i < n;i++)
        {
            L[i] = p[L[i]];
            R[i] = p[R[i]];
        }

        printf("Case #%d:\n", k++);
        for(i = 0;i < n;i++)
        {
            if(op[i] == 1)
            {
                printf("%d\n", sum(rc, R[i]) - sum(lc, L[i] - 1));
                add(lc, L[i] ,1);
                add(rc, R[i] ,1);
            }
            else
            {
                add(lc, L[i] ,-1);
                add(rc, R[i] ,-1);
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值