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 ...
随机推荐
- MarkMonitor 目前最安全的域名注册商,因此,世界500强网站中的22%域名托管于markmonitor公司
也许你查询某个大型公司域名whios信息时,常常会发现很多这些大型公司的域名都在一家名为MarkMonitor的公司注册,那么markmonitor是家什么样的公司呢? MarkMonitor是一家从 ...
- 快速访问GitHub
Github网站可以访问,但是由于网络代理商的原因,造成访问速度很慢. 本文采取方法:手动更改hosts Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与 ...
- Hadoop 的使用
hadoop:hadoop启动:./sbin/start-dfs.shhadoop关闭:./sbin/stop-dfs.shbin文件用于在HDFS创建数据HDFS 中创建用户目录:./bin/hdf ...
- JS判断数组是否包含某元素
我在学习ES6数组拓展时,发现了新增了不少了有趣的数组方法,突然想好工作中判断数组是否包含某个元素是非常常见的操作,那么这篇文章顺便做个整理. 1.for循环结合break 可能很多人第一会想到for ...
- sql 查询出当天记录数据
select updatetime,NewComment,HistoryID,sum(1) over(partition by UpdateTime) from LPProjectHistoryord ...
- $CF1141B Maximal Continuous Rest$
告诉你一天n件事情 a[i]为1是休息 a[i]为2是工作 求最长连续的休息时间(即最长的1 可以作为环状来求.(即环状最长的1 这题就可以用前缀和贪心等什么操作.. 然后用\(ans1ans2\)瞎 ...
- printf的实型
参 数 说 明 %f 按实数格式输出,整数部分按实际位数输出,6位小数 %m.nf 总位数m(含小数点),其中有n位小数 %-m.nf 同上,左对齐 %0.xf 输出小数点后x位 %f 后面如 ...
- Django与 Ajax
什么是json? 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子 ...
- 【转】linux下vi命令大全
转自:http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi ...
- 6.11---上传图片遇到的bug,字节流输入流输出流----图解----图片必须是post
!!!这里要注意不能是目录必须是指定的文件名+目录,不然就存照片到指定的目录不成功 ----------------------------------------完整controller-servi ...