UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
N个数排成一列,Q个询问,每次询问一段区间内的数的极差是多少。
Input
第一行两个整数N(1≤N≤50000),Q(1≤Q≤200000)。接下来一行N个整数a1 a2 a3 ....an,(1≤ai≤1000000000)。接下来Q行,每行两个整数L,R(1≤L≤R≤N)。
Output
对于每个询问输出一行,一个整数表示区间内的极差。
Sample input and output
| Sample Input | Sample Output |
|---|---|
5 3 |
8 |
题目链接:http://acm.uestc.edu.cn/#/contest/show/155
分析:线段树点更新裸题,继续复习线段树,这题要算的是极差,只需要建树和查询两部分,无需更新,所以建树的时候只要去求最大值和最小值即可,然后极差一减得出答案!
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=;
struct Node
{
int l,r,minn,maxn;
}tree[N<<];
void build(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
if(l==r)
{
scanf("%d",&tree[pos].maxn);
tree[pos].minn=tree[pos].maxn;
return;
}
int mid=(l+r)/;
build(l,mid,pos*);
build(mid+,r,pos*+);
tree[pos].maxn=max(tree[pos*].maxn,tree[pos*+].maxn);
tree[pos].minn=min(tree[pos*].minn,tree[pos*+].minn);
}
int query1(int l,int r,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].minn;
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
return query1(l,r,pos*);
else if(l>mid)
return query1(l,r,pos*+);
else return min(query1(l,mid,pos*),query1(mid+,r,pos*+));
}
int query2(int l,int r,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].maxn;
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
return query2(l,r,pos*);
else if(l>mid)
return query2(l,r,pos*+);
else return max(query2(l,mid,pos*),query2(mid+,r,pos*+));
}
int main()
{
int x,y;
scanf("%d%d",&x,&y);
build(,x,);
while(y--)
{
int p,q;
scanf("%d%d",&p,&q);
printf("%d\n",query2(p,q,)-query1(p,q,));
}
return ;
}
UESTC 1591 An easy problem A【线段树点更新裸题】的更多相关文章
- hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- POJ 3667 线段树区间合并裸题
题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...
- FZU Problem 2171 防守阵地 II (线段树区间更新模板题)
http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...
- HDU 1698 Just a Hook (线段树区间更新入门题)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU1698:Just a Hook(线段树区域更新模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem Description In the game of DotA, Pudge’s meat ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
随机推荐
- 【python】元组
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32Type & ...
- 通过EntityFramework来操作MySQL数据库
自己首次用到了EF,为了利人利己,故将自己今天学的记录下来. 这个项目要用到的工具是VS2015.MySQL5.7.12 . 首先我们先建一个解决方案,里面建两个项目分别是Silentdoer.Mai ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- android操作系统在itop4412开发板上的烧写
ITOP4412启动模式: 1.EMMC启动模式:拨码开关1-3为011 2.SD卡启动模式:拨码开关1-3为100uboot:初始化内存控制器,访问存储器,把操作系统内核从存储器读取出来放到内存中, ...
- 点分治X2
1.聪聪可可 点分治板子 然而想那个 t1[1]*t1[2]*2+t1[0]*t1[0]想了好久 就是最基本的组合方法 毕竟(2,5)和(5,2)可是要算两次的 画画图就好了 (不要像我一样盯着大佬们 ...
- bzoj 3139: [Hnoi2013]比赛
Description 沫沫非常喜欢看足球赛,但因为沉迷于射箭游戏,错过了最近的一次足球联赛.此次联 赛共N支球队参加,比赛规则如下: (1) 每两支球队之间踢一场比赛. (2) 若平局,两支球队各得 ...
- bzoj 2727: [HNOI2012]双十字
Description 在C 部落,双十字是非常重要的一个部落标志.所谓双十字,如下面两个例子,由两条水平的和一条竖直的"1"线段组成,要求满足以下几个限制: 我们可以找到 5 个 ...
- Search 命令详解
*查: ls : 查看文件等信息 /cat: 查看文件只显示最后一页. /cat > filename:创建新文件 /cat file1 file2 > file:合并文件 / -A ...
- NumPy学习笔记 一
NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- Django学习日记02_项目环境
创建一个工程: django-admin.py startproject mySite 将会产生以下文件: mySite/ manage.py mySite/ __init_ ...