1188 - Fast Queries
Time Limit: 3 second(s) Memory Limit: 64 MB

Given an array of N integers indexed from 1 to N, and q queries, each in the form i j, you have to find the number of distinct integers from index i to j (inclusive).

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105)q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers range in [0, 105].

Each of the next q lines will contain a query which is in the form i j (1 ≤ i ≤ j ≤ N).

Output

For each test case, print the case number in a single line. Then for each query you have to print a line containing number of distinct integers from index i to j.

Sample Input

Output for Sample Input

1

8 5

1 1 1 2 3 5 1 2

1 8

2 3

3 6

4 5

4 8

Case 1:

4

1

4

2

4

Note

Dataset is huge. Use faster I/O methods.

题目链接:LighOJ 1188

莫队大法好,看到n的范围是1e5也就直接上莫队了,用vis记录数字出现次数,显然对于区间的增减进行更新vis,若缩小区间导致vis[val]为0则说明少了一个数,反之为1则就是增加一个数,700MS还可以,另外一种不同的做法是用树状数组更新统计。

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<bitset>
  7. #include<cstdio>
  8. #include<string>
  9. #include<deque>
  10. #include<stack>
  11. #include<cmath>
  12. #include<queue>
  13. #include<set>
  14. #include<map>
  15. using namespace std;
  16. #define INF 0x3f3f3f3f
  17. #define CLR(x,y) memset(x,y,sizeof(x))
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. typedef pair<int,int> pii;
  22. typedef long long LL;
  23. const double PI=acos(-1.0);
  24. const int N=100010;
  25. const int M=50010;
  26. struct info
  27. {
  28. int l,r;
  29. int id,b;
  30. bool operator<(const info &t)const
  31. {
  32. if(b==t.b)
  33. return r<t.r;
  34. return b<t.b;
  35. }
  36. };
  37. info Q[M];
  38. int arr[N];
  39. int vis[N];
  40. int ans[M];
  41. int main(void)
  42. {
  43. int tcase,i,j;
  44. int n,m,l,r;
  45. scanf("%d",&tcase);
  46. for (int q=1; q<=tcase; ++q)
  47. {
  48. scanf("%d%d",&n,&m);
  49. for (i=1; i<=n; ++i)
  50. scanf("%d",&arr[i]);
  51. int unit=(int)sqrt(n);
  52. for (i=1; i<=m; ++i)
  53. {
  54. scanf("%d%d",&Q[i].l,&Q[i].r);
  55. Q[i].id=i;
  56. Q[i].b=Q[i].l/unit;
  57. }
  58. sort(Q+1,Q+1+m);
  59. CLR(vis,0);
  60. int L=1,R=0;
  61. int temp=0;
  62. for (i=1; i<=m; ++i)
  63. {
  64. while (L>Q[i].l)
  65. {
  66. --L;
  67. ++vis[arr[L]];
  68. if(vis[arr[L]]==1)
  69. ++temp;
  70. }
  71. while (L<Q[i].l)
  72. {
  73. --vis[arr[L]];
  74. if(vis[arr[L]]==0)
  75. --temp;
  76. ++L;
  77. }
  78. while (R>Q[i].r)
  79. {
  80. --vis[arr[R]];
  81. if(vis[arr[R]]==0)
  82. --temp;
  83. --R;
  84. }
  85. while (R<Q[i].r)
  86. {
  87. ++R;
  88. ++vis[arr[R]];
  89. if(vis[arr[R]]==1)
  90. ++temp;
  91. }
  92. ans[Q[i].id]=temp;
  93. }
  94. printf("Case %d:\n",q);
  95. for (i=1; i<=m; ++i)
  96. printf("%d\n",ans[i]);
  97. }
  98. return 0;
  99. }

LightOJ 1188 Fast Queries(简单莫队)的更多相关文章

  1. Strange Queries(莫队)

    题目 You are given an array with n integers a1, a2, ..., an, and q queries to answer. Each query consi ...

  2. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

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

  3. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  4. BZOJ 1878 hh的项链(简单莫队)

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的项链变得 ...

  5. 【 Gym - 101138D 】Strange Queries (莫队算法)

    BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤  ...

  6. BZOJ 3339 && luogu4137 Rmq Problem / mex(莫队)

    P4137 Rmq Problem / mex 题目描述 有一个长度为n的数组{a1,a2,-,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 输入输出格式 输入格式: 第一行n,m. ...

  7. 洛谷 P3901 数列找不同(莫队)

    题目链接:https://www.luogu.com.cn/problem/P3901 这道题简单莫队模板题,然后$add$和$del$分别处理$vis[]$从$0-->1$和从$1--> ...

  8. 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)) 在 ...

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

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

随机推荐

  1. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  2. SPI的通信试验 --verilog (从机-全双工)

    SPI的 有关知识参考FPGA作为主机的通信实验. 本实验中FPGA作为从机通过SPI与MCU等通信的试验,可以在时钟上升沿接收数据并且在时钟下降沿发送数据,模仿全双工模式.接收的 数据作为地址,通过 ...

  3. 脚踏实地学C#2-引用类型和值类型

    引用类型和值类型介绍 CLR支持两种类型,引用类型和值类型两种基本的类型: 值类型下有int.double.枚举等类型同时也可以称为结构,如int结构类型.double结构类型,所有的值类型都是隐式密 ...

  4. Oracle 日常应用和操作笔记

    简单整理oracle日常应用笔记. 1.采用excel表格中的数据直接粘贴数据库记录中,默认会在后面加一个空格“”,操作完成后一定要记得对空格匹配然后修改一下. 2.查询数据库里的所有表结构, 采用s ...

  5. $GLOBALS['HTTP_RAW_POST_DATA'] 和$_POST的区别

    $_POST:通过 HTTP POST 方法传递的变量组成的数组.是自动全局变量. $GLOBALS['HTTP_RAW_POST_DATA'] :总是产生 $HTTP_RAW_POST_DATA 变 ...

  6. UML中的关联关系

    UML中的关联关系其内在意思就是has a 如图:  相对于依赖关系,关联关系在代码中有所体现.上图中的关联关系在代码中体现为       其中water 中将Climate作为其中的属性. 当然,关 ...

  7. 人人都可以开发高可用高伸缩应用——论Azure Service Fabric的意义

    今天推荐的文章其实是微软的一篇官方公告,宣布其即将发布的一个支撑高可用高伸缩云服务的框架--Azure Service Fabric. 前两天,微软Azure平台的CTO Mark Russinovi ...

  8. IOS杂谈

    1 IOS名称是iPhone Operating System 的缩写,原本这个系统名为iPhone OS,意思是iPhone 操作系统. 2 IOS的开发环境是Xcode.Xcode就成为了iPho ...

  9. loj 1013(LCS+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25839 思路:第一小问可以很快求出了,两个字符串的长度-LCS,然 ...

  10. sqlserver日常维护脚本

    SQL code --备份declare @sql varchar(8000) set @sql='backup database mis to disk=''d:\databack\mis\mis' ...