1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 #define rep(i, a, n) for (ll i = a; i < n; i++) #define pb push_back #define SEG_ROOT 1,0,n-1 #define SEG_L o<<1,l,m #define SEG_R o<<1|1,m+1,r const double pi = acos(-1.0); const int N = 500'000; ll s[N + 10]; ll t[N + 10]; ll dp[N + 10]; ll a[3][N + 10]; vector<tuple<int, int, ll> > rlv; ll seg[2][N * 4 + 10]; ll segans[N * 4 + 10]; const ll INF = 0x3f3f3f3f3f3f3f3f; ll query(int o, int l, int r, int ql, int qr) { if (l == ql && r == qr) return seg[0][o]; int m = (l + r) / 2; if (qr <= m) { return query(SEG_L, ql, qr); } else if (ql > m) { return query(SEG_R, ql, qr); } else { return max(query(SEG_L, ql, m), query(SEG_R, m + 1, qr)); } } void update(int o, int l, int r, int pos) { if (l == r) { seg[0][o] = dp[l]; return; } int m = (l + r) / 2; if (pos <= m) update(SEG_L, pos); else update(SEG_R, pos); seg[0][o] = max(seg[0][o << 1], seg[0][o << 1 | 1]); } void build(int o, int l, int r) { if (l == r) { seg[0][o] = dp[l]; return; } int m = (l + r) / 2; build(SEG_L); build(SEG_R); seg[0][o] = max(seg[0][o << 1], seg[0][o << 1 | 1]); }
void buildans(int o, int l, int r) { if (l == r) { seg[0][o] = max(max(dp[l], s[l]), l > 0 ? dp[l-1] : -INF); seg[1][o] = t[l]; segans[o] = seg[0][o] + t[l]; return; } int m = (l + r) / 2; buildans(SEG_L); buildans(SEG_R); seg[0][o] = max(seg[0][o << 1], seg[0][o << 1 | 1]); seg[1][o] = max(seg[1][o << 1], seg[1][o << 1 | 1]); segans[o] = max(seg[0][o << 1] + seg[1][o << 1 | 1], max(segans[o << 1], segans[o << 1 | 1])); };
vector<ll> qans(int o, int l, int r, int ql, int qr) { if (ql == l && qr == r) { return {segans[o], seg[0][o], seg[1][o]}; } int m = (l + r) / 2; if (qr <= m) { return qans(SEG_L, ql, qr); } else if (ql > m) { return qans(SEG_R, ql, qr); } auto lres = qans(SEG_L, ql, m); auto rres = qans(SEG_R, m + 1, qr); return {max(max(lres[0], rres[0]), lres[1] + rres[2]), max(lres[1], rres[1]), max(lres[2], rres[2])}; } int main() { int n, q; scanf("%d %d", &n, &q); ll a2 = 0; rep(i, 0, 3) { rep(j, 0, n) { scanf("%lld", &a[i][j]); } } rep(i, 0, n) a2 += a[2][i]; s[0] = a[0][0]; rep(i, 1, n) { s[i] = s[i - 1] + a[0][i] - a[1][i - 1]; } t[0] = a2 + a[1][0]; rep(i, 1, n) { t[i] = t[i - 1] + a[1][i] - a[2][i - 1]; } rep(i, 0, q) { int r, l; ll v; scanf("%d %d %lld", &l, &r, &v); rlv.pb({r - 1, l - 1, v}); } ll ans = -INF; sort(rlv.begin(), rlv.end()); vector<int> pos; unsigned long itr = 0; rep(i, 0, n){ dp[i] = -INF; } rep(i, 0, n) { while (pos.size() && s[pos.back()] <= s[i]) { pos.pop_back(); } pos.pb(i); while (itr < rlv.size() && get<0>(rlv[itr]) == i) { dp[i] = max( dp[i], s[*lower_bound(pos.begin(), pos.end(), get<1>(rlv[itr]))] - get<2>(rlv[itr])); if (dp[i] != -INF) { ans = max(ans, dp[i] + t[i]); } itr++; } } build(SEG_ROOT); itr = 0; for(auto [r,l,v]:rlv){ ll newdpi = query(SEG_ROOT, max(0,l-1), max(0,r-1)) - v; if (newdpi > dp[r]) { dp[r] = newdpi; ans = max(ans, dp[r] + t[r]); update(SEG_ROOT, r); } } buildans(SEG_ROOT); for (auto [r, l, v] : rlv) { auto qres = qans(SEG_ROOT, l, r); if (qres[0] <= -INF) continue; ans = max(ans, qres[0] - v); } printf("%lld\n", ans); return 0; }
|