题目描述

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. C#内存映射文件学习[转]

    原文链接 内存映射文件是由一个文件到进程地址空间的映射. C#提供了允许应用程序把文件映射到一个进程的函(MemoryMappedFile.CreateOrOpen).内存映射文件与虚拟内存有些类似, ...

  2. shell 2 解析

    ---- shell 3 /home/oracle/utility/macro/call_autopurge_arch.sh Description: Call purge archive log f ...

  3. Android中集成第三方支付

    常见的第三方支付解决方案 支付宝支付 微信支付 银联支付 Ping++统一支付平台(需要继承服务器端和客户端) 短信支付 支付宝的集成流程 相关资料链接: 支付宝支付指引流程:支付指引流程 支付宝An ...

  4. C#中的事件机制

    这几天把事件学了一下,总算明白了一些.不多说了,直接代码. class Program { static void Main(string[] args) { CatAndMouse h = new ...

  5. springmvc系列一 之配置介绍(包含官网doc)

    1.springmvc 官网参考地址: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 2 ...

  6. 将php中session存入redis中

    PHP 的会话默认是以文件的形式存在的,可以配置到 Redis 中,即提高了访问速度,又能很好地实现会话共享! 配置方式如下: 方法一:修改 php.ini 的设置 session.save_hand ...

  7. Boost库编译安装

    一.Boost库介绍         Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库工作组成员发起,其 ...

  8. .net 操作xml --移除注释节点

    /// <summary> /// xml字符串转xml文档 忽略注释信息 /// </summary> /// <param name="sXml" ...

  9. 迅为7寸Android嵌入式安卓触摸屏,工业一体机方案

    嵌入式安卓触摸屏板卡介绍-工业级核心板: 嵌入式安卓触摸屏功能接口介绍: 品质保障: 核心板连接器:进口连接器,牢固耐用,国产连接器无法比拟(为保证用户自行设计的产品品质,购买核心板用户可免费赠送底板 ...

  10. leetcode_894. All Possible Full Binary Trees

    https://leetcode.com/problems/all-possible-full-binary-trees/ 给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子 ...