[USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)
题意
两列$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分治)的更多相关文章
- 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 ...
- 洛谷 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 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- [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 ...
- 洛谷 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 ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
- P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...
- [USACO17FEB]Why Did the Cow Cross the Road III G
嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...
- [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)
题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...
随机推荐
- LINQPad 4 初次使用心得
最近学习EntityFramework,于是接触了LinqPad这款享誉已久的软件,深感相见恨晚.软件具体不多做介绍了,只简单介绍下使用方法. 数据库操作 添加数据库连接 1,首先通过点击Add co ...
- 初步认识cookie
cookie是由服务器创建,客户端读取及保存它的 同类请求指的是资源路径相同 Cookie的默认路径绑定是所请求的资源路径绑定的 ,指定路径时必须要有项目名称(说明是哪个项目) 使用cookie时还要 ...
- Python_01-入门基础
以后我会发表一系列python脚本的学习资料,python版本为2.x. 目录: 1 Python入门基础 1.1 学习资源 1.2 所有语言的入门程序---Hello World! 1.3 帮助函 ...
- for 续6
---------siwuxie095 for 实际运用样例(/f 的使用不列出来): for %%i in (*) do echo %%i 显示当前目录下 ,所有非文 ...
- Python MySQLdb连接报2003错误原因
经测试,本地连接使用:localhost会报2003错误. 解决办法: 使用:127.0.0.1代替:localhost.
- Golang之排序算法
冒泡排序 package main //冒泡排序 import "fmt" func bsort(a []int) { ; i < len(a); i++ { ; j < ...
- Excel中保留有效数字的问题
在工作表界面中按 <alt>+<F11>,进入代码页面,然后再 WORKBOOK中插入模块,把以下代码COPY入模块中.就可以在工作表中使用 =YXSZ(数值,保留位数). 如 ...
- Laravel 上传文件处理
文件上传 获取上传的文件 可以使用 Illuminate\Http\Request 实例提供的 file 方法或者动态属性来访问上传文件, file 方法返回 Illuminate\Http\Uplo ...
- hra 直线
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- Android 4.0关于开机启动服务
针对使用App应用管理强制停止的App,重启系统后不能收到开机启动, 需要运行一次后,在下次再启动时,才可以正确收到.