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. LaTeX 在线编辑器(LaTeX online editors)

    eqneditor:有强大的几乎所有常用的数学符号对应的图标形式,便于快速完成latex公式编辑且易于粘贴拷贝. 此外,更为重要的一点是,随着编辑窗口内公式的编辑,会在页面的底部,自动生成其对应的 h ...

  2. 第二周 Leetcode 493. Reverse Pairs(HARD)

    leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...

  3. JAVA Swing 事件监听

    EventListner 接口 它是一个标记接口,每一个监听器接口扩展.这个类定义在java.util包. 类声明 以下是声明java.util.EventListener接口: public int ...

  4. SpringMVC+MyBaties关于上传(跟新)图片的问题

    /** * 方法名:uploadPhoto * 描 述:TODO(上传图片) * 作 者:池彦龙 * 时 间:2017/03/30 * 返回类型: * 参 数: * 异 常: */ Controlle ...

  5. jsp jquery js 的基本路径获取

    引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决!   1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...

  6. Java8080端口被占用解决办法

    netstat -ano | findstr 8080 taskkill -pid 3196-f

  7. 【学习笔记】OI玄学道—代码坑点

    [学习笔记]\(OI\) 玄学道-代码坑点 [目录] [逻辑运算符的短路运算] [\(cmath\)里的贝塞尔函数] 一:[逻辑运算符的短路运算] [运算规则] && 和 || 属于逻 ...

  8. url参数为数组

    //url中state参数为数组 ?baseline_id=12&version_id=34&state[]=complete&state[]=hangup&state ...

  9. 关于vector.size()和string.length() 的返回类型 size_type

    今天写循环的时候碰到一个问题,发现:string.length()返回的类型是size_type.它是unsigned 类型.string::size_type它在不同的机器上,长度是可以不同的,并非 ...

  10. 【Leetcode 3】Longest Substring Without Repeating Characters0

    Description: Given a string, find the length of the longest substring without repeating characters. ...