HDU 5372 (树状数组 离散化)
2016-05-24 15:06
302 查看
Segment Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1688 Accepted Submission(s): 523
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<=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.
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个操作:
1 x:在[x,x+i]增加一条线段;0 x:删除第x次增加操作的线段.
这个增加线段操作的i表示第i个增加操作,(坑)。每次增加完一条线段输出有多少条
线段包含在他之内。
因为增加的线段长度递增,用树状数组统计下它之外的线段就行了。每次更新增加线段
的右端点和左端点,统计增加线段右端点右方有多少左端点左方有多少。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
#define maxn 211111
int n;
struct node {
int op;
int l, r;
}p[maxn];
vector <int> num;
map <int, int> gg;
int cnt;
int add[maxn][2];
void init () {//离散化
num.clear ();
for (int i = 1; i <= n; i++) {
num.push_back (p[i].l);
num.push_back (p[i].r);
}
sort (num.begin (), num.end ());
cnt = 0;
int Max = num.size ();
gg.clear ();
for (int i = 0; i < Max; i++) {
if (!i || num[i] != num[i-1]) {
gg[num[i]] = ++cnt;
}
}
for (int i = 1; i <= n; i++) {
p[i].l = gg[p[i].l];
p[i].r = gg[p[i].r];
}
}
int l[maxn*2], r[maxn*2];
int lowbit (int x) {
return x&(-x);
}
void add_r (int pos, int num) {
for (int i = pos; i > 0; i -= lowbit (i)) {
r[i] += num;
}
}
int sum_r (int pos) {
int ans = 0;
for (int i = pos; i <= cnt; i += lowbit (i)) {
ans += r[i];
}
return ans;
}
void add_l (int pos, int num) {
for (int i = pos; i <= cnt; i += lowbit (i)) {
l[i] += num;
}
}
int sum_l (int pos) {
int ans = 0;
for (int i = pos; i > 0; i -= lowbit (i)) {
ans += l[i];
}
return ans;
}
void solve () {
int tot = 0;
memset (l, 0, sizeof l);
memset (r, 0, sizeof r);
for (int i = 1; i <= n; i++) {
if (p[i].op) {
add_r (p[i].r, -1);
add_l (p[i].l, -1);
tot--;
}
else {
int sub = sum_r (p[i].r+1) + sum_l (p[i].l-1);
printf ("%d\n", tot-sub);
add_r (p[i].r, 1), add_l (p[i].l, 1);
tot++;
}
}
}
int main () {
int kase = 0;
while (scanf ("%d", &n) == 1) {
printf ("Case #%d:\n", ++kase);
int add_cnt = 1;
for (int i = 1; i <= n; i++) {
int u;
scanf ("%d%d", &p[i].op, &u);
if (p[i].op) {
p[i].l = add[u][0], p[i].r = add[u][1];
}
else {
p[i].l = u, p[i].r = u+add_cnt;
add[add_cnt][0] = p[i].l;
add[add_cnt][1] = p[i].r;
add_cnt++;
}
}
init ();
solve ();
}
return 0;
}
相关文章推荐
- c语言实现TCP的socket通信
- 架构漫谈(五):什么是软件
- 如何提高银行系统的安全性?
- 自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果
- 通过bat命令批量删除VS查找历史记录
- 关于.NET技术前途问题的讨论
- redis协议
- 异形卵
- IT人论房价 (三) 租售比和泡沫
- IT人论房价 (三) 租售比和泡沫
- jpa查询
- Flying to the Mars
- linux 混杂设备miscdevice
- vector 二维
- matlab求凸包
- STL之string
- java基础第十天_集合
- Java中遭遇NaN
- IT项目管理之沉没成本
- Oracle EBS 供应商分行创建