您的位置:首页 > 其它

poj 3264 线段树区间最大最小值

2015-08-07 23:22 323 查看
题意:

给n组数据,q个询问,每次询问区间l,r最大值与最小值的差是多少。

解析:

睡前一水,却错了2发。

询问的时候写错了,并不是直接去更新maxx-minn的最大值。。。

是直接更新maxx,minn,然后最后一减就行了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 50000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int minn, maxx;
int maxPoint[maxn << 2];
int minPoint[maxn << 2];

void pushup(int rt)
{
maxPoint[rt] = max(maxPoint[rt << 1], maxPoint[rt << 1 | 1]);
minPoint[rt] = min(minPoint[rt << 1], minPoint[rt << 1 | 1]);
}

void build(int lo, int hi, int rt)
{
if (lo == hi)
{
scanf("%d", &maxPoint[rt]);
minPoint[rt] = maxPoint[rt];
return;
}
int mi = (lo + hi) >> 1;
build(lson);
build(rson);
pushup(rt);
}

void query(int L, int H, int lo, int hi, int rt)
{
if (L <= lo && hi <= H)
{
maxx = max(maxx, maxPoint[rt]);
minn = min(minn, minPoint[rt]);
return;
}
int mi = (lo + hi) >> 1;
if (L <= mi)
query(L, H, lson);
if (mi < H)
query(L, H, rson);
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int n, q;
while (~scanf("%d%d", &n, &q))
{
memset(maxPoint, 0, sizeof(maxPoint));
memset(minPoint, 0, sizeof(minPoint));
build(1, n, 1);
while (q--)
{
minn = inf, maxx = 0;
int fr, to;
scanf("%d%d", &fr, &to);
query(fr, to, 1, n, 1);
printf("%d\n", maxx - minn);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: