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
题意:给你n个操作,每次添加线段或者删除第i个添加操作中添加的线段,问你每次添加操作中,所添加的线段会覆盖多少条完整的线段。
这题思路挺简单,用树状数组或者线段树进行单点更新,然后求得区间内包括的线段就可以,但有两个注意点:
1.每次添加线段都比之前的所有线段长,所以求区间内线段数的时候不用考虑横穿整个区间。仅仅需考虑所有在区间内或者部分在区间内。部分在区间外的线段。
2.这里的删减操作是删除第i个添加操作的线段,添加操作也是添加一条长为当前第i个添加操作的长度是i。注意是添加操作,不是总的操作! 这里wa了10多次。。。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 200050
struct node{
int l,r,f;
}a[maxn];
int b1[2*maxn],b2[2*maxn],pos[2*maxn],caozuo[2*maxn];
int lowbit(int x){
return x&(-x);
} void update1(int pos,int num){
while(pos<=2*maxn){
b1[pos]+=num;pos+=lowbit(pos);
}
} int getsum1(int pos){
int num=0;
while(pos>0){
num+=b1[pos];pos-=lowbit(pos);
}
return num;
} void update2(int pos,int num){
while(pos<=2*maxn){
b2[pos]+=num;pos+=lowbit(pos);
}
} int getsum2(int pos){
int num=0;
while(pos>0){
num+=b2[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int n,m,i,j,d,tot,s1,s2,t1,t2,num1=0,caozuo1;
while(scanf("%d",&n)!=EOF)
{
tot=0;caozuo1=0;
//memset(caozuo,0,sizeof(caozuo));
//memset(pos,0,sizeof(pos));
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].f,&d);
if(a[i].f==0){
caozuo1++;caozuo[caozuo1]=i;
//caozuo++;a[i].idx=caozuo;
a[i].l=d;a[i].r=d+caozuo1;
tot++;pos[tot]=a[i].l;
tot++;pos[tot]=a[i].r;
}
else if(a[i].f==1){
d=caozuo[d];
a[i].l=a[d].l;a[i].r=a[d].r;
}
}
num1++;
printf("Case #%d:\n",num1);
memset(b1,0,sizeof(b1));
memset(b2,0,sizeof(b2));
sort(pos+1,pos+1+tot);
m=1;
for(i=2;i<=tot;i++){
if(pos[i]!=pos[m]){
m++;pos[m]=pos[i];
}
}
for(i=1;i<=n;i++){
if(a[i].f==0){
t1=lower_bound(pos+1,pos+m+1,a[i].l)-pos;
t2=lower_bound(pos+1,pos+m+1,a[i].r)-pos;
s1=getsum1(t2)-getsum1(t1-1);
s2=getsum2(t2);
printf("%d\n",s1-s2);
update1(t1,1);
update2(t1,1);
update2(t2,-1);
}
else if(a[i].f==1){
t1=lower_bound(pos+1,pos+m+1,a[i].l)-pos;
t2=lower_bound(pos+1,pos+m+1,a[i].r)-pos;
update1(t1,-1);
update2(t1,-1);
update2(t2,1);
}
}
}
return 0;
}
/*
5
0 3
0 4
0 1
0 4
0 -1
*/


最新文章

  1. 浏览器中CSS的BUG
  2. uC/OS-II测试(TEST)块
  3. 移动互联网实战--wifi定位和架构
  4. BaseActivity与BaseFragment的封装
  5. Ubuntu 安装 Courier New字体
  6. IIC协议及其对ACK应答信号的处理
  7. Javascript:阻止浏览器默认右键事件,并显示定制内容
  8. Broadcast详解
  9. iOS消息推送相关
  10. UPS电源效果及有关名词解析
  11. A*算法的理解与简单实现
  12. ServletContainerInitializer初始化器
  13. Linux:Day20(上) openssh和CA
  14. 前台js根据当前时间生成订单号
  15. 3D游戏的角色移动和旋转
  16. PhantomJS在Selenium中被标记为过时的应对措施
  17. C#数组维数及不同维数中元素个数的获取
  18. 如何使用Ubuntu中的avdManager命令行为“System-Image;Androd-27;GoogleAPI;x86”创建一个AVD?
  19. Java学习--编译器javac
  20. Gitlab Webhooks, External Services, and API(二)

热门文章

  1. strlen() 和 sizeof() 的区别
  2. iOS开发—— UIImage数据的保存
  3. keytool常用操作
  4. 学习《深度学习入门:基于Python的理论与实现》高清中文版PDF+源代码
  5. HDU 4862 Jump 费用流
  6. Vue2.0组件实现动态搜索引擎(一)
  7. ifreq、ifconf
  8. C# 从需要登录的网站上抓取数据
  9. 【Android 进阶】图片载入框架之Glide
  10. Ural 1303 Minimal Coverage(贪心)