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