GSS1 - Can you answer these queries I

$$GSS1$$ - $$Can$$ $$you$$ $$answer$$ $$these$$ $$queries$$ $$I$$

SPOJ

一、大致题意

$$q$$ 个询问，求 $$[x,y]$$ 的最大子段和。

二、实现代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
const int N = 1e6 + 10;
typedef long long LL;

int n, m, a[N];

struct Node {
int l, r;
int lmax;
int rmax;
int tmax;
int sum;
} tr[N << 2];

void calc(Node &u, Node &l, Node &r) {
u.sum = l.sum + r.sum;
u.lmax = max(l.lmax, l.sum + r.lmax);
u.rmax = max(r.rmax, r.sum + l.rmax);
u.tmax = max({l.tmax, r.tmax, l.rmax + r.lmax});
}

void pushup(int u) {
calc(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r) {
tr[u] = {l, r, 0, 0, 0, 0};
if (l == r) {
tr[u].sum = tr[u].lmax = tr[u].rmax = tr[u].tmax = a[l];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}

Node query(int u, int l, int r) {
if (l <= tr[u].l && r >= tr[u].r) return tr[u];

int mid = tr[u].l + tr[u].r >> 1;
if (l > mid) return query(u << 1 | 1, l, r);
if (r <= mid) return query(u << 1, l, r);

Node a = query(u << 1, l, r), b = query(u << 1 | 1, l, r), res;
calc(res, a, b);
return res;
}

int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];

build(1, 1, n);

cin >> m;
int x, y;
while (m--) {
cin >> x >> y;
printf("%d\n", query(1, x, y).tmax);
}
return 0;
}

posted @ 2022-04-28 18:26  糖豆爸爸  阅读(46)  评论(0编辑  收藏  举报
Live2D