码迷,mamicode.com
首页 > 编程语言 > 详细

kattis Curious Cupid (莫队算法)

时间:2017-02-27 22:54:57      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:sam   output   tom   inpu   ++   hidden   memset   back   莫队   

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 (1N50000,1M50000,1K1000000)

  • .

  • The second line contains N

integers a0,a1,a2,,aN?1, where ai (0ai<K) is the language spoken by the i
  • th man.

  • The third line contains N

integers b0,b1,b2,,bN?1, 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 1Sample 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 (莫队算法)

标签:sam   output   tom   inpu   ++   hidden   memset   back   莫队   

原文地址:http://www.cnblogs.com/jianrenfang/p/6476752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!