题目链接: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. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  2. IE 11 保护模式害惨了我

    花了几乎两天,一直用IE, 就说好好的 动态域名 为什么一直不能访问.用其它浏览器一试,我哭了,都是好的.

  3. java之main

    Java中用户向系统传递参数的三种基本方式 main方法 在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的 ...

  4. 重定向 url cookie

    想象下面两行伪代码: 1. setCookie(...);2. redirect(new_web_url); 首先设置一个Cookie,然后重定向到另外一个网址,这个网址跟当前网站的域名不同 在多数情 ...

  5. php-fpm 老是warning 进程退出问题

    http://yangjunwei.com/a/723.html 分析Centos系统下LNMP频繁502 Bad Gateway问题 2012-01-28 杨俊伟 )     最近VPS总是出现 N ...

  6. 也谈谈规范JS代码的几个注意点

    也谈谈规范JS代码的几个注意点 写JS代码差不多也有两年了吧,从刚开始的“初生牛犊不怕虎”乱写一通到后来也慢慢知道去规范一下自己写的代码.这种感觉就像是代码是你的作品,你希望它保持一份不仅干净而且也优 ...

  7. .NET异常问题总结

    输入字符串的格式不正确. 有格式化字符替换符号{0}等,“{”和“}”作为特殊符号出现,如果有多余的“{”和“}”就会出错 GZIP压缩出现FF-F0-F1…是无效的输入流 要解压的字节流无效,可能是 ...

  8. FFmpeg.exe使用随笔

    一.将图片合成为MP4 1.将照片合成为h264 ffmpeg -framerate 12 -i %3d.png -c:v libx264 -pix_fmt yuv420p test.h2642.将h ...

  9. 原生js 实现购物车价格和总价 统计

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. #ifdef #ifndef使用

    #ifdef THREAD_ON while (TRUE) #endif 如上没定义THREAD_ON时,是不会跑到while中去的 如上没定义THREAD_ON时,是会跑到else中去的 #ifnd ...