Kattis-Curious Cupid(莫队)

本文介绍了一种使用莫队算法解决区间查询问题的方法,具体为在给定的男女语言偏好列表中,寻找指定区间内能形成最多语言匹配的情侣对数。

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

题目链接

Curious Cupid

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  i th man speaks language  ai  and the  i th 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   (1N50000,1M50000,1K1000000) .

  • The second line contains  N  integers  a0,a1,a2,,aN1 , where  ai  ( 0ai<K ) is the language spoken by the  i th man.

  • The third line contains  N  integers  b0,b1,b2,,bN1 , where  bi  ( 0bi<K ) is the language spoken by the  i th woman.

  • In the next  M  lines, each line contains two integers  L  and  R  ( 0LR<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
题意:

有K种不同的语言,每个人只说一种语言。有N个男性,N个女性。

现在需要进行男女配对,每个人都想找一个和自己用同一种语言的异性,第i个男性的语言是ai, 第i个女性的语言是bi。

现在给出M个区间作为询问,每次需要回答在[L, R]区间的男女最多能够配对成多少对情侣。

(1 <= N <= 50000, 1 <= M <= 50000, 1 <= K <= 1000000), (0 <= ai < K, 0 <= bi < K), (0 <= L <= R < N)。


题解:

这题是多组区间查询,我们考虑莫队算法对查询排序
相邻两个区间的转移可以通过维护区间内不同数字的个数进行转移
转移复杂度O(1)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=5e4+100;
const int maxm=1e6+100;
int a[maxn],b[maxn];
int cnt1[maxm],cnt2[maxm];

int n,m,k;
int unit;

struct node
{
    int l,r,id,ans;
    bool operator < (const node &t) const{
        if(l/unit==t.l/unit) return r<t.r;
        else return l/unit<t.l/unit;
    }
}q[maxm];
void solve()
{
    int l=1,r=0;
    int ans=0;
    rep(i,1,m+1)
    {
        while(l<q[i].l)
        {
            if(a[l]==b[l]) ans--;
            else
            {
                if(cnt1[a[l]]<=cnt2[a[l]]) ans--;
                if(cnt2[b[l]]<=cnt1[b[l]]) ans--;
            }
            cnt1[a[l]]--,cnt2[b[l]]--;
            l++;
        }
        while(l>q[i].l)
        {
            l--;
            if(a[l]==b[l]) ans++;
            else
            {
                if(cnt1[a[l]]<cnt2[a[l]]) ans++;
                if(cnt2[b[l]]<cnt1[b[l]]) ans++;
            }
            cnt1[a[l]]++,cnt2[b[l]]++;
        }
        while(r>q[i].r)
        {
            if(a[r]==b[r]) ans--;
            else
            {
                if(cnt1[a[r]]<=cnt2[a[r]]) ans--;
                if(cnt2[b[r]]<=cnt1[b[r]]) ans--;
            }
            cnt1[a[r]]--,cnt2[b[r]]--;
            r--;
        }
        while(r<q[i].r)
        {
            r++;
            if(a[r]==b[r]) ans++;
            else
            {
                if(cnt1[a[r]]<cnt2[a[r]]) ans++;
                if(cnt2[b[r]]<cnt1[b[r]]) ans++;
            }
            cnt1[a[r]]++,cnt2[b[r]]++;
        }
        q[i].ans=ans;
    }
}
bool cmp(node a,node b)
{
    return a.id<b.id;
}


int main()
{
    scanf("%d%d%d",&n,&m,&k);
    rep(i,1,n+1) scanf("%d",&a[i]);
    rep(i,1,n+1) scanf("%d",&b[i]);
    rep(i,1,m+1) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i,q[i].l++,q[i].r++;
    unit=(int)sqrt(n);
    sort(q+1,q+m+1);
    solve();
    sort(q+1,q+m+1,cmp);
    rep(i,1,m+1) printf("%d\n",q[i].ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值