[COGS2479]偏序

题目大意:

\(n(n\le50000)\)个四元组,求四维偏序。

思路:

CDQ分治套CDQ分治套树状数组。

细节:

第二层CDQ之前要备份数组\(a\),否则第二层CDQ结束以后\(a\)就不对了。

源代码:

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<algorithm>
  4. inline int getint() {
  5. register char ch;
  6. while(!isdigit(ch=getchar()));
  7. register int x=ch^'0';
  8. while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
  9. return x;
  10. }
  11. const int N=50001;
  12. int n,ans;
  13. struct Node {
  14. int id,x,y,z,type;
  15. };
  16. Node a[N],tmp[N];
  17. inline bool cmp1(const Node &p1,const Node &p2) {
  18. return p1.x<p2.x;
  19. }
  20. inline bool cmp2(const Node &p1,const Node &p2) {
  21. return p1.y<p2.y;
  22. }
  23. class FenwickTree {
  24. private:
  25. int val[N];
  26. int lowbit(const int &x) const {
  27. return x&-x;
  28. }
  29. public:
  30. void modify(int p,const int &x) {
  31. for(;p<=n;p+=lowbit(p)) val[p]+=x;
  32. }
  33. int query(int p) const {
  34. int ret=0;
  35. for(;p;p-=lowbit(p)) ret+=val[p];
  36. return ret;
  37. }
  38. };
  39. FenwickTree t;
  40. void cdq2(const int &b,const int &e) {
  41. if(b==e) return;
  42. const int mid=(b+e)>>1;
  43. cdq2(b,mid);
  44. cdq2(mid+1,e);
  45. int p=b,q=mid+1;
  46. for(;q<=e;q++) {
  47. if(a[q].type==1) continue;
  48. for(;p<=mid&&a[p].y<a[q].y;p++) {
  49. if(a[p].type==1) t.modify(a[p].z,1);
  50. }
  51. ans+=t.query(a[q].z);
  52. }
  53. while(--p>=b) {
  54. if(a[p].type==1) t.modify(a[p].z,-1);
  55. }
  56. std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp2);
  57. }
  58. void cdq1(const int &b,const int &e) {
  59. if(b==e) return;
  60. const int mid=(b+e)>>1;
  61. cdq1(b,mid);
  62. cdq1(mid+1,e);
  63. for(register int i=b;i<=mid;i++) a[i].type=1;
  64. for(register int i=mid+1;i<=e;i++) a[i].type=2;
  65. std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp1);
  66. std::copy(&a[b],&a[e]+1,&tmp[b]);
  67. cdq2(b,e);
  68. std::copy(&tmp[b],&tmp[e]+1,&a[b]);
  69. }
  70. int main() {
  71. freopen("partial_order.in","r",stdin);
  72. freopen("partial_order.out","w",stdout);
  73. n=getint();
  74. for(register int i=1;i<=n;i++) a[i].id=i;
  75. for(register int i=1;i<=n;i++) a[i].x=getint();
  76. for(register int i=1;i<=n;i++) a[i].y=getint();
  77. for(register int i=1;i<=n;i++) a[i].z=getint();
  78. cdq1(1,n);
  79. printf("%d\n",ans);
  80. return 0;
  81. }

[COGS2479]偏序的更多相关文章

  1. cogs2479 偏序(CDQ套CDQ)

    题目链接 思路 四维偏序 \(CDQ\)套\(CDQ\),第一维默认有序.第二维用第一个\(CDQ\)变成有序的.并且对每个点标记上第一维属于左边还是右边.第二个\(CDQ\)处理第三维,注意两个\( ...

  2. cogs2479 偏序 cdq+树套树

    cdq+树状数组套替罪羊树. cdq归并a,树套树解决b,c. 记住平衡树树根不能暴力清零!!! // It is made by XZZ #include<cstdio> #includ ...

  3. 几道很Interesting的偏序问题

    若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...

  4. 【COGS2479】 HZOI2016—偏序

    http://cogs.pro/cogs/problem/problem.php?pid=2479 (题目链接) 题意 四维偏序. Solution CDQ套CDQ. 细节 第二次分治不能直接按照mi ...

  5. [COGS2479 && COGS2639]高维偏序(CDQ分治,bitset)

    COGS2479:四维偏序. CDQ套CDQ CDQ:对a分治,对b排序,再对a打标记,然后执行CDQ2 CDQ2:对b分治,对c归并排序,对d树状数组. #include<cstdio> ...

  6. COGS2479(四维偏序)

    题意:给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 分析:cdq分治 ...

  7. 【教程】CDQ套CDQ——四维偏序问题

    前言 上一篇文章已经介绍了简单的CDQ分治,包括经典的二维偏序和三维偏序问题,还有带修改和查询的二维/三维偏序问题.本文讲介绍多重CDQ分治的嵌套,即多维偏序问题. 四维偏序问题       给定N( ...

  8. c++模板函数实例化的偏序机制

    一:废话 今天在stackoverflow上看到一个关于c++模板specialization的问题: http://stackoverflow.com/questions/18283851/temp ...

  9. COGS 2479 偏序 题解

    [题意] 给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 对于30%的 ...

随机推荐

  1. 防范xss的正确姿势

    防范xss的正确姿势 xss攻击是web攻击中非常常见的一种攻击手段.如果你还没有听说过xss攻击,可以先了解xss的相关知识和原理,例如: XSS)" target="_blan ...

  2. 大规模实时流处理平台架构-zz

    随着不同网络质量下接入终端设备种类的增多,服务端转码已经成为视频点播和直播产品中必备的能力之一.直播产品讲究时效性,希望在一定的时间内让所有终端看到不同尺寸甚至是不同质量的视频,因此对转码的实时性要求 ...

  3. DevExpress 行事历(Scheduler)的常用属性、事件和方法

    一.TcxScheduler[TcxScheduler常用属性]1.Storage    - 邦定一个Storage为Scheduler显示提供数据 2.DateNavigate.ColCount   ...

  4. linux用户权限 -> 系统用户管理

    用户基本概述: Linux用户属于多用户操作系统,在windows中,可以创建多个用户,但不允许同一时间多个用户进行系统登陆,但是Linux可以同时支持多个用户同时登陆操作系统,登陆后互相之间并不影响 ...

  5. JS可以监控手机的返回键吗?

    html5的话 一进页面就pushState,然后监控onpopstate不过好像没有办法知道是前进还是后退我的奇淫巧计是,一个数字变量,pushState一个锚,锚是这个数字,前进一个页面数字+1, ...

  6. Docker - CentOS安装Docker

    如果要在CentOS下安装Docker容器,必须是CentOS 7 (64-bit).CentOS 6.5 (64-bit) 或更高的版本,并要求 CentOS 系统内核高于 3.10. uname ...

  7. java基础50 配置文件类(Properties)

    1. 配置文件类Properties的概念 主要生产配置文件与读取配置文件的信息 2.Properties要注意的细节 1.如果配置文件一旦使用了中文,那么在使用store方法生产的配置文件额时候字符 ...

  8. WinForm界面开发之 启动界面

    我们在开发桌面应用程序的时候,由于程序启动比较慢,往往为了提高用户的体验,增加一个闪屏,也就是SplashScreen,好处有:1.让用户看到加载的过程,提高程序的交互响应:2.可以简短展示或者介绍程 ...

  9. hdu 4813(2013长春现场赛A题)

    把一个字符串分成N个字符串 每个字符串长度为m Sample Input12 5 // n mklmbbileay Sample Outputklmbbileay # include <iost ...

  10. for循环输出菱形

    package com.hanqi; public class lingxing { public static void main(String[] args) { for(int m=1;m< ...