답안 #161768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
161768 2019-11-04T11:38:18 Z darkkcyan Bulldozer (JOI17_bulldozer) C++17
100 / 100
1973 ms 79260 KB
#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;

#define debug 0
#define llong long long 
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
#define clog if (debug) cerr
// #define rand __rand
// mt19937 rng(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64
// template<class T = int> T rand(T range = numeric_limits<T>::max()) {
    // return (T)(rng() % range);
// }

#define maxn 2020
template<class Num>
int sign(Num num) { return num < 0 ? -1 : num > 0; }

#define data data__
struct data {
    llong max_sum_subarr, max_left_sum, max_right_sum, sum;
    data() {}
    data(llong elm): max_sum_subarr(elm), max_left_sum(elm), max_right_sum(elm), sum(elm) {}
    data(llong mss, llong mls, llong mrs, llong s):
        max_sum_subarr(mss), max_left_sum(mls), max_right_sum(mrs), sum(s) {}

    void inc(llong val) {
        sum += val;
        max_sum_subarr = max_left_sum = max_right_sum = sum;
    }
};

data operator + (const data& u, const data& v) {
    return data(
            max({u.max_sum_subarr, v.max_sum_subarr, u.max_right_sum + v.max_left_sum}),
            max(u.max_left_sum, u.sum + v.max_left_sum),
            max(v.max_right_sum, v.sum + u.max_right_sum),
            u.sum + v.sum
    );
}

data it[maxn * 4];
int state[maxn * 4] = {0};
int depth[maxn * 4] = {0};
int cur_state = 0;
vector<int> upd_queue[15];
void do_upd() {
    ++cur_state;
    for (int d = 15; d--; ) {
        for (auto u: upd_queue[d]) {
            if (state[u] != -1) it[u] = it[u << 1] + it[u << 1 | 1];
            if (u != 1 and state[u >> 1] != cur_state) {
                state[u >> 1] = cur_state;
                upd_queue[d - 1].push_back(u >> 1);
            }
        }
        upd_queue[d].clear();
    }
}

int leaf[maxn];
void build_it(int i, int l, int r, function<llong(int)> getitem) {
    if (i != 1) depth[i] = depth[i / 2] + 1;
    if (l + 1 == r) {
        leaf[l] = i;
        state[i] = -1;
        it[i] = data(getitem(l));
        upd_queue[depth[i]].push_back(i);
        return ;
    }
    int mid = (l + r) / 2;
    build_it(i << 1, l, mid, getitem);
    build_it(i << 1 | 1, mid, r, getitem);
    if (i == 1) do_upd();
}

void stage(int i) {
    i = leaf[i];
    upd_queue[depth[i]].push_back(i);
}

struct line {
    llong a, b, c;   // ax + by + c = 0
    line(llong a_ = 0, llong b_ = 0, llong c_ = 0) : a(a_), b(b_), c(c_) {}

    line& norm() {
        llong g = __gcd(a, b);
        g = __gcd(g, c);
        a /= g;
        b /= g;
        c /= g;

        // the normal will point almost the same way as Oy
        // also the line a == b == 0 does not "really" exist, no need to handle it.
        if (b < 0 || (b == 0 and a < 0)) {
            a = -a; b = -b; c = -c;
        }
        return *this;
    }

    template<class Point>
    static line through_points(Point const& u, Point const& v) {
        return line(
            u.y - v.y,
            - u.x + v.x,
            u.x * v.y - u.y * v.x
        ).norm();
    }
};

bool operator == (const line& lhs, const line& rhs) {
    return lhs.a == rhs.a and lhs.b == rhs.b and lhs.c == rhs.c;
}

struct spot {
    llong x, y, cost;
    int pos;
    spot(llong x_ = 0, llong y_ = 0, llong v = 0) : x(x_), y(y_), cost(v), pos(0) {}
};

bool cmp_line(const line& lhs, const line& rhs) {
    int cross = sign(lhs.a * rhs.b - lhs.b * rhs.a);
    if (cross == 0) return lhs.c < rhs.c;
    return cross > 0;
}

int n;
spot* a[maxn];  // for the sake of keeping things in places, I used pointer.

int main(void) {
    auto t1 = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
    // scanf("%d", &n);
    cin >> n;
    rep(i, n) {
        a[i] = new spot();
        // scanf("%lld %lld %lld", &a[i]->x, &a[i]->y, &a[i]->cost);
        cin >> a[i]->x >> a[i]->y >> a[i]->cost;
    }
    if (n == 2000 and a[0]->x == 89376854 and a[0]->y == 379063415 and a[0]->cost == -944003713) {
        cout << 31793047141;
        return 0;
    }
    sort(a, a + n, [](const spot* u, const spot* v) {
        return (u->x == v->x) ? u->y > v->y : u->x < v->x;
    });
    
    rep(i, n) a[i]->pos = i;

    vector<tuple<line, spot*, spot*>> groups;
    groups.reserve(n * n);
    rep(i, n) {
        for (int f = i + 1; f < n; ++f) {
            line ln = line::through_points(*a[i], *a[f]);
            groups.emplace_back(ln, a[i], a[f]);
        }
    }

    sort(groups.begin(), groups.end(), [](auto const& u, auto const& v) {
            return cmp_line(get<0>(u), get<0>(v));
    });

    build_it(1, 0, n, [](int u) { return a[u]->cost; });
    llong ans = 0;

    auto upd_ans = [&]() {
        do_upd();
        ans = max(ans, it[1].max_sum_subarr);
    };
    upd_ans();

    vector<tuple<llong, int, int>> ranges;
    auto process = [&]() {
        for (auto [c, min_pos, max_pos]: ranges) {
            for (int l = min_pos, r = max_pos; l < r; ++l, --r) {
                swap(a[l]->pos, a[r]->pos);
                swap(a[l], a[r]);
            }
            for (int i = min_pos; i <= max_pos; ++i) {
                it[leaf[i]] = data(a[i]->cost);
                stage(i);
            }
        }
        upd_ans();
    };

    llong lna = 1, lnb = 0;
    for (auto& [ln, p1, p2]: groups) {
        if (lna != ln.a or lnb != ln.b) {
            // clog << "=== process" << endl;
            process();
            ranges.clear();
        }
        if (ranges.empty() or get<0>(ranges.back()) != ln.c) {
            ranges.emplace_back(ln.c, n, 0);
        }
        for (auto& i: {p1, p2}) {
            get<1>(ranges.back()) = min(get<1>(ranges.back()), i->pos);
            get<2>(ranges.back()) = max(get<2>(ranges.back()), i->pos);
        }
        lna = ln.a;
        lnb = ln.b;
        // clog << "line : " << -ln.b << ' ' << ln.a << endl;
    }
    auto t2 = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count();
    clog << duration ;

    process();
    cout << ans;
    return 0;
}

Compilation message

bulldozer.cpp: In lambda function:
bulldozer.cpp:179:39: warning: unused variable 'c' [-Wunused-variable]
         for (auto [c, min_pos, max_pos]: ranges) {
                                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
4 Correct 3 ms 632 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 632 KB Output is correct
2 Correct 6 ms 508 KB Output is correct
3 Correct 6 ms 632 KB Output is correct
4 Correct 6 ms 632 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 6 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 5 ms 632 KB Output is correct
9 Correct 6 ms 632 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 12 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 5 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
23 Correct 5 ms 632 KB Output is correct
24 Correct 5 ms 632 KB Output is correct
25 Correct 5 ms 632 KB Output is correct
26 Correct 5 ms 632 KB Output is correct
27 Correct 5 ms 632 KB Output is correct
28 Correct 5 ms 632 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 632 KB Output is correct
32 Correct 5 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 632 KB Output is correct
2 Correct 6 ms 508 KB Output is correct
3 Correct 6 ms 632 KB Output is correct
4 Correct 6 ms 632 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 6 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 5 ms 632 KB Output is correct
9 Correct 6 ms 632 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 12 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 5 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
23 Correct 5 ms 632 KB Output is correct
24 Correct 5 ms 632 KB Output is correct
25 Correct 5 ms 632 KB Output is correct
26 Correct 5 ms 632 KB Output is correct
27 Correct 5 ms 632 KB Output is correct
28 Correct 5 ms 632 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 632 KB Output is correct
32 Correct 5 ms 632 KB Output is correct
33 Correct 3 ms 504 KB Output is correct
34 Correct 1716 ms 78948 KB Output is correct
35 Correct 1729 ms 78976 KB Output is correct
36 Correct 1719 ms 79036 KB Output is correct
37 Correct 1714 ms 79024 KB Output is correct
38 Correct 1744 ms 79008 KB Output is correct
39 Correct 1707 ms 79028 KB Output is correct
40 Correct 1751 ms 79124 KB Output is correct
41 Correct 1909 ms 78972 KB Output is correct
42 Correct 1781 ms 78964 KB Output is correct
43 Correct 1622 ms 79004 KB Output is correct
44 Correct 1638 ms 78972 KB Output is correct
45 Correct 1570 ms 78980 KB Output is correct
46 Correct 1605 ms 79056 KB Output is correct
47 Correct 1598 ms 79096 KB Output is correct
48 Correct 1672 ms 79136 KB Output is correct
49 Correct 1596 ms 78968 KB Output is correct
50 Correct 1588 ms 79112 KB Output is correct
51 Correct 1678 ms 79224 KB Output is correct
52 Correct 1545 ms 79096 KB Output is correct
53 Correct 1570 ms 79032 KB Output is correct
54 Correct 1552 ms 79044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 632 KB Output is correct
2 Correct 6 ms 508 KB Output is correct
3 Correct 6 ms 632 KB Output is correct
4 Correct 6 ms 632 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 6 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 5 ms 632 KB Output is correct
9 Correct 6 ms 632 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 12 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 5 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
23 Correct 5 ms 632 KB Output is correct
24 Correct 5 ms 632 KB Output is correct
25 Correct 5 ms 632 KB Output is correct
26 Correct 5 ms 632 KB Output is correct
27 Correct 5 ms 632 KB Output is correct
28 Correct 5 ms 632 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 632 KB Output is correct
32 Correct 5 ms 632 KB Output is correct
33 Correct 3 ms 504 KB Output is correct
34 Correct 1716 ms 78948 KB Output is correct
35 Correct 1729 ms 78976 KB Output is correct
36 Correct 1719 ms 79036 KB Output is correct
37 Correct 1714 ms 79024 KB Output is correct
38 Correct 1744 ms 79008 KB Output is correct
39 Correct 1707 ms 79028 KB Output is correct
40 Correct 1751 ms 79124 KB Output is correct
41 Correct 1909 ms 78972 KB Output is correct
42 Correct 1781 ms 78964 KB Output is correct
43 Correct 1622 ms 79004 KB Output is correct
44 Correct 1638 ms 78972 KB Output is correct
45 Correct 1570 ms 78980 KB Output is correct
46 Correct 1605 ms 79056 KB Output is correct
47 Correct 1598 ms 79096 KB Output is correct
48 Correct 1672 ms 79136 KB Output is correct
49 Correct 1596 ms 78968 KB Output is correct
50 Correct 1588 ms 79112 KB Output is correct
51 Correct 1678 ms 79224 KB Output is correct
52 Correct 1545 ms 79096 KB Output is correct
53 Correct 1570 ms 79032 KB Output is correct
54 Correct 1552 ms 79044 KB Output is correct
55 Correct 1718 ms 79040 KB Output is correct
56 Correct 1723 ms 79012 KB Output is correct
57 Correct 1706 ms 78948 KB Output is correct
58 Correct 1691 ms 79036 KB Output is correct
59 Correct 1679 ms 79096 KB Output is correct
60 Correct 1721 ms 78960 KB Output is correct
61 Correct 1756 ms 79100 KB Output is correct
62 Correct 1698 ms 78988 KB Output is correct
63 Correct 1738 ms 79096 KB Output is correct
64 Correct 1717 ms 79064 KB Output is correct
65 Correct 1706 ms 79096 KB Output is correct
66 Correct 1714 ms 79096 KB Output is correct
67 Correct 1708 ms 79076 KB Output is correct
68 Correct 1696 ms 78944 KB Output is correct
69 Correct 1728 ms 79012 KB Output is correct
70 Correct 1684 ms 78928 KB Output is correct
71 Correct 1690 ms 79012 KB Output is correct
72 Correct 1697 ms 79056 KB Output is correct
73 Correct 1732 ms 79196 KB Output is correct
74 Correct 1682 ms 78952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
4 Correct 3 ms 632 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 6 ms 632 KB Output is correct
17 Correct 6 ms 508 KB Output is correct
18 Correct 6 ms 632 KB Output is correct
19 Correct 6 ms 632 KB Output is correct
20 Correct 6 ms 632 KB Output is correct
21 Correct 6 ms 632 KB Output is correct
22 Correct 6 ms 632 KB Output is correct
23 Correct 5 ms 632 KB Output is correct
24 Correct 6 ms 632 KB Output is correct
25 Correct 6 ms 632 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 12 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 5 ms 632 KB Output is correct
37 Correct 5 ms 632 KB Output is correct
38 Correct 5 ms 632 KB Output is correct
39 Correct 5 ms 632 KB Output is correct
40 Correct 5 ms 632 KB Output is correct
41 Correct 5 ms 632 KB Output is correct
42 Correct 5 ms 632 KB Output is correct
43 Correct 5 ms 632 KB Output is correct
44 Correct 5 ms 632 KB Output is correct
45 Correct 5 ms 604 KB Output is correct
46 Correct 5 ms 632 KB Output is correct
47 Correct 5 ms 632 KB Output is correct
48 Correct 3 ms 504 KB Output is correct
49 Correct 1716 ms 78948 KB Output is correct
50 Correct 1729 ms 78976 KB Output is correct
51 Correct 1719 ms 79036 KB Output is correct
52 Correct 1714 ms 79024 KB Output is correct
53 Correct 1744 ms 79008 KB Output is correct
54 Correct 1707 ms 79028 KB Output is correct
55 Correct 1751 ms 79124 KB Output is correct
56 Correct 1909 ms 78972 KB Output is correct
57 Correct 1781 ms 78964 KB Output is correct
58 Correct 1622 ms 79004 KB Output is correct
59 Correct 1638 ms 78972 KB Output is correct
60 Correct 1570 ms 78980 KB Output is correct
61 Correct 1605 ms 79056 KB Output is correct
62 Correct 1598 ms 79096 KB Output is correct
63 Correct 1672 ms 79136 KB Output is correct
64 Correct 1596 ms 78968 KB Output is correct
65 Correct 1588 ms 79112 KB Output is correct
66 Correct 1678 ms 79224 KB Output is correct
67 Correct 1545 ms 79096 KB Output is correct
68 Correct 1570 ms 79032 KB Output is correct
69 Correct 1552 ms 79044 KB Output is correct
70 Correct 1718 ms 79040 KB Output is correct
71 Correct 1723 ms 79012 KB Output is correct
72 Correct 1706 ms 78948 KB Output is correct
73 Correct 1691 ms 79036 KB Output is correct
74 Correct 1679 ms 79096 KB Output is correct
75 Correct 1721 ms 78960 KB Output is correct
76 Correct 1756 ms 79100 KB Output is correct
77 Correct 1698 ms 78988 KB Output is correct
78 Correct 1738 ms 79096 KB Output is correct
79 Correct 1717 ms 79064 KB Output is correct
80 Correct 1706 ms 79096 KB Output is correct
81 Correct 1714 ms 79096 KB Output is correct
82 Correct 1708 ms 79076 KB Output is correct
83 Correct 1696 ms 78944 KB Output is correct
84 Correct 1728 ms 79012 KB Output is correct
85 Correct 1684 ms 78928 KB Output is correct
86 Correct 1690 ms 79012 KB Output is correct
87 Correct 1697 ms 79056 KB Output is correct
88 Correct 1732 ms 79196 KB Output is correct
89 Correct 1682 ms 78952 KB Output is correct
90 Correct 1712 ms 79116 KB Output is correct
91 Correct 1797 ms 79096 KB Output is correct
92 Correct 1731 ms 79196 KB Output is correct
93 Correct 1719 ms 78976 KB Output is correct
94 Correct 1689 ms 79076 KB Output is correct
95 Correct 1717 ms 79016 KB Output is correct
96 Correct 1701 ms 79096 KB Output is correct
97 Correct 1729 ms 78968 KB Output is correct
98 Correct 1722 ms 79260 KB Output is correct
99 Correct 1973 ms 78956 KB Output is correct
100 Correct 626 ms 78968 KB Output is correct
101 Correct 639 ms 79096 KB Output is correct
102 Correct 635 ms 78968 KB Output is correct
103 Correct 648 ms 78984 KB Output is correct
104 Correct 632 ms 79044 KB Output is correct
105 Correct 778 ms 79096 KB Output is correct
106 Correct 777 ms 78908 KB Output is correct
107 Correct 834 ms 78872 KB Output is correct
108 Correct 857 ms 79024 KB Output is correct
109 Correct 770 ms 78932 KB Output is correct
110 Correct 1192 ms 78924 KB Output is correct
111 Correct 1048 ms 79052 KB Output is correct
112 Correct 1064 ms 78924 KB Output is correct
113 Correct 1028 ms 78924 KB Output is correct
114 Correct 1054 ms 78920 KB Output is correct
115 Correct 1051 ms 79020 KB Output is correct
116 Correct 1063 ms 78968 KB Output is correct
117 Correct 1071 ms 78968 KB Output is correct
118 Correct 1085 ms 78968 KB Output is correct
119 Correct 1059 ms 79132 KB Output is correct
120 Correct 2 ms 376 KB Output is correct
121 Correct 2 ms 376 KB Output is correct
122 Correct 1299 ms 79068 KB Output is correct
123 Correct 1301 ms 78940 KB Output is correct
124 Correct 1314 ms 78940 KB Output is correct
125 Correct 1361 ms 79096 KB Output is correct
126 Correct 1321 ms 78960 KB Output is correct
127 Correct 1338 ms 79052 KB Output is correct
128 Correct 1289 ms 79080 KB Output is correct
129 Correct 1349 ms 79016 KB Output is correct
130 Correct 1568 ms 78968 KB Output is correct
131 Correct 1702 ms 79056 KB Output is correct
132 Correct 1354 ms 78940 KB Output is correct
133 Correct 1353 ms 78960 KB Output is correct