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 ...
随机推荐
- Oracle查询使用空间比较大的前15个表
-- 查询使用空间比较大的前15个表 select a.* from( ,2) all_size from dba_segments where SEGMENT_TYPE='TABLE' order ...
- C#画图——Graphics
C#要实现简单的画图功能可以利用Graphics这个类,要使用Graphics必需using命名空间System.Drawing(此名明空间下都是关于图形的操作).首先创建画布: Bitmap bmp ...
- C. Dasha and Password 预处理 + dp
http://codeforces.com/contest/761/problem/C 对于每一个字符串,可以预处理出其到达数字,字母,和特殊符号所需的最小步数. 然后就是在n个东西中,选出数字.字母 ...
- 希尔排序法及其js实现
希尔(Shell)排序又称为缩小增量排序,它是一种插入排序.它是直接插入排序算法的加强版. 希尔增量: 希尔增量是指希尔提出了一种冲破二次时间屏障的算法. Donald Shell 提出了一种冲破二次 ...
- No rule to make target ...
在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...
- spark on yarn模式下内存资源管理(笔记2)
1.spark 2.2内存占用计算公式 https://blog.csdn.net/lingbo229/article/details/80914283 2.spark on yarn内存分配** 本 ...
- System.Web.Optimization找不到引用怎么办
新建Bootstap for MVC5出现的问题, 通过打开VS 工具->NUGET程序包管理器->控制台 输入以下命令进行完成,一切完成 Install-Package Microsof ...
- html页面比较长,如何用js实现网页一打开显示在网页的中部?
加入js代码 <style type="text/css"> body { height: 2000px; } </style> <script ty ...
- thinkphp5生成二维码
1.运用composer下载拓展到vendor下 composer require aferrandini/phpqrcode 2.common.php 里面写生成二维码函数 <?php // ...
- MFC定时器的使用
巧妙地使用定时器能达到意想不到的效果,写界面的时候能实现渐变,也能帮助多线程控制等我们知道,在VC的MFC中,已经为我们封装好了很多全面和强大的函数集,所以在MFC编程时,巧妙地调用MFC函数库可以为 ...