[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 ...
随机推荐
- less gradient-vertical 方法的实现
// Vertical gradient using CSS where possible, and base64-encoded SVG for IE9 (enables use of this i ...
- LDA处理文档主题分布代码
[python] LDA处理文档主题分布代码入门笔记 http://blog.csdn.net/eastmount/article/details/50824215
- ConcurrentHashMap的简单理解
一.效率低下的HashTable容器HashTable容器使用synchronized来保证线程安全,但在线程竞争激烈的情况下HashTable的效率非常低下.因为当一个线程访问HashTable的同 ...
- token的作用及实现原理
1:首先,先了解一下request和session的区别request 指在一次请求的全过程中有效,即从http请求到服务器处理结束,返回响应的整个过程,存放在HttpServletRequest对象 ...
- mysql 一次性插入上万条数据测试专用
无聊期间 记录下 mysql 一次性插入上万条数据 测试的时候可以用 首先 创建一个表 t3 create table t3(id int)ENGINE = MyISAM; \d // 表示吧m ...
- sqlserver流程控制(待续)
if else: if(1=1) begin--必须1个=号print '111'--begin end 之间必须要有内容end else beginprint '222'end while: DEC ...
- 创建Kafka0.8.2生产者与消费者
一.下载安装Kafka0.8.2 二.vi config/server.properties 三.修改为advertised.host.name=192.168.1.76 四.rm -rf /tmp ...
- kafka常用运维命令
列出所有topic:bin/kafka-topics.sh --zookeeper localhost:2181 --list说明:其实就是去检查zk上节点的/brokers/topics子节点,打印 ...
- hive分隔符总结
ascii对应的表Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex ----------------- ...
- python 开发简单的聊天工具-乾颐堂
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...