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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define per(i,a,n) for (int i=a;i<n;i++) #define w(i) T[(i)].w #define ls(i) T[(i)].ls #define rs(i) T[(i)].rs const int MAXN(2e3+7);
int N, T; struct nd { ll x, y, w; }pepo[MAXN]; ll xx[MAXN], yy[MAXN]; int cntx, cnty; ll ynum[MAXN]; ll outt;
ll mxx[MAXN << 2], lmx[MAXN << 2], rmx[MAXN << 2], smm[MAXN << 2]; inline void PushUp(int rt) { smm[rt] = smm[rt<<1]+smm[rt<<1|1]; lmx[rt] = max(lmx[rt<<1], smm[rt<<1]+lmx[rt<<1|1]); rmx[rt] = max(rmx[rt<<1|1], rmx[rt<<1]+smm[rt<<1|1]); mxx[rt] = max(mxx[rt << 1], mxx[rt << 1 | 1]); mxx[rt] = max(mxx[rt], rmx[rt<<1]+lmx[rt<<1|1]); } inline void Build(int l, int r, int rt) { if (l == r) { mxx[rt] = lmx[rt] = rmx[rt] = smm[rt] = 0; return; } int mid = (l + r) >> 1; Build(l, mid, rt << 1); Build(mid + 1, r, rt << 1 | 1); PushUp(rt); } inline void Add(int L, ll C, int l, int r, int rt) { if (l == r) { lmx[rt] = rmx[rt] = mxx[rt] = smm[rt] = smm[rt] + C; return; } int mid = (l + r) >> 1; if (L <= mid) { Add(L, C, l, mid, rt << 1); } else { Add(L, C, mid + 1, r, rt << 1 | 1); } PushUp(rt); }
int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> T; while(T--) { outt = 0; cin >> N; per(i, 1, N+1) { cin>>pepo[i].x>>pepo[i].y>>pepo[i].w; xx[i]=pepo[i].x, yy[i]=pepo[i].y; } sort(xx+1, xx+1+N, [](const ll& i, const ll& j) {return i<j;}); sort(yy+1, yy+1+N, [](const ll& i, const ll& j) {return i<j;}); cntx = unique(xx+1, xx+1+N)-xx; cnty = unique(yy+1, yy+1+N)-yy; per(i, 1, N+1) { pepo[i].x = lower_bound(xx+1, xx+cntx, pepo[i].x)-xx; pepo[i].y = lower_bound(yy+1, yy+cnty, pepo[i].y)-yy; }
if(cntx<cnty) { sort(pepo+1, pepo+1+N, [](const nd& i, const nd& j) { return i.x==j.x?i.y<j.y:i.x<j.x; }); int pepcntup = 1, pepcntdwn; per(up, 1, cntx) { Build(1, cnty-1, 1); while (pepcntup <= N && pepo[pepcntup].x <= up) { nd tmpp = pepo[pepcntup++]; Add(tmpp.y, tmpp.w, 1, cnty-1, 1); } pepcntdwn = pepcntup; per(dn, up, cntx) { while (pepcntdwn <= N && pepo[pepcntdwn].x <= dn) { nd tmpp = pepo[pepcntdwn++]; Add(tmpp.y, tmpp.w, 1, cnty-1, 1); } outt = max(outt, mxx[1]); } } } else { sort(pepo+1, pepo+1+N, [](const nd& i, const nd& j) { return i.y==j.y?i.x<j.x:i.y<j.y; }); int pepcntup = 1, pepcntdwn; per(up, 1, cnty) { Build(1, cntx-1, 1); while (pepcntup <= N && pepo[pepcntup].y <= up) { nd tmpp = pepo[pepcntup++]; Add(tmpp.x, tmpp.w, 1, cntx-1, 1); } pepcntdwn = pepcntup; per(dn, up, cntx) { while (pepcntdwn <= N && pepo[pepcntdwn].y <= dn) { nd tmpp = pepo[pepcntdwn++]; Add(tmpp.x, tmpp.w, 1, cntx-1, 1); } outt = max(outt, mxx[1]); } } } cout << outt << '\n'; }
return 0; }
|