kattis Curious Cupid (莫队算法)
Posted 贱人方
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kattis Curious Cupid (莫队算法)相关的知识,希望对你有一定的参考价值。
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 ith man speaks language ai and the ith 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 (1≤N
-
.
-
The second line contains N
-
th man.
-
The third line contains N
-
th woman.
-
In the next M
-
.
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 |
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back typedef long long ll; using namespace std; const int N = 5e4+10; const int M = 1e6+10; ll num[N],up[N],dw[N],ans,aa,bb,cc; int n,m,k; int x[N],y[N],pos[N]; int cntx[M],cnty[M]; struct qnode { int l,r,id; } qu[N]; bool cmp(qnode a,qnode b) { if(pos[a.l]==pos[b.l]) return a.r<b.r; return pos[a.l]<pos[b.l]; } void update(int p,int d) { if(x[p]==y[p]) { cntx[x[p]]+=d; cnty[x[p]]+=d; ans+=d; } else { if(d==1) { if(cnty[x[p]]>cntx[x[p]]) { ans+=d; } if(cntx[y[p]]>cnty[y[p]]) { ans+=d; } } else { if(cnty[x[p]]>=cntx[x[p]]) { ans+=d; } if(cntx[y[p]]>=cnty[y[p]]) { ans+=d; } } cntx[x[p]]+=d; cnty[y[p]]+=d; } } int main() { int bk,pl,pr,id; scanf("%d%d%d",&n,&m,&k); memset(num,0,sizeof num); bk=ceil(sqrt(1.0*n)); for(int i=1; i<=n; i++) { scanf("%d",&x[i]); pos[i]=(i-1)/bk; } for(int i=1; i<=n; i++) { scanf("%d",&y[i]); } for(int i=0; i<m; i++) { scanf("%d%d",&qu[i].l,&qu[i].r); qu[i].l++; qu[i].r++; qu[i].id=i; } sort(qu,qu+m,cmp); pl=1,pr=0; ans=0; for(int i=0; i<m; i++) { id=qu[i].id; if(pr<qu[i].r) { for(int j=pr+1; j<=qu[i].r; j++) update(j,1); } else { for(int j=pr; j>qu[i].r; j--) update(j,-1); } pr=qu[i].r; if(pl<qu[i].l) { for(int j=pl; j<qu[i].l; j++) update(j,-1); } else { for(int j=pl-1; j>=qu[i].l; j--) update(j,1); } pl=qu[i].l; up[id]=ans; } for(int i=0; i<m; i++) printf("%d\n",up[i]); return 0; }
以上是关于kattis Curious Cupid (莫队算法)的主要内容,如果未能解决你的问题,请参考以下文章