题目链接: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 ;
}

最新文章

  1. Linux yum配置文件详解
  2. Excel常用操作
  3. Qt5 新特性
  4. Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
  5. Html5前端框架
  6. Redis 3.2.100 Windows 32位下载
  7. OWIN-WebAPI-Windows Service
  8. [Windows编程] 开发DLL必读《Best Practices for Creating DLLs》
  9. 简单实现android和wp聊天
  10. 酒店管理web项目总结
  11. Django用户继承AbstractUser后密码为明文
  12. Kubernetes 新时代的宠儿
  13. &lt;解决方法&gt;Centos安装使用Chromedriver
  14. Luogu P3919【模板】可持久化数组(可持久化线段树/平衡树)
  15. ThreadLocal使用注意
  16. My first paper is now available online
  17. javascript将算法复杂度从O(n^2)做到O(n)
  18. 【Spring】SpringMVC非注解配置的两种方式
  19. day5模块学习--shelve模块
  20. 使用NuGet发布自己的.NET NuGet 包( .NET Standard &amp; Windows)

热门文章

  1. 【BZOJ】【1293】【SCOI2009】生日礼物
  2. NYOJ-58 最小步数 AC 分类: NYOJ 2014-01-22 22:01 217人阅读 评论(0) 收藏
  3. ”sql Server2008 应用程序无法启动,因为应用程序的并行配置不正确。 找不到从属程序集。“C:\windows\SysWOW64\DTSPipelinePerf100.dll”的激活上下文生成失败“的解决方案
  4. 设计模式之Builder模式
  5. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
  6. HTML5 webSQL
  7. 华为上机:求2的N次幂的值
  8. STL erase函数
  9. hdu 3590 PP and QQ
  10. maven也是apache下的项目