题意

两列$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. MyBatis 注解式开发

  2. Python_02-控制语句

    目录:  1         控制结构...  1.1      分支语句...  1.1.1   if语句的嵌套...  1.2      for循环...  1.2.1   Python 循环中的 ...

  3. react-native 组件的导入、导出

    一.前言背景: 学习react native的关键在于组件,依靠组件的拼接达到想要的效果,由此可见,组件就像一块块功能各异的零件,最终搭建出我们想要的效果. 今天我们就从组件的导入.导出开始 下面是我 ...

  4. 用Diff和Patch工具维护源码

    在Unix系统下,维护源码版本可以使用很多方法,其中最常用的当然是大名鼎鼎的CVS,但实际上,简单的版本维护工作并没有必要使用复杂的CVS等专门的版本维护工具,Unix标配中的diff和patch工具 ...

  5. php闭包bindTo方法用法

    从手册知道,Closure::bindTo — 复制当前闭包对象,绑定指定的$this对象和类作用域. 创建并返回一个 匿名函数, 它与当前对象的函数体相同.绑定了同样变量,但可以绑定不同的对象,也可 ...

  6. Linux Terminator

    NAME Terminator - Multiple GNOME terminals in one window SYNOPSIS terminator [options] DESCRIPTION T ...

  7. Shadow Map 实现极其细节

    这里不介绍算法原理,只说说在实现过程中遇到的问题,以及背后的原因.开发环境:opengl 2.0  glsl 1.0. 第一个问题:产生深度纹理. 在opengl中每一次离屏渲染需要向opengl提供 ...

  8. catkin

    catkin  ros https://github.com/dirkholz/pcl_online_viewer rosrun  ???

  9. [Selenium]通过JavaScript来对隐藏的元素执行操作

    对不可见元素进行操作时,如果通过普通的方式不可行,可以尝试用Javascript Scroll hidden element into view ((JavascriptExecutor) drive ...

  10. iOS7中的多任务I

    [改变了后台任务的运行方式] 在iOS6和之前的系统中,系统在用户退出应用后,如果应用正在执行后台任务的话,系统会保持活跃状态直到后台任务完成或者是超时以后,才会进入真正的低功耗休眠状态. 而在iOS ...