Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ostream& operator<<(ostream &out, string str) {
  5.     for(char c : str) out << c;
  6.     return out;
  7. }
  8.  
  9. template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
  10.     return out << "(" << p.first << ", " << p.second << ")";
  11. }
  12.  
  13. template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
  14.     out << "{";
  15.     for(auto it = a.begin(); it != a.end(); it = next(it))
  16.         out << (it != a.begin() ? ", " : "") << *it;
  17.     return out << "}";
  18. }
  19.  
  20. void dump() { cerr << "\n"; }
  21. template<class T, class... Ts> void dump(T a, Ts... x) {
  22.     cerr << a << ", ";
  23.     dump(x...);
  24. }
  25.  
  26. #ifdef DEBUG
  27. #  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
  28. #else
  29. #  define debug(...) false
  30. #endif
  31.  
  32. #define REP(i, n) for(int i = 0; i < n; i++)
  33. #define FOR(i, a, b) for(int i = a; i <= b; i++)
  34. #define ST first
  35. #define ND second
  36.  
  37. template<class T> int size(T && a) { return a.size(); }
  38.  
  39. using LL = long long;
  40. using PII = pair<int, int>;
  41.  
  42. using T = LL;
  43. struct Node {
  44.     T left, mx, sum, right;
  45.     void add(int x) {
  46.         left += x;
  47.         mx += x;
  48.         sum += x;
  49.         right += x;
  50.     }
  51.     int size = 1;
  52. };
  53.  
  54. Node operator+(Node &a, Node &b) {
  55.     Node ret;
  56.     ret.sum = a.sum + b.sum;
  57.     ret.left = max(a.left, a.sum + b.left);
  58.     ret.right = max(b.right, b.sum + a.right);
  59.     ret.mx = max(max(a.mx, b.mx), a.right + b.left);
  60.     ret.size = a.size + b.size;
  61.     return ret;
  62. }
  63.  
  64. struct Tree {
  65.     vector<Node> tree;
  66.     int size = 1;
  67.  
  68.     Tree(int n) {
  69.         while(size < n) size *= 2;
  70.         tree.resize(size * 2);
  71.         for(int i = size - 1; i >= 1; i--)
  72.             tree[i].size = tree[i * 2].size * 2;
  73.     }
  74.  
  75.     void add(int pos, int val) {
  76.         tree[pos += size].add(val);
  77.         while(pos /= 2)
  78.             tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
  79.     }
  80.  
  81.     LL query() {
  82.         int l = size, r = size * 2 - 1;
  83.         Node ret_l = tree[l], ret_r = tree[r];
  84.         while(l + 1 < r) {
  85.             ret_l = ret_l + tree[l + 1];
  86.             ret_r = tree[r - 1] + ret_r;
  87.             l /= 2, r /= 2;
  88.         }
  89.         return (ret_l + ret_r).mx;
  90.     }
  91. };
  92.  
  93. int main() {
  94.     ios_base::sync_with_stdio(0);
  95.     cin.tie(0);
  96.  
  97.     int T;
  98.     cin >> T;
  99.     REP(it, T) {
  100.         int n;
  101.         cin >> n;
  102.  
  103.         map<int, vector<PII>> mapka;
  104.         map<int, int> all_y;
  105.         REP(i, n) {
  106.             int x, y, w;
  107.             cin >> x >> y >> w;
  108.             mapka[x].emplace_back(y, w);
  109.             all_y[y] = 1;
  110.         }
  111.  
  112.         int cnt = 0;
  113.         map<int, int> scale;
  114.         for(auto &[y, id] : all_y)
  115.             scale[y] = ++cnt;
  116.  
  117.         vector<vector<PII>> pts;
  118.         for(auto &[x, v] : mapka) {
  119.             for(auto &[y, w] : v)
  120.                 y = scale[y];
  121.             pts.emplace_back(v);
  122.         }
  123.  
  124.         LL ans = 0;
  125.         REP(l, size(pts)) {
  126.             Tree tree(cnt + 1);
  127.             FOR(r, l, size(pts) - 1) {
  128.                 for(auto &[y, w] : pts[r])
  129.                     tree.add(y, w);
  130.                 ans = max(ans, tree.query());
  131.             }
  132.         }
  133.  
  134.         cout << ans << "\n";
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement