HDU 5372 Segment Game (树状数组)
题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数。
题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。时间复杂度nlogn
由于坐标范围很大,需要离散。
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstdarg>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define root 1, 1, n
#define lc (k << 1)
#define rc (k << 1 | 1)
#define middle ((L + R) >> 1)
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) //typedef __int64 LL;
//typedef long long LL;
typedef pair<int, int> Pair;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-10;
//LL MOD = 1000000007; struct Operator {
int type, lb;//所有操作,lb表示左边界
}op[MAXN];
int c1[MAXN], c2[MAXN], n;//两个树状数组
int h1[MAXN], h2[MAXN], L[MAXN];//用于hash,L[i]表示第i次询问的左边界 int lowbit(int x) { return x & (-x); } void update(int *c, int n, int k, int v) {
while(k <= n) {
c[k] += v;
k += lowbit(k);
}
} int query(int *c, int k) {
int ans = ;
while(k > ) {
ans += c[k];
k -= lowbit(k);
}
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
FIN;
// FOUT;
#endif
int cas = ;
while(~scanf("%d", &n)) {
mem0(c1); mem0(c2);
int cnt = , sz1 = , sz2 = ;
rep (i, , n) {
scanf("%d %d", &op[i].type, &op[i].lb);
if(op[i].type == ) {
cnt ++;
h1[sz1++] = op[i].lb;
h2[sz2++] = op[i].lb + cnt;
L[cnt] = op[i].lb;
}
} sort(h1, h1 + sz1); sz1 = unique(h1, h1 + sz1) - h1;
sort(h2, h2 + sz2); sz2 = unique(h2, h2 + sz2) - h2;
printf("Case #%d:\n", ++cas); cnt = ;
rep (i, , n) {
if( !op[i].type ) { //(cnt_seg - query(c1, lb - 1)) - (cnt_seg - query(c2, rb)) = q2(rb) - q1(lb - 1)
++cnt;
int lb = lower_bound(h1, h1 + sz1, op[i].lb) - h1 + ;
int rb = lower_bound(h2, h2 + sz2, op[i].lb + cnt) - h2 + ;
printf("%d\n", query(c2, rb) - query(c1, lb - ));
update(c1, sz1, lb, );
update(c2, sz2, rb, );
}
else {
update(c1, sz1, lower_bound(h1, h1 + sz1, L[op[i].lb]) - h1 + , -);
update(c2, sz2, lower_bound(h2, h2 + sz2, L[op[i].lb] + op[i].lb) - h2 + , -);
}
}
}
return ;
}
HDU 5372 Segment Game (树状数组)的更多相关文章
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 5101 Select --离散化+树状数组
题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...
随机推荐
- 20170814xlVBA部分代号收盘价转置
原始数据: 转置效果: Sub TransformData() Dim Rng As Range Dim Arr As Variant Dim Dic As Object Dim dCode As O ...
- 12月3日周日,关联:has_many(dependent::delete_all和destroy的区别) 注意看log; where等查询语句的用法。 layout传递参数❌
错误❌: 1.belongs_to :job, dependent: :destroy //尝试删除一条resumen后,job没有同步删除?? 答:建立一对多的关系,如job和resume.应该在j ...
- codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)
题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...
- 移动端web开发技巧(转)
原文链接:http://liujinkai.com/2015/06/06/mobile-web-skill/ 移动端web开发技巧 这是一个最好的时代,因为我们站在潮流中:但也是一个最坏的时代,因为我 ...
- python-day9-数据类型总结
数据类型总结: 常用: 数字 字符串 列表 元组 字典 不常用:集合 1.按照存值个数: 1个:数字,字符串 多个:列表,元组,字典,(集合) 2.按照可变不可变: 可变:列表,字典,(集合) 不可 ...
- 用DFS 解决全排列问题的思想详解
首先考虑一道奥数题目: □□□ + □□□ = □□□,要将数字1~9分别填入9个□中,使得等式成立.例如173+286 = 459.请输出所有合理的组合的个数. 我们或许可以枚举每一位上所有的数,然 ...
- hdu2955(概率DP)
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...
- 对象不能从 DBNull 转换为其他类型
SQL语句:select sum (datediff(second,Begin_Time,End_Time)) as 总时长 from 数据表这是我开始的程序:int 总时长=0;总时长 =Conve ...
- Freemaker FTL指令常用标签及语法
https://blog.csdn.net/pengpengpeng85/article/details/52070602 FTL指令常用标签及语法 注意:使用freemaker,要求所有标签必须闭合 ...
- eclipse设置(颜色,字体等)
1.设置jsp中js块的颜色: 选择Preferences菜单项.在弹出的窗口的左侧树形菜单依次选择:MyEclipse.Files and Editors.Javascript.Editor.Syn ...