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. 【题解】SDOI2009学校食堂

    不知道有没有人跟我有一样的感觉……实际上很多的状压DP都不难,然而调到心碎……这题题面看起来很长,还有混合的‘位运算’来吓唬人(实际上就是异或而已).但实际上只要仔细阅读,发现也是一道水水的裸题. 首 ...

  2. [洛谷P2482][SDOI2010]猪国杀

    题目大意:猪国杀,又一道大模拟题 题解:模拟,对于一个没有玩过三国杀的人来说,一堆细节不知道,写的十分吃力 卡点:无数,不想说什么了,这告诉我要多玩游戏 C++ Code: #include < ...

  3. [Leetcode] word break 拆分词语

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 用PHP迭代器来实现一个斐波纳契数列

    斐波纳契数列通常做法是用递归实现,当然还有其它的方法.这里现学现卖,用PHP的迭代器来实现一个斐波纳契数列,几乎没有什么难度,只是把类里的next()方法重写了一次.注释已经写到代码中,也是相当好理解 ...

  5. vue.单选和多选,纯css自定义单选框样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. [05]Git查看、删除、重命名远程分支和tag

    Git查看.删除.重命名远程分支和tag 2015-06-15:加入姊妹篇: 2013-11-06:加入重命名远程分支的内容: 2013-01-09:加入删除远程tag的内容. 姊妹篇:使用Git.G ...

  7. HTTP中的URL长度限制

    首先,其实http 1.1 协议中对url的长度是不受限制的,协议原文: The HTTP protocol does not place any a priori limit on the leng ...

  8. openstack 问题澄清

    1. neutron中plugin与agent是一一对应的吗? 在不使用ml2时,plugin与agent一一对应,如ovs-plugin与ovs-agent:当使用ml2 plugin时,该plug ...

  9. C++11 lambda函数符

    #include<iostream> #include<vector> #include<algorithm> #include<cmath> #inc ...

  10. Codeforces 106 DIV2 ACD

    B表示完全看不懂..就不弄了.. E字符串先不管了.到时候系统学下字符串再处理 A #include <map> #include <set> #include <lis ...