洛谷P1816 忠诚
来源:互联网 发布:vpn代理软件破解版 编辑:程序博客网 时间:2024/04/30 17:52
题目来源https://www.luogu.org/problem/show?pid=1816
RMQ问题。
anc[i][k]记录第i个数往前2^k的数的编号。
lcs[i][k]记录从第i-2^k个数到第i个数之间的最小值。
更新时anc[x][i]=anc[anc[x][i-1]][i-1],lcs[x][i]=min(lcs[x][i-1],lcs[anc[x][i-1]][i-1])
#include <algorithm>#include <iostream>#include <cstring>#include <sstream>#include <cstdlib>#include <string>#include <cstdio>#include <cctype>#include <vector>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>using namespace std;const int maxn=1e5+1;const int maxh=20;int n,m;long long a[maxn];long long anc[maxn][maxh]={0};long long lcs[maxn][maxh]={0};void dfs(){ for(int i=0;i<maxh;i++) { anc[1][i]=1; lcs[1][i]=a[1]; } for(int x=1;x<=n;x++) { for(int i=1;i<maxh;i++) { anc[x][i]=anc[anc[x][i-1]][i-1]; lcs[x][i]=min(lcs[x][i-1],lcs[anc[x][i-1]][i-1]); } anc[x+1][0]=x; lcs[x+1][0]=min(a[x+1],a[x]); }}long long swim(int x,int h){ long long tot=9e10; for(int i=0;h>0;i++) { if(h&1) { tot=min(tot,lcs[x][i]); x=anc[x][i]; } h=(h>>1); } return tot;}int main(){ ios::sync_with_stdio(false); cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i]; dfs(); for(int i=1;i<=m;i++) { int x,y;cin>>x>>y; if(x>y)swap(x,y); if(x==y)cout<<a[x]<<" "; else cout<<swim(y,y-x)<<" "; } return 0;}
1 0
- 洛谷P1816 忠诚
- 洛谷P1816 忠诚
- 洛谷P1816 忠诚 题解
- 洛谷p1816忠诚
- 洛谷P1816 忠诚
- 忠诚
- P1816 统计数字
- P1816统计数字
- ST算法解RMQ模板(洛谷1816 忠诚)
- 谈谈“忠诚”
- 【wikioi2173】 忠诚
- tyvj1038忠诚
- tyvj1038忠诚
- 【tyvj1038】忠诚
- 【tyvj1038】忠诚
- tyvj-忠诚
- Vijos P1816 统计数字【序列处理】
- 【BZOJ】【P1816】【Cqoi2010】【扑克牌】【题解】【水题】
- hdu4496 D-City 并查集
- 机器学习中的范数规则化之(二)核范数与规则项参数选择
- 正则校验非法输入double值
- jQuery获取checkbox选中的值
- 两分钟理解Android中SP和DP的区别
- 洛谷P1816 忠诚
- 【UML学习】-- 用例图
- jquery源码分析初步
- 循环一个数组下标得到另一个数组里的数字(电话号码实例)
- 1属地(1)
- 历史人物之林冲
- 更改maven打包JDK版本时遇到的问题汇总
- 【深度学习:目标检测】深度学习检测方法梳理:R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD系列
- JAVA学习笔记1之抽象类