poj 3264 Balanced Lineup(线段树单点修改区间查询)
来源:互联网 发布:js实现div上移下移 编辑:程序博客网 时间:2024/06/10 15:57
题目链接:点击打开链接
线段树单点修改区间查询
每个结点保存最大值和最小值
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define M 50010#define inf 10000000int MIN[M<<2];int MAX[M<<2];void pushup(int rt){ MIN[rt]=min(MIN[rt<<1],MIN[rt<<1|1]); MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);}void build(int l,int r,int rt){ MIN[rt]=inf; MAX[rt]=0; if(l==r) return; int m=(l+r)>>1; build(lson); build(rson);}void update(int l,int r,int rt,int p,int d){ MIN[rt]=min(d,MIN[rt]); MAX[rt]=max(d,MAX[rt]); if(l==r) return ; int m=(l+r)>>1; if(m>=p) update(lson,p,d); else update(rson,p,d); pushup(rt);}int querymax(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r)return MAX[rt]; int m=(l+r)>>1; int res=0; if(m>=L) res=max(res,querymax(L,R,lson)); if(m<R) res=max(res,querymax(L,R,rson)); return res;}int querymin(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r)return MIN[rt]; int m=(l+r)>>1; int res=inf; if(m>=L) res=min(res,querymin(L,R,lson)); if(m<R) res=min(res,querymin(L,R,rson)); return res;}int main(){ int N,Q; while(~scanf("%d%d",&N,&Q)){ build(1,N,1); for(int i=1;i<=N;i++){ int t; scanf("%d",&t); update(1,N,1,i,t); } for(int i=1;i<=Q;i++){ int s,t; scanf("%d%d",&s,&t); cout<<querymax(s,t,1,N,1)-querymin(s,t,1,N,1)<<endl; } } return 0;}
0 0
- poj 3264 Balanced Lineup(线段树单点修改区间查询)
- POJ 3264 Balanced Lineup (线段树单点更新 区间查询)
- POJ 3264-Balanced Lineup(线段树:单点更新,区间查询)
- POJ 3264 Balanced Lineup(线段树区间查询)
- POJ 3264 Balanced Lineup 线段树的构建+区间查询
- POJ 3264 Balanced Lineup(线段树点修改 查询最大最小总和 修改 增加/模板)
- POJ 3264 Balanced Lineup 线段树 单点更新 求区间最值
- Balanced Lineup (POJ_3264) 线段树+区间查询
- G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- POJ 3264-Balanced Lineup详解(线段树区间求值)
- poj 3264 Balanced Lineup线段树区间最值差
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- poj Balanced Lineup (线段树区间最值+优化)
- POJ 3264 - Balanced Lineup(线段树)
- Balanced Lineup(POJ-3264)(线段树)
- POJ - 3264 - Balanced Lineup (线段树)
- 选择排序
- HDU - 1061 Rightmost Digit
- uva11038 组合计数
- Cocos2d-x3.2与设计模式(一)创建型模式之ABSTRACT FACTORY(抽象工厂)--对象创建型模式
- LeetCode之旅(28)
- poj 3264 Balanced Lineup(线段树单点修改区间查询)
- 一个在奋斗学习的青年,今年27岁,传智播客c#
- hunnu11332(BigDecimal的应用)
- day6,page30,total190
- T1赋值运算符函数
- 深水网箱环境监测系统UI设计 Login页面更改
- JAVA 学习第四天
- HDU 3015——Disharmony Trees(树状数组 2009 Multi-University Training Contest 12 - Host by FZU)
- T2Simple模式