AOJ 351 rmq或 基础线段树 求解
来源:互联网 发布:jsp调用java显示图片 编辑:程序博客网 时间:2024/05/23 12:59
题目链接:AOJ 351
RMQ:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>using namespace std;#define debug 0 const int maxn = 100000 + 5;int a[maxn],n,m,Max[maxn][20],Min[maxn][20]; void initRMQ() { for (int i = 1; i <= n; i++) { Max[i][0] = Min[i][0] = a[i]; } for(int j = 1; j < 20; j++)//初始化 for(int i = 1; i <= n; i++) if (i + (1 << j) - 1 <= n) { Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]); Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]); }} int main() {#if debug freopen("in.txt", "r", stdin);#endif//debug int l, r; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); initRMQ(); while (m--) { scanf("%d%d", &l, &r); int x = (int)log2(r - l + 1);//query查询
int maxAns = max(Max[l][x], Max[r - (1 << x) + 1][x]); int minAns = min(Min[l][x], Min[r - (1 << x) + 1][x]); printf("%d\n", maxAns - minAns); } } return 0;}线段树:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<stack>using namespace std;#define debug 0 const int maxn = 100000 + 5;int Max[maxn << 2], Min[maxn << 2];int n, m, ans1, ans2;#define Mid ((l + r) >> 1)#define ls rt << 1, l , Mid#define rs rt << 1 | 1, Mid + 1 , rvoid build(int rt, int l, int r) { if (l == r){ scanf("%d", &Max[rt]);//建树时读入 Min[rt] = Max[rt]; }else { build(rt << 1, l, (l + r) >> 1); build(rt << 1 | 1, ((l + r) >> 1) + 1, r); Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]); }} void query(int rt, int l, int r, int L, int R) {//查询 if (L <= l && r <= R) { ans1 = max(ans1, Max[rt]); ans2 = min(ans2, Min[rt]); }else { if (L <= ((l + r) >> 1)){ query(ls, L, R); } if (R > ((l + r) >> 1)) { query(rs, L, R); } }} int main() {#if debug freopen("in.txt", "r", stdin);#endif //debug int a, b; while (~scanf("%d%d", &n, &m)) { build(1, 1, n); while (m--) { ans1 = -0x3f3f3f3f; ans2 = 0x3f3f3f3f; //printf("%d\n", m); scanf("%d%d", &a, &b); query(1, 1, n, a, b); printf("%d\n", ans1 - ans2); } } return 0;}
1 0
- AOJ 351 rmq或 基础线段树 求解
- zoj 3633 rmq或线段树
- RMQ with Shifts 线段树基基础
- uva12299 RMQ with Shifts(线段树基础)
- poj 3264 --Balanced Lineup (RMQ或线段树)
- poj 3875 RMQ或线段树求区间最值
- POJ 3264 Balanced Lineup -- RMQ或线段树
- 【POJ】3368-Frequent values(RMQ或线段树)
- poj 3264 Balanced Lineup(简单线段树 或 rmq)
- poj 3368 Frequent values (RMQ或线段树)
- V-Parenthesis 前缀+ZKW线段树或RMQ
- POJ3264 【RMQ基础题—ST-线段树】
- poj3368(线段树,RMQ)
- 线段树/RMQ问题
- 线段树RMQ
- VJ16216/RMQ/线段树
- 线段树求RMQ
- 线段树 Circular RMQ
- 浙大PATj甲级 1048
- tabpageindacator与fragment的应用
- C++建立动态二维数组
- iOS-initWithNibName/awakeFromNib/initWithCoder 区别
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- AOJ 351 rmq或 基础线段树 求解
- 387.leetcode First Unique Character in a String(easy)[统计字符串字符次数]
- 杭电2091空心三角形
- python装饰器的学习
- Python3爬虫学习笔记2.1——爬文字-糗事百科
- 【31-35】剑指offer
- bzoj1251(splay序列操作)
- Java的HashMap和HashTable
- Parsing XML Data