题目链接:https://www.luogu.org/problemnew/show/P3709

离散化+区间众数..?

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn = 500000+10;
  7. inline int read()
  8. {
  9. int k=0;
  10. char c;
  11. c=getchar();
  12. while(!isdigit(c))c=getchar();
  13. while(isdigit(c)){k=(k<<3)+(k<<1)+c-'0';c=getchar();}
  14. return k;
  15. }
  16. int n, m, a[maxn], ans[maxn], cnt[maxn], bl, curL = 1, curR = 0, answer = 0, b[maxn], tot, sum[maxn];
  17. struct query{
  18. int l, r, p;
  19. }e[maxn];
  20. bool cmp(query a, query b)
  21. {
  22. return (a.l/bl) == (b.l/bl) ? a.r < b.r : a.l < b.l;
  23. }
  24. void add(int pos)
  25. {
  26. sum[cnt[a[pos]]]--;
  27. cnt[a[pos]]++;
  28. sum[cnt[a[pos]]]++;
  29. answer = max(answer, cnt[a[pos]]);
  30. }
  31. void remove(int pos)
  32. {
  33. sum[cnt[a[pos]]]--;
  34. cnt[a[pos]]--;
  35. sum[cnt[a[pos]]]++;
  36. while(!sum[answer]) answer--;
  37. }
  38. int main()
  39. {
  40. n = read(); m = read();
  41. bl = sqrt(n);
  42. for(int i = 1; i <= n; i++)
  43. a[i] = b[++tot] = read();
  44. sort(b+1, b+1+tot);
  45. tot = unique(b+1, b+1+tot)-b-1;
  46. for(int i = 1; i <= n; i++)
  47. a[i] = lower_bound(b+1,b+1+tot,a[i])-b;//离散化
  48. for(int i = 1; i <= m; i++)
  49. {
  50. e[i].l = read(); e[i].r = read(); e[i].p = i;
  51. }
  52. sort(e+1, e+1+m, cmp);
  53. for(int i = 1; i <= m; i++)
  54. {
  55. int L = e[i].l, R = e[i].r;
  56. while(curL < L) remove(curL++);
  57. while(curL > L) add(--curL);
  58. while(curR < R) add(++curR);
  59. while(curR > R) remove(curR--);
  60. ans[e[i].p] = answer;
  61. }
  62. for(int i = 1; i <= m; i++)
  63. printf("%d\n",0-ans[i]);
  64. return 0;
  65. }
  66. /*
  67. 7 5
  68. 52 1 52 52 1000000000 1000000000 1000000000
  69. 1 3
  70. 1 2
  71. 1 6
  72. 2 5
  73. 2 2
  74. */

【luogu P3709 大爷的字符串题】 题解的更多相关文章

  1. luogu P3709 大爷的字符串题

    二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...

  2. P3709 大爷的字符串题 (莫队)

    题目 P3709 大爷的字符串题 题意:求\([l,r]\)中众数的个数. 解析 维护两个数组: \(cnt[x]\),数\(x\)出现的次数. \(sum[x]\),出现次数为\(x\)的数的个数. ...

  3. P3709 大爷的字符串题(莫队+结论)

    题目 P3709 大爷的字符串题 做法 有一个显然的结论:一段区间里最小答案为众数的个数 用莫队来离线求众数 \(tmp_i\)表示出现\(i\)次的数的个数,\(num_i\)表示\(i\)出现的次 ...

  4. 洛谷 P3709 大爷的字符串题

    https://www.luogu.org/problem/show?pid=3709 题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个 ...

  5. 洛谷P3709 大爷的字符串题(莫队)

    题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...

  6. P3709 大爷的字符串题(50分)

    题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...

  7. 【Luogu】P3709大爷的字符串题(莫队算法)

    题目链接 语文题啊…… 看题解发现是让求区间中最多的数的个数,于是果断理解了一会题解……莫队套上完事. sum[i]表示i这个数出现的次数,cnt[i]表示出现i次的数有几个,然后乱搞搞……就好了 # ...

  8. 【题解】洛谷P3709大爷的字符串题

    最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...

  9. luogu 3709 大爷的字符串题 构造 莫队 区间众数

    题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...

随机推荐

  1. Http编程之HttpClient

    在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET ...

  2. php之mongodb插入数据后如何返回当前插入记录ID

    <?php /** *插入记录 *参数: *$table_name:表名 *$record:记录 * *返回值: *成功:true *失败:false */ function insert($t ...

  3. 关于MySQLServer5.6配置问题

    配置MySQL MySQL数据库下载以后在根目录添加my.ini配置文件 需要注意的是配置文件的二个属性: basedir=D:\MySqlServer # mysql所在目录 根据需求改 MySQL ...

  4. C# 在窗体的子线程中创建新窗体

    在子线程中如果简单的调用新窗体的话,新出来的窗体会直接一闪而过.没有停留.效果很差 具体解决方法 如下: 在母窗体中建立委托 public delegate void setShowChartForm ...

  5. python 对象属性与 getattr & setattr

    Python对象的属性可以通过obj.__dict__获得,向其中添加删除元素就可以实现python对象属性的动态添加删除的效果,不过我们应该使用更加正规的getattr和setattr来进行这类操作 ...

  6. Maven学习篇一:eclipse构建运行maven web项目

    1.new->other->maven project->next 2.选择创建简单项目(或者直接去掉勾,在后面选择maven-archetype-webapp) 3.设置坐标,名称 ...

  7. abc098D Xor Sum 2(two point)

    题意 题目链接 给出一个序列,求出有多少区间满足\(A[l] \oplus A[l+1] \oplus \dots \oplus A[r] = A[l] + A[l + 1] +\dots+ A[r] ...

  8. PHP环境配置解释

    PHP中注释:#,//,/* */ 一.修改Apache配置 DocumentRoot  "G:\PHP"        //修改完需要重启Apache //以下二选一 ----- ...

  9. FCKEditor编辑器添加中文字体的方法

    默认情况下,FCKEditor在进行文本编辑时,无法使用中文字体.让其添加中文字体的方法: 1.打开 fckconfig.js 文件,找到第154行(大概),会发现: 程序代码: FCKConfig. ...

  10. Java入门到精通——调错篇之Spring2.5利用aspect实现AOP时报错: error at ::0 can't find referenced pointcut XXX

    一.问题描述及原因. 利用Aspect注解实现AOP的时候出现了error at ::0 can't find referenced pointcut XXX.一看我以为注解写错了,结果通过查询相关资 ...