Kattis Cupid 莫队算法

本文介绍了一个使用莫队算法解决情侣配对问题的经典案例。问题涉及N个男性和N个女性,每人都说一种语言,目标是在给定的询问区间内找到最多能匹配的情侣数。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 NNMM and KK (1N50000,1M50000,1K1000000)(1≤N≤50000,1≤M≤50000,1≤K≤1000000).

  • The second line contains NN integers a0,a1,a2,,aN1a0,a1,a2,…,aN−1, where aiai (0ai<K0≤ai<K) is the language spoken by the iith man.

  • The third line contains NN integers b0,b1,b2,,bN1b0,b1,b2,…,bN−1, where bibi (0bi<K0≤bi<K) is the language spoken by the iith woman.

  • In the next MM lines, each line contains two integers LL and RR (0LR<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 1Sample 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值