题意

两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$

题解

可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置,$y$表示在第二列的位置,$z$表示权值

两条线交叉,就是$x<x'$且$y>y'$,又要满足差值的绝对值小于等于$k$,就是$|z-z'|<=k$

于是就转化为了一个三维偏序问题,直接上CDQ

具体细节请看代码

 // luogu-judger-enable-o2
//minamoto
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,:;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=1e5+;
int n,k,b[N];
ll ans;int c[N];
inline void add(int x){
for(;x<=n;x+=x&-x) c[x]+=;
}
inline void clear(int x){
for(;x<=n;x+=x&-x)
if(c[x]) c[x]=;
else break;
}
inline int query(int x){
int res=;x=x>n?n:x;if(x<=) return ;
for(;x;x-=x&-x) res+=c[x];
return res;
}
struct node{
int x,y,z;
node(){}
node(int x,int y,int z):x(x),y(y),z(z){}
inline bool operator <(const node &b)const
{return x!=b.x?x<b.x:
y!=b.y?y>b.y:
z<b.z;}
}a[N],p[N];
void CDQ(int l,int r){
if(l==r) return;
int mid=l+r>>;
CDQ(l,mid),CDQ(mid+,r);
for(int j=mid+,i=l;j<=r;++j){
while(i<=mid&&a[i].y>a[j].y) add(a[i++].z);
ans+=1ll*query(a[j].z-k-)+query(n)-query(a[j].z+k);
}
for(int i=l;i<=mid;++i) clear(a[i].z);
for(int i=l,j=l,k=mid+;i<=r;){
if(k>r||(j<=mid&&a[j].y>a[k].y)) p[i++]=a[j++];
else p[i++]=a[k++];
}
for(int i=l;i<=r;++i) a[i]=p[i];
}
int main(){
//freopen("testdata.in","r",stdin);
n=read(),k=read();
for(int i=;i<=n;++i){
int x=read();b[x]=i;
}
for(int i=;i<=n;++i){
int x=read();
a[i]=node(b[x],i,x);
}
sort(a+,a++n);
CDQ(,n);
printf("%lld",ans);
return ;
}

[USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)的更多相关文章

  1. bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)

    题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...

  2. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  3. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  4. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  5. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  6. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

  7. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】

    题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...

  8. [USACO17FEB]Why Did the Cow Cross the Road III G

    嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...

  9. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

随机推荐

  1. IOS CGAffineTransform 用于视图平移,放缩,旋转

    转载于:http://blog.csdn.net/lc_obj/article/details/17454825 CGAffineTransform 今天碰到了一个旋转放缩图片的一个demo,在看的过 ...

  2. 转)bash快捷键

    粗体表示推荐,也许对每个人不同. Ctrl-A 相当于HOME键,用于将光标定位到本行最前面 Ctrl-E 相当于End键,即将光标移动到本行末尾 Ctrl-B 相当于左箭头键,用于将光标向左移动一格 ...

  3. java代码实现网络远程开机

    http://my.oschina.net/kingfire/blog/156764 概述 远程开机(Wake onLAN)是指通过网络实现对服务器或者pc启动运行,现在很多网卡都支持的这个功能. 其 ...

  4. golang之切片

    1.切片:切片是数组的一个引用,因此切片是引用类型 2.切片的长度可以改变,因此,切片是个可变的数组. 3.切片遍历方式和数组一样,可以用len()求长度 4.cap可以求出slice最大的容量,0& ...

  5. Linux之RPM GPG签名

    原文地址:http://linux.chinaunix.net/techdoc/system/2007/09/26/968723.shtml GPG在Linux上的应用主要是实现官方发布的包的签名机制 ...

  6. [SoapUI]获取Project,Test Suite,Test Case各个级别参数的值

    String testResultPath = testRunner.testCase.testSuite.project.getPropertyValue( "testResultPath ...

  7. [Groovy]SoapUI怎样在Groovy脚本中读取变量的值

    def saveFilePath = context.expand( '${#Project#saveFilePath}' ) def myOutFile = saveFilePath+"t ...

  8. android4.2 高用zing拍照后,返回其它页面操作时,主线程关掉或程序退出的问题解决

    产生错误的代码: @Override protected void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(n ...

  9. 在ceph中:pool、PG、OSD的关系

    原文:http://www.cnblogs.com/me115/p/6366374.html Pool是存储对象的逻辑分区,它规定了数据冗余的类型和对应的副本分布策略:支持两种类型:副本(replic ...

  10. platform总线驱动代码分析

    /************************************************************************/ Linux内核版本:2.6.35.7 运行平台:三 ...