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≤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 |
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 (莫队算法)的更多相关文章
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- 信心题--FUOJ2226(莫队算法)
http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- 【题解】HAOI2007分割矩阵
水题盛宴啦啦啦……做起来真的极其舒服,比某些毒瘤题好太多了…… 数据范围极小 --> 状压 / 搜索 / 高维度dp:观察要求的均方差,开始考虑是不是能够换一下式子.我们用\(a_{x}\)来表 ...
- [洛谷P3195][HNOI2008]玩具装箱TOY
题目大意:有n个物体,大小为$c_i$.把第i个到第j个放到一起,容器的长度为$x=j-i+\sum\limits_{k-i}^{j} c_k$,若长度为x,费用为$(x-L)^2$.费用最小. 题解 ...
- Java操作Redis存储对象类型数据
背景描述 关于JAVA去操作Redis时,如何存储一个对象的数据,大家是非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用.存储一个对象是是 ...
- myisam_sort_buffer_size vs sort_buffer_size
Q: I am MySQL on server with 6GB RAM. I need to know what is the difference between myisam_sort_buff ...
- [NOI2002] 银河英雄传说 (带权并查集)
题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...
- 类名.class 类名.this 详解
我们知道在java中,一个类在被加载的时候虚拟机就会自动的生成一个这个类的一个Class类型的“类对象”,每个类都对应着一个这样的类对象,通过这个Class类型的类对象,我们就能够使用“内省与反射”机 ...
- JS 中 call 和 apply 的理解和使用
本文受到了知乎问题 如何理解和熟练运用js中的call及apply? 的启发. obj.call(thisObj, arg1, arg2, ...); obj.apply(thisObj, [arg1 ...
- 常用原生客户端js
var el = document.createElement('pre'); // 创建 <pre></pre>元素 el.id = 'sss'; // 添加id <p ...
- 教主泡嫦娥(RQNOJ 595)
题目描述 [问题背景] 2012年12月21日下午3点14分35秒,全世界各国的总统以及领导人都已经汇聚在中国的方舟上. 但也有很多百姓平民想搭乘方舟,毕竟他们不想就这么离开世界,所以他们决定要么登上 ...
- tr/td
在HTML中,tr代表行,td代表列. 说明: 1.tr与td必须一起使用,并且输入的内容必须在td里面: 2.td必须在tr里面,表示在一行中的列: 3.在一个tr里面,有x个td,就表示在这一行里 ...