There are K K different languages in the world. Each person speaks one and only one language. There are exactly N N single men and N 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 N men to stand in a line, and likewise for the N N women. Cupid knows that the i ith man speaks language ai ai and the i ith woman speaks language bi 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 N, M M and K K (1≤N≤50000,1≤M≤50000,1≤K≤1000000) (1≤N≤50000,1≤M≤50000,1≤K≤1000000).
-
The second line contains N N integers a0,a1,a2,…,aN−1 a0,a1,a2,…,aN−1, where ai ai ( 0≤ai<K 0≤ai<K) is the language spoken by the i ith man.
-
The third line contains N N integers b0,b1,b2,…,bN−1 b0,b1,b2,…,bN−1, where bi bi ( 0≤bi<K 0≤bi<K) is the language spoken by the i ith woman.
-
In the next M M lines, each line contains two integers L L and R R ( 0≤L≤R<N 0≤L≤R<N), representing the starting and ending index of the interval. That is, Cupid is looking at men L,L+1,…,R L,L+1,…,R and women L,L+1,…,R 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 |
题意:
有K种不同的语言,每个人只说一种语言。有N个男性,N个女性。
现在需要进行男女配对,每个人都想找一个和自己用同一种语言的异性,第i个男性的语言是ai, 第i个女性的语言是bi。
现在给出M个区间作为询问,每次需要回答在[L, R]区间的男女最多能够配对成多少对情侣。
题解:裸的莫队算法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,a,b) for(i=a;i<=b;i++)
#define per(i,a,b) for(i=a;i>=b;i--)
int n,m,k;
struct node{
int l,r,lab;
}e[50005];
bool cmp(node a,node b){
if((a.l)/250==(b.l)/250){
return a.r<b.r;
}
else return a.l<b.l;
}
int a[50005],b[50005],ans[50005],num1[1000005],num2[1000005];
int main(){
scanf("%d%d%d",&n,&m,&k);
int i,j,x,y;
rep(i,1,n)scanf("%d",&a[i]);
rep(i,1,n)scanf("%d",&b[i]);
rep(i,1,m)scanf("%d%d",&e[i].l,&e[i].r),e[i].lab=i,e[i].l+=1,e[i].r+=1;
sort(e+1,e+1+n,cmp);
int now=(a[1]==b[1]),ls=1,rs=1;
num1[a[ls]]++;
num2[b[ls]]++;
rep(i,1,m){
while(ls<e[i].l){
num1[a[ls]]--;
num2[b[ls]]--;
if(a[ls]==b[ls]){
now--;
ls++;
continue;
}
if(num2[a[ls]]>num1[a[ls]])now--;
if(num1[b[ls]]>num2[b[ls]])now--;
ls++;
}
while(ls>e[i].l){
ls--;
num1[a[ls]]++;
num2[b[ls]]++;
if(a[ls]==b[ls]){
now++;
continue;
}
if(num2[a[ls]]>=num1[a[ls]])now++;
if(num1[b[ls]]>=num2[b[ls]])now++;
}
while(rs<e[i].r){
rs++;
num1[a[rs]]++;
num2[b[rs]]++;
if(a[rs]==b[rs]){
now++;
continue;
}
if(num2[a[rs]]>=num1[a[rs]])now++;
if(num1[b[rs]]>=num2[b[rs]])now++;
}
while(rs>e[i].r){
num1[a[rs]]--;
num2[b[rs]]--;
if(a[rs]==b[rs]){
rs--;
now--;
continue;
}
if(num2[a[rs]]>num1[a[rs]])now--;
if(num1[b[rs]]>num2[b[rs]])now--;
rs--;
}
ans[e[i].lab]=now;
}
rep(i,1,m)printf("%d\n",ans[i]);
return 0;
}