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

离散化+区间众数..?

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 500000+10;
inline int read()
{
int k=0;
char c;
c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c)){k=(k<<3)+(k<<1)+c-'0';c=getchar();}
return k;
}
int n, m, a[maxn], ans[maxn], cnt[maxn], bl, curL = 1, curR = 0, answer = 0, b[maxn], tot, sum[maxn];
struct query{
int l, r, p;
}e[maxn];
bool cmp(query a, query b)
{
return (a.l/bl) == (b.l/bl) ? a.r < b.r : a.l < b.l;
}
void add(int pos)
{
sum[cnt[a[pos]]]--;
cnt[a[pos]]++;
sum[cnt[a[pos]]]++;
answer = max(answer, cnt[a[pos]]);
}
void remove(int pos)
{
sum[cnt[a[pos]]]--;
cnt[a[pos]]--;
sum[cnt[a[pos]]]++;
while(!sum[answer]) answer--;
}
int main()
{
n = read(); m = read(); bl = sqrt(n); for(int i = 1; i <= n; i++)
a[i] = b[++tot] = read(); sort(b+1, b+1+tot);
tot = unique(b+1, b+1+tot)-b-1;
for(int i = 1; i <= n; i++)
a[i] = lower_bound(b+1,b+1+tot,a[i])-b;//离散化 for(int i = 1; i <= m; i++)
{
e[i].l = read(); e[i].r = read(); e[i].p = i;
} sort(e+1, e+1+m, cmp); for(int i = 1; i <= m; i++)
{
int L = e[i].l, R = e[i].r;
while(curL < L) remove(curL++);
while(curL > L) add(--curL);
while(curR < R) add(++curR);
while(curR > R) remove(curR--);
ans[e[i].p] = answer;
}
for(int i = 1; i <= m; i++)
printf("%d\n",0-ans[i]);
return 0;
}
/*
7 5
52 1 52 52 1000000000 1000000000 1000000000
1 3
1 2
1 6
2 5
2 2
*/

最新文章

  1. ABP源码分析九:后台工作任务
  2. Kafka使用入门教程
  3. Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色
  4. 验证视图状态MAC失败。如果此应用程序由网络场或群集承载,请确保配置指定了相同的validationKey和验证算法(转)
  5. 口水话 闭包中this的指向
  6. JS打造的跟随鼠标移动的酷炫拓扑图案
  7. css巧妙实现分隔线
  8. 【转】eclipse使用git提交到osc
  9. ActiveMQ in Action(4) - Security
  10. 学习Matplotlib
  11. 在Android Studio 上安装Genymotion插件
  12. Kotlin 随笔小计
  13. 小程序sitemap配置
  14. 关于EXCEPT和INTERSECT的用法和例子
  15. linux resin 安装 配置 相关
  16. alpha阶段 代码结构及技术难点简介
  17. Katalon Studio学习笔记(二)——请求响应中文乱码解决方法
  18. activiti helloworld 续
  19. 怎么设置输入的EditText字母自己主动大写
  20. Python小白学习之路(六)—— 【元祖】【元祖相关功能】

热门文章

  1. C语言中extern的用法--转
  2. IE7不兼容slideDown()
  3. BNU27945——整数边直角三角形——————【简单数学推导】
  4. 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档
  5. SpringSecurity 3.2入门(8)自定义权限控制数据库设计
  6. SpringSecurity 3.2入门(3)单用户登录
  7. 【MSDN】 SqlServer DBCC解析
  8. Git和GitHub在线学习资源整理(转)
  9. IO流之缓冲流
  10. vue.js练习经验总结