【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)
传送门
Solution
考虑这是一个四维偏序对吧。
直接cdq套在一起,然后这题有两种实现方法(树状数组的更快!)
代码实现1(cdq+cdq+cdq)
/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=100010;
struct node
{
int a,b,c,opt1,opt2;
}a[N<<1],tmp1[N],tmp2[N],tmp3[N];
int n,ans;
void cdq3(int,int);
void cdq2(int,int);
void cdq1(int,int);
int main()
{
n=gi();
for(int i=1;i<=n;i++)a[i].a=gi();
for(int i=1;i<=n;i++)a[i].b=gi();
for(int i=1;i<=n;i++)a[i].c=gi();
cdq1(1,n);
printf("%d\n",ans);
return 0;
}
void cdq1(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq1(l,mid);cdq1(mid+1,r);
int tot=l-1;int L=l,R=mid+1;
while(L<=mid && R<=r)
{
if(a[L].a<a[R].a){a[L].opt1=0;tmp1[++tot]=a[L++];}
else{a[R].opt1=1;tmp1[++tot]=a[R++];}
}
while(L<=mid){a[L].opt1=0;tmp1[++tot]=a[L++];}
while(R<=r){a[R].opt1=1;tmp1[++tot]=a[R++];}
for(int i=l;i<=r;i++)a[i]=tmp1[i];
cdq2(l,r);
}
void cdq2(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq2(l,mid);cdq2(mid+1,r);
int tot=l-1;int L=l,R=mid+1;
while(L<=mid && R<=r)
{
if(tmp1[L].b<tmp1[R].b){tmp1[L].opt2=0;tmp2[++tot]=tmp1[L++];}
else{tmp1[R].opt2=1;tmp2[++tot]=tmp1[R++];}
}
while(L<=mid){tmp1[L].opt2=0;tmp2[++tot]=tmp1[L++];}
while(R<=r){tmp1[R].opt2=1;tmp2[++tot]=tmp1[R++];}
for(int i=l;i<=r;i++)tmp1[i]=tmp2[i];
cdq3(l,r);
}
void cdq3(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq3(l,mid);cdq3(mid+1,r);
int tot=l-1;int L=l,R=mid+1,cnt=0;
while(L<=mid && R<=r)
{
if(tmp2[L].c<tmp2[R].c){if((tmp2[L].opt1|tmp2[L].opt2)==0)cnt++;tmp3[++tot]=tmp2[L++];}
else{if((tmp2[R].opt1&tmp2[R].opt2)==1)ans+=cnt;tmp3[++tot]=tmp2[R++];}
}
while(R<=r){if((tmp2[R].opt1&tmp2[R].opt2)==1)ans+=cnt;tmp3[++tot]=tmp2[R++];}
while(L<=mid)tmp3[++tot]=tmp2[L++];
for(int i=l;i<=r;i++)tmp2[i]=tmp3[i];
}
代码实现2(cdq+cdq+BIT)
/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=100010;
struct node
{
int a,b,c,opt;
}a[N<<1],tmp1[N],tmp2[N];
int n,tot,ans,c[N];
int lowbit(int x){return x&(-x);}
void Add(int x,int d){while(x<=n){c[x]+=d;x+=lowbit(x);}}
int query(int x){int ret=0;while(x){ret+=c[x];x-=lowbit(x);}return ret;}
void cdq2(int,int);
void cdq1(int,int);
int main()
{
n=gi();
for(int i=1;i<=n;i++)a[i].a=gi();
for(int i=1;i<=n;i++)a[i].b=gi();
for(int i=1;i<=n;i++)a[i].c=gi();
cdq1(1,n);
printf("%d\n",ans);
return 0;
}
void cdq1(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq1(l,mid);cdq1(mid+1,r);
tot=l-1;int L=l,R=mid+1;
while(L<=mid && R<=r)
{
if(a[L].a<a[R].a){a[L].opt=0;tmp1[++tot]=a[L++];}
else{a[R].opt=1;tmp1[++tot]=a[R++];}
}
while(L<=mid){a[L].opt=0;tmp1[++tot]=a[L++];}
while(R<=r){a[R].opt=1;tmp1[++tot]=a[R++];}
for(int i=l;i<=r;i++)a[i]=tmp1[i];
cdq2(l,r);
}
void cdq2(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq2(l,mid);cdq2(mid+1,r);
int L=l,R=mid+1;tot=0;
while(L<=mid && R<=r)
{
if(tmp1[L].b<tmp1[R].b)
{
tmp2[++tot]=tmp1[L];
if(!tmp1[L].opt)Add(tmp1[L].c,1);
L++;
}
else
{
tmp2[++tot]=tmp1[R];
if(tmp1[R].opt)ans+=query(tmp1[R].c);
R++;
}
}
while(R<=r)
{
tmp2[++tot]=tmp1[R];
if(tmp1[R].opt)ans+=query(tmp1[R].c);
R++;
}
for(int i=l;i<L;i++)if(!tmp1[i].opt)Add(tmp1[i].c,-1);
while(L<=mid){tmp2[++tot]=tmp1[L];L++;}
for(int i=l;i<=r;i++)tmp1[i]=tmp2[i-l+1];
}
【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)的更多相关文章
- LOJ3146 APIO2019路灯(cdq分治+树状数组)
每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- bzoj 3262 陌上花开 - CDQ分治 - 树状数组
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组
考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...
- BZOJ3262陌上花开(三维偏序问题(CDQ分治+树状数组))+CDQ分治基本思想
emmmm我能怎么说呢 CDQ分治显然我没法写一篇完整的优秀的博客,因为我自己还不是很明白... 因为这玩意的思想实在是太短了: fateice如是说道: 如果说对于一道题目的离线操作,假设有n个操作 ...
- hdu_4742_Pinball Game 3D(cdq分治+树状数组)
题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...
随机推荐
- Sophus链接错误
错误指示如下: CMakeFiles/run_vo.dir/run_vo.cpp.o: In function `main': run_vo.cpp:(.text.startup+0x1086): u ...
- android Run模式也会出现"Waiting for debugger"的解决方法
android Run模式也会出现"Waiting for debugger"的解决方法 出现“waiting for debugger”窗口是在debug模式下运行出现的.但是, ...
- GOIP connects with Elastix through “config by line”
GOIP connects with Elastix through “config by line” By grace Liu on May 17, 2013 in Elastix, Gateway ...
- Eclipse编辑XML自动提示(zz)
Eclipse编辑XML自动提示 博客分类: j2se XMLEclipseiBATISSpringSQL IED Eclipse Java EE IDE for Web Developers: D ...
- 【搜索】 Find The Multiple
#include<stdio.h> #include<stdlib.h> #include<string.h> bool found; void DFS(unsig ...
- Rest架构风格
一.REST介绍:: 1.REST是英文 Representational State Transfer的缩写 -- 表象化状态转变 或者 表述性状态转移 1.1 REST是 Web服务的一种架构风格 ...
- socketserver实例化过程
一.创建server对象时__init__的执行 找继承中的__init__ 这是ThreadingMixIn类中的方法 这是TCPServer类中的方法(父类BaserServer中还会用到fini ...
- java运行报错:nested exception is java.lang.NoSuchFieldError: INSTANCE,但使用@Test测试是好的
解决方法: 原因是,在tomcat里,同名不同版本的jar包,默认加载版本低的.我项目里有两个httpclient jar包.一个4.2.5 另一个是4.5.所以加载了4.2.5的,而我要用的是4. ...
- SimpleDateFormat转换时间,12,24时间格式[转]
SimpleDateFormat转换时间,12,24时间格式 来自:http://blog.csdn.net/dongguang1082/article/details/4387165 在使用Simp ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...