您的位置:首页 > 其它

HDU 5372 Segment Game 树状数组

2015-09-03 22:18 330 查看

Segment Game

[b]Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1407    Accepted Submission(s): 414
[/b]

[align=left]Problem Description[/align]
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)
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

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
4000
("%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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息