[BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
来源:互联网 发布:宽带多重网络怎么回事 编辑:程序博客网 时间:2024/06/06 20:58
Description
Farmer John is continuing to ponder the issue of cows crossing the road through his farm, introduced in the preceding two problems. He realizes now that the threshold for friendliness is a bit more subtle than he previously considered – breeds aa and bb are now friendly if |a-b|≤K, and unfriendly otherwise.Given the orderings of fields on either side of the road through FJ’s farm, please count t
he number of unfriendly crossing pairs of breeds, where a crossing pair of breeds is defined as in the preceding problems.
题解:
首先题目大意:两列n的排列,相同的数连边,假如一对数的差>k且有交叉,ans++,求ans。对于每个数有三个属性,一个是在第一个序列中的位置,一个是在第二个序列中的位置,一个是它的值,就是个三维偏序问题,简单的CDQ分治,树套树空间过不去。
代码:
#include<bits/stdc++.h>using namespace std;#define LL long long#define pa pair<int,int>const int Maxn=100010;const int inf=2147483647;int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); return x*f;}int n,k,a[Maxn],b[Maxn],posa[Maxn],posb[Maxn];struct Opt{int x,y,z;}q[Maxn],temp[Maxn];bool cmp(Opt a,Opt b){return a.x>b.x;}int s[Maxn];void add(int x,int y){for(;x<=n;x+=(x&-x))s[x]+=y;}int getsum(int x){int re=0;x=min(x,n);for(;x>0;x-=(x&-x))re+=s[x];return re;}LL ans=0;void solve(int l,int r){ if(l==r)return; int mid=l+r>>1; solve(l,mid);solve(mid+1,r); int i=l,j=mid+1,len=0; while(i<=mid&&j<=r) { if(q[i].y<q[j].y) { add(q[i].z,1); temp[++len]=q[i++]; } else { ans+=(LL)(getsum(q[j].z-k-1))+(LL)(getsum(n)-getsum(q[j].z+k)); temp[++len]=q[j++]; } } int t=i; while(i<=mid)temp[++len]=q[i++]; while(j<=r)ans+=(LL)(getsum(q[j].z-k-1))+(LL)(getsum(n)-getsum(q[j].z+k)),temp[++len]=q[j++]; for(int p=l;p<t;p++)add(q[p].z,-1); for(int p=1;p<=len;p++)q[p+l-1]=temp[p];}int main(){ n=read(),k=read(); for(int i=1;i<=n;i++)a[i]=read(),posa[a[i]]=i; for(int i=1;i<=n;i++)b[i]=read(),posb[b[i]]=i; for(int i=1;i<=n;i++)q[i].x=posa[i],q[i].y=posb[i],q[i].z=i; sort(q+1,q+1+n,cmp);solve(1,n); printf("%lld",ans);}
阅读全文
2 0
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套Treap
- BZOJ 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套splay【未AC】
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II(dp+树状数组)
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 解决win10 VC++6.0 应用程序无法正常运行 0xc0000142
- Opencv中Mat矩阵乘法——点乘、dot、mul运算详解
- 图像简史
- Foo bar 什么鬼?
- Java POI生成一个指定模板的.xlsx文件
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- 高斯滤波原理及实现
- [LC]657. Judge Route Circle
- c++牛客网刷题 day3
- Django settings.py installd_apps的坑……卧槽
- 基于stream开发java插件
- ADO.NET 数据访问
- 线程间的通信、同步方式与进程间通信方式
- RAM 大全-DRAM, SRAM, SDRAM的关系与区别