hdu 5372 Segment Game

Segment Game

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


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
 

Author
UESTC
 

Source
2015 Multi-University Training Contest 7
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5390  5389  5388  5386  5385 
 

#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
#define N 400000 + 10
#define M 200000 + 10

int c[2][N];
int n;

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

int sum(int t, int x)
{
    int res = 0;
    while(x)
    {
        res += c[t][x];
        x -= lowbit(x);
    }
    return res;
}

void update(int t, int x, int v)
{
    while(x <= N)
    {
        c[t][x] += v;
        x += lowbit(x);
    }
}

int x[M], y[M], op[M];
int go[N];

int main()
{
    int kase = 0;
    while(~scanf("%d", &n))
    {
        memset(c, 0, sizeof c);
        int  b;
        int tot = 0;

        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &op[i], &b);
            if(op[i] == 0)
            {
                go[++tot] = b;
                go[++tot] = b + ( tot >> 1 );
                x[i] = b;
                y[i] = b + ( tot >> 1 );
            }
            else
            {
                x[i] = go[(b << 1) - 1];
                y[i] = go[(b << 1)];
            }
        }
        sort(go + 1, go + 1 + tot);
        tot = unique(go + 1, go + 1 + tot) - (go + 1);
        printf("Case #%d:\n", ++kase);
        for(int i = 1; i <= n; i++)
        {
            x[i] = lower_bound(go + 1, go + 1 + tot, x[i]) - go ;
            y[i] = lower_bound(go + 1, go + 1 + tot, y[i]) - go ;
            if(op[i] == 0)
            {
                printf("%d\n", sum(1, y[i]) - sum(0, x[i] - 1));
                update(0, x[i], 1);
                update(1, y[i], 1);
            }
            else
            {
                update(0, x[i], -1);
                update(1, y[i], -1);
            }
        }
    }
    return 0;
}


/*

3
0 0
0 3
0 1
5
0 1
0 0
1 1
0 1
0 0

4
0 3
0 2
0 1
0 0

*/


你可能感兴趣的:(ACM,HDU)