题目链接: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. date_default_timezone_set()设置时区

    <?php echo function_exists(date_default_timezone_set)."<br>";//在这他总是返回1,这函数是判断这里面 ...

  2. libtcc使用问题一二

    问题来由: powersniff(参考博客的文章,在qq群下载最新版本)目前使用lua作为分析插件,但熟练lua的人不多.所以,移植python和tcc两类语言作为插件. tcc(即tiny c),h ...

  3. VB6对象与地址相互转换

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (lpDest As ...

  4. 如何书写高质量的jQuery代码(转)

    想必大家对于jQuery这个最流行的javascript类库都不陌 生,而且只要是前端开发人员肯定或多或少的使用或者接触过,在今天的这篇文章中,参考了一些资料及实际使用效率,将介绍一些书写高质量jQu ...

  5. UML,Powerdesigner 使用方法

    http://blog.163.com/guomaolin_gavin/blog/static/199618307201272674936220/

  6. Lab_5_SysOps_Resources_Linux_v2.5

    System Operations - Lab 5: Managing Resources Using Tagging (Linux) - 2.5 ========================== ...

  7. ruby md5加签验签方法

    # md5签名def md5_sign(data,key) return OpenSSL::Digest::MD5.hexdigest(data+key)end # md5验签def md5_veri ...

  8. OGNL语言

                                                             OGNL 一.概述 以下内容摘自Ognl的官网: OGNL stands for Ob ...

  9. ADF_Controller系列1_绑定TasksFlow、Region和Routers(Part1)

    2015-02-14 Created By BaoXinjian

  10. 最小的K个数:用快排的思想去解相关问题

    实现快速排序算法的关键在于先在数组中选择一个数字,接下来把数组中的数字分为两部分,比选择的数字小的数字移到数组的左边,比选择的数字大的数字移到数组的右边. 这个函数可以如下实现: int Partit ...