[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 ...
随机推荐
- oracle数据库在sys下直接修改用户密码
首先用sys登录上去, 在命令窗口或者是能执行输入sql语句的地方输入下面代码, 回车就好 alter user you_username(要修改的用户名) identified by you_pas ...
- linux shell脚本编程笔记(三): 三种引号的区别
双引号.单引号.反引号的区别 测试用例: OPDATE=`date -d '-1 day' +%Y%m%d` ) do FILEDATE=`date -d "-$i day" +% ...
- Linux 下批量创建用户(shell 命令)
第一种方法: 用shell批量创建用户,分为2中:1,批量创建的用户名无规律 :2.批量创建的用户名有规律首先,来说下批量创建的用户名无规律的shell:先把需要批量创建的用户名用一个文本文档列出来, ...
- weblogic 初始化
weblogic无法启动,或是忘记了登陆密码,需要初始化,可以删除weblogic配置然后重新生成配置.步骤如下 1> 找到weblogic的启动路径,打开jdeveloper,run后,查看日 ...
- PHP 中 Traits 的简单使用
PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西. Traits可以理解 ...
- Java Thread系列(五)synchronized
Java Thread系列(五)synchronized synchronized锁重入 关键字 synchronized 拥有锁重入的功能,也就是在使用 synchronized 时,当线程等到一个 ...
- 文件读取草稿(excel,csv)
using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using ...
- Button或者ImageButton的背景设为透明或者半透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明& ...
- UVa 10559 Blocks (DP)
题意:一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.求最大分数是多少. 析:dp[i][j][k] 表示消除 i ~ j,并且右边再拼上 k 个 颜色等于a[j] ...
- CodeForces 686B Little Robber Girl's Zoo (构造冒泡排序)
题意:给定一排列,让你通过一个区间交换的方式,完成排序. 析:这个题说了,最多不能超过20000次,而 n 最大才100,那么冒泡排序复杂度为 n * n,才10000,肯定是可以的,所以我们就模拟冒 ...