There are KK different languages in the world. Each person speaks one and only one language. There are exactly NN single men and NN 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 NN men to stand in a line, and likewise for the NN women. Cupid knows that the iith man speaks language aiai and the iith woman speaks language bibi.
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 NN, MM and KK (1≤N≤50000,1≤M≤50000,1≤K≤1000000)(1≤N≤50000,1≤M≤50000,1≤K≤1000000).
-
The second line contains NN integers a0,a1,a2,…,aN−1a0,a1,a2,…,aN−1, where aiai (0≤ai<K0≤ai<K) is the language spoken by the iith man.
-
The third line contains NN integers b0,b1,b2,…,bN−1b0,b1,b2,…,bN−1, where bibi (0≤bi<K0≤bi<K) is the language spoken by the iith woman.
-
In the next MM lines, each line contains two integers LL and RR (0≤L≤R<N0≤L≤R<N), representing the starting and ending index of the interval. That is, Cupid is looking at men L,L+1,…,RL,L+1,…,R and women L,L+1,…,RL,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;
}