LOJ数列分块 9 题解
\(1.\) 题意
给定一个长度 \(n\) 序列,每次查询区间 \(l, r\) 的众数。
\(2.\) 思路
如果边界是 \([l,r]\),\(l\) 在第 \(a\) 块,\(r\) 在第 \(b\) 块,可以分成三个部分:
- \(l\) 到 \(a\) 最后一块
- \([a+1→b−1]\) 块
- 第 \(b\) 块到 \(r\)
根据上面的性质,如果我们预先处理 \([a+1→b−1]\) 块的众数,再去遍历判断第一部分和第三部分是否有更合适的众数,这道题就能做出来了。
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("no-stack-protector")
#define int long long
using namespace std;
template <class T> inline void read(T &x){
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x){
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
const int N = 2e5 + 10;
const int M = 1e3 + 10;
int n, b, tot, a[N], id[N], val[N], cnt[N], dp[M][M];
vector<int> block[N];
unordered_map<int,int> mp;
inline void init(int pos){
memset(cnt, 0, sizeof cnt);
int maxx = 0, ans = 0;
for(int i = (pos - 1) * b + 1; i <= n; i ++){
cnt[a[i]] ++;
if(cnt[a[i]] > maxx || (cnt[a[i]] == maxx && val[a[i]] < val[ans])) ans = a[i], maxx = cnt[a[i]];
dp[pos][id[i]] = ans;
}
}
inline int getlen(int l, int r, int x){
return upper_bound(block[x].begin(), block[x].end(), r) - lower_bound(block[x].begin(), block[x].end(), l);
}
int query(int l, int r){
register int ans, maxx;
ans = dp[id[l] + 1][id[r] - 1];
maxx = getlen(l, r, ans);
for(register int i = l; i <= min(id[l] * b, r); i++){
int tmp = getlen(l, r, a[i]);
if(tmp > maxx || (tmp == maxx && val[a[i]] < val[ans]))
ans = a[i], maxx = tmp;
}
if(id[l] == id[r]) return ans;
for(register int i = (id[r] - 1) * b + 1; i <= r; i++){
int tmp = getlen(l, r, a[i]);
if(tmp > maxx || (tmp == maxx && val[a[i]] < val[ans]))
ans = a[i], maxx = tmp;
}
return ans;
}
signed main(){
read(n); b = 100;
for(int i = 1; i <= n; i ++){
read(a[i]);
if(mp[a[i]] == 0) mp[a[i]] = ++ tot, val[tot] = a[i];
a[i] = mp[a[i]];
block[a[i]].push_back(i);
}
for(int i = 1; i <= n; i ++) id[i] = (i - 1) / b + 1;
for(int i = 1; i <= id[n]; i ++) init(i);
for(int i = 1, l, r; i <= n; i ++){
read(l), read(r);
print(val[query(l, r)]), puts("");
}
return 0;
}
LOJ数列分块 9 题解的更多相关文章
- [Loj] 数列分块入门 1 - 9
数列分块入门 1 https://loj.ac/problem/6277 区间加 + 单点查询 #include <iostream> #include <cstdio> #i ...
- loj 数列分块入门 6 9(区间众数)
6 题意 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及单点插入,单点询问,数据随机生成. 题解 参考:http://hzwer.com/8053.html 每个块内用一个\(vecto ...
- loj 数列分块入门 5 7 8
5 题意 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间开方,区间求和. 思路 用\(tag\)记录这一块是否已全为\(1\). 除分块外,还可用 树状数组+并查集(链表) 或者 线 ...
- LOJ 数列分块入门系列
目录 1.区间加+单点查 每个块维护tag,散的暴力改. code: #include<bits/stdc++.h> using namespace std; const int maxn ...
- LOJ 6277-6280 数列分块入门 1-4
数列分块是莫队分块的前置技能,练习一下 1.loj6277 给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值. 直接分块+tag即可 #include <bits/stdc++.h ...
- LibreOJ6279. 数列分块入门 3 题解
题目链接:https://loj.ac/problem/6279 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,询问区间内小于某个值 \(x\) 的前驱(比其 ...
- LibreOJ 6277. 数列分块入门 1 题解
题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 \( ...
- LOJ 6277:数列分块入门 1(分块入门)
#6277. 数列分块入门 1 内存限制:256 MiB时间限制:100 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 3 测试数据 题目描述 给出一 ...
- LOJ #6285. 数列分块入门 9-分块(查询区间的最小众数)
#6285. 数列分块入门 9 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2 题目描述 给 ...
随机推荐
- c++对c的拓展_内联函数
目的:保持处理宏的高效及安全性 解决的问题:1.c中预处理宏有些难以发现的问题 2.c++ 中预处理不能访问类成员,不能作用类的成员函数 作用:无函数调用时开销,又可像普通函数般进行参数.返回值类型安 ...
- windows+ubuntu双系统时间同步问题
windows+ubuntu双系统时间同步问题 给Ubuntu更新时间,在终端输入: sudo apt-get install ntpdate sudo ntpdate time.windows.co ...
- 百兆以太网(100BASE-TX)的波形和眼图
沾了公司的光用了那台采样率吓死人的示波器看了下百兆以太网的三电平波形和眼图. 之前我也强调过百兆的三电平是不能从1状态越过0状态跳到-1状态的,从眼图上能明显看出来. 可以看出这个信号还是不错的.甚至 ...
- 分布式任务调度平台XXL-JOB安装及使用
一.为什么需要任务调度平台 在Java中,传统的定时任务实现方案,比如Timer,Quartz等都或多或少存在一些问题: 不支持集群.不支持统计.没有管理平台.没有失败报警.没有监控等等而且在现在分布 ...
- Day 003:PAT练习--1041 考试座位号 (15 分)
题目要求: 我写的代码如下: //考试座位号 #include<iostream> #include<algorithm> #include<string> usi ...
- C++的三种继承方式详解以及区别
目录 目录 C++的三种继承方式详解以及区别 前言 一.public继承 二.protected继承 三.private继承 四.三者区别 五.总结 后话 C++的三种继承方式详解以及区别 前言 我发 ...
- 尤娜故事-迷雾-springboot扮酷小技巧
前情回顾 从前,有一个简单的通道系统叫尤娜-- 尤娜系统的第一次飞行中换引擎的架构垂直拆分改造 四种常用的微服务架构拆分方式 尤娜,我去面试了 正文 我回到日常的尤娜系统建设中,最近事情比较少,总有一 ...
- Python 查找算法_众里寻他千百度,蓦然回首那人却在灯火阑珊处(线性、二分,分块、插值查找算法)
查找算法是用来检索序列数据(群体)中是否存在给定的数据(关键字),常用查找算法有: 线性查找: 线性查找也称为顺序查找,用于在无序数列中查找. 二分查找: 二分查找也称为折半查找,其算法用于有序数列. ...
- ONNXRuntime学习笔记(三)
接上一篇完成的pytorch模型训练结果,模型结构为ResNet18+fc,参数量约为11M,最终测试集Acc达到94.83%.接下来有分两个部分:导出onnx和使用onnxruntime推理. 一. ...
- JuiceFS 在数据湖存储架构上的探索
大家好,我是来自 Juicedata 的高昌健,今天想跟大家分享的主题是<JuiceFS 在数据湖存储架构上的探索>,以下是今天分享的提纲: 首先我会简单的介绍一下大数据存储架构变迁以及它 ...