[POJ3264]Balanced Lineup(线段树,区间最值差)
2024-10-16 09:21:06
题目链接:http://poj.org/problem?id=3264
一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值。
线段树维护当前节点下属叶节点的两个最值,查询后作差即可。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef pair<int, int> pii;
const int maxn = ;
int n, q, a, b;
pii node[maxn<<]; void pushUP(int rt) {
int ll = min(node[rt<<].first, node[rt<<|].first);
int rr = max(node[rt<<].second, node[rt<<|].second);
node[rt] = pii(ll, rr);
} void build(int l, int r, int rt) {
if(l == r) {
scanf("%d", &node[rt].first);
node[rt].second = node[rt].first;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushUP(rt);
} int querymax(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return node[rt].second;
}
int m = (l + r) >> ;
int ans = -;
if(L <= m) ans = max(ans, querymax(L, R, lson));
if(m < R) ans = max(ans, querymax(L, R, rson));
return ans;
} int querymin(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return node[rt].first;
}
int m = (l + r) >> ;
int ans = 0x7f7f7f;
if(L <= m) ans = min(ans, querymin(L, R, lson));
if(m < R) ans = min(ans, querymin(L, R, rson));
return ans;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &q)) {
build(, n, );
while(q--) {
scanf("%d %d", &a, &b);
int ll = querymin(a, b, , n, );
int rr = querymax(a, b, , n, );
printf("%d\n", rr - ll);
}
}
return ;
}
最新文章
- Linux yum配置文件详解
- Excel常用操作
- Qt5 新特性
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
- Html5前端框架
- Redis 3.2.100 Windows 32位下载
- OWIN-WebAPI-Windows Service
- [Windows编程] 开发DLL必读《Best Practices for Creating DLLs》
- 简单实现android和wp聊天
- 酒店管理web项目总结
- Django用户继承AbstractUser后密码为明文
- Kubernetes 新时代的宠儿
- <;解决方法>;Centos安装使用Chromedriver
- Luogu P3919【模板】可持久化数组(可持久化线段树/平衡树)
- ThreadLocal使用注意
- My first paper is now available online
- javascript将算法复杂度从O(n^2)做到O(n)
- 【Spring】SpringMVC非注解配置的两种方式
- day5模块学习--shelve模块
- 使用NuGet发布自己的.NET NuGet 包( .NET Standard &; Windows)
热门文章
- 【BZOJ】【1293】【SCOI2009】生日礼物
- NYOJ-58 最小步数 AC 分类: NYOJ 2014-01-22 22:01 217人阅读 评论(0) 收藏
- ”sql Server2008 应用程序无法启动,因为应用程序的并行配置不正确。 找不到从属程序集。“C:\windows\SysWOW64\DTSPipelinePerf100.dll”的激活上下文生成失败“的解决方案
- 设计模式之Builder模式
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
- HTML5 webSQL
- 华为上机:求2的N次幂的值
- STL erase函数
- hdu 3590 PP and QQ
- maven也是apache下的项目