题意:有两种操作:(1)插入线段,第i次插入的线段左边界为Li,长度为i (2)删除线段,删除第x次插入的线段。每次插入线段之前询问有多少条线段被它覆盖。

思路:由于插入的线段长度是递增的,所以第i次插入的线段的长度比以前插入的所有线段都要长,从以前插入的线段里面任取一条,考虑其与当前线段的位置关系,主要有以下三种:

图中,下方表示当前线段。注意到,只有被当前线段覆盖的线段,它的左右边界和当前线段的左右边界的相对位置是不同的。也就是说,查询有多少个线段的右端点小于等于该线段右端点,再查询有多少条线段左端点小于该线段的左端点, 两者之差就是答案。因为不符合要求的线段同时进入了前者和后者,而符合要求的答案只进入了前者。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  1. //#pragma comment(linker, "/STACK:1024000000")
  2. #include <map>
  3. #include <set>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <deque>
  7. #include <queue>
  8. #include <stack>
  9. #include <vector>
  10. #include <cstdio>
  11. #include <string>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16.  
  17. using namespace std;
  18.  
  19. #define X first
  20. #define Y second
  21. #define pb push_back
  22. #define mp make_pair
  23. #define all(a) (a).begin(), (a).end()
  24. #define fillchar(a, x) memset(a, x, sizeof(a))
  25. #define copy(a, b) memcpy(a, b, sizeof(a))
  26.  
  27. typedef long long ll;
  28. typedef pair<int, int> pii;
  29. typedef unsigned long long ull;
  30.  
  31. //#ifndef ONLINE_JUDGE
  32. void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
  33. void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
  34. void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
  35. while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
  36. void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
  37. void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
  38. void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
  39. //#endif
  40. template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
  41. template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
  42. template<typename T>
  43. void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
  44. template<typename T>
  45. void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];}
  46.  
  47. #if 0
  48. const double PI = acos(-1.0);
  49. const int INF = 1e9 + 7;
  50. const double EPS = 1e-8;
  51. #endif
  52.  
  53. /* -------------------------------------------------------------------------------- */
  54.  
  55. const int maxn = 4e5 + ;
  56.  
  57. int u[maxn], v[maxn], w[maxn], tot;
  58. pii rem[maxn];
  59.  
  60. class TA {
  61. int C[maxn], n;
  62. inline int lowbit(int x) { return x & -x; }
  63. public:
  64. void init(int n) {
  65. this->n = n;
  66. for (int i = ; i <= n; i ++) C[i] = ;
  67. }
  68. void update(int p, int x) {
  69. while (p <= n) {
  70. C[p] += x;
  71. p += lowbit(p);
  72. }
  73. }
  74. int query(int p) {
  75. int ans = ;
  76. while (p) {
  77. ans += C[p];
  78. p -= lowbit(p);
  79. }
  80. return ans;
  81. }
  82. };
  83. TA lta, rta;
  84.  
  85. inline int find(int x) {
  86. return lower_bound(w, w + tot, x) - w + ;
  87. }
  88.  
  89. int main() {
  90. #ifndef ONLINE_JUDGE
  91. freopen("in.txt", "r", stdin);
  92. //freopen("out.txt", "w", stdout);
  93. #endif // ONLINE_JUDGE
  94. int cas = , n;
  95. while (cin >> n) {
  96. tot = ;
  97. int len = ;
  98. for (int i = ; i < n; i ++) {
  99. scanf("%d%d", u + i, v + i);
  100. if (u[i] == ) {
  101. rem[len] = mp(v[i], v[i] + len);
  102. w[tot ++] = v[i];
  103. w[tot ++] = v[i] + len ++;
  104. }
  105. }
  106. sort(w, w + tot);
  107. tot = unique(w, w + tot) - w;
  108. lta.init(tot);
  109. rta.init(tot);
  110. printf("Case #%d:\n", ++ cas);
  111. len = ;
  112. for (int i = ; i < n; i ++) {
  113. if(u[i] == ) {
  114. pii buf = rem[v[i]];
  115. int lid = find(buf.X), rid = find(buf.Y);
  116. lta.update(lid, - );
  117. rta.update(rid, - );
  118. }
  119. else {
  120. int lid = find(v[i]), rid = find(v[i] + len ++);
  121. printf("%d\n", rta.query(rid) - lta.query(lid - ));
  122. lta.update(lid, );
  123. rta.update(rid, );
  124. }
  125. }
  126. }
  127. return ;
  128. }

[hdu5372 Segment Game]树状数组的更多相关文章

  1. HDU 5372 Segment Game (树状数组)

    题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数. 题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端 ...

  2. 当前插入的线段能完整覆盖存在的几条线段 树状数组 HDU 5372 Segment Game

    http://acm.hdu.edu.cn/showproblem.php? pid=5372 Segment Game Time Limit: 3000/1500 MS (Java/Others)  ...

  3. 2015 多校联赛 ——HDU5372(树状数组)

    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 有0, ...

  4. POJ3928Ping pong[树状数组 仿逆序对]

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 Descrip ...

  5. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  6. POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2691   Accepted: 996 Descript ...

  7. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

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

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

  9. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

随机推荐

  1. jmeter DB2数据库连接与操作

    1.需要把数据库连接jar包拷贝到 jmeter lib目录下 先创建一个数据库连接配置元件 2.添加jdbc请求(我用的后置处理器) 3.可以通过beanshell 对结果集进行操作 beanshe ...

  2. 排序1 - 选择排序 & 插入排序

    请原谅我没有按照之前图片的分类来介绍排序算法,先说最简单的两种排序算法(冒泡略过),选择排序和插入排序,之前老是容易记混.默认输出升序的序列啊,哈哈. 选择排序 对于输入长度为n的数组,一共比较n-1 ...

  3. 【翻译】TensorFlow卷积神经网络识别CIFAR 10Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow

    原网址:https://data-flair.training/blogs/cnn-tensorflow-cifar-10/ by DataFlair Team · Published May 21, ...

  4. shiro:自定义remle(二)

    SpringMVC+SpringMVC+Mybatis项目 1:导入相关依赖 <dependencies> <!--测试依赖--> <dependency> < ...

  5. C#集合类型——Array、ArrayList、List 之浅谈

    在学习或工作中,集合是经常用到的,可以换一句话说“无项目无集合”,“项目皆有集合”.它一般存储一系列数据或者将一系列数据进行相关操作.在这里先大略谈一些集合类型的相关知识用于回顾. 数组(Array) ...

  6. python 基础篇 错误和异常处理

    语法错误 所谓语法错误,也就是你写的代码不符合编程规范,无法被识别与执行,比如下面这个例子: if name is not None print(name) If 语句漏掉了冒号,不符合 Python ...

  7. python-Django与Apache整合wsgi模块

    1.安装wsgi模块 yum search mod_wsgi yum install -y mod_wsgi 2.会在httpd下有配置文件 cd /etc/httpd/conf.d/wsgi.con ...

  8. ThinkPHP框架初步掌握

    为了帮老师用ThinkSNS二次开发一个微博系统,专门花了几天学习ThinkPHP框架,现在将一些ThinkPHP入门知识作以记录. 首先声明: 本文不是完全教程,只是将开发中碰到的问题作以总结,如果 ...

  9. KVM虚拟化平台环境部署

    一:安装依赖包 二:配置网卡 三:配置环境 实验环境: KVM01   192.168.200.10 关闭防火墙及相关的安全机制 [root@KVM01 ~]# systemctl stop fire ...

  10. 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...