Submission #282811

# Submission time Handle Problem Language Result Execution time Memory
282811 2020-08-25T02:04:30 Z rama_pang Bulldozer (JOI17_bulldozer) C++14
100 / 100
1415 ms 67740 KB
#include <bits/stdc++.h>
using namespace std;

using lint = long long;

struct Node {
  lint l = 0;
  lint r = 0;
  lint mx = 0;
  lint sum = 0;
  Node() {}
  Node(lint v) {
    l = r = mx = max(v, 0ll);
    sum = v;
  }
  Node(lint l, lint r, lint mx) : l(l), r(r), mx(mx) {}
};

Node Merge(const Node &a, const Node &b) {
  Node res;
  res.sum = a.sum + b.sum;
  res.l = max(a.l, a.sum + b.l);
  res.r = max(b.r, b.sum + a.r);
  res.mx = max({a.mx, b.mx, a.r + b.l});
  return res;
}

class SegTree {
 public:
  int sz;
  vector<Node> tree;

  SegTree() {}
  SegTree(int n) {
    sz = 1;
    while (sz < n) sz *= 2;
    tree.resize(2 * sz);
  }

  void Modify(int p, int x) {
    tree[p += sz] = Node(x);
    for (p /= 2; p > 0; p /= 2) {
      tree[p] = Merge(tree[p * 2], tree[p * 2 + 1]);
    }
  }

  lint Query() {
    return tree[1].mx;
  }
};

class DisjointSet {
 public:
  int sz;
  vector<int> p;
  vector<int> hist;

  DisjointSet() {}
  DisjointSet(int sz) : sz(sz), p(sz) {
    iota(begin(p), end(p), 0);
  } 

  int Find(int x) {
    return p[x] == x ? x : p[x] = Find(p[x]);
  }

  int Unite(int x, int y) {
    hist.emplace_back(x);
    hist.emplace_back(y);
    x = Find(x), y = Find(y);
    if (x == y) return 0;
    p[x] = y;
    return 1;
  }

  void Clear() {
    for (auto i : hist) {
      p[i] = i;
    }
    hist.clear();
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N;
  cin >> N;
  vector<int> X(N), Y(N), V(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> V[i];
  }
  
  vector<pair<pair<int, int>, pair<int, int>>> all;
  auto Add = [&](int p, int q) {
    int x = X[p] - X[q];
    int y = Y[p] - Y[q];
    if (x == 0) {
      y = 1;
    } else if (y == 0) {
      x = 1;
    } else {
      if (y < 0) {
        x *= -1;
        y *= -1;
      }
      int g = __gcd(abs(x), abs(y));
      x /= g;
      y /= g;
    }
    all.push_back({{x, y}, {p, q}});
  };

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      Add(i, j);
    }
  }

  auto angcmp = [&](pair<int, int> a, pair<int, int> b) {
    return 1ll * a.first * b.second - 1ll * a.second * b.first > 0;
  };
  sort(begin(all), end(all), [&](const pair<pair<int, int>, pair<int, int>> a, 
                                 const pair<pair<int, int>, pair<int, int>> b) {
    return angcmp(a.first, b.first);
  });

  vector<int> ord(N);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), [&](int i, int j) {
    return Y[i] < Y[j] || (Y[i] == Y[j] && X[i] < X[j]);
  });
  vector<int> pos(N);
  for (int i = 0; i < N; i++) {
    pos[ord[i]] = i;
  }

  SegTree seg(N);
  for (int i = 0; i < N; i++) {
    seg.Modify(i, V[ord[i]]);
  }
  lint ans = seg.Query();

  for (int f = 0; f < (int) all.size(); f++) {
    int until = f;
    while (until + 1 < (int) all.size() && all[until + 1].first == all[f].first) {
      until++;
    }
    static vector<pair<int, int>> v; 
    v.clear();
    for (int i = f; i <= until; i++) {
      v.emplace_back(all[i].second);
    }
    f = until;
    
    static DisjointSet D(N);
    static vector<pair<int, int>> swaps;
    static vector<int> elems;
    D.Clear(); swaps.clear(); elems.clear();
    for (auto i : v) {
      D.Unite(i.first, i.second);
      elems.emplace_back(i.first);
      elems.emplace_back(i.second);
    }
    sort(begin(elems), end(elems));
    elems.resize(unique(begin(elems), end(elems)) - begin(elems));

    sort(begin(elems), end(elems), [&](int i, int j) {
      if (D.Find(i) != D.Find(j)) return D.Find(i) < D.Find(j);
      return Y[i] < Y[j] || (Y[i] == Y[j] && X[i] < X[j]);
    });

    for (int i = 0; i < (int) elems.size(); i++) {
      int j = i;
      while (j + 1 < (int) elems.size() && D.Find(elems[j + 1]) == D.Find(elems[i])) {
        j++;
      }
      for (int k = i; k <= j; k++) {
        int other = j - (k - i);
        if (k < other) {
          swaps.emplace_back(elems[k], elems[other]);
        }
      }
      i = j;
    }

    for (auto i : swaps) {
      swap(pos[i.first], pos[i.second]);
      swap(ord[pos[i.first]], ord[pos[i.second]]);
      seg.Modify(pos[i.first], V[i.first]);
      seg.Modify(pos[i.second], V[i.second]);
    }
    ans = max(ans, seg.Query());
  }

  cout << ans << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 288 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 568 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 568 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 1354 ms 33528 KB Output is correct
34 Correct 1392 ms 33472 KB Output is correct
35 Correct 1364 ms 33364 KB Output is correct
36 Correct 1374 ms 33544 KB Output is correct
37 Correct 1347 ms 33344 KB Output is correct
38 Correct 1349 ms 33344 KB Output is correct
39 Correct 1344 ms 33600 KB Output is correct
40 Correct 1364 ms 33344 KB Output is correct
41 Correct 1392 ms 33344 KB Output is correct
42 Correct 1373 ms 33344 KB Output is correct
43 Correct 1287 ms 33344 KB Output is correct
44 Correct 1287 ms 33348 KB Output is correct
45 Correct 1295 ms 33344 KB Output is correct
46 Correct 1331 ms 33472 KB Output is correct
47 Correct 1313 ms 33356 KB Output is correct
48 Correct 1309 ms 33472 KB Output is correct
49 Correct 1319 ms 33472 KB Output is correct
50 Correct 1322 ms 33476 KB Output is correct
51 Correct 1328 ms 33344 KB Output is correct
52 Correct 1311 ms 33384 KB Output is correct
53 Correct 1304 ms 33344 KB Output is correct
54 Correct 1316 ms 33344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 568 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 1354 ms 33528 KB Output is correct
34 Correct 1392 ms 33472 KB Output is correct
35 Correct 1364 ms 33364 KB Output is correct
36 Correct 1374 ms 33544 KB Output is correct
37 Correct 1347 ms 33344 KB Output is correct
38 Correct 1349 ms 33344 KB Output is correct
39 Correct 1344 ms 33600 KB Output is correct
40 Correct 1364 ms 33344 KB Output is correct
41 Correct 1392 ms 33344 KB Output is correct
42 Correct 1373 ms 33344 KB Output is correct
43 Correct 1287 ms 33344 KB Output is correct
44 Correct 1287 ms 33348 KB Output is correct
45 Correct 1295 ms 33344 KB Output is correct
46 Correct 1331 ms 33472 KB Output is correct
47 Correct 1313 ms 33356 KB Output is correct
48 Correct 1309 ms 33472 KB Output is correct
49 Correct 1319 ms 33472 KB Output is correct
50 Correct 1322 ms 33476 KB Output is correct
51 Correct 1328 ms 33344 KB Output is correct
52 Correct 1311 ms 33384 KB Output is correct
53 Correct 1304 ms 33344 KB Output is correct
54 Correct 1316 ms 33344 KB Output is correct
55 Correct 1362 ms 33440 KB Output is correct
56 Correct 1360 ms 33344 KB Output is correct
57 Correct 1384 ms 33344 KB Output is correct
58 Correct 1356 ms 33472 KB Output is correct
59 Correct 1364 ms 33344 KB Output is correct
60 Correct 1367 ms 33344 KB Output is correct
61 Correct 1415 ms 33344 KB Output is correct
62 Correct 1362 ms 33440 KB Output is correct
63 Correct 1346 ms 33344 KB Output is correct
64 Correct 1355 ms 33384 KB Output is correct
65 Correct 1379 ms 33344 KB Output is correct
66 Correct 1365 ms 33528 KB Output is correct
67 Correct 1368 ms 33344 KB Output is correct
68 Correct 1375 ms 33524 KB Output is correct
69 Correct 1344 ms 33344 KB Output is correct
70 Correct 1359 ms 33512 KB Output is correct
71 Correct 1359 ms 33344 KB Output is correct
72 Correct 1345 ms 33440 KB Output is correct
73 Correct 1348 ms 33344 KB Output is correct
74 Correct 1375 ms 33416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 288 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 4 ms 568 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 3 ms 512 KB Output is correct
39 Correct 4 ms 512 KB Output is correct
40 Correct 3 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 3 ms 512 KB Output is correct
44 Correct 4 ms 512 KB Output is correct
45 Correct 3 ms 512 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 3 ms 512 KB Output is correct
48 Correct 1354 ms 33528 KB Output is correct
49 Correct 1392 ms 33472 KB Output is correct
50 Correct 1364 ms 33364 KB Output is correct
51 Correct 1374 ms 33544 KB Output is correct
52 Correct 1347 ms 33344 KB Output is correct
53 Correct 1349 ms 33344 KB Output is correct
54 Correct 1344 ms 33600 KB Output is correct
55 Correct 1364 ms 33344 KB Output is correct
56 Correct 1392 ms 33344 KB Output is correct
57 Correct 1373 ms 33344 KB Output is correct
58 Correct 1287 ms 33344 KB Output is correct
59 Correct 1287 ms 33348 KB Output is correct
60 Correct 1295 ms 33344 KB Output is correct
61 Correct 1331 ms 33472 KB Output is correct
62 Correct 1313 ms 33356 KB Output is correct
63 Correct 1309 ms 33472 KB Output is correct
64 Correct 1319 ms 33472 KB Output is correct
65 Correct 1322 ms 33476 KB Output is correct
66 Correct 1328 ms 33344 KB Output is correct
67 Correct 1311 ms 33384 KB Output is correct
68 Correct 1304 ms 33344 KB Output is correct
69 Correct 1316 ms 33344 KB Output is correct
70 Correct 1362 ms 33440 KB Output is correct
71 Correct 1360 ms 33344 KB Output is correct
72 Correct 1384 ms 33344 KB Output is correct
73 Correct 1356 ms 33472 KB Output is correct
74 Correct 1364 ms 33344 KB Output is correct
75 Correct 1367 ms 33344 KB Output is correct
76 Correct 1415 ms 33344 KB Output is correct
77 Correct 1362 ms 33440 KB Output is correct
78 Correct 1346 ms 33344 KB Output is correct
79 Correct 1355 ms 33384 KB Output is correct
80 Correct 1379 ms 33344 KB Output is correct
81 Correct 1365 ms 33528 KB Output is correct
82 Correct 1368 ms 33344 KB Output is correct
83 Correct 1375 ms 33524 KB Output is correct
84 Correct 1344 ms 33344 KB Output is correct
85 Correct 1359 ms 33512 KB Output is correct
86 Correct 1359 ms 33344 KB Output is correct
87 Correct 1345 ms 33440 KB Output is correct
88 Correct 1348 ms 33344 KB Output is correct
89 Correct 1375 ms 33416 KB Output is correct
90 Correct 1352 ms 33344 KB Output is correct
91 Correct 1343 ms 33472 KB Output is correct
92 Correct 1357 ms 33472 KB Output is correct
93 Correct 1364 ms 33396 KB Output is correct
94 Correct 1358 ms 33344 KB Output is correct
95 Correct 1378 ms 33344 KB Output is correct
96 Correct 1359 ms 33384 KB Output is correct
97 Correct 1338 ms 33344 KB Output is correct
98 Correct 1348 ms 33344 KB Output is correct
99 Correct 1367 ms 33348 KB Output is correct
100 Correct 813 ms 67612 KB Output is correct
101 Correct 831 ms 67612 KB Output is correct
102 Correct 817 ms 67740 KB Output is correct
103 Correct 828 ms 67740 KB Output is correct
104 Correct 835 ms 67616 KB Output is correct
105 Correct 964 ms 59804 KB Output is correct
106 Correct 944 ms 59932 KB Output is correct
107 Correct 947 ms 59932 KB Output is correct
108 Correct 952 ms 59932 KB Output is correct
109 Correct 965 ms 59820 KB Output is correct
110 Correct 1273 ms 33348 KB Output is correct
111 Correct 1279 ms 33344 KB Output is correct
112 Correct 1292 ms 33344 KB Output is correct
113 Correct 1293 ms 33348 KB Output is correct
114 Correct 1271 ms 33344 KB Output is correct
115 Correct 1274 ms 33344 KB Output is correct
116 Correct 1276 ms 33340 KB Output is correct
117 Correct 1266 ms 33492 KB Output is correct
118 Correct 1267 ms 33348 KB Output is correct
119 Correct 1270 ms 33344 KB Output is correct
120 Correct 1 ms 384 KB Output is correct
121 Correct 0 ms 384 KB Output is correct
122 Correct 1259 ms 33340 KB Output is correct
123 Correct 1249 ms 33344 KB Output is correct
124 Correct 1271 ms 33472 KB Output is correct
125 Correct 1246 ms 33344 KB Output is correct
126 Correct 1253 ms 33392 KB Output is correct
127 Correct 1235 ms 33340 KB Output is correct
128 Correct 1237 ms 33344 KB Output is correct
129 Correct 1248 ms 33436 KB Output is correct
130 Correct 1269 ms 33472 KB Output is correct
131 Correct 1242 ms 33600 KB Output is correct
132 Correct 1247 ms 33344 KB Output is correct
133 Correct 1283 ms 33508 KB Output is correct