[COGS2580]偏序 II
[COGS2580]偏序 II
题目大意:
\(n(n\le50000)\)个五元组,求五维偏序。
思路:
CDQ分治套CDQ分治套CDQ分治套树状数组。
时间复杂度\(\mathcal O(n\log^4 n)\)。
源代码:
#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;
}
typedef long long int64;
const int N=50001;
int n;
int64 ans;
struct Node {
int a,b,c,d,t1,t2;
};
Node a[N];
inline bool cmp1(const Node &p1,const Node &p2) {
return p1.a<p2.a;
}
inline bool cmp2(const Node &p1,const Node &p2) {
return p1.b<p2.b;
}
inline bool cmp3(const Node &p1,const Node &p2) {
return p1.c<p2.c;
}
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 cdq3(const int &b,const int &e) {
if(b==e) return;
const int mid=(b+e)>>1;
cdq3(b,mid);
cdq3(mid+1,e);
int p=b,q=mid+1;
for(;q<=e;q++) {
if(a[q].t1==1||a[q].t2==1) continue;
for(;p<=mid&&a[p].c<a[q].c;p++) {
if(a[p].t1==1&&a[p].t2==1) {
t.modify(a[p].d,1);
}
}
ans+=t.query(a[q].d);
}
while(--p>=b) {
if(a[p].t1==1&&a[p].t2==1) {
t.modify(a[p].d,-1);
}
}
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp3);
}
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);
for(register int i=b;i<=mid;i++) a[i].t2=1;
for(register int i=mid+1;i<=e;i++) a[i].t2=2;
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp2);
cdq3(b,e);
std::sort(&a[b],&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].t1=1;
for(register int i=mid+1;i<=e;i++) a[i].t1=2;
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp1);
cdq2(b,e);
std::sort(&a[b],&a[e]+1,cmp1);
}
int main() {
freopen("partial_order_two.in","r",stdin);
freopen("partial_order_two.out","w",stdout);
n=getint();
for(register int i=1;i<=n;i++) a[i].a=getint();
for(register int i=1;i<=n;i++) a[i].b=getint();
for(register int i=1;i<=n;i++) a[i].c=getint();
for(register int i=1;i<=n;i++) a[i].d=getint();
cdq1(1,n);
printf("%lld\n",ans);
return 0;
}
[COGS2580]偏序 II的更多相关文章
- [HZOI2016]偏序&[HZOI2015]偏序II K维偏序问题
description Cogs: [HZOI2016]偏序 [HZOI2015]偏序 II data range \[ n\le 5\times 10^4\] solution 嵌套\(CDQ\)的 ...
- COGS 2580. [HZOI 2015]偏序 II
COGS 2580. [HZOI 2015]偏序 II 题目传送门 题目大意:给n个元素,每个元素有具有4个属性a,b,c,d,求i<j并且ai<aj,bi<bj,ci<cj, ...
- 【CJOJ2375】 【HZOI 2015】偏序 II(cdq分治,树状数组)
传送门 CJOJ Solution 具体实现参考上一篇Blog(四维偏序) 代码实现1(cdq+cdq+cdq+BIT) /* mail: mleautomaton@foxmail.com autho ...
- 几道很Interesting的偏序问题
若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...
- CDQ分治嵌套模板:多维偏序问题
CDQ分治2 CDQ套CDQ:四维偏序问题 题目来源:COGS 2479 偏序 #define LEFT 0 #define RIGHT 1 struct Node{int a,b,c,d,bg;}; ...
- $CDQ$分治总结
A.\(CDQ\) 分治 特别基础的教程略. \(CDQ\)分治的优缺点: ( 1 )优点:代码量少,常数极小,可以降低处理维数. ( 2 )缺点:必须离线处理. \(CDQ\)分治与其他分治最本质的 ...
- poj2352树状数组解决偏序问题
树状数组解决这种偏序问题是很厉害的! /* 输入按照y递增,对于第i颗星星,它的level就是之前出现过的星星中,横坐标小于i的总数 */ #include<iostream> #incl ...
- bzoj3262: 陌上花开 三维偏序cdq分治
三维偏序裸题,cdq分治时,左侧的x一定比右侧x小,然后分别按y排序,对于左侧元素按y大小把z依次插入到树状数组里,其中维护每个左侧元素对右侧元素的贡献,在bit查询即可 /************* ...
- 【算法学习】【洛谷】cdq分治 & P3810 三维偏序
cdq是何许人也?请参看这篇:https://wenku.baidu.com/view/3b913556fd0a79563d1e7245.html. 在这篇论文中,cdq提出了对修改/询问型问题(Mo ...
随机推荐
- torch.Tensor.view (Python method, in torch.Tensor)
返回具有相同数据但大小不同的新张量.返回的张量共享相同的数据,必须具有相同数量的元素,但可能有不同的大小. Example >>> x = torch.randn(4, 4) > ...
- Intent 对象在 Android 开发中的应用
转自(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-intent/) Android 是一个开放性移动开发平台,运行在该平 ...
- device tree --- #address-cells and #size-cells property【转】
转自:http://www.cnblogs.com/youchihwang/p/7050846.html device tree source Example1 / { #address-cells ...
- 管道和xargs区别
一直弄不懂,管道不就是把前一个命令的结果作为参数给下一个命令吗,那在 | 后面加不加xargs有什么区别 NewUserFF 写道: 懒蜗牛Gentoo 写道: 管道是实现“将前面的标准输出作为后面的 ...
- EPC摘抄
S6a MME – HSS 完成用户位置信息的交换和用户签约信息的管理,传送控制面信息 Diameter MME:主要负责信令处理及移动性管理,功能包括:NAS信令及其安全:跟踪区域(Tracking ...
- restful API 规范(转)
1. URI URI 表示资源,资源一般对应服务器端领域模型中的实体类. URI规范 不用大写: 用中杠-不用下杠_: 参数列表要encode: URI中的名词表示资源集合,使用复数形式. 资源集合 ...
- Fiddler 4 抓包(APP HTTPS )
一.手机连接Fiddler 1.配置fiddler 1.安装fiddler,基本下一步下一步即可: 2.打开fiddler,点击顶部栏Tools——>Options 3.在HTTPS页签勾选“D ...
- Java 中 日期 时间 加减
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //方法1(推荐,功能强大灵活多变) Ca ...
- Java 容器的打印
Java容器类库中的两种主要类型,它们的区别在于容器中每个"槽"保存的元素个数 Clollection容器只能在保存一个元素,此类容器包括: List,它以特定顺序保存一组元素 S ...
- 减小VirtualBox虚拟硬盘文件的大小
虚拟机使用久了就会发现虚拟硬盘越来越大,但是进入虚拟机里的系统用命令看了下,实际占用的空间远没有虚拟硬盘大小那么大,这个让人很不爽,而且在分享虚拟机镜像的时候也很不方便.VirtualBox似乎没有提 ...