题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372

题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始

0操作 0 x:询问[x, x+i](i为该操作的编号)区间内有多少个完整的线段,并加入线段[x, x+i](线段直接重叠不影响)

1操作 1 x:删除0操作中插入的编号为x的线段,(不影响其他线段,不会重复删除同一线段,删除的线段一定是已经插入的)

解:题目有一个重要的条件:后面插入的线段一定比前面的长。那么考虑现在要插入线段[x, y],(-oo,x)有a个现存线段的左端点,(y,+oo)有b个现存线段的右端点,现存线段数为num个,则ans=num-a-b;用离散化+树状数组搞的话可以求出(-oo,y]内的右端点数c,有c=num-b;联立前式解得ans=c-a;

 /*
* Problem: hdu5372 Segment Game
* Author: SHJWUDP
* Created Time: 2015/8/11 星期二 21:57:35
* File Name: 1006.cpp
* State: Accepted
* Memo:
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; struct Hash : vector<int> {
void prepare() {
sort(begin(), end());
erase(unique(begin(), end()), end());
}
int get(int x) {
return lower_bound(begin(), end(), x)-begin()+;
}
};
struct Fenwick {
int n;
vector<int> c;
void init(int n) {
this->n=n;
c.assign(n+, );
}
int lowbit(int x) {
return x & -x;
}
void add(int x, int v) {
while(x<=n) {
c[x]+=v; x+=lowbit(x);
}
}
int getsum(int x) {
int res=;
while(x>) {
res+=c[x]; x-=lowbit(x);
}
return res;
}
} fwl, fwr; int n;
vector<pair<int, int> > arr;
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
int now=;
while(~scanf("%d", &n)) {
arr.resize(n+);
vector<pair<int, int> > A;
Hash hash;
int tmpLen=;
for(int i=; i<=n; i++) {
scanf("%d%d", &arr[i].first, &arr[i].second);
if(arr[i].first==) {
A.push_back(make_pair(arr[i].second,
arr[i].second+(++tmpLen)));
hash.push_back(A.back().first);
hash.push_back(A.back().second);
}
}
hash.prepare();
fwl.init(hash.size()+);
fwr.init(hash.size()+);
int pos=;
printf("Case #%d:\n", ++now);
for(int i=; i<=n; i++) {
int a=arr[i].first;
int b=arr[i].second;
if(a==) {
int x=A[pos].first=hash.get(A[pos].first);
int y=A[pos].second=hash.get(A[pos].second);
pos++;
printf("%d\n", fwr.getsum(y)-fwl.getsum(x-));
fwl.add(x, );
fwr.add(y, );
} else {
int x=A[b-].first;
int y=A[b-].second;
fwl.add(x, -);
fwr.add(y, -);
}
}
}
return ;
}

[2015hdu多校联赛补题]hdu5372 Segment Game的更多相关文章

  1. [2015hdu多校联赛补题]hdu5384 Danganronpa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...

  2. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  3. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  4. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  5. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  6. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  7. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  8. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  9. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

随机推荐

  1. python requests库入门[转]

    首先,确认一下: Requests 已安装 Requests是 最新的 让我们从一些简单的示例开始吧. 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: > ...

  2. JUQERY 获取同名称的所有CHECKBOX ,获取已经选择的,并且jquery进行勾选!

    var @(Perfix)_CheckArray=[]; @(Perfix)_CheckArray.length=0; var checkedItems = $('input[name="@ ...

  3. 关于CGContextSetBlendMode: invalid context 0x0的错误

    在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...

  4. STM32F4 SPI2初始化及收发数据【使用库函数】

    我的STM32F4 Discovery上边有一个加速度传感器LIS302DL.在演示工程中,ST的工程师使用这个传感器做了个很令人羡慕的东西:解算开发板的姿态.当开发板倾斜时候,处于最上边的LED点亮 ...

  5. 认识zookeeper

    之前稍微看了一下Hadoop相关的技术文档,有了解到Hbase用zookeeper做分布式应用程序协调服务. 现在做的项目里,也是用zookeeper做集群和负载均衡(从类名LoadBalance看来 ...

  6. Storm Ack框架笔记

    Storm利用Acker Bolt节点跟踪消息,当Spout发送出去的消息以及这些消息所衍生出来的消息均被处理后,Spout将受到对应于该消息的Ack.实现要点: 1.Storm中每条发送出去的消息都 ...

  7. ant 自定义taskdef的工作目录

    上次同事在用ant执行多层目录的测试用例的时候遇到了一些问题,也就是自定义的taskdef的工作目录总是在开始执行ant的那个目录,而有一些地方用到了当前目录,因此很多测试用命的代码出现了“找不到自定 ...

  8. vc6开发ActiveX并发布全攻略(三)(转)

    一.环境: windows xp sp3 Microsoft VC++ 6.0 二.制作文件 打开iexpress.exe(windows提供的一个向导式cab制作工具,位置:C:\WINDOWS\s ...

  9. 磁盘IO的性能指标

    磁盘IO的性能单位: bps (byte/s)   每秒钟 byte 数 kbps = bps / 1024 mbps = kbps / 1024 相关计算可以参考下面代码:  https://git ...

  10. Unity3D学习笔记

    双击或F-居中显示对象 Alt-旋转场景 Align With View-正视主镜头 添加质量 使成为预制物体, 即flash中元件, 预制物体在Hierarchy中名字成蓝色, Assets是的对象 ...