C++#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
const int N = 2e5;
int n, a[N + 5], x[N + 5], y[N + 5];
int match[N + 5];
priority_queue<pair<int, int>> s0[N + 5], s1[N + 5], mis;
tuple<int, int, int> operator+(pair<int, int> a, pair<int, int> b) {
return {a.first + b.first, a.second, b.second};
}
struct SegmentTree {
#define ls (u << 1)
#define rs (ls | 1)
struct node {
pair<int, int> max0, max1;
pair<int, int> cand0, cand1;
tuple<int, int, int> cand2, cand3, cand4;
int cMin, cTag;
} seg[N * 4 + 5];
void pushUp(int u) {
seg[u].cMin = min(seg[ls].cMin, seg[rs].cMin),
seg[u].max0 = max(seg[ls].max0, seg[rs].max0),
seg[u].max1 = max(seg[ls].max1, seg[rs].max1),
seg[u].cand2 = max({seg[ls].max0 + seg[rs].max1, seg[ls].cand2, seg[rs].cand2}),
seg[u].cand4 = max({seg[rs].max0 + seg[ls].max1, seg[ls].cand4, seg[rs].cand4}),
seg[u].cand3 = {-inf * 2, 0, 0};
pair<int, int> lCand, rCand;
if (seg[rs].cMin == seg[u].cMin) seg[u].cand0 = seg[rs].cand0, seg[u].cand3 = max(seg[u].cand3, seg[rs].cand3), rCand = seg[rs].cand1;
else seg[u].cand0 = max(seg[ls].cand0, seg[rs].max0), seg[u].cand3 = max(seg[u].cand3, seg[rs].cand2), rCand = seg[rs].max1;
if (seg[ls].cMin == seg[u].cMin) seg[u].cand1 = seg[ls].cand1, seg[u].cand3 = max(seg[u].cand3, seg[ls].cand3), lCand = seg[ls].cand0;
else seg[u].cand1 = max(seg[ls].max1, seg[rs].cand1), seg[u].cand3 = max(seg[u].cand3, seg[ls].cand2), lCand = seg[ls].max0;
seg[u].cand3 = max(seg[u].cand3, lCand + rCand);
}
void apply(int k, int u) { seg[u].cMin += k, seg[u].cTag += k; }
void pushDown(int u) {
if (!seg[u].cTag) return ;
apply(seg[u].cTag, ls), apply(seg[u].cTag, rs), seg[u].cTag = 0;
}
void update(int l, int r, int k, int u, int tl, int tr) {
if (l > r) return ;
if (l <= tl && tr <= r) return apply(k, u);
int mid = (tl + tr) >> 1;
pushDown(u);
if (l <= mid) update(l, r, k, ls, tl, mid);
if (r > mid) update(l, r, k, rs, mid + 1, tr);
pushUp(u);
}
void insert(int x, int u, int tl, int tr) {
if (tl == tr) {
seg[u].max0 = {-inf, 0};
for (; !s0[tl].empty(); s0[tl].pop()) {
auto [v, j] = s0[tl].top();
if (!match[j]) { seg[u].max0 = {v, j}; break; }
}
seg[u].max1 = {-inf, 0};
for (; !s1[tl].empty(); s1[tl].pop()) {
auto [v, j] = s1[tl].top();
if (match[j] == 1) { seg[u].max1 = {v, j}; break; }
}
seg[u].cand0 = seg[u].max0, seg[u].cand1 = {-inf, 0},
seg[u].cand2 = seg[u].cand3 = seg[u].max0 + seg[u].max1,
seg[u].cand4 = {-inf * 2, 0, 0};
return ;
}
int mid = (tl + tr) >> 1;
pushDown(u);
if (x <= mid) insert(x, ls, tl, mid);
else insert(x, rs, mid + 1, tr);
pushUp(u);
}
pair<pair<int, int>, int> queryPrefix(int r, int u, int tl, int tr) {
if (tr <= r) return {seg[u].cMin ? seg[u].max0 : seg[u].cand0, seg[u].cMin};
int mid = (tl + tr) >> 1;
pushDown(u);
if (r > mid) {
auto ret = queryPrefix(r, rs, mid + 1, tr);
if (ret.second)
ret.first = max(ret.first, seg[ls].cMin ? seg[ls].max0 : seg[ls].cand0),
ret.second = min(ret.second, seg[ls].cMin);
return ret;
}
return queryPrefix(r, ls, tl, mid);
}
pair<int, int> querySuffix(int l, int u, int tl, int tr) {
if (l <= tl) return seg[u].max0;
int mid = (tl + tr) >> 1;
pushDown(u);
auto ret = querySuffix(l, rs, mid + 1, tr);
if (l <= mid) ret = max(ret, querySuffix(l, ls, tl, mid));
return ret;
}
#undef ls
#undef rs
} seg;
ll ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", a + i, x + i, y + i);
s0[a[i]].emplace(x[i], i), mis.emplace(y[i], i);
}
for (int i = 1; i <= n; ++i) seg.insert(i, 1, 1, n);
for (int i = 1; i <= n; ++i) {
auto [res, l, r] = max({seg.queryPrefix(i, 1, 1, n).first + make_pair(0, 0), make_pair(0, 0) + seg.querySuffix(i, 1, 1, n), seg.seg[1].cand3, seg.seg[1].cand4});
pair<int, int> top = {-inf, 0};
for (; !mis.empty(); mis.pop()) {
auto [v, j] = mis.top();
if (!match[j]) { top = {v, j}; break; }
}
if (top.first > res) { ans += top.first, match[top.second] = 2, seg.insert(a[top.second], 1, 1, n), seg.update(i + 1, n, 1, 1, 1, n); }
else {
ans += res;
if (!l) match[r] = 1, s1[a[r]].emplace(y[r] - x[r], r), seg.insert(a[r], 1, 1, n), seg.update(i + 1, a[r], 1, 1, 1, n);
else if (!r) match[l] = 1, s1[a[l]].emplace(y[l] - x[l], l), seg.insert(a[l], 1, 1, n), seg.update(a[l] + 1, i, -1, 1, 1, n);
else
match[l] = 1, s1[a[l]].emplace(y[l] - x[l], l), match[r] = 2,
seg.insert(a[l], 1, 1, n), seg.insert(a[r], 1, 1, n),
seg.update(1, a[l], 1, 1, 1, n), seg.update(1, a[r], -1, 1, 1, n), seg.update(i + 1, n, 1, 1, 1, n);
}
printf("%lld\n",ans);
}
}