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

来源:互联网 发布:淘宝网休闲鞋女 编辑:程序博客网 时间:2024/06/05 09:57

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
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

第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 400007void 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 0
原创粉丝点击