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 ...
随机推荐
- CH09 开机自动烧录QSPI
版本信息: 版本 REV2018 时间 05/22/2018 XILINX ZYNQ LINUX篇 基于米联MZ7X系列 电子版自学资料 常 ...
- SSM整合所需的maven配置文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 前端require代码抽离小技巧
DEMO 文件目录结构 plugin.js // /CommonJS规范 // var exports = module.exports; exports.test = function () { c ...
- DeepID_V2解读
港中文汤晓鸥团队在DeepID_v1基础上提出的新版本架构,发表于NIPS2014 一.Architecture 网络架构基本上与DeepId_v1一致. 二.Pipeline 图片被分成20regi ...
- element-ui 表格错行
//表格错行 .el-table th.gutter { display: table-cell !important }
- nginx 反向代理配置(二)
上一篇文章主要是对 nginx 各个模块做了一个介绍,以及对什么是反向代理在文章开头做了一个简单介绍,这篇文章我们主要来看下如何进行 nginx 反向代理的配置 proxy 模块 nginx ...
- SQL SERVER-邮件配置
存储发邮件 USE msdb GO EXEC sp_send_dbmail @profile_name = 'mail_pro', @recipients='Jinwei.chang@quantacn ...
- c# 定制处理未处理异常
- c# 定义和调用索引器
- webpack多页面打包配置
单页面应用:整个应用里面只有一个html文件.现在主流的框架,vue,react都是单页面应用. 所以webpack绝大部分都是对单页面打包.那么webpack如何对多页面进行打包 index.htm ...