Curious Cupid
There are K different languages in the world. Each person speaks one and only one language. There are exactly N single men and N
single women.
Cupid, the god of love, wants to match every single man to a single woman, and vice versa. Everybody wants to find a partner who speaks the same language as s/he does. Communication between the couple is very important! Cupid asks these N
men to stand in a line, and likewise for the N women. Cupid knows that the ith man speaks language ai and the ith woman speaks language bi
.
It is too hard for Cupid to match all people at the same time. What Cupid does is to repeatedly look at some specific interval in these two lines, pick the men and women in that interval and find the maximum number of man-woman pairs who speak the same language and can be matched.
Input
The first line contains three integers N
, M and K (1≤N≤50000,1≤M≤50000,1≤K≤1000000)
.
The second line contains N
integers a0,a1,a2,…,aN−1, where ai (0≤ai<K) is the language spoken by the i
th man.
The third line contains N
integers b0,b1,b2,…,bN−1, where bi (0≤bi<K) is the language spoken by the i
th woman.
In the next M
lines, each line contains two integers L and R (0≤L≤R<N), representing the starting and ending index of the interval. That is, Cupid is looking at men L,L+1,…,R and women L,L+1,…,R
.
Output
For each interval, print the maximum number of couples Cupid could match.
Sample Input 1 | Sample Output 1 |
---|---|
3 4 2 |
1 |
分析:莫队算法的应用;
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int n,m,k,t,cnta[maxn],cntb[maxn],a[maxn],b[maxn],bel[maxn],tot,cnt,ans[maxn];
long long ret;
struct node
{
int l,r,id,block;
bool operator<(const node&p)const
{
return block==p.block?r<p.r:block<p.block;
}
}qu[maxn];
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&k);
int sz=(int)sqrt(n);
for(i=;i<=n;i++)
{
bel[i]=tot;
if(++cnt==sz)tot++,cnt=;
}
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)scanf("%d",&b[i]);
for(i=;i<=m;i++)scanf("%d%d",&qu[i].l,&qu[i].r),qu[i].l++,qu[i].r++,qu[i].id=i,qu[i].block=bel[qu[i].l];
sort(qu+,qu+m+);
int l=,r=;
for(i=;i<=m;i++)
{
while(r < qu[i].r)
{
++cnta[a[++r]];
if(cntb[a[r]]>=cnta[a[r]])++ret;
++cntb[b[r]];
if(cnta[b[r]]>=cntb[b[r]])++ret;
}
while(l > qu[i].l)
{
++cnta[a[--l]];
if(cntb[a[l]]>=cnta[a[l]])++ret;
++cntb[b[l]];
if(cnta[b[l]]>=cntb[b[l]])++ret;
}
while(r > qu[i].r)
{
if(cntb[a[r]]>=cnta[a[r]])--ret;
--cnta[a[r]];
if(cnta[b[r]]>=cntb[b[r]])--ret;
--cntb[b[r--]];
}
while(l < qu[i].l)
{
if(cntb[a[l]]>=cnta[a[l]])--ret;
--cnta[a[l]];
if(cnta[b[l]]>=cntb[b[l]])--ret;
--cntb[b[l++]];
}
ans[qu[i].id]=ret;
}
for(i=;i<=m;i++)printf("%d\n",ans[i]);
return ;
}
Curious Cupid的更多相关文章
- kattis Curious Cupid (莫队算法)
Curious Cupid There are K different languages in the world. Each person speaks one and only one lang ...
- HNU 13308 Help cupid
Help cupid Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13308&c ...
- hdu 5112 A Curious Matt
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5112 A Curious Matt Description There is a curious ma ...
- HDU 5112 A Curious Matt 水题
A Curious Matt Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- Viz World and Viz Curious Maps 教程 -- 基础篇
0. 开篇之前的一些废话 本文的内容是之前因为一些原因而写的,现在打算分享出来,内容就不做更改纯迁移了…毕竟我也太久没摸过加密狗了( ╯□╰ ).内容定位是教程,对应的 Curious World M ...
- hdoj 5112 A Curious Matt
A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- 水题:HDU 5112 A Curious Matt
Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on the ...
- HDU 1756 Cupid's Arrow 判断点在多边形的内部
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Curious Robin Hood(树状数组+线段树)
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...
随机推荐
- javaBean注意事项
1.重写tostring方法 2.属性第一位小写
- 杂项:BPM
ylbtech-杂项:BPM 1.返回顶部 1. BPM,即业务流程管理,是一种以规范化的构造端到端的卓越业务流程为中心,以持续的提高组织业务绩效为目的的系统化方法,常见商业管理教育如EMBA.MBA ...
- 如何在vue项目中引入阿里巴巴的iconfont图库
1. 打开 http://www.iconfont.cn/ 2. 选择我们喜欢的图标,点击上面的小车,加入图标库,即右侧的购物车 3.点击购物车,点击下载代码 4.解压下载的文件夹,将文件夹复制到 a ...
- A. Power Consumption Calculation
http://codeforces.com/problemset/problem/10/A 题很简单,就是题意难懂啊... #include <stdio.h> #include < ...
- 枚举详解之EnumSet、EnumMap用法
枚举简单例子 /** * @author shuliangzhao * @Title: Color * @ProjectName design-parent * @Description: TODO ...
- GoLang 编译exe添加ICO图标
我们在做Go开发的时候在Window平台下编译出来的exe后大部分都是没有图标,看起来很难看.下面我们说下如何添加一个图标. 1.首先在根目录下,exe的同级目录下创建.rc文件, IDI_ICON1 ...
- Java Socket编程 深入讲解?你之前真的学懂了吗
很多人学习Socket往往会碰到这样哪样的问题,可以看看下面,加深理解.能看懂多少看懂多少. Socket是Java网络编程的基础,了解还是有好处的,这篇文章主要讲解Socket的基础编程.Socke ...
- JS/JQuery操作DOM元素笔记
原因 自己目前在搭建一个.NET Core的框架,正在构建权限这块的东西,今天设置权限界面,需要使用JavaScript操作DOM元素,记录一下. 页面大概是酱紫的(我使用的AdminLTE和LayU ...
- BZOJ 4140 凸包+二进制分组
思路: $(x_0-x)^2+(y_0-y)^2<=x^2+y^2$ $y>=(-x_0/y_0)x+(x_0^2+y_0^2)/2y0$ 这显然就是凸包了 以一个斜率不断向下(上)走 ...
- jsp 中包含 一个路径为变量的文件
<head> <base href="<%=basePath%>"> <% String fileroot="MyJsp.jsp ...