【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 ...
随机推荐
- applicationContext-common.xml]; nested exception is java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
14:59:16,747 ERROR ContextLoader:350 - Context initialization failedorg.springframework.beans.factor ...
- response设置编码格式
response设置编码的三种方式 在java后台的Action代码或者Servlet代码中用response的方法来设置输出内容的编码方式,有以下三个方法: 1.response.setCharac ...
- kbmmw 的HTTPSmartService入门
前面介绍过kbmmw 中的smartservice. 这个既可以用于kbmmw 的客户端,也可以使用http 访问. 在新版的kbmmw里面,作者加强了http 的支持,我们可以只使用HTTPSmar ...
- 用rpm命令安装定时器crontab
crontab -l command not found 准备以下安装包: ls -l总用量 1004-rw-r--r-- 1 root root 76296 10月 9 16:01 croni ...
- Hadoop3集群搭建之——配置ntp服务
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 下篇: Hadoop3集群搭建之——hive安装 Hadoop3集群搭建之——hbase安装及简 ...
- spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found
解决方法,在repository加一个注解.如下图所示: @Component
- 2019.01.19 codeforces893F.Subtree Minimum Query(线段树合并)
传送门 线段树合并菜题. 题意简述:给一棵带点权的有根树,多次询问某个点ppp子树内距离ppp不超过kkk的点的点权最小值,强制在线. 思路: 当然可以用dfsdfsdfs序+主席树水过去. 然而线段 ...
- codeforces题目合集(持续更新中)
CF280CCF280CCF280C 期望dp CF364DCF364DCF364D 随机化算法 CF438DCF438DCF438D 线段树 CF948CCF948CCF948C 堆 CF961EC ...
- Le Chapitre IX
Je crois qu'il profita, pour son évasion[evazjɔ̃]逃跑, d'une migration d'oiseaux sauvages[sovaʒ]未驯化的. ...
- 百度图片http://img[0-9]\.imgtn.*?g此形式的链接图片下载方式
"""给出图片链接列表, 下载图片""" print(pic_urls) for pic_url in pic_urls: try: hos ...