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
0 0 1
0 0 0
0 0
2 2
0 1
1 2
1
0
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的更多相关文章

  1. kattis Curious Cupid (莫队算法)

    Curious Cupid There are K different languages in the world. Each person speaks one and only one lang ...

  2. HNU 13308 Help cupid

    Help cupid Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13308&c ...

  3. hdu 5112 A Curious Matt

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5112 A Curious Matt Description There is a curious ma ...

  4. HDU 5112 A Curious Matt 水题

    A Curious Matt Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  5. Viz World and Viz Curious Maps 教程 -- 基础篇

    0. 开篇之前的一些废话 本文的内容是之前因为一些原因而写的,现在打算分享出来,内容就不做更改纯迁移了…毕竟我也太久没摸过加密狗了( ╯□╰ ).内容定位是教程,对应的 Curious World M ...

  6. hdoj 5112 A Curious Matt

    A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) ...

  7. 水题:HDU 5112 A Curious Matt

    Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on the ...

  8. HDU 1756 Cupid's Arrow 判断点在多边形的内部

    Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. Curious Robin Hood(树状数组+线段树)

    1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...

随机推荐

  1. LCA__st算法&&树上倍增

    st表 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ]; ] ...

  2. createrepo

    [root@iio enp]# createrepo -g /enp/comps.xml .Spawning worker 0 with 1362 pkgsWorkers FinishedSaving ...

  3. MyEclipse个性设置

    MyEclipse个性设置 (1)Myeclipse 打开 jsp 的默认编辑器不好,会同时打开预览.所以做如下更改 Windows–>Perferences–>General–>E ...

  4. Akka源码分析-ask模式

    在我之前的博文中,已经介绍过要慎用Actor的ask.这里我们要分析一下ask的源码,看看它究竟是怎么实现的. 开发时,如果要使用ask方法,必须要引入akka.pattern._,这样才能使用ask ...

  5. SpringBoot2.0整合Redission

    Redisson是redis一个很强大的客户端,有兴趣的同学可以看我的下一篇文章,这篇主要讲如何整合,费话不多说,直接上干货(大牛请绕道) 首先创建RedissionConfig文件 import o ...

  6. blockhouses

    题意 : 给你一张图上面" X " 代表墙 , " . " 代表空地 , 让你在空地上放置炮台 , 条件是 不能 让彼此的炮台 可以互相看见 ( 隔着墙就看不见 ...

  7. BZOJ 2946 SA/SAM

    思路: 1. 二分+后缀数组 2.SAM //By SiriusRen #include <cstdio> #include <cstring> #include <al ...

  8. ASP.NET 之 Chart Control for .Net Framework

    ps:曾经靠着这张图做了很多的图形报表

  9. LAMP配置课程基础知识详解

    听了一天的课程,我本人对这个还是很感兴趣的. [root@localhost~]#     root 用户名 localhost 本地 ~  家目录 不同用户不同 #当前用户是管理员 $当前用户是普通 ...

  10. JS——arguments

    1.只在函数中使用 2.返回的是实参的数组 <script> getNum(1, 2);//(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ ...