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 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- Springboot2.0 集成shiro权限管理
在springboot中结合shiro教程搭建权限管理,其中几个小细节的地方对新手不友好,伸手党更是无法直接运行代码,搭建过程容易遇坑,记录一下.关键的地方也给注释了. 版本:springboot版本 ...
- DDX_Control、SubclassWindow和SubclassDlgItem
文章参考地址:http://blog.sina.com.cn/s/blog_86fe5b440101au88.html:http://www.cnblogs.com/riskyer/p/3424278 ...
- 【BZOJ 3925】[Zjoi2015]地震后的幻想乡 期望概率dp+状态压缩+图论知识+组合数学
神™题........ 这道题的提示......(用本苣蒻并不会的积分积出来的)并没有 没有什么卵用 ,所以你发现没有那个东西并不会 不影响你做题 ,然后你就可以推断出来你要求的是我们最晚挑到第几大的 ...
- bzoj4145 AMPPZ2014 The Prices 状压dp
这个题.......很可以,很小清晰......反正正经的东西我都没想到:重点在于——————我不会处理那个多出来的路费所以当时我就骚骚的弄了一颗树包状压其实这是一个类01背包的状压在每个状态用01背 ...
- TCP ------ TCP四次挥手(断开连接)及断开过程
1.正常情况下,调用close(),产生的其中一个效果就是发送FIN,只有双方都调用close(),才会出现正常的四次挥手. 2.如果是服务器,发起四次挥手是在关闭accept()返回的套接字,而不是 ...
- kafka.common.ConsumerRebalanceFailedException异常解决
kafka.common.ConsumerRebalanceFailedException: group_dd-1446432618163-2746a209 can't rebalance after ...
- idea使用(一)
基本上正式开发的常用工具基本都集成了,而且基本都在你非常容易触到的位置.说说我比较常用的: 1.ant 你懂的 2.maven你也懂的 3.SVN相比之下,IDEA的SVN的提交提供了更多的选项和功能 ...
- IE设置信任站点和安全级别(bat文件)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1 ...
- Ubuntu下hadoop集群搭建
--修改IP地址(克隆镜像后可修改可不修改) http://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html -------------- ...
- 关于UML
http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141433.html