hdu 5372 Segment Game 2015多校联合训练赛#7 树状数组

Segment Game

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


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
 

Source

第i次加入的线段的长度是i,删除的也是第i次加入的线段!!!!第i次有没有!注意啊

先离散化

然后由于长度是递增的。两个树状数组分别记录,起点和终点的个数。


那么加入线段时l,r(起始,终止位置), <= r的终止个数 - < l 的起始位置就是答案了。 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define maxn 400007

void add(int *tree,int p,int n){
    while(p < maxn){
        tree[p] += n;
        p += (p&(-p));
    }
}
int query(int *tree,int p){
    int ans = 0;
    while(p > 0){
        ans += tree[p];
        p -= (p&(-p));
    }
    return ans;
}
int w[maxn],ls[maxn],rs[maxn];
int num[maxn];

int en[maxn],be[maxn];
vector<int> seg;
map<int,int>haha;
int main(){
    int n,tt=1;
    //freopen("1004.in","r",stdin);
    //freopen("1004x.out","w",stdout);
    while(scanf("%d",&n) != EOF){
        haha.clear();
        seg.clear();
        memset(be,0,sizeof(be));
        memset(en,0,sizeof(en));

        int ty,p;
        for(int i = 0;i < n; i++){
            scanf("%d%d",&ty,&p);
            if(ty == 0){
                w[i] = 1;
                ls[i] = p;
                rs[i] = p+seg.size()+1;
                haha[p] = 0;
                haha[p+seg.size()+1] = 0;
                seg.push_back(i);
            }
            else {
                p = seg[p-1];
                w[i] = -1;
                ls[i] = ls[p];
                rs[i] = rs[p];
            }
        }
        int cnt = 2;
        map<int,int>::iterator  it = haha.begin();
        while(it != haha.end()){
            it->second = cnt++;
            it++;
        }
        for(int i = 0;i < n; i++){
            ls[i] = haha[ls[i]];
            rs[i] = haha[rs[i]];
        }

        printf("Case #%d:\n",tt++);
        for(int i = 0;i < n; i++){
            if(w[i] == 1){
                printf("%d\n",query(en,rs[i]) - query(be,ls[i]-1) );
                add(en,rs[i],1);
                add(be,ls[i],1);
            }
            else {
                add(en,rs[i],-1);
                add(be,ls[i],-1);
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值