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≤50000,1≤M≤50000,1≤K≤1000000)

  • .

  • The second line contains N

integers a0,a1,a2,…,aN−1, where ai (0≤ai<K) is the language spoken by the i

  • th man.

  • The third line contains N

integers b0,b1,b2,…,bN−1, where bi (0≤bi<K) is the language spoken by the i

  • th woman.

  • In the next M

lines, each line contains two integers L and R (0≤L≤R<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
#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+;
const int M = 1e6+;
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==) {
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,,sizeof num);
bk=ceil(sqrt(1.0*n));
for(int i=; i<=n; i++) {
scanf("%d",&x[i]);
pos[i]=(i-)/bk;
}
for(int i=; i<=n; i++) {
scanf("%d",&y[i]);
}
for(int i=; 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=,pr=;
ans=;
for(int i=; i<m; i++) {
id=qu[i].id;
if(pr<qu[i].r) {
for(int j=pr+; j<=qu[i].r; j++)
update(j,);
} else {
for(int j=pr; j>qu[i].r; j--)
update(j,-);
}
pr=qu[i].r;
if(pl<qu[i].l) {
for(int j=pl; j<qu[i].l; j++)
update(j,-);
} else {
for(int j=pl-; j>=qu[i].l; j--)
update(j,);
}
pl=qu[i].l;
up[id]=ans;
} for(int i=; i<m; i++)
printf("%d\n",up[i]);
return ;
}

kattis Curious Cupid (莫队算法)的更多相关文章

  1. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  3. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  4. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  5. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  6. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  7. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  8. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  9. 信心题--FUOJ2226(莫队算法)

    http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...

随机推荐

  1. [CF543D]Road Improvement

    题目大意:给定一个无根树,给每条边黑白染色,求出每个点为根时,其他点到根的路径上至多有一条黑边的染色方案数,模$1e9+7$. 题解:树形$DP$不难想到,记$f_u$为以$1$为根时,以$u$为根的 ...

  2. TextView AutoLink, ClikSpan 与长按事件冲突的解决

    前言 首先,我们先来复习一下 autoLink 和 ClickableSpan 是干什么用的. autoLink 当中有五个属性值:分别是 phone.email.map.web.all 和 none ...

  3. [HEOI2017]分手是祝愿 期望概率dp 差分

    经分析可知:I.操作每个灯可看做一种异或状态 II.每个状态可看做是一些异或状态的异或和,而且每个异或状态只能由它本身释放或放入 III.每一种异或状态只有存在不存在两中可行状态,因此这些灯只有同时处 ...

  4. Kafka配置文档

    http://kafka.apache.org/08/configuration.html

  5. 使用vue开发webApp,安卓手机自带回退键的问题解决

    首先,我先为大家说明,为什么我要写这篇随笔: 因为我们写的webapp,在安卓手机上,按一次回退键,就会退出app,回到桌面,而不是像原生app一样,会有一个提示,例如,“再按一次退出应用”的这种提示 ...

  6. 着色方案(bzoj 1079)

    Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...

  7. loj6030 「雅礼集训 2017 Day1」矩阵

    传送门:https://loj.ac/problem/6030 [题解] 以下把白称为0,黑称为1. 发现只有全空才是无解,否则考虑构造. 每一列,只要有0的格子都需要被赋值1次,所以设有x列有含有0 ...

  8. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

  9. 联系人数据存储Demo源代码

    源码下载地址:07-联系人数据存储.zip35.8 KB // MJPerson.h // //  MJPerson.h //  07-联系人数据存储 // //  Created by apple ...

  10. 程序异常退出 却没有产生core文件

    程序异常退出  却没有产生core文件 http://www.cnblogs.com/my_life/articles/4107333.html