Submission #498510

# Submission time Handle Problem Language Result Execution time Memory
498510 2021-12-25T11:12:02 Z 600Mihnea Bulldozer (JOI17_bulldozer) C++17
100 / 100
1965 ms 197628 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Point {
  ll x;
  ll y;
  ll value;
};

struct R {
  int sgn;
  ll up;
  ll down;
  int i;
  int j;
};

R get(ll x, ll y) {
  R sol;

  sol.sgn = +1;
  if (x < 0 && y < 0) {
    x *= -1;
    y *= -1;
  }
  if (x < 0 || y < 0) {
    sol.sgn = -1;
  }
  sol.up = abs(x);
  sol.down = abs(y);
  return sol;
}

bool operator < (R a, R b) {
  if (a.sgn != b.sgn) {
    return a.sgn < b.sgn;
  }
  if (a.sgn == -1) {
    swap(a, b);
  }
  return a.up * b.down < b.up * a.down;
}

bool initialCmp(Point a, Point b) {
  if (a.y == b.y) {
    return a.x < b.x;
  }
  return a.y < b.y;
}

const int N = 2000 + 7;
int n;
R panta[N][N];
Point points[N];
ll value[N];

void sneakyHack() {
  for (int i = 1; i <= n; i++) {
    swap(points[i].x, points[i].y);
  }
}

int ord[N];
int inv[N];

struct Node {
  ll sol;
  ll sum;
  ll pref;
  ll suf;
};

Node operator + (Node a, Node b) {
  ll sol = max(max(a.sol, b.sol), a.suf + b.pref);
  ll sum = a.sum + b.sum;
  ll pref = max(a.pref, a.sum + b.pref);
  ll suf = max(b.suf, b.sum + a.suf);
  return {sol, sum, pref, suf};
}

Node tree[4 * N];

void build(int v, int tl, int tr) {
  if (tl == tr) {
    tree[v] = {max(value[tl], 0LL), value[tl], max(value[tl], 0LL), max(value[tl], 0LL)};
  } else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    tree[v] = tree[2 * v] + tree[2 * v + 1];
  }
}

void upd(int v, int tl, int tr, int i) {
  if (tr < i || i < tl) {
    return;
  }
  if (tl == tr) {
    tree[v] = {max(value[tl], 0LL), value[tl], max(value[tl], 0LL), max(value[tl], 0LL)};
  } else {
    int tm = (tl + tr) / 2;
    upd(2 * v, tl, tm, i);
    upd(2 * v + 1, tm + 1, tr, i);
    tree[v] = tree[2 * v] + tree[2 * v + 1];
  }
}

int main() {
  bool isHome;

  isHome = true;
  isHome = false;

  if (isHome) {
    freopen ("input", "r", stdin);
  } else {
    ios::sync_with_stdio(0); cin.tie(0);
  }

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> points[i].x >> points[i].y >> points[i].value;
  }

  ll best = 0;

  for (int iteration = 1; iteration <= 2; iteration++) {
    sneakyHack();
    sort(points + 1, points + n + 1, initialCmp);

    for (int i = 1; i <= n; i++) {
      ord[i] = i;
      inv[i] = i;
      for (int j = i + 1; j <= n; j++) {
        panta[i][j] = get(points[i].x - points[j].x, points[i].y - points[j].y);
        panta[i][j].i = i;
        panta[i][j].j = j;
      }
      value[i] = points[i].value;
    }
    vector<R> all;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (points[i].y != points[j].y) {
          all.push_back(panta[i][j]);
        }
      }
    }
    build(1, 1, n);

    stable_sort(all.begin(), all.end());

    /**

    second : 22 16 10 8 9 11 23 17 15 29
    => 19

    ------------------------------------------

    second : 29 22 16 10 8 9 11 23 17 15
    => 21

    **/

    best = max(best, tree[1].sol);

    /// all[it] < all[it + 1]
    int l = 0;
    while (l < (int) all.size()) {
      int r = l;
      while (r + 1 < (int) all.size() && !(all[r] < all[r + 1])) {
        r++;
      }

      for (int it = l; it <= r; it++) {
        int i = all[it].i;
        int j = all[it].j;
        int pos_i = inv[i];
        int pos_j = inv[j];

        swap(ord[pos_i], ord[pos_j]);
        inv[ord[pos_i]] = pos_i;
        inv[ord[pos_j]] = pos_j;

        swap(value[pos_i], value[pos_j]);
        upd(1, 1, n, pos_i);
        upd(1, 1, n, pos_j);
      }

      best = max(best, tree[1].sol);

      l = r + 1;
    }



  }

  cout << best << "\n";




  return 0;
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:118:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |     freopen ("input", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1232 KB Output is correct
2 Correct 1 ms 1232 KB Output is correct
3 Correct 1 ms 1232 KB Output is correct
4 Correct 2 ms 1232 KB Output is correct
5 Correct 2 ms 1232 KB Output is correct
6 Correct 1 ms 1232 KB Output is correct
7 Correct 1 ms 1232 KB Output is correct
8 Correct 1 ms 1232 KB Output is correct
9 Correct 2 ms 1232 KB Output is correct
10 Correct 2 ms 1232 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1252 KB Output is correct
2 Correct 3 ms 1252 KB Output is correct
3 Correct 3 ms 1252 KB Output is correct
4 Correct 3 ms 1252 KB Output is correct
5 Correct 4 ms 1252 KB Output is correct
6 Correct 4 ms 1252 KB Output is correct
7 Correct 4 ms 1252 KB Output is correct
8 Correct 3 ms 1252 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 3 ms 1252 KB Output is correct
22 Correct 4 ms 1252 KB Output is correct
23 Correct 4 ms 1252 KB Output is correct
24 Correct 3 ms 1252 KB Output is correct
25 Correct 3 ms 1252 KB Output is correct
26 Correct 3 ms 1252 KB Output is correct
27 Correct 3 ms 1252 KB Output is correct
28 Correct 4 ms 1252 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 3 ms 1252 KB Output is correct
31 Correct 3 ms 1252 KB Output is correct
32 Correct 3 ms 1252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1252 KB Output is correct
2 Correct 3 ms 1252 KB Output is correct
3 Correct 3 ms 1252 KB Output is correct
4 Correct 3 ms 1252 KB Output is correct
5 Correct 4 ms 1252 KB Output is correct
6 Correct 4 ms 1252 KB Output is correct
7 Correct 4 ms 1252 KB Output is correct
8 Correct 3 ms 1252 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 3 ms 1252 KB Output is correct
22 Correct 4 ms 1252 KB Output is correct
23 Correct 4 ms 1252 KB Output is correct
24 Correct 3 ms 1252 KB Output is correct
25 Correct 3 ms 1252 KB Output is correct
26 Correct 3 ms 1252 KB Output is correct
27 Correct 3 ms 1252 KB Output is correct
28 Correct 4 ms 1252 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 3 ms 1252 KB Output is correct
31 Correct 3 ms 1252 KB Output is correct
32 Correct 3 ms 1252 KB Output is correct
33 Correct 1776 ms 164908 KB Output is correct
34 Correct 1759 ms 164832 KB Output is correct
35 Correct 1756 ms 164816 KB Output is correct
36 Correct 1760 ms 164816 KB Output is correct
37 Correct 1756 ms 164924 KB Output is correct
38 Correct 1766 ms 164832 KB Output is correct
39 Correct 1765 ms 164948 KB Output is correct
40 Correct 1742 ms 164792 KB Output is correct
41 Correct 1748 ms 164812 KB Output is correct
42 Correct 1728 ms 164740 KB Output is correct
43 Correct 1693 ms 164904 KB Output is correct
44 Correct 1703 ms 164820 KB Output is correct
45 Correct 1756 ms 164876 KB Output is correct
46 Correct 1765 ms 164836 KB Output is correct
47 Correct 1738 ms 164948 KB Output is correct
48 Correct 1709 ms 164828 KB Output is correct
49 Correct 1719 ms 164820 KB Output is correct
50 Correct 1717 ms 164828 KB Output is correct
51 Correct 1707 ms 164916 KB Output is correct
52 Correct 1721 ms 165016 KB Output is correct
53 Correct 1705 ms 164908 KB Output is correct
54 Correct 1717 ms 164788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1252 KB Output is correct
2 Correct 3 ms 1252 KB Output is correct
3 Correct 3 ms 1252 KB Output is correct
4 Correct 3 ms 1252 KB Output is correct
5 Correct 4 ms 1252 KB Output is correct
6 Correct 4 ms 1252 KB Output is correct
7 Correct 4 ms 1252 KB Output is correct
8 Correct 3 ms 1252 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 3 ms 1252 KB Output is correct
22 Correct 4 ms 1252 KB Output is correct
23 Correct 4 ms 1252 KB Output is correct
24 Correct 3 ms 1252 KB Output is correct
25 Correct 3 ms 1252 KB Output is correct
26 Correct 3 ms 1252 KB Output is correct
27 Correct 3 ms 1252 KB Output is correct
28 Correct 4 ms 1252 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 3 ms 1252 KB Output is correct
31 Correct 3 ms 1252 KB Output is correct
32 Correct 3 ms 1252 KB Output is correct
33 Correct 1776 ms 164908 KB Output is correct
34 Correct 1759 ms 164832 KB Output is correct
35 Correct 1756 ms 164816 KB Output is correct
36 Correct 1760 ms 164816 KB Output is correct
37 Correct 1756 ms 164924 KB Output is correct
38 Correct 1766 ms 164832 KB Output is correct
39 Correct 1765 ms 164948 KB Output is correct
40 Correct 1742 ms 164792 KB Output is correct
41 Correct 1748 ms 164812 KB Output is correct
42 Correct 1728 ms 164740 KB Output is correct
43 Correct 1693 ms 164904 KB Output is correct
44 Correct 1703 ms 164820 KB Output is correct
45 Correct 1756 ms 164876 KB Output is correct
46 Correct 1765 ms 164836 KB Output is correct
47 Correct 1738 ms 164948 KB Output is correct
48 Correct 1709 ms 164828 KB Output is correct
49 Correct 1719 ms 164820 KB Output is correct
50 Correct 1717 ms 164828 KB Output is correct
51 Correct 1707 ms 164916 KB Output is correct
52 Correct 1721 ms 165016 KB Output is correct
53 Correct 1705 ms 164908 KB Output is correct
54 Correct 1717 ms 164788 KB Output is correct
55 Correct 1750 ms 164824 KB Output is correct
56 Correct 1751 ms 164784 KB Output is correct
57 Correct 1740 ms 164796 KB Output is correct
58 Correct 1786 ms 164832 KB Output is correct
59 Correct 1754 ms 164804 KB Output is correct
60 Correct 1749 ms 164900 KB Output is correct
61 Correct 1759 ms 164812 KB Output is correct
62 Correct 1754 ms 164820 KB Output is correct
63 Correct 1752 ms 164752 KB Output is correct
64 Correct 1757 ms 164888 KB Output is correct
65 Correct 1800 ms 164924 KB Output is correct
66 Correct 1751 ms 165052 KB Output is correct
67 Correct 1736 ms 164920 KB Output is correct
68 Correct 1755 ms 164812 KB Output is correct
69 Correct 1736 ms 164912 KB Output is correct
70 Correct 1859 ms 164816 KB Output is correct
71 Correct 1924 ms 164844 KB Output is correct
72 Correct 1965 ms 164844 KB Output is correct
73 Correct 1908 ms 164844 KB Output is correct
74 Correct 1850 ms 164808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1232 KB Output is correct
2 Correct 1 ms 1232 KB Output is correct
3 Correct 1 ms 1232 KB Output is correct
4 Correct 2 ms 1232 KB Output is correct
5 Correct 2 ms 1232 KB Output is correct
6 Correct 1 ms 1232 KB Output is correct
7 Correct 1 ms 1232 KB Output is correct
8 Correct 1 ms 1232 KB Output is correct
9 Correct 2 ms 1232 KB Output is correct
10 Correct 2 ms 1232 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 3 ms 1252 KB Output is correct
17 Correct 3 ms 1252 KB Output is correct
18 Correct 3 ms 1252 KB Output is correct
19 Correct 3 ms 1252 KB Output is correct
20 Correct 4 ms 1252 KB Output is correct
21 Correct 4 ms 1252 KB Output is correct
22 Correct 4 ms 1252 KB Output is correct
23 Correct 3 ms 1252 KB Output is correct
24 Correct 3 ms 1252 KB Output is correct
25 Correct 3 ms 1252 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 3 ms 1252 KB Output is correct
37 Correct 4 ms 1252 KB Output is correct
38 Correct 4 ms 1252 KB Output is correct
39 Correct 3 ms 1252 KB Output is correct
40 Correct 3 ms 1252 KB Output is correct
41 Correct 3 ms 1252 KB Output is correct
42 Correct 3 ms 1252 KB Output is correct
43 Correct 4 ms 1252 KB Output is correct
44 Correct 3 ms 1252 KB Output is correct
45 Correct 3 ms 1252 KB Output is correct
46 Correct 3 ms 1252 KB Output is correct
47 Correct 3 ms 1252 KB Output is correct
48 Correct 1776 ms 164908 KB Output is correct
49 Correct 1759 ms 164832 KB Output is correct
50 Correct 1756 ms 164816 KB Output is correct
51 Correct 1760 ms 164816 KB Output is correct
52 Correct 1756 ms 164924 KB Output is correct
53 Correct 1766 ms 164832 KB Output is correct
54 Correct 1765 ms 164948 KB Output is correct
55 Correct 1742 ms 164792 KB Output is correct
56 Correct 1748 ms 164812 KB Output is correct
57 Correct 1728 ms 164740 KB Output is correct
58 Correct 1693 ms 164904 KB Output is correct
59 Correct 1703 ms 164820 KB Output is correct
60 Correct 1756 ms 164876 KB Output is correct
61 Correct 1765 ms 164836 KB Output is correct
62 Correct 1738 ms 164948 KB Output is correct
63 Correct 1709 ms 164828 KB Output is correct
64 Correct 1719 ms 164820 KB Output is correct
65 Correct 1717 ms 164828 KB Output is correct
66 Correct 1707 ms 164916 KB Output is correct
67 Correct 1721 ms 165016 KB Output is correct
68 Correct 1705 ms 164908 KB Output is correct
69 Correct 1717 ms 164788 KB Output is correct
70 Correct 1750 ms 164824 KB Output is correct
71 Correct 1751 ms 164784 KB Output is correct
72 Correct 1740 ms 164796 KB Output is correct
73 Correct 1786 ms 164832 KB Output is correct
74 Correct 1754 ms 164804 KB Output is correct
75 Correct 1749 ms 164900 KB Output is correct
76 Correct 1759 ms 164812 KB Output is correct
77 Correct 1754 ms 164820 KB Output is correct
78 Correct 1752 ms 164752 KB Output is correct
79 Correct 1757 ms 164888 KB Output is correct
80 Correct 1800 ms 164924 KB Output is correct
81 Correct 1751 ms 165052 KB Output is correct
82 Correct 1736 ms 164920 KB Output is correct
83 Correct 1755 ms 164812 KB Output is correct
84 Correct 1736 ms 164912 KB Output is correct
85 Correct 1859 ms 164816 KB Output is correct
86 Correct 1924 ms 164844 KB Output is correct
87 Correct 1965 ms 164844 KB Output is correct
88 Correct 1908 ms 164844 KB Output is correct
89 Correct 1850 ms 164808 KB Output is correct
90 Correct 1803 ms 164808 KB Output is correct
91 Correct 1838 ms 164824 KB Output is correct
92 Correct 1754 ms 164868 KB Output is correct
93 Correct 1784 ms 164840 KB Output is correct
94 Correct 1769 ms 164916 KB Output is correct
95 Correct 1750 ms 164808 KB Output is correct
96 Correct 1742 ms 164856 KB Output is correct
97 Correct 1767 ms 164820 KB Output is correct
98 Correct 1736 ms 164864 KB Output is correct
99 Correct 1729 ms 164992 KB Output is correct
100 Correct 1169 ms 197628 KB Output is correct
101 Correct 1165 ms 197584 KB Output is correct
102 Correct 1185 ms 197584 KB Output is correct
103 Correct 1164 ms 197536 KB Output is correct
104 Correct 1179 ms 197588 KB Output is correct
105 Correct 1340 ms 164916 KB Output is correct
106 Correct 1348 ms 164848 KB Output is correct
107 Correct 1324 ms 165000 KB Output is correct
108 Correct 1344 ms 164892 KB Output is correct
109 Correct 1372 ms 164764 KB Output is correct
110 Correct 1481 ms 163380 KB Output is correct
111 Correct 1457 ms 163456 KB Output is correct
112 Correct 1466 ms 163560 KB Output is correct
113 Correct 1477 ms 163432 KB Output is correct
114 Correct 1470 ms 163524 KB Output is correct
115 Correct 1494 ms 163368 KB Output is correct
116 Correct 1478 ms 163460 KB Output is correct
117 Correct 1486 ms 163396 KB Output is correct
118 Correct 1480 ms 163408 KB Output is correct
119 Correct 1470 ms 163416 KB Output is correct
120 Correct 0 ms 332 KB Output is correct
121 Correct 0 ms 332 KB Output is correct
122 Correct 1736 ms 164860 KB Output is correct
123 Correct 1769 ms 164824 KB Output is correct
124 Correct 1752 ms 164836 KB Output is correct
125 Correct 1717 ms 164912 KB Output is correct
126 Correct 1710 ms 164932 KB Output is correct
127 Correct 1717 ms 164996 KB Output is correct
128 Correct 1718 ms 164900 KB Output is correct
129 Correct 1719 ms 164844 KB Output is correct
130 Correct 1740 ms 164892 KB Output is correct
131 Correct 1711 ms 164904 KB Output is correct
132 Correct 1704 ms 164860 KB Output is correct
133 Correct 1714 ms 164928 KB Output is correct