POJ-3264-Balanced Lineup-单点更新
题目链接:http://poj.org/problem?
id=3264
这是一个单点更新的模板题,就不详解了,HDU敌兵布阵那题我有详解;链接:http://blog.csdn.net/wlxsq/article/details/46897219
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
using namespace std;
/*这是一个线段树单点更新的模板题。只是每一个节点存两个数据。最大值和最小值;
*/
const int inf=0xffffff0;
int MinV,MaxV;
int q,n,a,b;
struct node
{int l,r,MinV,MaxV;
}node[200010];
void PushUp(int rt,int v) // 更新父节点;
{node[rt].MinV=min(node[rt].MinV,v);node[rt].MaxV=max(node[rt].MaxV,v);
}
void build(int l,int r,int rt)
{int mid=(l+r)>>1;node[rt].l=l;node[rt].r=r;node[rt].MinV=inf;node[rt].MaxV=-inf;if(l==r) return;else{build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);}
}
void Insert(int rt,int p,int v)
{int l=node[rt].l;int r=node[rt].r;if(l==r&&p==l){node[rt].MinV=v;node[rt].MaxV=v;return;}//node[rt].MinV=min(v,node[rt].MinV);//node[rt].MaxV=max(v,node[rt].MaxV);int mid=(l+r)>>1;if(p<=mid) Insert(rt<<1,p,v);else Insert(rt<<1|1,p,v);PushUp(rt,v);
}
void query(int l,int r,int rt)
{if(node[rt].MaxV<=MaxV&&node[rt].MinV>=MinV) return; // 往下搜没有什么意义了,能够直接退出;if(l==node[rt].l&&r==node[rt].r){MinV=min(node[rt].MinV,MinV);MaxV=max(node[rt].MaxV,MaxV);return;}int mid=(node[rt].l+node[rt].r)>>1;if(r<=mid) query(l,r,rt<<1);else if(l>mid) query(l,r,rt<<1|1);else{query(l,mid,rt<<1);query(mid+1,r,rt<<1|1);}
}
int main()
{while(~scanf("%d%d",&n,&q)){build(1,n,1);for(int i=1;i<=n;i++){scanf("%d",&a);Insert(1,i,a);}for(int i=1;i<=q;i++){scanf("%d%d",&a,&b);MinV=inf;MaxV=-inf;query(a,b,1);printf("%d\n",MaxV-MinV);}}return 0;
}
POJ-3264-Balanced Lineup-单点更新相关推荐
- POJ 3264 Balanced Lineup
POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- poj 3264 Balanced Lineup RMQ问题 线段树
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- POJ 3264 Balanced Lineup
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53629 Accepted: 25223 ...
- POJ 3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 24349 Accepted: 11348 ...
- POJ 3264 Balanced Lineup 【线段树】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...
- 【RMQ】POJ 3264 Balanced Lineup
前言 这题出现在RMQRMQRMQ的WordWordWord文档中,我就直接把以前ACACAC过的程序交上去,结果WAWAWA了,然后才发现这道题还要求最小值... 链接 http://poj.org ...
- POJ 3264 Balanced Lineup (RMQ)
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- POJ 3264: Balanced Lineup
2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:查询区间最大值/最小值之差,最基础的线段树应用. 代码: #include <cstdio>/ ...
- POJ 3264.Balanced Lineup-RMQ(ST)详解
先写一道水题的博客,为后面要写的博客做一个铺垫. ヾ(◍°∇°◍)ノ゙ RMQ(Range Minimum/Maximum Query),即区间最值查询,对于长度为n的数列A,回答若干询问RMQ(A, ...
最新文章
- 算法每日学打卡:java语言基础题目打卡(16-18)
- Redis的两种持久化方式
- python回归预测例子_案例实战 | 逻辑回归实现客户流失预测(附Python代码与源数据)...
- 【转】RabbitMQ六种队列模式-5.主题模式
- 实战:kafka实现日志收集系统
- neo4j安装_怎样安装Neo4j APOC扩展包?
- 【招聘】阿里2022届春招实习生 - 机器学习/NLP/CV等
- 1022 D进制的A+B (20 分)—PAT (Basic Level) Practice (中文)
- C#.NET通用权限管理系统组件中用少数几行代码实现记录页面状态
- 小学题的python实现
- 软件技术专业-就业提示(一、实施工程师)
- 灰度发布 java_灰度发布系统的实现步骤
- 阿里云-云开发平台存储篇——给应用接上后端存储能力
- POI 读写EXCEL日期类型单元格
- 服务器和普通电脑有什么区别?
- 批量本地英语文档翻译软件
- php对联广告,纯DIV/CSS对联漂浮广告代码(无JS)
- 校园小组汇报毕业答辩PPT模板
- safari中判断app是否安装
- matlab cuda 加速,Matlab中调用CUDA加速的方法……
热门文章
- C语言遥控器程序,红外遥控
- 管道的另一端上无任何进程。_历史上突然消失的二位牛人,其下落无任何记载...
- 学习笔记Kafka(六)—— Kafka Consumer API及开发实例
- 计算机网络怎么删除,怎么删除网络协议
- led灯串怎么摆造型_如何驱动LED灯串小绝招
- 系统优化怎么做-Tomcat优化
- 通过设置Cookie 让弹框显示一次
- 2020-12-01 Halcon初学者知识[1] 初始界面
- h5页面提示只能在微信浏览器中打开_电子问卷h5怎么做?
- js中的几种跨域方法