luogu P3709 大爷的字符串题
二次联通门 : luogu P3709 大爷的字符串题
/*
luogu P3709 大爷的字符串题 莫队
看了半天题目 + 题解
才弄懂了要求什么。。。 维护两个数组
一个记录数字i出现了几次
一个记录出现了i次的有几个数。。 */
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath> #define Max 200090 void read (int &now)
{
now = ;
register char word = getchar ();
while (word < '' || word > '')
word = getchar ();
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
} int belong[Max]; struct Query_Data
{
int l, r; int Id; bool operator < (const Query_Data &now) const
{
if (belong[this->l] == belong[now.l])
return this->r < now.r;
return belong[this->l] < belong[now.l];
}
}; int number[Max];
int N, M; int count[Max];
int Answer[Max], Result; Query_Data query[Max];
int __rank[Max];
int __count[Max]; inline void Updata (int now, bool type)
{
if (type)
{
if (count[number[now]] == Result)
Result ++;
__count[count[number[now]]] --;
++ count[number[now]];
__count[count[number[now]]] ++;
}
else
{
if (count[number[now]] == Result && __count[count[number[now]]] == )
Result --;
__count[count[number[now]]] --;
count[number[now]] --;
++ __count[count[number[now]]];
}
} int main (int argc, char *argv[])
{
read (N);
read (M);
int K_Size = sqrt (N);
for (int i = ; i <= N; i ++)
{
read (number[i]);
__rank[i] = number[i];
belong[i] = (i + ) / K_Size;
}
std :: sort (__rank + , __rank + + N);
int Size = std :: unique (__rank + , __rank + + N) - __rank - ;
for (int i = ; i <= N; i ++)
number[i] = std :: lower_bound (__rank + , __rank + + Size, number[i]) - __rank;
for (int i = ; i <= M; i ++)
{
read (query[i].l);
read (query[i].r);
query[i].Id = i;
}
std :: sort (query + , query + + M);
int l = , r = ;
for (int i = ; i <= M; i ++)
{
while (l < query[i].l)
Updata (l ++, false);
while (l > query[i].l)
Updata (-- l, true);
while (r < query[i].r)
Updata (++ r, true);
while (r > query[i].r)
Updata (r --, false);
Answer[query[i].Id] = Result;
}
for (int i = ; i <= M; i ++)
printf ("%d\n", -Answer[i]);
//system ("pause");
return ;
}
luogu P3709 大爷的字符串题的更多相关文章
- 【luogu P3709 大爷的字符串题】 题解
题目链接:https://www.luogu.org/problemnew/show/P3709 离散化+区间众数..? #include <iostream> #include < ...
- 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次的数有几个,然后乱搞搞……就好了 # ...
- luogu 3709 大爷的字符串题 构造 莫队 区间众数
题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...
- P3709 大爷的字符串题
题意 询问区间众数出现的次数 思路 唯有水题快人心 离散化+莫队 莫队一定要先加后减,有事会出错的 莫队维护区间众数: 维护两个数组,一个数组记录权值为x的出现次数,一个记录出现次数为x的数的个数 a ...
随机推荐
- Codeforces Round #588 (Div. 1) 简要题解
1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...
- 在论坛中出现的比较难的sql问题:33(递归 连续日期问题 )
原文:在论坛中出现的比较难的sql问题:33(递归 连续日期问题 ) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...
- mac下卸载android studio
Execute these commands from the terminal rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/P ...
- [转]全网最!详!细!tarjan算法讲解
转发地址:https://blog.csdn.net/qq_34374664/article/details/77488976 原版的地址好像挂了..... 看到别人总结的很好,自己就偷个懒吧..以下 ...
- git本地下载github上的某项目的部分资源
注意以下命令要在git bash下进行,不要是cmd,或者是powershell. cd 到某个目录下 git init git remote add -f origin <url> g ...
- 预编译And作用域链
首先要理解什么是预编译: 预编译就是在JS执行前的一瞬间创建一个AO对象,这个创建AO的过程叫做预编译. console.log(a) var a = 1; function c(b){ b = 10 ...
- ZYNQ工程PL顶层创建block module报错解决
参考文档 https://www.cnblogs.com/chengqi521/p/7977616.html 前言 对于ZYNQ的开发,顶层一般为PS+PL,而PL一般会封装成顶层,只露出接口. 而P ...
- 在SAP除了使用Cordova生产移动应用外,还有这种方式
本文和Jerry过去的文章不太一样,算不上Jerry的知识分享,只是记录一下Jerry用React-Native把应用安装到Android手机上遇到的一些问题,方便以后查看. Jerry的同事Leo用 ...
- C语言中参数的传递
普通情况: 调用函数参数的传递,是采取入栈的方式,先上图一张: Fun是被调用的函数,而为了演示其参数传递的过程,特意多设了几个参数,其传递参数的汇编代码如下: 可以看出,汇编代码中将这9个参数的前5 ...
- python selectors模块实现 IO多路复用机制的上传下载
import selectorsimport socketimport os,time BASE_DIR = os.path.dirname(os.path.abspath(__file__))''' ...