题:http://codeforces.com/gym/101964/problem/K

分析:https://blog.csdn.net/qq_43202683/article/details/98115901

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. inline int read(){
  5. int sum=,x=;
  6. char ch=getchar();
  7. while(ch<''||ch>''){
  8. if(ch=='-')
  9. x=;
  10. ch=getchar();
  11. }
  12. while(ch>=''&&ch<='')
  13. sum=(sum<<)+(sum<<)+(ch^),ch=getchar();
  14. return x?sum:-sum;
  15. }
  16. inline void write(ll x){
  17. if(x<)
  18. putchar('-'),x=-x;
  19. if(x>)
  20. write(x/);
  21. putchar(x%+'');
  22. }
  23. #define pb push_back
  24. #define lowbit(x) (x&(-x))
  25. const int M=5e5+;
  26. int ans[M];
  27. int tree[M];
  28. void add(int i,int x){
  29. while(i<M)
  30. tree[i]+=x,i+=(i&(-i));
  31. return ;
  32. }
  33. int query(int i){
  34. int res=;
  35. while(i){
  36. res+=tree[i];
  37. i-=(i&(-i));
  38. }
  39. return res;
  40. }
  41. struct node{
  42. int x,y,op,id;
  43. bool operator <(const node &b)const{
  44. return id<b.id;
  45. }
  46. }a[M],temp[M];
  47. vector<pair<int, int> > book;
  48. void cdq1(int l, int r)//算点对矩阵的贡献(小于等于)
  49. {
  50. if(r-l<=) return;
  51. int midd = (r+l)>>;
  52. cdq1(l, midd);
  53. cdq1(midd, r);
  54. int i= l,j= midd,n = ;
  55. while(i< midd &&j< r){
  56. if(a[i].x <= a[j].x){
  57. if(a[i].op == ){
  58. add(a[i].y, );
  59. book.push_back(make_pair(a[i].y, ));
  60. }
  61. temp[n++] = a[i++];
  62. }
  63. else{
  64. if(a[j].op==){
  65. ans[a[j].id]+=query(a[j].y);
  66. }
  67. else if(a[j].op==)
  68. ans[a[j].id]-=query(a[j].y);
  69. temp[n++]=a[j++];
  70. }
  71. }
  72. while(i<midd)
  73. temp[n++] = a[i++];
  74. while(j<r){
  75. if(a[j].op == )
  76. ans[a[j].id] += query(a[j].y);
  77. else if(a[j].op == )
  78. ans[a[j].id] -= query(a[j].y);
  79. temp[n++]=a[j++];
  80. }
  81. for(i =l;i<r;++i)
  82. a[i]=temp[i - l];
  83. for(i = ; i <book.size(); ++i)
  84. add(book[i].first, -book[i].second);
  85. book.clear();
  86. return;
  87. }
  88. void cdq2(int l,int r)
  89. {
  90. if(r-l<= )
  91. return;
  92. int midd=(l+r)>>;
  93. cdq2(l,midd);
  94. cdq2(midd,r);
  95. int i= l,j=midd,n=;
  96. while(i<midd&&j<r){
  97. if(a[i].x<a[j].x){
  98. if(a[i].op == ){
  99. add(a[i].y,);
  100. book.pb(make_pair(a[i].y, ));
  101. }
  102. else if(a[i].op==){
  103. add(a[i].y,-);
  104. book.pb(make_pair(a[i].y,-));
  105. }
  106. temp[n++]=a[i++];
  107. }
  108. else{
  109. if(a[j].op == ){
  110. ans[a[j].id] +=query(a[j].y-);
  111. }
  112. temp[n++]=a[j++];
  113. }
  114. }
  115. while(i<midd)
  116. temp[n++]=a[i++];//temp[o++] = e[lp++]?? = temp[lp++],debug???
  117. while(j<r){
  118. if(a[j].op == ) {
  119. ans[a[j].id] +=query(a[j].y - );
  120. }
  121. temp[n++]=a[j++];
  122. }
  123. for(i=l;i<r;i++)
  124. a[i]=temp[i-l];
  125. for(i=;i<book.size();i++)
  126. add(book[i].first,-book[i].second);
  127. book.clear();
  128. return;
  129. }
  130. int lisan[M<<];
  131. int main(){
  132. int n=read();
  133. int tot=,num=;
  134. for(int i=;i<n;i++){
  135. int op=read();
  136. if(op==){
  137. a[tot].x=read(),a[tot].y=read();
  138. a[tot].op=,a[tot].id=i;
  139. lisan[++num]=a[tot].x,lisan[++num]=a[tot].y;
  140. tot++;
  141. }
  142. else{
  143. int x1=read(),y1=read(),x2=read(),y2=read();
  144. a[tot].op=,a[tot].x=x1-,a[tot].y=y1-,a[tot].id=i,tot++;
  145. a[tot].op=,a[tot].x=x2,a[tot].y=y2,a[tot].id=i,tot++;
  146. a[tot].op=,a[tot].x=x1-,a[tot].y=y2,a[tot].id=i,tot++;
  147. a[tot].op=,a[tot].x=x2,a[tot].y=y1-,a[tot].id=i,tot++;
  148. lisan[++num]=x1-,lisan[++num]=x2,lisan[++num]=y1-,lisan[++num]=y2;
  149. }
  150.  
  151. }
  152. sort(lisan+,lisan+num+);
  153. int m=unique(lisan+,lisan++num)-lisan-;
  154. for(int i=;i<tot;i++){
  155. a[i].x=lower_bound(lisan+,lisan++m,a[i].x)-lisan;
  156. a[i].y=lower_bound(lisan+,lisan++m,a[i].y)-lisan;
  157. // cout<<a[i].x<<"~~~~"<<a[i].y<<endl;
  158. }
  159. cdq1(,tot);
  160.  
  161. sort(a,a+tot);
  162. cdq2(,tot);
  163. ll ANS=0ll;
  164. for(int i=;i<n;i++){
  165. ANS+=ans[i];
  166. write(ANS);
  167. puts("");
  168. }
  169. return ;
  170. }

2018SEERC Points and Rectangles (CDQ分治)的更多相关文章

  1. BZOJ 2716 Violet 3 天使玩偶 CDQ分治

    题目大意:初始给定平面上的一个点集.提供两种操作: 1.将一个点增加点集 2.查询距离一个点最小的曼哈顿距离 K-D树是啥...不会写... 我仅仅会CDQ分治 对于一个询问,查询的点与这个点的位置关 ...

  2. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. HDU - 6183:Color it (线段树&动态开点||CDQ分治)

    Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B ...

  4. CodeForces - 762E:Radio stations (CDQ分治||排序二分)

    In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...

  5. HDU 6183 Color it cdq分治 + 线段树 + 状态压缩

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Pro ...

  6. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  7. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

  8. HDU5618 & CDQ分治

    Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...

  9. 初识CDQ分治

    [BZOJ 1176:单点修改,查询子矩阵和]: 1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 200 ...

随机推荐

  1. cf1208 D Restore Permutation (二分+树状数组)

    题意 让你构造一个长度为n的序列,记为p1……pn,(这个序列是1~n的全排列的一种) 给你n个数,记为s1……sn,si的值为p1……pi-1中小于pi的数的和. 思路 显然,应该倒着来,也就是从p ...

  2. h5-transform二维变换-扑克牌小案例

    html代码:6张扑克牌 <div class="pkBox"> <img src="../img/pk1.jpg" alt="&q ...

  3. C# 串口编程,扫码枪使用

    一.串口通信简介 串行接口(串口)是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件.一般完成这种功能的电路,我们称为 ...

  4. Redis主从复制以及主从复制原理

    Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的 API.从 2010年 3 月 15 日起,Redis 的开 ...

  5. php对象:get_object_vars(), get_parent_class(),is_subclass_of(),interface_exists()

    get_object_vars():获得对象的属性,以关联数组形式返回 get_parent_class():获得对象的父类 is_subclass_of():判断对象是否某类(参数2)的子类实例出的 ...

  6. iOS 中的延时操作方法

    1. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_q ...

  7. 干货 | DRDS 与TiDB浅析

    干货 | DRDS 与TiDB浅析 北京it爷们儿 京东云开发者社区  4月17日 在谈论数据库架构和数据库优化的时候,会常听到"分库分表"."分片".&quo ...

  8. 2.0 虚拟机linu开启ssh服务与FTP

    2.1.1.当本地机器ssh连接过一次虚拟主机.虚拟主机重启过或者配置发生改变 需要重新配对密钥,需要先清除本地缓存的密钥  ssh-keygen -R "ip"  2.1.2. ...

  9. 生成随机数(Random类)和获取用户输入(Scanner类)

    生成指定范围内的随机数 Math.random() 生成随机数,随机数在0到1之间,类型是 double. public class randCase { public static void mai ...

  10. JAVA初学者——DOS命令及基本数据类型

    Hello!大家好!我是浩宇大熊猫~ 昨天看了韩顺平老师第二节视频的课,记忆尤新的是那个数据类型那一块,可是昨天感觉没掌握就没有发博客. 今天,又看了一遍,加上看了一些其他老师的,有所收获,所以分享给 ...