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, introduced in the preceding two problems. He realizes now that the threshold for friendliness is a bit more subtle than he previously considered -- breeds aa and bb are now friendly if |a - b| \leq K∣a−b∣≤K, and unfriendly otherwise. Given the orderings of fields on either side of the road through FJ's farm, please count the number of unfriendly crossing pairs of breeds, where a crossing pair of breeds is defined as in the preceding problems.
思考过前两个问题后,农民约翰正在继续思考如何对付穿过农场的牛的问题。 他现在意识到,友好的品种的标准比他以前想的稍微微妙一些 -对于品种a,ba,b 如果|a - b| \leq K∣a−b∣≤K,现在是友好的。 否则是不友好的。给定这条路两边的田地的顺序,请计算有多少交叉的不良品种对,其中一对品种在前问题被定义。
输入输出格式
输入格式:
The first line of input contains NN (1 \leq N \leq 100,0001≤N≤100,000) and KK (0 \leq K < N0≤K<N). The next NN lines describe the order, by breed ID, of fields on one side of the road; each breed ID is an integer in the range 1 \ldots N1…N. The last NN lines describe the order, by breed ID, of the fields on the other side of the road. Each breed ID appears exactly once in each ordering.
一行包含NN (1 \leq N \leq 100,0001≤N≤100,000)与KK (0 \leq K < N0≤K<N),接下来NN行按顺序描述了小路一旁田地的品种的ID号,每一个ID号是一个在1...N1...N之间的整数。倒数NN行描述了小路另一旁田地的品种的ID号。每个ID只在一个顺序中出现一次
输出格式:
Please output the number of unfriendly crossing pairs of breeds.
请输出不友好的品种对的数量。
输入输出样例
说明
In this example, breeds 1 and 4 are unfriendly and crossing, as are breeds 1 and 3.
题意就是求|a[i].a-a[j].a|>k,a[i].b>a[j].b&&a[i].c<a[j].c || a[i].b<a[j].b&&a[i].c>a[j].c
满足这个即可,这就是一个三维偏序问题,和陌上花开一样,一维排序,一维CDQ,一维树状数组。
这样在第三维中,因为是绝对值,所以ans为query(num-k-1)+query(n)-query(num+k),转化一下形式
一点点小技巧即可。
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio> #define ll long long
#define N 100007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,k;ll ans;
int tr[N];
struct Node
{
int a,b,c;
}a[N]; bool cmp1(Node x,Node y)
{
if (x.a==y.a&&x.b==y.b)return x.c<y.c;
if (x.a==y.a) return x.b<y.b;
return x.a<y.a;
}
bool cmp2(Node x,Node y)
{
if (x.b==y.b) return x.c<y.c;
return x.b>y.b;
}
inline int lowbit(int x){return x&(-x);}
inline void update(int x,int num)
{
for (int i=x;i<=n;i+=lowbit(i))
tr[i]+=num;
}
inline int query(int x)
{
int res=;
for (int i=x;i>=;i-=lowbit(i))
res+=tr[i];
return res;
}
void cdq(int l,int r)
{
if (l==r) return;
int mid=(l+r)>>;
cdq(l,mid),cdq(mid+,r);
sort(a+l,a+mid+,cmp2),sort(a+mid+,a+r+,cmp2);
int i=l,j=mid+;
while(j<=r)
{
while(i<=mid&&a[i].b>a[j].b)update(a[i].c,),i++;
ans+=(ll)(query(a[j].c-k-)+query(n)-query(min(a[j].c+k,n))),j++;
}
for (int j=l;j<i;j++)update(a[j].c,-);
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout); n=read(),k=read();
for (int i=;i<=n;i++)
{
int x=read();
a[x].a=i;
}
for (int i=;i<=n;i++)
{
int x=read();
a[x].b=i;
}
for (int i=;i<=n;i++)
a[i].c=i;
sort(a+,a+n+,cmp1);
cdq(,n);
printf("%lld\n",ans);
}
bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)的更多相关文章
- 洛谷 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 ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
- 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...
随机推荐
- 一个页面通过iframe,获取另一个页面的form
document.getElementsByTagName("iframe")[0].contentWindow.document.forms[0].submit(); var z ...
- Jquery操作常用表单元素
由于对前端的东西不是很熟练,导致jquery操作表单的东西总是忘记,每次用都要查一下,效率低下,记录下来,以便下次使用. CheckBox checkbox改变事件 $('#IsAllSearch') ...
- Html标签杂记
<html> <head> <title> </title> </head> <body> </body> < ...
- iOS捷径(Workflow 2.0)拓展
前言 iOS12 捷径(Workflow 2.0)入门 iOS12 捷径(Workflow 2.0)进阶 iOS12捷径(Workflow 2.0)实例大全 注:本文主要介绍如何获取URL Schem ...
- (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记
1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...
- 老潘 - ListView分析 - 学以致用篇(一)
ListView分析学以致用篇(1) 在我们查看别人的博客的时候,一个人是一个风格的.先说下我的风格,我喜欢思想类比,然后介绍知识,不太喜欢填鸭式的灌输.如果只是想单纯的从我的博客中直接看到代码,我个 ...
- vue1.0生命周期
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- git 控制操作
克隆文件 git clone https://gitee.com/xxxxx/xxxxx.git 克隆分支文件 git clone -b 分支名 https://gitee.com/xxxxx/xxx ...
- 编程规范:allocator
一.作用 标准库allocator类定义在头文件memory中,它帮助我们将内存分配和对象构造分离开来 allocator<T> a //定义一个名为a的allocator对象,它可以为类 ...
- (3)Gojs model简介
(3)Gojs model简介 在GoJS中,model用来存储表的基本数据,包括node.link等具体对象和属性,与其在视觉上的展示效果不相关.model中往往只保存相对简单的数据,最方便且持久化 ...