#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);
// }
template<class Num>
int sign(Num num) { return num < 0 ? -1 : num > 0; }
struct itnode {
llong max_sum_subarr, max_left_sum, max_right_sum, sum;
itnode *lchild, *rchild;
int l, r; // [l; r)
itnode(int l_, int r_, function<llong(int)> getitem)
: lchild(0), rchild(0), l(l_), r(r_)
{
if (l + 1 == r) {
assign(getitem(l));
}
if (l + 1 >= r) return ;
int mid = (l + r) / 2;
lchild = new itnode(l, mid, getitem);
rchild = new itnode(mid, r, getitem);
merge();
}
~itnode() {
if (!lchild) return ;
delete lchild; delete rchild;
}
void assign(llong val) {
sum = val;
max_sum_subarr = sum;
max_right_sum = max_left_sum = max(sum, 0ll);
}
void merge() {
if (!lchild || !rchild) return ;
sum = lchild->sum + rchild->sum;
max_sum_subarr = max({lchild->max_sum_subarr, rchild->max_sum_subarr, lchild->max_right_sum + rchild->max_left_sum});
max_left_sum = max(lchild->max_left_sum, lchild->sum + rchild->max_left_sum);
max_right_sum = max(rchild->max_right_sum, rchild->sum + lchild->max_right_sum);
}
void inc(int pos, llong val) {
if (pos < l or pos >= r) return ;
if (l + 1 == r) {
assign(sum + val);
return ;
}
lchild->inc(pos, val);
rchild->inc(pos, val);
merge();
}
void dolog() {
if (!debug) return ;
if (!lchild) {
// clog << setw(4) << sum << ' ';
} else {
lchild->dolog();
rchild->dolog();
}
}
};
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;
}
#define maxn 2020
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) {}
};
struct cmp_line {
bool operator()(const line& lhs, const line& rhs) const {
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);
cin >> n;
rep(i, n) {
a[i] = new spot();
cin >> a[i]->x >> a[i]->y >> a[i]->cost;
}
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;
clog << "ok boomer" << endl;
vector<tuple<line, spot*, spot*>> groups;
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));
});
clog << "ok boomer" << endl;
itnode tree(0, n, [](int u) { return a[u]->cost; });
llong ans = 0;
auto upd_ans = [&]() {
// clog << setw(3) << ans << ' ' << setw(3) << tree.max_sum_subarr << ": ";
// tree.dolog(); clog << endl;
ans = max(ans, tree.max_sum_subarr);
};
upd_ans();
clog << "ok boomer" << endl;
vector<tuple<llong, int, int>> ranges;
auto process = [&]() {
for (auto [c, min_pos, max_pos]: ranges) {
llong sum = 0;
for (int i = min_pos + 1; i <= max_pos; ++i) {
tree.inc(i, -a[i]->cost);
sum += a[i]->cost;
}
tree.inc(min_pos, sum);
}
upd_ans();
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]);
}
llong sum = 0;
for (int i = min_pos + 1; i <= max_pos; ++i) {
sum += a[i]->cost;
tree.inc(i, a[i]->cost);
}
tree.inc(min_pos, -sum);
}
upd_ans();
};
line cur_line(1, 0, 0);
for (auto& [ln, p1, p2]: groups) {
if (cur_line.a != ln.a or cur_line.b != ln.b) {
// clog << "=== process" << endl;
process();
ranges.clear();
}
if (ranges.empty() or cur_line.c != 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);
}
cur_line = ln;
// clog << "line : " << -ln.b << ' ' << ln.a << endl;
}
process();
cout << ans;
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count();
clog << duration ;
return 0;
}
Compilation message
bulldozer.cpp: In lambda function:
bulldozer.cpp:173:19: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto [c, min_pos, max_pos]: ranges) {
^
bulldozer.cpp:173:39: warning: unused variable 'c' [-Wunused-variable]
for (auto [c, min_pos, max_pos]: ranges) {
^
bulldozer.cpp:182:19: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto [c, min_pos, max_pos]: ranges) {
^
bulldozer.cpp:182:39: warning: unused variable 'c' [-Wunused-variable]
for (auto [c, min_pos, max_pos]: ranges) {
^
bulldozer.cpp: In function 'int main()':
bulldozer.cpp:199:16: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto& [ln, p1, p2]: groups) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
4 |
Correct |
3 ms |
728 KB |
Output is correct |
5 |
Correct |
3 ms |
984 KB |
Output is correct |
6 |
Correct |
3 ms |
856 KB |
Output is correct |
7 |
Correct |
3 ms |
856 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Correct |
3 ms |
856 KB |
Output is correct |
10 |
Correct |
3 ms |
856 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 |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
728 KB |
Output is correct |
2 |
Correct |
7 ms |
728 KB |
Output is correct |
3 |
Correct |
6 ms |
856 KB |
Output is correct |
4 |
Correct |
6 ms |
856 KB |
Output is correct |
5 |
Correct |
6 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
856 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
7 ms |
856 KB |
Output is correct |
9 |
Correct |
7 ms |
728 KB |
Output is correct |
10 |
Correct |
7 ms |
856 KB |
Output is correct |
11 |
Correct |
2 ms |
252 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 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 |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
3 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
296 KB |
Output is correct |
21 |
Correct |
6 ms |
856 KB |
Output is correct |
22 |
Correct |
6 ms |
856 KB |
Output is correct |
23 |
Correct |
6 ms |
856 KB |
Output is correct |
24 |
Correct |
6 ms |
856 KB |
Output is correct |
25 |
Correct |
6 ms |
856 KB |
Output is correct |
26 |
Correct |
6 ms |
732 KB |
Output is correct |
27 |
Correct |
6 ms |
856 KB |
Output is correct |
28 |
Correct |
6 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
856 KB |
Output is correct |
30 |
Correct |
6 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
856 KB |
Output is correct |
32 |
Correct |
6 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
728 KB |
Output is correct |
2 |
Correct |
7 ms |
728 KB |
Output is correct |
3 |
Correct |
6 ms |
856 KB |
Output is correct |
4 |
Correct |
6 ms |
856 KB |
Output is correct |
5 |
Correct |
6 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
856 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
7 ms |
856 KB |
Output is correct |
9 |
Correct |
7 ms |
728 KB |
Output is correct |
10 |
Correct |
7 ms |
856 KB |
Output is correct |
11 |
Correct |
2 ms |
252 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 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 |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
3 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
296 KB |
Output is correct |
21 |
Correct |
6 ms |
856 KB |
Output is correct |
22 |
Correct |
6 ms |
856 KB |
Output is correct |
23 |
Correct |
6 ms |
856 KB |
Output is correct |
24 |
Correct |
6 ms |
856 KB |
Output is correct |
25 |
Correct |
6 ms |
856 KB |
Output is correct |
26 |
Correct |
6 ms |
732 KB |
Output is correct |
27 |
Correct |
6 ms |
856 KB |
Output is correct |
28 |
Correct |
6 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
856 KB |
Output is correct |
30 |
Correct |
6 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
856 KB |
Output is correct |
32 |
Correct |
6 ms |
852 KB |
Output is correct |
33 |
Execution timed out |
2055 ms |
82644 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
728 KB |
Output is correct |
2 |
Correct |
7 ms |
728 KB |
Output is correct |
3 |
Correct |
6 ms |
856 KB |
Output is correct |
4 |
Correct |
6 ms |
856 KB |
Output is correct |
5 |
Correct |
6 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
856 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
7 ms |
856 KB |
Output is correct |
9 |
Correct |
7 ms |
728 KB |
Output is correct |
10 |
Correct |
7 ms |
856 KB |
Output is correct |
11 |
Correct |
2 ms |
252 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 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 |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
3 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
296 KB |
Output is correct |
21 |
Correct |
6 ms |
856 KB |
Output is correct |
22 |
Correct |
6 ms |
856 KB |
Output is correct |
23 |
Correct |
6 ms |
856 KB |
Output is correct |
24 |
Correct |
6 ms |
856 KB |
Output is correct |
25 |
Correct |
6 ms |
856 KB |
Output is correct |
26 |
Correct |
6 ms |
732 KB |
Output is correct |
27 |
Correct |
6 ms |
856 KB |
Output is correct |
28 |
Correct |
6 ms |
856 KB |
Output is correct |
29 |
Correct |
6 ms |
856 KB |
Output is correct |
30 |
Correct |
6 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
856 KB |
Output is correct |
32 |
Correct |
6 ms |
852 KB |
Output is correct |
33 |
Execution timed out |
2055 ms |
82644 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
4 |
Correct |
3 ms |
728 KB |
Output is correct |
5 |
Correct |
3 ms |
984 KB |
Output is correct |
6 |
Correct |
3 ms |
856 KB |
Output is correct |
7 |
Correct |
3 ms |
856 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Correct |
3 ms |
856 KB |
Output is correct |
10 |
Correct |
3 ms |
856 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 |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
7 ms |
728 KB |
Output is correct |
17 |
Correct |
7 ms |
728 KB |
Output is correct |
18 |
Correct |
6 ms |
856 KB |
Output is correct |
19 |
Correct |
6 ms |
856 KB |
Output is correct |
20 |
Correct |
6 ms |
856 KB |
Output is correct |
21 |
Correct |
7 ms |
856 KB |
Output is correct |
22 |
Correct |
7 ms |
856 KB |
Output is correct |
23 |
Correct |
7 ms |
856 KB |
Output is correct |
24 |
Correct |
7 ms |
728 KB |
Output is correct |
25 |
Correct |
7 ms |
856 KB |
Output is correct |
26 |
Correct |
2 ms |
252 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
380 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 |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
380 KB |
Output is correct |
34 |
Correct |
3 ms |
380 KB |
Output is correct |
35 |
Correct |
2 ms |
296 KB |
Output is correct |
36 |
Correct |
6 ms |
856 KB |
Output is correct |
37 |
Correct |
6 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
856 KB |
Output is correct |
39 |
Correct |
6 ms |
856 KB |
Output is correct |
40 |
Correct |
6 ms |
856 KB |
Output is correct |
41 |
Correct |
6 ms |
732 KB |
Output is correct |
42 |
Correct |
6 ms |
856 KB |
Output is correct |
43 |
Correct |
6 ms |
856 KB |
Output is correct |
44 |
Correct |
6 ms |
856 KB |
Output is correct |
45 |
Correct |
6 ms |
856 KB |
Output is correct |
46 |
Correct |
6 ms |
856 KB |
Output is correct |
47 |
Correct |
6 ms |
852 KB |
Output is correct |
48 |
Execution timed out |
2055 ms |
82644 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |