Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- ostream& operator<<(ostream &out, string str) {
- for(char c : str) out << c;
- return out;
- }
- template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
- return out << "(" << p.first << ", " << p.second << ")";
- }
- template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
- out << "{";
- for(auto it = a.begin(); it != a.end(); it = next(it))
- out << (it != a.begin() ? ", " : "") << *it;
- return out << "}";
- }
- void dump() { cerr << "\n"; }
- template<class T, class... Ts> void dump(T a, Ts... x) {
- cerr << a << ", ";
- dump(x...);
- }
- #ifdef DEBUG
- # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
- #else
- # define debug(...) false
- #endif
- #define REP(i, n) for(int i = 0; i < n; i++)
- #define FOR(i, a, b) for(int i = a; i <= b; i++)
- #define ST first
- #define ND second
- template<class T> int size(T && a) { return a.size(); }
- using LL = long long;
- using PII = pair<int, int>;
- using T = LL;
- struct Node {
- T left, mx, sum, right;
- void add(int x) {
- left += x;
- mx += x;
- sum += x;
- right += x;
- }
- int size = 1;
- };
- Node operator+(Node &a, Node &b) {
- Node ret;
- ret.sum = a.sum + b.sum;
- ret.left = max(a.left, a.sum + b.left);
- ret.right = max(b.right, b.sum + a.right);
- ret.mx = max(max(a.mx, b.mx), a.right + b.left);
- ret.size = a.size + b.size;
- return ret;
- }
- struct Tree {
- vector<Node> tree;
- int size = 1;
- Tree(int n) {
- while(size < n) size *= 2;
- tree.resize(size * 2);
- for(int i = size - 1; i >= 1; i--)
- tree[i].size = tree[i * 2].size * 2;
- }
- void add(int pos, int val) {
- tree[pos += size].add(val);
- while(pos /= 2)
- tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
- }
- LL query() {
- int l = size, r = size * 2 - 1;
- Node ret_l = tree[l], ret_r = tree[r];
- while(l + 1 < r) {
- ret_l = ret_l + tree[l + 1];
- ret_r = tree[r - 1] + ret_r;
- l /= 2, r /= 2;
- }
- return (ret_l + ret_r).mx;
- }
- };
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int T;
- cin >> T;
- REP(it, T) {
- int n;
- cin >> n;
- map<int, vector<PII>> mapka;
- map<int, int> all_y;
- REP(i, n) {
- int x, y, w;
- cin >> x >> y >> w;
- mapka[x].emplace_back(y, w);
- all_y[y] = 1;
- }
- int cnt = 0;
- map<int, int> scale;
- for(auto &[y, id] : all_y)
- scale[y] = ++cnt;
- vector<vector<PII>> pts;
- for(auto &[x, v] : mapka) {
- for(auto &[y, w] : v)
- y = scale[y];
- pts.emplace_back(v);
- }
- LL ans = 0;
- REP(l, size(pts)) {
- Tree tree(cnt + 1);
- FOR(r, l, size(pts) - 1) {
- for(auto &[y, w] : pts[r])
- tree.add(y, w);
- ans = max(ans, tree.query());
- }
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement