二次联通门 : 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 大爷的字符串题的更多相关文章

  1. 【luogu P3709 大爷的字符串题】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3709 离散化+区间众数..? #include <iostream> #include < ...

  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. luogu 3709 大爷的字符串题 构造 莫队 区间众数

    题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...

  9. P3709 大爷的字符串题

    题意 询问区间众数出现的次数 思路 唯有水题快人心 离散化+莫队 莫队一定要先加后减,有事会出错的 莫队维护区间众数: 维护两个数组,一个数组记录权值为x的出现次数,一个记录出现次数为x的数的个数 a ...

随机推荐

  1. 解决windows 激活问题

    解决windows 激活问题 下载 然后 搞定  重启

  2. linux主机内存告警shell脚本

    #!/bin/sh ramusage=$(free | awk '/Mem/{printf("RAM Usage: %.2f\n"), $3/$2*100}'| awk '{pri ...

  3. jquery.marquee

    http://aamirafridi.com/jquery/jquery-marquee-plugin#examples <script src="/plugins/marquee/j ...

  4. Java之协程(quasar)

    一.前面我们简单的说了一下,Python中的协程原理.这里补充Java的协程实现过程.有需要可以查看python之协程. 二.Java协程,其实做Java这么久我也没有怎么听过Java协程的东西,但是 ...

  5. elementUI图片墙上传

    elementUI提供了照片墙上传的功能,我们直接拿来用. 以下是实现代码: <template> <div style="padding: 50px;"> ...

  6. element-ui 文件上传

    <el-form-item> <el-upload ref="upload" class="upload-demo" :action=&quo ...

  7. git推送代码Gogs报401错误

    1.git push 报错:RPC failed; HTTP 401 curl 22 The requested URL returned error: 401 The remote end hung ...

  8. Android笔记(三十四) Android中线程之间的通信(六)Handle中的post()方法详解

    我们之前都是使用sendMessage()方法来发送消息,使用handleMessage来处理消息的,今天我们来看另外一种方法,先看代码: package cn.lixyz.handlertest; ...

  9. 【OF框架】在Visual Studio中发布Docker镜像,推送镜像到Azure容器注册表

    准备 拥有Azure账号,已经创建 Azure容器注册表,获得注册表地址.账号.密码 本地已经在Visual Studio登录Azure账号. 本地已经拥有Docker环境 注意:首次发布Docker ...

  10. 用js刷剑指offer(栈的压入、弹出序列)

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...