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;
}

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值