题意
给出$n$个数,每次询问区间$(l, r)$内最大字段和
sol
在合并子树的时候,答案仅有四种情况
打四个标记维护即可
查询同理,用类似update的方式合并
注意查询的时候不能按照以前的方式写,因为不知道变量的下界,最稳妥的办法就是判三种情况
/* */ #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<vector> #include<set> #include<queue> #include<cmath> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second //#define int long long #define ll long long #define rg register #define sc(x) scanf("%d", &x); #define pt(x) printf("%d ", x); #define db(x) double x #define rep(x) for(int i = 1; i <= x; i++) #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? eof : *p1++) char buf[(1 << 22)], *p1 = buf, *p2 = buf; char obuf[1<<24], *o = obuf; #define os *o++ = 'n'; using namespace std; using namespace __gnu_pbds; const int maxn = 50001, inf = 1e9 + 10, mod = 1e9 + 7; const double eps = 1e-9; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } void print(int x) { if(x > 9) print(x / 10); *o++ = x % 10 + '0'; } #define ls k << 1 #define rs k << 1 | 1 int n, m; int a[maxn]; struct node { int l, r, lmx, rmx, mx, sum; }t[maxn << 2]; void update(int k) { t[k].sum = t[ls].sum + t[rs].sum; t[k].mx = max(t[ls].mx, t[rs].mx); t[k].mx = max(t[k].mx, t[ls].rmx + t[rs].lmx); t[k].rmx = max(t[rs].rmx, t[rs].sum + t[ls].rmx); t[k].lmx = max(t[ls].lmx, t[ls].sum + t[rs].lmx); } void build(int k, int ll, int rr) { t[k] = (node) {ll, rr, 0, 0, 0}; if(ll == rr) { t[k].lmx = t[k].rmx = t[k].mx = t[k].sum = a[ll]; return ; } int mid = ll + rr >> 1; build(ls, ll, mid); build(rs, mid + 1, rr); update(k); } node merge(node a, node b) { node now; now.sum = a.sum + b.sum; now.mx = max(a.mx, b.mx); now.mx = max(now.mx, a.rmx + b.lmx); now.rmx = max(b.rmx, b.sum + a.rmx); now.lmx = max(a.lmx, a.sum + b.lmx); // printf("%d %d %d %dn", now.mx, now.lmx, now.rmx, now.sum); return now; } node query(int k, int ll, int rr) { node ans = (node) {0, 0, 0, 0, 0}; if(ll <= t[k].l && t[k].r <= rr) return t[k]; int mid = t[k].l + t[k].r >> 1; /*if(ll <= mid) ans = query(ls, ll, rr); if(rr > mid) ans = merge(ans, query(rs, ll, rr)); wa!*/ if(ll > mid) return query(rs, ll, rr); else if(rr <= mid) return query(ls, ll, rr); else return merge(query(ls, ll, rr), query(rs, ll, rr)); return ans; } main() { //freopen("a.in", "r", stdin); n = read(); for(int i = 1; i <= n; i++) a[i] = read(); build(1, 1, n); int m = read(); while(m--) { int x = read(), y = read(); printf("%dn", query(1, x, y).mx); } //fwrite(obuf, o-obuf, 1 , stdout); return 0; } /* 5 -10 12 1 -45 134 5 1 5 2 3 4 5 1 4 3 5 */
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/608250.html