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 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- [POI2011]Lightning Conductor
题面在这里 description 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\). 对于每个\(1\le i\le n\),找到最小的非负整数\(p\), 满足对于任意的\(1 ...
- [学习笔记]LCT进阶操作
LCT总结——应用篇(附题单)(LCT) 一般都是维护链的操作.split即可搞定. 进阶操作的话,处理好辅助树和原树的关系即可搞定. 其实,最大的区别就是,splay随便转,辅助树形态变了,但是原树 ...
- 如何使用Navicat恢复数据库脚本
Navicat 可以做数据库备份,当然也可以做数据库脚本恢复了.操作很简单. 1.连接需要恢复的数据库.鼠标右键点击,选择[运行SQL文件] 2.在弹出的窗口中选择sql文件,继续下一步即可. 余不赘 ...
- 带依赖包的maven打包配置
转载自:http://outofmemory.cn/code-snippet/2594/carry-yilai-bao-maven-dabao-configuration 可以在maven的packa ...
- Python爬虫学习笔记之抓取猫眼的排行榜
代码: import json import requests from requests.exceptions import RequestException import re import ti ...
- bzoj1251: 序列终结者 fhqtreap写法
fhqtreap的速度果然很快 花了时间学了下指针写法 没有旋转 只有分裂以及合并操作 其实还是蛮好写的 #include<cstdio> #include<cstring> ...
- I wrote a JSONHelper extension
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonso ...
- 扑克牌(cards)
扑克牌 思路 这题也是二分!! 我们二分有几套牌,然后再去检验是否符合,至于怎么想到的,不要问我,我也不知道 那么我们主要解决的就是check函数 我们将二分的套数和每种牌的数量进行比较,如果该种牌的 ...
- wxpython SizerItem的大小控制
wxpython SizerItem的大小控制 proportion控制控件相对大小,proportion如果为0,表示默认大小.比如一个box里面有两个相同控件A,B,如果A,B的proportio ...
- TensorFlow 官方文档中文版【转】
转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/ TensorFlow 官方文档中文版 你正在阅读的项目可能会比 Android 系统更加深远 ...