Submission #442674

# Submission time Handle Problem Language Result Execution time Memory
442674 2021-07-08T14:37:37 Z valerikk Bulldozer (JOI17_bulldozer) C++17
100 / 100
957 ms 55620 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

const int S = 1 << 11;
const int N = S + 7;

struct event {
    ll x, y;
    int i, j;
};

bool operator<(const event &l, const event &r) {
    return l.x * r.y - l.y * r.x < 0;
}

bool operator==(const event &l, const event &r) {
    return l.x * r.y - l.y * r.x == 0;
}

int n;
ll x[N], y[N], w[N];
int ord[N];
ll sum[2 * S];
ll mx[2 * S];
ll pref[2 * S], suff[2 * S];
int where[N];
vector<event> events;

void init(int i, ll val) {
    i += S;
    sum[i] = val;
    mx[i] = pref[i] = suff[i] = max(0ll, val);
}

void recalc(int i) {
    int l = i << 1, r = i << 1 | 1;
    sum[i] = sum[l] + sum[r];
    mx[i] = max({mx[l], mx[r], suff[l] + pref[r]});
    pref[i] = max(pref[l], sum[l] + pref[r]);
    suff[i] = max(suff[r], sum[r] + suff[l]);
}

void build() {
    for (int i = 0; i < S; i++) init(i, 0);
    for (int i = 0; i < n; i++) init(i, w[ord[i]]);
    for (int i = S - 1; i > 0; i--) recalc(i);
}

void upd(int i, ll val) {
    init(i, val);
    i += S;
    while (i > 1) {
        i >>= 1;
        recalc(i);
    }
}

ll get() {
    return mx[1];
}

void my_swap(int i, int j) {
    int pi = where[i], pj = where[j];
    upd(pi, w[j]);
    upd(pj, w[i]);
    swap(ord[where[i]], ord[where[j]]);
    swap(where[i], where[j]);
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i] >> w[i];
    }
    for (int i = 0; i < n; i++) ord[i] = i;
    sort(ord, ord + n, [&](const int &i, const int &j) {
        return x[i] < x[j] || (x[i] == x[j] && y[i] > y[j]);
    });
    for (int i = 0; i < n; i++) where[ord[i]] = i;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            ll dx = x[i] - x[j], dy = y[i] - y[j];
            dy *= -1;
            swap(dx, dy);
            if (dy < 0 || (dy == 0 && dx < 0)) {
                dx *= -1;
                dy *= -1;
            }
            events.push_back({dx, dy, i, j});
        }
    }
    sort(events.begin(), events.end());
    build();
    ll ans = get();
    int i = 0;
    while (i < events.size()) {
        int j = i;
        while (j < events.size() && events[j] == events[i]) j++;
        vector<int> arr;
        for (int k = i; k < j; k++) arr.push_back(min(where[events[k].i], where[events[k].j]));
        sort(arr.begin(), arr.end());
        arr.erase(unique(arr.begin(), arr.end()), arr.end());
        int k = 0;
        while (k < arr.size()) {
            int l = k;
            while (l + 1 < arr.size() && arr[l + 1] == arr[l] + 1) l++;
            int L = arr[k], R = arr[l] + 1;
            for (int t = 0; L + t < R - t; t++) my_swap(ord[L + t], ord[R - t]);
            k = l + 1;
        }
        ans = max(ans, get());
        i = j;
    }
    cout << ans << endl;
    return 0;
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:104:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |     while (i < events.size()) {
      |            ~~^~~~~~~~~~~~~~~
bulldozer.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |         while (j < events.size() && events[j] == events[i]) j++;
      |                ~~^~~~~~~~~~~~~~~
bulldozer.cpp:112:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         while (k < arr.size()) {
      |                ~~^~~~~~~~~~~~
bulldozer.cpp:114:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |             while (l + 1 < arr.size() && arr[l + 1] == arr[l] + 1) l++;
      |                    ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 652 KB Output is correct
2 Correct 1 ms 636 KB Output is correct
3 Correct 1 ms 652 KB Output is correct
4 Correct 1 ms 652 KB Output is correct
5 Correct 1 ms 652 KB Output is correct
6 Correct 1 ms 652 KB Output is correct
7 Correct 1 ms 652 KB Output is correct
8 Correct 1 ms 644 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 644 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 452 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 636 KB Output is correct
2 Correct 3 ms 652 KB Output is correct
3 Correct 3 ms 652 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 3 ms 636 KB Output is correct
6 Correct 3 ms 652 KB Output is correct
7 Correct 3 ms 636 KB Output is correct
8 Correct 3 ms 652 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 3 ms 652 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 3 ms 732 KB Output is correct
22 Correct 3 ms 652 KB Output is correct
23 Correct 3 ms 652 KB Output is correct
24 Correct 3 ms 652 KB Output is correct
25 Correct 3 ms 652 KB Output is correct
26 Correct 3 ms 652 KB Output is correct
27 Correct 3 ms 652 KB Output is correct
28 Correct 3 ms 652 KB Output is correct
29 Correct 3 ms 652 KB Output is correct
30 Correct 3 ms 652 KB Output is correct
31 Correct 3 ms 652 KB Output is correct
32 Correct 3 ms 652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 636 KB Output is correct
2 Correct 3 ms 652 KB Output is correct
3 Correct 3 ms 652 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 3 ms 636 KB Output is correct
6 Correct 3 ms 652 KB Output is correct
7 Correct 3 ms 636 KB Output is correct
8 Correct 3 ms 652 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 3 ms 652 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 3 ms 732 KB Output is correct
22 Correct 3 ms 652 KB Output is correct
23 Correct 3 ms 652 KB Output is correct
24 Correct 3 ms 652 KB Output is correct
25 Correct 3 ms 652 KB Output is correct
26 Correct 3 ms 652 KB Output is correct
27 Correct 3 ms 652 KB Output is correct
28 Correct 3 ms 652 KB Output is correct
29 Correct 3 ms 652 KB Output is correct
30 Correct 3 ms 652 KB Output is correct
31 Correct 3 ms 652 KB Output is correct
32 Correct 3 ms 652 KB Output is correct
33 Correct 921 ms 49792 KB Output is correct
34 Correct 922 ms 49800 KB Output is correct
35 Correct 903 ms 49744 KB Output is correct
36 Correct 901 ms 49784 KB Output is correct
37 Correct 898 ms 49896 KB Output is correct
38 Correct 954 ms 49724 KB Output is correct
39 Correct 894 ms 49852 KB Output is correct
40 Correct 921 ms 49812 KB Output is correct
41 Correct 939 ms 49788 KB Output is correct
42 Correct 957 ms 49784 KB Output is correct
43 Correct 896 ms 49768 KB Output is correct
44 Correct 905 ms 49780 KB Output is correct
45 Correct 914 ms 49808 KB Output is correct
46 Correct 887 ms 49808 KB Output is correct
47 Correct 900 ms 49772 KB Output is correct
48 Correct 904 ms 49780 KB Output is correct
49 Correct 905 ms 49700 KB Output is correct
50 Correct 899 ms 49828 KB Output is correct
51 Correct 909 ms 49760 KB Output is correct
52 Correct 900 ms 49808 KB Output is correct
53 Correct 908 ms 49780 KB Output is correct
54 Correct 875 ms 49784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 636 KB Output is correct
2 Correct 3 ms 652 KB Output is correct
3 Correct 3 ms 652 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 3 ms 636 KB Output is correct
6 Correct 3 ms 652 KB Output is correct
7 Correct 3 ms 636 KB Output is correct
8 Correct 3 ms 652 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 3 ms 652 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 3 ms 732 KB Output is correct
22 Correct 3 ms 652 KB Output is correct
23 Correct 3 ms 652 KB Output is correct
24 Correct 3 ms 652 KB Output is correct
25 Correct 3 ms 652 KB Output is correct
26 Correct 3 ms 652 KB Output is correct
27 Correct 3 ms 652 KB Output is correct
28 Correct 3 ms 652 KB Output is correct
29 Correct 3 ms 652 KB Output is correct
30 Correct 3 ms 652 KB Output is correct
31 Correct 3 ms 652 KB Output is correct
32 Correct 3 ms 652 KB Output is correct
33 Correct 921 ms 49792 KB Output is correct
34 Correct 922 ms 49800 KB Output is correct
35 Correct 903 ms 49744 KB Output is correct
36 Correct 901 ms 49784 KB Output is correct
37 Correct 898 ms 49896 KB Output is correct
38 Correct 954 ms 49724 KB Output is correct
39 Correct 894 ms 49852 KB Output is correct
40 Correct 921 ms 49812 KB Output is correct
41 Correct 939 ms 49788 KB Output is correct
42 Correct 957 ms 49784 KB Output is correct
43 Correct 896 ms 49768 KB Output is correct
44 Correct 905 ms 49780 KB Output is correct
45 Correct 914 ms 49808 KB Output is correct
46 Correct 887 ms 49808 KB Output is correct
47 Correct 900 ms 49772 KB Output is correct
48 Correct 904 ms 49780 KB Output is correct
49 Correct 905 ms 49700 KB Output is correct
50 Correct 899 ms 49828 KB Output is correct
51 Correct 909 ms 49760 KB Output is correct
52 Correct 900 ms 49808 KB Output is correct
53 Correct 908 ms 49780 KB Output is correct
54 Correct 875 ms 49784 KB Output is correct
55 Correct 899 ms 49772 KB Output is correct
56 Correct 920 ms 49736 KB Output is correct
57 Correct 903 ms 49800 KB Output is correct
58 Correct 899 ms 49764 KB Output is correct
59 Correct 915 ms 49812 KB Output is correct
60 Correct 896 ms 49816 KB Output is correct
61 Correct 900 ms 49808 KB Output is correct
62 Correct 900 ms 49860 KB Output is correct
63 Correct 913 ms 49808 KB Output is correct
64 Correct 893 ms 49748 KB Output is correct
65 Correct 906 ms 49832 KB Output is correct
66 Correct 921 ms 49772 KB Output is correct
67 Correct 900 ms 49784 KB Output is correct
68 Correct 922 ms 49808 KB Output is correct
69 Correct 903 ms 49852 KB Output is correct
70 Correct 893 ms 49808 KB Output is correct
71 Correct 891 ms 49828 KB Output is correct
72 Correct 912 ms 49736 KB Output is correct
73 Correct 897 ms 49824 KB Output is correct
74 Correct 909 ms 49832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 652 KB Output is correct
2 Correct 1 ms 636 KB Output is correct
3 Correct 1 ms 652 KB Output is correct
4 Correct 1 ms 652 KB Output is correct
5 Correct 1 ms 652 KB Output is correct
6 Correct 1 ms 652 KB Output is correct
7 Correct 1 ms 652 KB Output is correct
8 Correct 1 ms 644 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 644 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 452 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 3 ms 636 KB Output is correct
17 Correct 3 ms 652 KB Output is correct
18 Correct 3 ms 652 KB Output is correct
19 Correct 3 ms 652 KB Output is correct
20 Correct 3 ms 636 KB Output is correct
21 Correct 3 ms 652 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 652 KB Output is correct
24 Correct 3 ms 652 KB Output is correct
25 Correct 3 ms 652 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 3 ms 732 KB Output is correct
37 Correct 3 ms 652 KB Output is correct
38 Correct 3 ms 652 KB Output is correct
39 Correct 3 ms 652 KB Output is correct
40 Correct 3 ms 652 KB Output is correct
41 Correct 3 ms 652 KB Output is correct
42 Correct 3 ms 652 KB Output is correct
43 Correct 3 ms 652 KB Output is correct
44 Correct 3 ms 652 KB Output is correct
45 Correct 3 ms 652 KB Output is correct
46 Correct 3 ms 652 KB Output is correct
47 Correct 3 ms 652 KB Output is correct
48 Correct 921 ms 49792 KB Output is correct
49 Correct 922 ms 49800 KB Output is correct
50 Correct 903 ms 49744 KB Output is correct
51 Correct 901 ms 49784 KB Output is correct
52 Correct 898 ms 49896 KB Output is correct
53 Correct 954 ms 49724 KB Output is correct
54 Correct 894 ms 49852 KB Output is correct
55 Correct 921 ms 49812 KB Output is correct
56 Correct 939 ms 49788 KB Output is correct
57 Correct 957 ms 49784 KB Output is correct
58 Correct 896 ms 49768 KB Output is correct
59 Correct 905 ms 49780 KB Output is correct
60 Correct 914 ms 49808 KB Output is correct
61 Correct 887 ms 49808 KB Output is correct
62 Correct 900 ms 49772 KB Output is correct
63 Correct 904 ms 49780 KB Output is correct
64 Correct 905 ms 49700 KB Output is correct
65 Correct 899 ms 49828 KB Output is correct
66 Correct 909 ms 49760 KB Output is correct
67 Correct 900 ms 49808 KB Output is correct
68 Correct 908 ms 49780 KB Output is correct
69 Correct 875 ms 49784 KB Output is correct
70 Correct 899 ms 49772 KB Output is correct
71 Correct 920 ms 49736 KB Output is correct
72 Correct 903 ms 49800 KB Output is correct
73 Correct 899 ms 49764 KB Output is correct
74 Correct 915 ms 49812 KB Output is correct
75 Correct 896 ms 49816 KB Output is correct
76 Correct 900 ms 49808 KB Output is correct
77 Correct 900 ms 49860 KB Output is correct
78 Correct 913 ms 49808 KB Output is correct
79 Correct 893 ms 49748 KB Output is correct
80 Correct 906 ms 49832 KB Output is correct
81 Correct 921 ms 49772 KB Output is correct
82 Correct 900 ms 49784 KB Output is correct
83 Correct 922 ms 49808 KB Output is correct
84 Correct 903 ms 49852 KB Output is correct
85 Correct 893 ms 49808 KB Output is correct
86 Correct 891 ms 49828 KB Output is correct
87 Correct 912 ms 49736 KB Output is correct
88 Correct 897 ms 49824 KB Output is correct
89 Correct 909 ms 49832 KB Output is correct
90 Correct 891 ms 49736 KB Output is correct
91 Correct 900 ms 49788 KB Output is correct
92 Correct 906 ms 49772 KB Output is correct
93 Correct 895 ms 49808 KB Output is correct
94 Correct 891 ms 49808 KB Output is correct
95 Correct 893 ms 49736 KB Output is correct
96 Correct 893 ms 49784 KB Output is correct
97 Correct 899 ms 49808 KB Output is correct
98 Correct 907 ms 49744 KB Output is correct
99 Correct 901 ms 49812 KB Output is correct
100 Correct 588 ms 55504 KB Output is correct
101 Correct 596 ms 55620 KB Output is correct
102 Correct 589 ms 55492 KB Output is correct
103 Correct 606 ms 55512 KB Output is correct
104 Correct 594 ms 55492 KB Output is correct
105 Correct 704 ms 54272 KB Output is correct
106 Correct 701 ms 54212 KB Output is correct
107 Correct 709 ms 54148 KB Output is correct
108 Correct 706 ms 54260 KB Output is correct
109 Correct 700 ms 54212 KB Output is correct
110 Correct 651 ms 49832 KB Output is correct
111 Correct 647 ms 49804 KB Output is correct
112 Correct 643 ms 49828 KB Output is correct
113 Correct 664 ms 49808 KB Output is correct
114 Correct 650 ms 49700 KB Output is correct
115 Correct 651 ms 49820 KB Output is correct
116 Correct 644 ms 49796 KB Output is correct
117 Correct 659 ms 49808 KB Output is correct
118 Correct 655 ms 49784 KB Output is correct
119 Correct 649 ms 49808 KB Output is correct
120 Correct 1 ms 340 KB Output is correct
121 Correct 1 ms 460 KB Output is correct
122 Correct 895 ms 49852 KB Output is correct
123 Correct 904 ms 49808 KB Output is correct
124 Correct 911 ms 49820 KB Output is correct
125 Correct 896 ms 49808 KB Output is correct
126 Correct 896 ms 49812 KB Output is correct
127 Correct 898 ms 49780 KB Output is correct
128 Correct 906 ms 49788 KB Output is correct
129 Correct 901 ms 49736 KB Output is correct
130 Correct 892 ms 49784 KB Output is correct
131 Correct 892 ms 49784 KB Output is correct
132 Correct 876 ms 49748 KB Output is correct
133 Correct 876 ms 49736 KB Output is correct