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,求 ...
随机推荐
- centos7: svbversion版本的安装配置+tortoisesvn登录验证
centos7: svbversion版本的安装配置+tortoisesvn登录验证 命令工具:svnadmin create #创建版本库 hotcopy #版本库热备份 Islocks #打印所有 ...
- spring boot:创建一个简单的web(maven web project)
1.新建一个maven web project; 2.在pom.xml文件中添加相应的依赖包: 3.新建一个HelloController请求控制类: 4.编写index.jsp页面: 5.编写启动类 ...
- Linux系统基本常识
在虚拟机里装一个Linux(centos),有时间可以装个mac玩一下.(使用centos或者Ubuntu时安装软件将会非常方便) ifconfig –a 显示当前Linux主机的 ip 地址 如何让 ...
- 20170528xlVBA凑数一例
Public Sub MakeUp() Dim Sht As Worksheet Set Sht = ThisWorkbook.Worksheets("设置") Dim Total ...
- H5基础知识(一)
一.概述 HTML5 是html4.0 升级版 结构 Html5 .样式 css3 .行为: API 都有所增强 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web ...
- python-day9-集合数据类型
pythons=['alex','egon','yuanhao','wupeiqi','gangdan','biubiu']linuxs=['wupeiqi','oldboy','gangdan'] ...
- 基础最短路(模板 bellman_ford)
Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...
- OC MRC之计数器的基本操作(代码分析)
/* 1.方法的基本使用 1> retain :计数器+1,会返回对象本身 2> release :计数器-1,没有返回值 3> retainCount :获取当前的计数器 4> ...
- html <a>标签介绍
<a href="javascript:void(0)" click="function(){}" />= a 标签样式 一组专门的预定义的 ...
- pygame精灵类实现房子爆炸效果
# coding=utf8 import random import pygame from pygame.locals import * from cStringIO import StringIO ...