题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数。

题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。时间复杂度nlogn

由于坐标范围很大,需要离散。

  1. #pragma comment(linker, "/STACK:1677721600")
  2. #include <map>
  3. #include <set>
  4. #include <stack>
  5. #include <queue>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <vector>
  10. #include <cstdio>
  11. #include <cctype>
  12. #include <cstdarg>
  13. #include <cstring>
  14. #include <cstdlib>
  15. #include <iostream>
  16. #include <algorithm>
  17. using namespace std;
  18. #define INF 0x3f3f3f3f
  19. #define inf (-((LL)1<<40))
  20. #define root 1, 1, n
  21. #define lc (k << 1)
  22. #define rc (k << 1 | 1)
  23. #define middle ((L + R) >> 1)
  24. #define lson k<<1, L, (L + R)>>1
  25. #define rson k<<1|1, ((L + R)>>1) + 1, R
  26. #define mem0(a) memset(a,0,sizeof(a))
  27. #define mem1(a) memset(a,-1,sizeof(a))
  28. #define mem(a, b) memset(a, b, sizeof(a))
  29. #define FIN freopen("in.txt", "r", stdin)
  30. #define FOUT freopen("out.txt", "w", stdout)
  31. #define rep(i, a, b) for(int i = a; i <= b; i ++)
  32. #define dec(i, a, b) for(int i = a; i >= b; i --)
  33.  
  34. //typedef __int64 LL;
  35. //typedef long long LL;
  36. typedef pair<int, int> Pair;
  37. const int MAXN = + ;
  38. const int MAXM = ;
  39. const double eps = 1e-10;
  40. //LL MOD = 1000000007;
  41.  
  42. struct Operator {
  43. int type, lb;//所有操作,lb表示左边界
  44. }op[MAXN];
  45. int c1[MAXN], c2[MAXN], n;//两个树状数组
  46. int h1[MAXN], h2[MAXN], L[MAXN];//用于hash,L[i]表示第i次询问的左边界
  47.  
  48. int lowbit(int x) { return x & (-x); }
  49.  
  50. void update(int *c, int n, int k, int v) {
  51. while(k <= n) {
  52. c[k] += v;
  53. k += lowbit(k);
  54. }
  55. }
  56.  
  57. int query(int *c, int k) {
  58. int ans = ;
  59. while(k > ) {
  60. ans += c[k];
  61. k -= lowbit(k);
  62. }
  63. return ans;
  64. }
  65.  
  66. int main()
  67. {
  68. #ifndef ONLINE_JUDGE
  69. FIN;
  70. // FOUT;
  71. #endif
  72. int cas = ;
  73. while(~scanf("%d", &n)) {
  74. mem0(c1); mem0(c2);
  75. int cnt = , sz1 = , sz2 = ;
  76. rep (i, , n) {
  77. scanf("%d %d", &op[i].type, &op[i].lb);
  78. if(op[i].type == ) {
  79. cnt ++;
  80. h1[sz1++] = op[i].lb;
  81. h2[sz2++] = op[i].lb + cnt;
  82. L[cnt] = op[i].lb;
  83. }
  84. }
  85.  
  86. sort(h1, h1 + sz1); sz1 = unique(h1, h1 + sz1) - h1;
  87. sort(h2, h2 + sz2); sz2 = unique(h2, h2 + sz2) - h2;
  88. printf("Case #%d:\n", ++cas);
  89.  
  90. cnt = ;
  91. rep (i, , n) {
  92. if( !op[i].type ) { //(cnt_seg - query(c1, lb - 1)) - (cnt_seg - query(c2, rb)) = q2(rb) - q1(lb - 1)
  93. ++cnt;
  94. int lb = lower_bound(h1, h1 + sz1, op[i].lb) - h1 + ;
  95. int rb = lower_bound(h2, h2 + sz2, op[i].lb + cnt) - h2 + ;
  96. printf("%d\n", query(c2, rb) - query(c1, lb - ));
  97. update(c1, sz1, lb, );
  98. update(c2, sz2, rb, );
  99. }
  100. else {
  101. update(c1, sz1, lower_bound(h1, h1 + sz1, L[op[i].lb]) - h1 + , -);
  102. update(c2, sz2, lower_bound(h2, h2 + sz2, L[op[i].lb] + op[i].lb) - h2 + , -);
  103. }
  104. }
  105. }
  106. return ;
  107. }

HDU 5372 Segment Game (树状数组)的更多相关文章

  1. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  2. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  3. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  4. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  5. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  6. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  7. HDU 3854 Glorious Array(树状数组)

    题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...

  8. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. HDU 5101 Select --离散化+树状数组

    题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...

随机推荐

  1. centos7: svbversion版本的安装配置+tortoisesvn登录验证

    centos7: svbversion版本的安装配置+tortoisesvn登录验证 命令工具:svnadmin create #创建版本库 hotcopy #版本库热备份 Islocks #打印所有 ...

  2. spring boot:创建一个简单的web(maven web project)

    1.新建一个maven web project; 2.在pom.xml文件中添加相应的依赖包: 3.新建一个HelloController请求控制类: 4.编写index.jsp页面: 5.编写启动类 ...

  3. Linux系统基本常识

    在虚拟机里装一个Linux(centos),有时间可以装个mac玩一下.(使用centos或者Ubuntu时安装软件将会非常方便) ifconfig –a 显示当前Linux主机的 ip 地址 如何让 ...

  4. 20170528xlVBA凑数一例

    Public Sub MakeUp() Dim Sht As Worksheet Set Sht = ThisWorkbook.Worksheets("设置") Dim Total ...

  5. H5基础知识(一)

    一.概述 HTML5  是html4.0 升级版 结构 Html5 .样式 css3 .行为: API  都有所增强 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web ...

  6. python-day9-集合数据类型

    pythons=['alex','egon','yuanhao','wupeiqi','gangdan','biubiu']linuxs=['wupeiqi','oldboy','gangdan'] ...

  7. 基础最短路(模板 bellman_ford)

    Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...

  8. OC MRC之计数器的基本操作(代码分析)

    /* 1.方法的基本使用 1> retain :计数器+1,会返回对象本身 2> release :计数器-1,没有返回值 3> retainCount :获取当前的计数器 4> ...

  9. html <a>标签介绍

    <a href="javascript:void(0)" click="function(){}" />= a 标签样式     一组专门的预定义的 ...

  10. pygame精灵类实现房子爆炸效果

    # coding=utf8 import random import pygame from pygame.locals import * from cStringIO import StringIO ...