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 ...
随机推荐
- [Codeforces Round49F] Session in BSU
[题目链接] http://codeforces.com/contest/1027/problem/F [算法] 二分图匹配 [代码] #include<bits/stdc++.h> #p ...
- Flink之Stateful Operators
Implementing Stateful Functions source function的stateful看官网,要加lock Declaring Keyed State at the Runt ...
- jsp jquery js 的基本路径获取
引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决! 1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...
- JQuery 总结
JQuery官方网站 http://jquery.com/ 1.JQuery概念 A.Jquery是一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器,jQu ...
- c++ pow函数
函数名称: pow 函数原型: double pow( double x, double y ); 函数功能: 计算x的y次幂 例:z=pow(x,y); x=9,y=8 z就是9 ...
- day02_12/12/2016_bean的实例化之普通工厂方式
- [转]linux grep命令
转自:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表 ...
- ios cocos2d-x 多点触摸
需要在 AppController.mm文件中添加一句多点触摸支持: [_view setMultipleTouchEnabled:true];
- MySQL的or/in/union与索引优化 | 架构师之路
假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...
- [ HNOI 2006 ] 公路修建问题
\(\\\) \(Description\) 一个\(N\)个点\(M\)条边的图,每条边可以选择\(w_i,p_i\)两个边权之一,现求一个生成树上的最大边权最小值,要求这棵生成树上至少有\(K\) ...