http://acm.hdu.edu.cn/showproblem.php?

pid=5372

Segment Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1284    Accepted Submission(s): 375

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
Hint
For the second case in the sample: At the first add operation,Lillian adds a segment [1,2] on the line. At the second add operation,Lillian adds a segment [0,2] on the line. At the delete operation,Lillian deletes a segment which added at the first add operation. At the third add operation,Lillian adds a segment [1,4] on the line. At the fourth add operation,Lillian adds a segment [0,4] on the line
 
Author
UESTC
 
Source
 

题意:

每次插入一个线段。或删除一个已存在的线段,每次插入后输出当前插入的线段能完整覆盖存在的几条线段。

题解:对于新插入的线段。查询有多少个线段左端点大于等于该线段的左端点。

再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。

时间复杂度nlogn

一共就4种情况。画绘图应该能发现。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef pair<int, int> pii;
typedef long long ll;
const int N = 450007;
struct Tree {
int c[N], maxn;
void init(int n) { maxn = n; for (int i = 0; i <= n; i++)c[i] = 0; }
int lowbit(int x) { return x&-x; }
int sum(int x) {
int ans = 0;
while (x)ans += c[x], x -= lowbit(x);
return ans;
}
void update(int pos, int val) {
while (pos <= maxn)c[pos] += val, pos += lowbit(pos);
}
}A, B;
int n;
set<pii> s;
int op[N], l[N], r[N];
pii a[N];
vector<int>G;
int main() {
int cas = 0;
while (cin>>n) {
G.clear();
int top = 0;
for (int i = 1; i <= n; i++) {
rd(op[i]), rd(l[i]);
if (op[i] == 0)
{
G.push_back(l[i]);
r[i] = l[i] + (++top);
G.push_back(r[i]);
}
}
printf("Case #%d:\n", ++cas);
sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end());
top = 0;
for (int i = 1; i <= n; i++)
if (op[i] == 0) {
l[i] = lower_bound(G.begin(), G.end(), l[i]) - G.begin() + 1;
r[i] = lower_bound(G.begin(), G.end(), r[i]) - G.begin() + 1;
a[++top] = { l[i], r[i] };
}
A.init(G.size()); B.init(G.size());
int all = 0;
for (int i = 1; i <= n; i++)
{
if (op[i] == 0)
{
int ans = B.sum(r[i]);
ans -= A.sum(l[i]-1);
pt(ans); putchar('\n');
A.update(l[i], 1);
B.update(r[i], 1);
all++;
}
else {
A.update(a[l[i]].first, -1);
B.update(a[l[i]].second, -1);
all--;
}
}
}
return 0;
}

当前插入的线段能完整覆盖存在的几条线段 树状数组 HDU 5372 Segment Game的更多相关文章

  1. 学习笔记--函数式线段树(主席树)(动态维护第K极值(树状数组套主席树))

    函数式线段树..资瓷 区间第K极值查询 似乎不过似乎划分树的效率更优于它,但是如果主席树套树状数组后,可以处理动态的第K极值.即资瓷插入删除,划分树则不同- 那么原理也比较易懂: 建造一棵线段树(权值 ...

  2. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  4. BZOJ_1012_[JSOI2008]_最大数maxnumber_(线段树/树状数组+RMQ)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1012 两种操作: 1.求序列末尾n个数中的最大值. 2.在序列末尾插入一个数. 分析 线段树求 ...

  5. 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  6. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  7. [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?

    其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...

  8. ZJOI 2017 树状数组(线段树套线段树)

    题意 http://uoj.ac/problem/291 思路 不难发现,九条カレン醬所写的树状数组,在查询区间 \([1,r]\) 的时候,其实在查询后缀 \([r,n]\) :在查询 \([l,r ...

  9. 【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出1.区间\([l,r]\)有多少范围在\([a,b]\)的数:2.区间\([l,r]\)有多 ...

随机推荐

  1. Django day08 多表操作 (五) 聚合,分组查询 和 F,Q查询

    一:聚合,分组查询 二:F, Q查询

  2. css3 选择器 权重问题 (第二部分)

    这篇博文有关css的权重问题,我个人认为这是css知识中很重要的一个知识点.因为在开发的过程能中我们会经常遇到这种问题,特别是如果你使用框架的时候,有些框架的某些标签有一些默认的样式.所以我们可以通过 ...

  3. caffe介绍

  4. LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)

    这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...

  5. 一个能让cin和scanf 一样快的方法:

    cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱.正因为这个兼容性的 ...

  6. SyntaxError: EOL while scanning string literal的解决

    2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...

  7. 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常

    代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常 设置 padding=0

  8. # Nginx常见问题

    try_files的使用 按顺序检查文件是否 存在 location /{ try_files $uri $uri/ /index.php } 解析:在/下寻找$uri,如果没有找到,则去找$uri/ ...

  9. centos下使用shell+expect远程登录主机

    # 安装expect yum install expect # 新建脚本文件running #!/usr/bin/expect spawn /usr/bin/ssh root@114.114.114. ...

  10. 【Android】实例 忐忑的精灵

    在Android Studio中创建项目,名称为“Animation And Multimedia”,然后在该项目中创建一个Module,名称为“Frame-By-Frame Animation”.在 ...