POJ 3264 Balanced Lineup
作者:互联网
树状数组求最大最小值,一次成型,不能跑两回啊!
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
using namespace std;
const int N = 50010;
int t1[N], t2[N];
int a[N];
int n, q;
int lowbit(int x) {
return x & -x;
}
void update(int x, int v) {
while (x <= n) {
t1[x] = max(t1[x], v);
t2[x] = min(t2[x], v);
x += lowbit(x);
}
}
int query(int x, int y) {
int m1 = a[x], m2 = a[x];
while (true) {
m1 = max(m1, a[y]), m2 = min(m2, a[y]);
if (x == y) break;
for (y -= 1; y - x >= lowbit(y); y -= lowbit(y))
m1 = max(m1, t1[y]), m2 = min(m2, t2[y]);
}
return m1 - m2;
}
int main() {
memset(t2, 0x3f, sizeof(t2));
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
update(i, a[i]);
}
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", query(a, b));
}
return 0;
}
标签:include,int,lowbit,t2,POJ,m2,Balanced,m1,Lineup 来源: https://www.cnblogs.com/littlehb/p/16224395.html