【luogu P3709 大爷的字符串题】 题解
题目链接: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
*/
【luogu P3709 大爷的字符串题】 题解的更多相关文章
- luogu P3709 大爷的字符串题
二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...
- P3709 大爷的字符串题 (莫队)
题目 P3709 大爷的字符串题 题意:求\([l,r]\)中众数的个数. 解析 维护两个数组: \(cnt[x]\),数\(x\)出现的次数. \(sum[x]\),出现次数为\(x\)的数的个数. ...
- P3709 大爷的字符串题(莫队+结论)
题目 P3709 大爷的字符串题 做法 有一个显然的结论:一段区间里最小答案为众数的个数 用莫队来离线求众数 \(tmp_i\)表示出现\(i\)次的数的个数,\(num_i\)表示\(i\)出现的次 ...
- 洛谷 P3709 大爷的字符串题
https://www.luogu.org/problem/show?pid=3709 题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个 ...
- 洛谷P3709 大爷的字符串题(莫队)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- P3709 大爷的字符串题(50分)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- 【Luogu】P3709大爷的字符串题(莫队算法)
题目链接 语文题啊…… 看题解发现是让求区间中最多的数的个数,于是果断理解了一会题解……莫队套上完事. sum[i]表示i这个数出现的次数,cnt[i]表示出现i次的数有几个,然后乱搞搞……就好了 # ...
- 【题解】洛谷P3709大爷的字符串题
最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...
- luogu 3709 大爷的字符串题 构造 莫队 区间众数
题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...
随机推荐
- TOJ 2119 Tangled in Cables
描述 You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfor ...
- Android开发:使用Fragment改造TabActivity
TabActivity在API 13(Android 3.2)被标记为过期,需要使用Fragment来实现,Fragment是Android 3.0引入的一个概念,主要就是为了适应各种不同的屏幕大小( ...
- Android开发_如何调用系统默认浏览器访问
Android开发_如何调用系统默认浏览器访问 2015-10-20 17:53 312人阅读 http://blog.sina.com.cn/s/blog_6efce07e010142w7.htm ...
- Design Pattern理解碎片
开发封闭原则(Open-Closed Principle OCP)Software entities(classes,modules,functions etc) should open for ex ...
- 线程中断方法interrupt() 与 cancel()
(一).关于interrupt() interrupt()并不直接中断线程,而是设定一个中断标识,然后由程序进行中断检查,确定是否中断. 1. sleep() & interr ...
- BulletedList项目控件基础CSS基础
呈现形态&控件语法: <ul id=“...”> <li></li> . <li></li> . <li></li ...
- SpringSecurity 3.2入门(9)自定义权限控制代码实现
1. 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,我们的所有控制将在 ...
- union、except和intersect查询
1. union联合查询 (合并) select r.room_id from room r union select rp.num from room_type rp 要求表1和表2的查询结果结构 ...
- Java集合篇三:Vector
package com.test.collection; import java.util.Vector; public class MyVector { /** * @param args */ p ...
- dataBinding与ListView及事件
2015年Google IO大会分布了DataBinding库,能够更快捷便利的实现MVVM结构模式.但是,通过对DataBinding的学习,其中踩过得坑,今天要在这里记录一下.对于DataBind ...