poj3264(简单线段树)
题目链接:https://vjudge.net/problem/POJ-3264
题意:线段树简单应用题,区间查询最大值和最小值的差。
思路:用线段树维护区间的最大值和最小值即可。
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=; struct node{
int l,r,Max,Min;
}tr[maxn<<]; int n,Q,a[maxn],ans1,ans2; void build(int v,int l,int r){
tr[v].l=l,tr[v].r=r;
if(l==r){
tr[v].Max=tr[v].Min=a[l];
return;
}
int mid=(tr[v].l+tr[v].r)>>;
build(v<<,l,mid);
build(v<<|,mid+,r);
tr[v].Max=max(tr[v<<].Max,tr[v<<|].Max);
tr[v].Min=min(tr[v<<].Min,tr[v<<|].Min);
} void query(int v,int l,int r){
if(l<=tr[v].l&&r>=tr[v].r){
ans1=max(ans1,tr[v].Max);
ans2=min(ans2,tr[v].Min);
return;
}
int mid=(tr[v].l+tr[v].r)>>;
if(l<=mid)
query(v<<,l,r);
if(r>mid)
query(v<<|,l,r);
} int main(){
scanf("%d%d",&n,&Q);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
build(,,n);
while(Q--){
int a,b;
scanf("%d%d",&a,&b);
ans1=,ans2=0x3f3f3f3f;
query(,a,b);
printf("%d\n",ans1-ans2);
}
return ;
}
poj3264(简单线段树)的更多相关文章
- HDOJ-1754(简单线段树)
最近开始重新学习线段树,先从最简单的开始吧! I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 ...
- J - Assign the task - hdu 3974(DFS建树+简单线段树)
题意:给一些节点简单额对应关系,可以组成一个树,如果树的某一个节点更新那么他的所有子节点都要更新,中间,会有一些查询 分析:题意倒也不难理解,但是但是不知道怎么建树...于是自能百度,看了kuangb ...
- hdu1556 Color the ball 简单线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 简单的线段树的应用 直接贴代码了: 代码: #include<iostream> # ...
- HihoCoder1070 区间最小值(简单线段树)
个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量weigh ...
- poj3264(线段树区间求最值)
题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ3264(线段树求区间最大最小值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41162 Accepted: 19327 ...
- CodeForces-816B:Karen and Coffee (简单线段树)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- hdu 1166 敌兵布阵(简单线段树or树状数组)
题意: N个工兵营地,第i个营地有ai个人. 三种操作: 1.第i个营地增加x个人. 2.第i个营地减少x个人. 3.查询第i个到第j个营地的总人数. 思路: 线段树or树状数组 代码:(树状数组) ...
随机推荐
- 浏览器表单自动填充默认样式 - autofill问题解决
这不知道是个什么神仙网站,解决办法写的这么清楚,页面有这么让人舒服 https://lhajh.github.io/css/chrome/2018/04/17/The-chrome-browser-f ...
- AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)
题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...
- B. Tell Your World(几何数学 + 思维)
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- linux 查看内网IP和外网IP
centos7 查看内网的ip,使用ifconfig 或在后面加上参数,都可以查看内网的ip,下面的10.105.33.17 即是内网的ip [root@VM_33_17_centos ~]#ifco ...
- Java线程中的异常处理
对于对线程,当主线程中有子线程运行出现异常时,主线程是不能捕获到该异常的,子线程会直接退出,不会记录任何日志. 解决: 1.子线程中try catch. 2.设置线程的未捕获异常处理器,Uncaugh ...
- Tree-based Model 如何处理categorical variable
categorical variable 分为 order variale 和 non-order variable,其中order variable直接使用sklearn.preprocess.La ...
- django-rest-framework之 json web token方式完成用户认证
json web token的介绍:https://blog.csdn.net/kevin_lcq/article/details/74846723 1. 安装 $ pip install djang ...
- RESTful API是什么?
1. REST 是Repersentational State Transfer的缩写 翻译为"表述性状态传递",那么什么是表述性状态传递呢?为了理解这个词语,我们从"R ...
- 学习笔记01:《开放平台产品成长之路-POP》
当看到一些比较好的书籍和视频内容时,希望自己可以总价归纳下来,一方面是好的东西希望可以分享给大家,另一方面希望自己能很好的吸收并转化成掌握的知识,所以有了这个系列的学习笔记,共勉,今天的你比昨天更博学 ...
- compare across commits online
https://gist.github.com/nevik/5689882 Examples: https://github.com/octocat/Spoon-Knife/compare/ed122 ...