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


hdu5372 Segment Game的更多相关文章

  1. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  2. [hdu5372 Segment Game]树状数组

    题意:有两种操作:(1)插入线段,第i次插入的线段左边界为Li,长度为i (2)删除线段,删除第x次插入的线段.每次插入线段之前询问有多少条线段被它覆盖. 思路:由于插入的线段长度是递增的,所以第i次 ...

  3. kafka的log存储解析——topic的分区partition分段segment以及索引等

    转自:http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相 ...

  4. ORA-10635: Invalid segment or tablespace type

    上周星期天在迁移数据时,碰到了ORA-10635: Invalid segment or tablespace type 错误,当时的操作环境如下: 操作系统版本: [oracle@xxxxx scr ...

  5. Oracle逻辑结构(TableSpace→Segment→Extent→Block)

    一.逻辑体系结构图 二.逻辑结构图组成介绍 从上表可以看出,一个数据库是由多个表空间(tablespace)组成,一个表空间又由多个段(segment)组成,一个段又由多个区(extent)组成,一个 ...

  6. IOS开发--自定义segment控件,方便自定义样式

    系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需 这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控 ...

  7. 填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT

    填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT 警告:发生了重入! 解释:在主循环里调用了一个函数,而在中断服务中又一次调用了同样的函数.当主循环运行到该函数中 ...

  8. c程序中出现segment error 和 bus error 的原因

    在c程序中,经常会遇到段错误(segment error)和总线错误(bus error),这两种问题出现的原因可能如下 段错误: 对一个NULL指针解引用. 访问程序进程以外的内存空间. 实际上,第 ...

  9. 【BZOJ-3165】Segment 李超线段树(标记永久化)

    3165: [Heoi2013]Segment Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 368  Solved: 148[Submit][Sta ...

随机推荐

  1. layer是什么

    layer是什么 总结 layer就是一个web弹框 简介 layer是一款web弹层组件,致力于服务各个水平段的开发人员. 可以让你想到即可做到的web弹窗 概述 [1]  layer,一个可以让你 ...

  2. zzulioj--1776--和尚特烦恼2——第几个素数(技巧模拟)

    1776: 和尚特烦恼2--第几个素数 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 267  Solved: 100 SubmitStatusWeb ...

  3. [JZOJ4274] [NOIP2015模拟10.28B组] 终章-剑之魂 解题报告(二进制)

    Description [背景介绍]古堡,暗鸦,斜阳,和深渊……等了三年,我独自一人,终于来到了这里……“终焉的试炼吗?就在这里吗?”我自言自语道.“终焉的试炼啊!就在这里啊!”我再一次自言自语道.“ ...

  4. POJ 3669 简单BFS

    标号 搜 完了-- //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...

  5. HDU-1035 Robot Motion 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...

  6. springMVC+request.session实现用户登录和访问权限控制

    用springmvc mybatis实现用户登录登出功能,使用session保持登录状态,并实现禁止未登录的用户访问.感谢谷歌资源,在这里做个学习记录加深自己的印象. 原文在我的https://my. ...

  7. P3649 [APIO2014]回文串(回文树)

    题目描述 给你一个由小写拉丁字母组成的字符串 ss .我们定义 ss 的一个子串的存在值为这个子串在 ss 中出现的次数乘以这个子串的长度. 对于给你的这个字符串 ss ,求所有回文子串中的最大存在值 ...

  8. HDU 4937 Lucky Number 搜索

    题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大, ...

  9. 【Henu ACM Round#16 E】Paths and Trees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉很像一道最短路. 然后就试了一发. 结果真的是.. 只要用一个优先队列优化的dijkstra算法求出每个点的最短路上的前一个点是 ...

  10. 美团实习生电面之谈(成功拿到offer)

    3月底进行了美团的一次实习生面试(Java研发project师).当时顺利的通过一面.以下是我的一面: 1.CPU由哪些部分组成 2.线程和进程的差别 3.Java类载入机制 4.怎样实现一个字符串的 ...