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
  1. 3 4 2
  2. 0 0 1
  3. 0 0 0
  4. 0 0
  5. 2 2
  6. 0 1
  7. 1 2
  1. 1
  2. 0
  3. 2
  4. 1
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #define inf 0x3f3f3f3f
  12. #define met(a,b) memset(a,b,sizeof a)
  13. #define pb push_back
  14. typedef long long ll;
  15. using namespace std;
  16. const int N = 5e4+;
  17. const int M = 1e6+;
  18. ll num[N],up[N],dw[N],ans,aa,bb,cc;
  19. int n,m,k;
  20. int x[N],y[N],pos[N];
  21. int cntx[M],cnty[M];
  22. struct qnode {
  23. int l,r,id;
  24. } qu[N];
  25. bool cmp(qnode a,qnode b) {
  26. if(pos[a.l]==pos[b.l])
  27. return a.r<b.r;
  28. return pos[a.l]<pos[b.l];
  29. }
  30. void update(int p,int d) {
  31. if(x[p]==y[p]) {
  32. cntx[x[p]]+=d;
  33. cnty[x[p]]+=d;
  34. ans+=d;
  35. } else {
  36. if(d==) {
  37. if(cnty[x[p]]>cntx[x[p]]) {
  38. ans+=d;
  39. }
  40. if(cntx[y[p]]>cnty[y[p]]) {
  41. ans+=d;
  42. }
  43. } else {
  44. if(cnty[x[p]]>=cntx[x[p]]) {
  45. ans+=d;
  46. }
  47. if(cntx[y[p]]>=cnty[y[p]]) {
  48. ans+=d;
  49. }
  50. }
  51. cntx[x[p]]+=d;
  52. cnty[y[p]]+=d;
  53. }
  54. }
  55. int main() {
  56. int bk,pl,pr,id;
  57. scanf("%d%d%d",&n,&m,&k);
  58. memset(num,,sizeof num);
  59. bk=ceil(sqrt(1.0*n));
  60. for(int i=; i<=n; i++) {
  61. scanf("%d",&x[i]);
  62. pos[i]=(i-)/bk;
  63. }
  64. for(int i=; i<=n; i++) {
  65. scanf("%d",&y[i]);
  66. }
  67. for(int i=; i<m; i++) {
  68. scanf("%d%d",&qu[i].l,&qu[i].r);
  69. qu[i].l++;
  70. qu[i].r++;
  71. qu[i].id=i;
  72. }
  73. sort(qu,qu+m,cmp);
  74. pl=,pr=;
  75. ans=;
  76. for(int i=; i<m; i++) {
  77. id=qu[i].id;
  78. if(pr<qu[i].r) {
  79. for(int j=pr+; j<=qu[i].r; j++)
  80. update(j,);
  81. } else {
  82. for(int j=pr; j>qu[i].r; j--)
  83. update(j,-);
  84. }
  85. pr=qu[i].r;
  86. if(pl<qu[i].l) {
  87. for(int j=pl; j<qu[i].l; j++)
  88. update(j,-);
  89. } else {
  90. for(int j=pl-; j>=qu[i].l; j--)
  91. update(j,);
  92. }
  93. pl=qu[i].l;
  94. up[id]=ans;
  95. }
  96.  
  97. for(int i=; i<m; i++)
  98. printf("%d\n",up[i]);
  99. return ;
  100. }

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. 对C++ templates类模板的几点补充(Traits类模板特化)

    前一篇文章<浅谈C++ templates 函数模板.类模板以及非类型模板参数>简单的介绍了什么是函数模板(这个最简单),类模板以及非类型模板参数.本文对类模板再做几点补充. 文章目录1. ...

  2. cdh版本的zookeeper安装以及配置(伪分布式模式)

    需要的软件包:zookeeper-3.4.5-cdh5.3.6.tar.gz  1.将软件包上传到Linux系统指定目录下: /opt/softwares/cdh 2.解压到指定的目录:/opt/mo ...

  3. win10 update orchestratorservere禁用

      1 Windows 10系统中有一项Update Orchestrator Service(更新协调器办事),在当地办事窗口中,我们发现 Update Orchestrator Service 办 ...

  4. 【POJ 2572 Advertisement】

    Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 947Accepted: 345Special Judge Description ...

  5. bzoj

    准确率爆棚啊,然而

  6. JS获取当前时间及时间戳相互转换

    1.获取当前时间的 时间戳 Date.parse(new Date()) 结果:1486347562000 2.获取当前 时间 new Date() 结果:Mon Feb 06 2017 10:19: ...

  7. Failed to resolve:com.android.support:appcompat-v7

    http://blog.csdn.net/mhl18820672087/article/details/78385361

  8. Ecplise添加XML自动提示

    这里以struts.xml为例 第一步: 首先找到 struts2的核心jar包,我这里是struts2-core-2.3.20.jar用压缩工具打开或者解压下来

  9. input 单选按钮radio 取消选中(转载)

    input单选按钮: 在radio按钮中添加属性tag  0代表未被选中 HTML代码: <input name="rdo1" value="AA" ty ...

  10. IOS 学习资料整理{非常有用,强烈推荐}

    绝地地的资源博客:我是雷锋不用谢~~啦啦啦 https://blog.csdn.net/kunga0814/article/details/82117090