当前位置:网站首页>ST表(动态规划倍增思路离线维护区间极值问题)
ST表(动态规划倍增思路离线维护区间极值问题)
2022-07-29 14:23:00 【阐上】
Acwing

C o d e : Code: Code:
#include<bits/stdc++.h>
#include<unordered_map>
#define debug cout << "debug--- "
#define debug_ cout << "\n---debug---\n"
#define oper(a) operator<(const a& ee)const
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
#define endl "\n"
#define ul (u << 1)
#define ur (u << 1 | 1)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll, int> PII;
const int N = 2e5 + 10, M = 2e3 + 10, mod = 1e9 + 7;
int INF = 0x3f3f3f3f; ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, B = 10, ki;
int f[N][18], a[N];
int lg[N];//预处理log2(N)是多少,可以加速很多
void init() {
//倍增动态规划
//f[i][j]维护 i 点开始长度为 2^j 段的最大值
for (int j = 0; j < 18; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
if (!j)f[i][j] = a[i];
else f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
int query(int l, int r) {
int len = r - l + 1;
int k = lg[len];
return max(f[l][k], f[r - (1 << k) + 1][k]);
//找一个最大的不会超过长度(下取整)的次方数 k
//前后两段取极值,包含了区间
}
void solve() {
cin >> n;
lg[0] = -1;
forr(i, 1, n)cin >> a[i], lg[i] = lg[i >> 1] + 1;//递推
init();
cin >> m;
while (m--)
{
int l, r;
cin >> l >> r;
cout << query(l, r) << endl;
}
}
int main() {
cinios;
int T = 1;
for (int t = 1; t <= T; t++) {
solve();
}
return 0;
}
/* */
边栏推荐
- EA&UML日拱一卒-活动图::Object actions(续)
- Nine kinds of way, teach you to read the resources files in the directory path
- 国内helm快速安装和添加常用charts仓库
- 【Postman】Download and installation (novice graphic tutorial)
- 升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
- rosbag data plotting MATLAB
- 【FreeSwitch开发实践】自定义模块创建与使用
- 这 6 款在线 PDF 转换工具,得试
- 2022开放原子全球开源峰会数据库分论坛圆满召开
- PAT serie a A1021 Deepest Root
猜你喜欢
随机推荐
企业需要知道的5个 IAM 最佳实践
没遇到过这三个问题都不好意思说用过Redis
Bika LIMS 开源LIMS集—— SENAITE的使用(分析/测试、方法)
兆骑科创海外高层次人才引进平台,企业项目对接,赛事活动路演
rk3399驱动添加电池adc开机检测功能
升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
进程间通信 --- system V三种通信方式(图文案例讲解)
嵌入式开发经验分享,把学习当作一种兴趣
Based on domestic, link global | schneider electric "industrial SI alliance partners hand in hand" to the industry in the future
苹果官方降价的原因找到了,它也面临销量下滑乃至出现库存问题
EA&UML日拱一卒-活动图::Variable Actions(续)
Children's programming electronics (graphical programming Scratch secondary level exam parsing (choice) in June 2022
StarRocks 2.3 新版本特性介绍
疫情之下的裁员浪潮,7点建议帮你斩获心仪offer
web会话管理与xss攻击
唯物辩证法-矛盾论(普遍性+特殊性+斗争性+同一性)
plsql连接oracle使用完毕之后关闭问题
这 6 款在线 PDF 转换工具,得试
The reason for Apple's official price reduction has been found, and it is also facing declining sales and even inventory problems
Zhaoqi Technology creates a platform for overseas high-level talent introduction, corporate project docking, and event roadshows









