题目描述

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.

请输出不友好的品种对的数量。

输入输出样例

输入样例#1: 复制

4 1
4
3
2
1
1
4
2
3
输出样例#1: 复制

2

说明

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分治,树状数组)的更多相关文章

  1. 洛谷 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 ...

  2. 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, ...

  3. BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

  4. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  5. 【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 样例输 ...

  6. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 214 Shortest Palindrome 最短回文串

    给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...

  2. C. Hongcow Builds A Nation 并查集

    http://codeforces.com/contest/745/problem/C 把他们并查集后, 其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的gover ...

  3. WinForm 对话框,流

    private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...

  4. .net主站和二级域名下实现session共享

    public class CrossDomainCookie : IHttpModule { private string m_RootDomain = string.Empty; #region I ...

  5. 安装CentOS--设置网络_1

    (1)在登录黑框中输入如下命令,让CentOS 7自动获取一个IP地址: # dhclient (2)正常情况下不会有任何输出内容.用如下命令查看获取到的IP地址: # ip addr 它将返回如图所 ...

  6. 简洁大方的wordpress主题,不容错过的主题,附带主题源码下载

    cu主题是由疯狂的大叔设计,界面简洁大方是它最大的特点之一. 手残君也比较喜爱这款主题,在使用的过程中,根据手残君的个人习惯,对其进行了优化. 标题优化 标题居中显示 增加标题div背景色 标题div ...

  7. javscript 导出html中的table到excel

    <script language="JavaScript" type="text/javascript"> /* * 默认转换实现函数,如果需要其他 ...

  8. web 自动化测试 selenium基础到应用(目录)

    第一章   自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...

  9. python3.x 判断当前版本【简单版】

    import sys,string Major_Version_Number = 0 #当前python的主版本 Minor_Version_Number = 0 #当前python的次版本 def ...

  10. 框架开发之Java注解的妙用

    注解的好处:1.能够读懂别人写的代码,特别是框架相关的代码.2.本来可能需要很多配置文件,需要很多逻辑才能实现的内容,就可以使用一个或者多个注解来替代,这样就使得编程更加简洁,代码更加清晰.3.(重点 ...