题目链接: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 大爷的字符串题】 题解的更多相关文章

  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. Beam编程系列之Python SDK Quickstart(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)

  2. Beam概念学习系列之Pipeline 数据处理流水线

    不多说,直接上干货! Pipeline 数据处理流水线 Pipeline将Source PCollection ParDo.Sink组织在一起形成了一个完整的数据处理的过程. Beam概念学习系列之P ...

  3. java线程中的 sleep() wait() yield()

    sleep()方法是让线程休眠  可以指定时间  其实就是让线程进入阻塞状态  指定的时间过后 进入就绪状态  不释锁 相当于抱着锁睡觉 wait()  让线程进入等待状态  被唤醒后才会继续执行   ...

  4. 两个三汇API使用的坑

    最近呼叫中心走火入魔了,我的<一步一步开发呼叫中心>系列编写过程中,遇到各种的问题,今天晚上,来记录一下纠结了我N久的一个问题: 内线通过板卡外呼时,如果对方的呼叫中心需要发送按键响应(如 ...

  5. Excle 导入DataSet

    using System.Data.OleDb;using System.Data; public void ReadExcelFiless()        {            //strin ...

  6. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...

  7. fegin client使用http url合约时报: [Request processing failed; nested exception is feign.FeignException: status 400 reading

    首先看feign client代码: @FeignClient(name = "SPRING-CLOUD-WEB-PROVIDER-GROUP2", url = "htt ...

  8. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

  9. File I/O 小结

    1 .I/0: input/output 2.java.io.File 3 .表示:文件或者文件夹(目录) 4. File f = new File("文件路径"); 5 .注意: ...

  10. JS多级树结构写法

    效果: 一.布局: <div class="three_tree"> <div class="tree_title_cut"> < ...