[COGS2479]偏序

题目大意:

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

思路:

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

细节:

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

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=50001;
int n,ans;
struct Node {
int id,x,y,z,type;
};
Node a[N],tmp[N];
inline bool cmp1(const Node &p1,const Node &p2) {
return p1.x<p2.x;
}
inline bool cmp2(const Node &p1,const Node &p2) {
return p1.y<p2.y;
}
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) val[p]+=x;
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) ret+=val[p];
return ret;
}
};
FenwickTree t;
void cdq2(const int &b,const int &e) {
if(b==e) return;
const int mid=(b+e)>>1;
cdq2(b,mid);
cdq2(mid+1,e);
int p=b,q=mid+1;
for(;q<=e;q++) {
if(a[q].type==1) continue;
for(;p<=mid&&a[p].y<a[q].y;p++) {
if(a[p].type==1) t.modify(a[p].z,1);
}
ans+=t.query(a[q].z);
}
while(--p>=b) {
if(a[p].type==1) t.modify(a[p].z,-1);
}
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp2);
}
void cdq1(const int &b,const int &e) {
if(b==e) return;
const int mid=(b+e)>>1;
cdq1(b,mid);
cdq1(mid+1,e);
for(register int i=b;i<=mid;i++) a[i].type=1;
for(register int i=mid+1;i<=e;i++) a[i].type=2;
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp1);
std::copy(&a[b],&a[e]+1,&tmp[b]);
cdq2(b,e);
std::copy(&tmp[b],&tmp[e]+1,&a[b]);
}
int main() {
freopen("partial_order.in","r",stdin);
freopen("partial_order.out","w",stdout);
n=getint();
for(register int i=1;i<=n;i++) a[i].id=i;
for(register int i=1;i<=n;i++) a[i].x=getint();
for(register int i=1;i<=n;i++) a[i].y=getint();
for(register int i=1;i<=n;i++) a[i].z=getint();
cdq1(1,n);
printf("%d\n",ans);
return 0;
}

[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. 【bzoj题解】2186 莎拉公主的困惑

    题目传送门. 题意:求\([1,n!]\)中与\(m!\)互质的数的个数,对质数\(R\)取模,\(n\geq m\). 答案应该等于\(\frac{n!}{m!}\phi(m!)=\frac{n!} ...

  2. CentOS7.3下的一个iptables配置

    centos7.3默认使用的防火墙应该是firewall,而不是iptables.而我们xxmj服务器使用的是iptables防火墙.所以,在配置防火墙之前,我们需要先关闭firewall,安装ipt ...

  3. sql 内联,左联,右联,全联

    联合查询效率较高,以下例子来说明联合查询(内联.左联.右联.全联)的好处: T1表结构(用户名,密码) userid (int) username varchar(20) password  varc ...

  4. C#.NET调用WSDL接口及方法

    1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...

  5. HDU 6057 Kanade's convolution

    题目链接:HDU-6057 题意: 思路:先按照官方题解推导出下面的式子: 现在唯一的问题就是怎么解决[bit(x)-bit(y)=bit(k)]的问题. 我们定义\( F(A,k)_{i}=\lef ...

  6. Python中使用LMDB

    在python中使用lmdb linux中,可以使用指令pip install lmdb安装lmdb包. 生成一个空的lmdb数据库文件 # -*- coding: utf-8 -*- import ...

  7. 四、springcloud之服务调用Feign(二)

    一.Fegin的常见应用 Feign的Encoder.Decoder和ErrorDecoder Feign将方法签名中方法参数对象序列化为请求参数放到HTTP请求中的过程,是由编码器(Encoder) ...

  8. python面向对象(五)之多态

    继承 ​ 在讲多态之前我们再复习下继承,下面是一个例子. ​ Circle 和 Rectangle 继承自 Shape,不同的图形,面积(area)计算方式不同. # shape.py class S ...

  9. 洛谷P3760异或和

    传送门啦 传送门啦 一般这种位运算的题都要把每一位拆开来看,因为位运算每个位的结果这和这一位的数有关. 这样我们用s[i]表示a的前缀和,即 $ a[1]+a[2]+....a[i] $ ,然后我们从 ...

  10. require和import的区别

    require:是一种common协议,大家按照这个约定书写自己的代码,实现模块化. import:是ES6的模块语法实现.是语言自身的模块实现.