题目

You are given an array with n integers a1, a2, ..., an, and q queries to answer.

Each query consists of four integers l1, r1, l2 and r2. Your task is to count the number of pairs of indices (i, j) satisfying the following conditions:

ai = aj

l1 ≤ i ≤ r1

l2 ≤ j ≤ r2

Input

The first line of the input contains an integer n (1 ≤ n ≤ 50 000) — the size of the given array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).

The third line contains an integer q (1 ≤ q ≤ 50 000) — the number of queries.

Each of the next q lines contains four integers l1, r1, l2, r2 (1 ≤ l1 ≤ r1 ≤ n, 1 ≤ l2 ≤ r2 ≤ n), describing one query.

Output

For each query count the number of sought pairs of indices (i, j) and print it in a separate line.

Examples

Input

7

1 5 2 1 7 2 2

8

1 3 4 5

2 3 5 7

1 4 3 7

2 6 4 7

1 6 2 5

3 3 6 7

4 5 1 4

2 3 4 5

Output

1

2

5

6

6

2

2

0

题目:

您将获得一个包含n个整数a1,a2,...,an和q查询的数组。

每个查询由四个整数l1,r1,l2和r2组成。您的任务是计算满足以下条件的索引对(i,j)的数量:

$ a_i = a_j $

L1 ≤ i ≤ R1

L2 ≤ j ≤ R2

输入

输入的第一行包含一个整数n(1≤N≤50 000) -给定阵列的大小。

第二行包含n个整数a1,a2,...,a(1≤ai≤n)。

第三行包含一个整数q(1≤q≤50000) - 查询数。

接下来的q行中的每一行包含四个整数l1,r1,l2,r2(1≤l1≤r1≤n,1≤l2≤r2≤n),描述一个查询。

输出

对于每个查询计数所寻求的索引对(i,j)的数量,并将其打印在单独的行中。

示例输入

  1. 7
  2. 1 5 2 1 7 2 2
  3. 8
  4. 1 3 4 5
  5. 2 3 5 7
  6. 1 4 3 7
  7. 2 6 4 7
  8. 1 6 2 5
  9. 3 3 6 7
  10. 4 5 1 4
  11. 2 3 4 5

示例输出

  1. 1
  2. 2
  3. 5
  4. 6
  5. 6
  6. 2
  7. 2
  8. 0

仔细观察题目我们不难发现这题我们需要维护两个区间内元素的出现次数

嗯?这不是裸裸的莫队吗?等等,两个\(50000\)的区间,我不敲时谁敲时?

这谁做得出啊??干脆维护一个区间算了,暴力枚举第二个区间算了!!

于是某年某月某日某蒟蒻以2000毫秒的好成绩卡了过去:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iomanip>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<ctime>
  8. #include<cmath>
  9. #include<vector>
  10. #include<queue>
  11. #include<map>
  12. #include<set>
  13. #define db double
  14. #define inf 0x7fffffff
  15. #define rg register int
  16. using namespace std;
  17. struct su{
  18. int l,r,ll,rr,t;
  19. }k[50001];
  20. long long now;
  21. int z[50001];
  22. int s[50001];
  23. int tot[50001];
  24. int tot2[50001];
  25. long long ans[50001];
  26. int n,m,q,f,l=1,r,ll=1,rr;
  27. inline int qr(){
  28. char ch;
  29. while((ch=getchar())<'0'||ch>'9');
  30. int res=ch^48;
  31. while((ch=getchar())>='0'&&ch<='9')
  32. res=res*10+(ch^48);
  33. return res;
  34. }
  35. inline bool cmp(su x,su y){
  36. return z[x.l]==z[y.l]?x.r<y.r:x.l<y.l;
  37. }
  38. inline void add(int x,int y){
  39. now-=(long long)tot2[x]*tot[x];
  40. tot[x]+=y;
  41. now+=(long long)tot2[x]*tot[x];
  42. }
  43. inline void add2(int x,int y){
  44. now-=(long long)tot[x]*tot2[x];
  45. tot2[x]+=y;
  46. now+=(long long)tot[x]*tot2[x];
  47. }
  48. int main(){
  49. //freopen(".in","r",stdin);
  50. //freopen(".out","w",stdout);
  51. n=qr();
  52. f=sqrt(n-1)+1;
  53. for(rg i=1;i<=n;++i) s[i]=qr();
  54. for(rg i=1,j=1;i<=n;++i)
  55. z[i]=i%f==0?j++:j;
  56. m=qr();
  57. for(rg i=1;i<=m;++i){
  58. k[i].l=qr();
  59. k[i].r=qr();
  60. k[i].ll=qr();
  61. k[i].rr=qr();
  62. k[i].t=i;
  63. }
  64. sort(k+1,k+m+1,cmp);
  65. for(rg i=1;i<=m;++i){
  66. while(l<k[i].l)add(s[l],-1),++l;
  67. while(k[i].l<l)add(s[l-1],1),--l;
  68. while(r<k[i].r)add(s[r+1],1),++r;
  69. while(k[i].r<r)add(s[r],-1),--r;
  70. while(ll<k[i].ll)add2(s[ll],-1),++ll;
  71. while(k[i].ll<ll)add2(s[ll-1],1),--ll;
  72. while(rr<k[i].rr)add2(s[rr+1],1),++rr;
  73. while(k[i].rr<rr)add2(s[rr],-1),--rr;
  74. ans[k[i].t]=now;
  75. }
  76. for(rg i=1;i<=m;++i)
  77. printf("%lld\n",ans[i]);
  78. return 0;
  79. }

Strange Queries(莫队)的更多相关文章

  1. CFGym101138D Strange Queries 莫队/分块

    正解:莫队/分块 解题报告: 传送门 ummm这题耗了我一天差不多然后我到现在还没做完:D 而同机房的大佬用了一个小时没有就切了?大概这就是大佬和弱鸡的差距趴QAQ 然后只是大概写下思想好了因为代码我 ...

  2. XOR Queries(莫队+trie)

    题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...

  3. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  4. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  5. cf375D. Tree and Queries(莫队)

    题意 题目链接 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. Sol 想到了主席树和启发式 ...

  6. [Codeforces375D]Tree and Queries(莫队算法)

    题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k 因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解 直接用一个数组统计出现 ...

  7. Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥

    传送门--Gym 传送门--BZOJ THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j ...

  8. LightOJ 1188 Fast Queries(简单莫队)

    1188 - Fast Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...

  9. CodeForces 376F Tree and Queries(假·树上莫队)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

随机推荐

  1. No.1101_第十次团队会议

    今天项目进展很多,大家都在现在的成果而开心,信心高涨,后面的任务的完成也基本都能指日可待.之前团队出现了各种问题,沟通出现了很多障碍,导致各方面受阻.现在大家再面对面坦诚相对,交流了一下自己的想法,结 ...

  2. someday团队Postmortem(事后诸葛亮会议)

    一.会议相关介绍: 时间:2018年1月12日 地点:第九实验楼五楼机房 参会人员:someday团队全体成员 二.每个成员在beta阶段的实践和alpha阶段有何改进? (一)设想和目标: 我们的软 ...

  3. 2017-08-20 block,inline和inline-block概念和区别

    display:inline.block.inline-block的区别 display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都 ...

  4. bzip2 以及 tar 压缩/解压缩/.打包等工具软件

    1. bzip2 命令 基础格式: bzip2 [Options] file1 file2 file3 指令选项:(默认功能为压缩) -c //将输出写至标准输出 -d //进行解压操作 -v //输 ...

  5. 【转帖】Git学习笔记 记录一下

    本文内容参考了廖雪峰老师的博文,并做了适当整理,方便大家查阅. 原帖地址 https://wangfanggang.com/Git/git/ 常用命令 仓库初始化 - git init 1 git i ...

  6. chrome-extension & inject.js

    chrome-extension & inject.js chrome-extension://gppongmhjkpfnbhagpmjfkannfbllamg/js/inject.js in ...

  7. 重启Hbase命令

    注意先启动hadoop,记得重启zookeeper. 具体操作如下: cd hadoop-2.7.4/sbin/ && ./stop-all.sh && ./start ...

  8. BZOJ 3171 循环格(费用流)

    题意 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位置(r,c),你可以沿着箭头防线在格子间行走.即如果(r ...

  9. P2704 [NOI2001]炮兵阵地

    题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...

  10. 【BZOJ1560】[JSOI2009]火星藏宝图(贪心,动态规划)

    [BZOJ1560][JSOI2009]火星藏宝图(贪心,动态规划) 题面 BZOJ 洛谷 题解 既然所有的位置的权值都大于\(0\),那么就可以直接贪心,按照行为第一关键字,列为第二关键字,来转移. ...