Submission #102673

#TimeUsernameProblemLanguageResultExecution timeMemory
102673MinnakhmetovBulldozer (JOI17_bulldozer)C++14
25 / 100
2069 ms282804 KiB
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
#pragma warning(disable : 4996)

ll gcd(ll a, ll b) {
	return a ? gcd(b % a, a) : b;
}

struct P {
	ll x, y, w;
	void normalize() {
		int g = gcd(x, y);
		x /= g;
		y /= g;
		if (y < 0 || y == 0 && x < 0) {
			x = -x;
			y = -y;
		}
	}
	bool operator < (const P &oth) const {
		int q1 = x <= 0,
			q2 = oth.x <= 0;
		return q1 == q2 ? (x * oth.y - y * oth.x > 0) : q1 < q2;
	}
	bool operator == (const P &oth) const {
		return x == oth.x && y == oth.y;
	}
};

struct Line {
	ll a, b, c;
	int i, j;
	P orth_vec;
	Line() {}
	Line(P &p, P &q, int i, int j) : i(i), j(j) {
		a = p.y - q.y;
		b = q.x - p.x;
		c = -p.y * (q.x - p.x) + (q.y - p.y) * p.x;

		ll g = gcd(gcd(a, b), c);

		a /= g;
		b /= g;
		c /= g;

		if (a < 0 || a == 0 && b < 0) {
			a = -a;
			b = -b;
			c = -c;
		}

		orth_vec = { a, b };
		orth_vec.normalize();
	}
	bool operator < (const Line &oth) const {
		if (!(orth_vec == oth.orth_vec))
			return orth_vec < oth.orth_vec;
		if (a == oth.a)
			return b == oth.b ? c < oth.c : b < oth.b;
		return a < oth.a;
	}
	bool operator == (const Line &oth) const {
		return a == oth.a && b == oth.b && c == oth.c;
	}
};

const ll INF = 1e18;
const int N = 2005;
P a[N];
ll t[N * 4], mnp[N * 4], mxp[N * 4], pref[N], up[N * 4];
int pos[N];
int n;

Line b[N * N];
vector<int> lt[N * N];
vector<pair<int, int>> ev;
P d[N * N];

void mrg(int v) {
	t[v] = max(t[v * 2], t[v * 2 + 1]);
	t[v] = max(t[v], mxp[v * 2 + 1] - mnp[v * 2]);
	mxp[v] = max(mxp[v * 2], mxp[v * 2 + 1]);
	mnp[v] = min(mnp[v * 2], mnp[v * 2 + 1]);
}

void build(int v = 1, int L = 0, int R = n) {
	if (L == R) {
		t[v] = 0;
		mxp[v] = mnp[v] = pref[L];
	}
	else {
		int m = (L + R) >> 1;
		build(v * 2, L, m);
		build(v * 2 + 1, m + 1, R);
		mrg(v);
	}
}

void push(int v, int L, int R) {
	if (up[v]) {
		if (L != R) {
			up[v * 2] += up[v];
			up[v * 2 + 1] += up[v];
		}
		mnp[v] += up[v];
		mxp[v] += up[v];
		up[v] = 0;
	}
}

void upd(int l, int r, int x, int v = 1, int L = 0, int R = n) {
	push(v, L, R);
	if (l > r)
		return;
	if (l == L && r == R) {
		up[v] += x;
		push(v, L, R);
	}
	else {
		int m = (L + R) >> 1;
		upd(l, min(m, r), x, v * 2, L, m);
		upd(max(m + 1, l), r, x, v * 2 + 1, m + 1, R);
		mrg(v);
	}
}

//void print() {
//	P tmp[N];
//	for (int i = 1; i <= n; i++) {
//		tmp[pos[i]] = a[i];
//	}
//	for (int i = 1; i <= n; i++)
//		cout << tmp[i].w << " ";
//	cout << "\n";
//	for (int i = 1; i <= n; i++)
//		cout << tmp[i].x << " " << tmp[i].y << "\n";
//	cout << "\n";
//}

signed main() {
#ifdef HOME
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 1; i <= n; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		a[i] = { x, y, w };
	}

	sort(a + 1, a + n + 1, [](P &a, P &b) {
		return a.x == b.x ? a.y > b.y : a.x < b.x;
	});

	int ct = 0;
	for (int i = 1; i <= n; i++) {
		pos[i] = i;
		pref[i] = pref[i - 1] + a[i].w;
		for (int j = i + 1; j <= n; j++) {
			b[ct++] = Line(a[i], a[j], i, j);
		}
	}

	sort(b, b + ct);

	int ctd = 0, ctl = 0;
	for (int i = 0, j = 0; i < ct; i++) {
		bool fl = i == 0;
		if (i > 0) {
			if (!(b[i - 1] == b[i])) {
				ctl++;
				fl = true;
			}
			if (!(b[i - 1].orth_vec == b[i].orth_vec)) {
				j++;
				fl = true;
			}
		}
		if (fl)
			ev.push_back({ j, ctl });
		lt[ctl].push_back(b[i].i);
		lt[ctl].push_back(b[i].j);
	}
	ctl++;

	for (int i = 0; i < ctl; i++) {
		sort(all(lt[i]));
		lt[i].erase(unique(all(lt[i])), lt[i].end());
	}

	build();

	ll ans = t[1];

	for (int i = 0; i < ev.size(); i++) {
		int lb = N, rb = -1;
		for (int x : lt[ev[i].second]) {
			lb = min(lb, pos[x]);
			rb = max(rb, pos[x]);
			upd(pos[x], n, -a[x].w);
		}
		for (int x : lt[ev[i].second]) {
			pos[x] = rb - pos[x] + lb;
			upd(pos[x], n, a[x].w);
		}

		if (i == ev.size() - 1 || ev[i + 1].first != ev[i].first)
			ans = max(ans, t[1]);
	}

	cout << ans;

	return 0;
}

Compilation message (stderr)

bulldozer.cpp:41:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable : 4996)
 
bulldozer.cpp: In member function 'void P::normalize()':
bulldozer.cpp:53:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   if (y < 0 || y == 0 && x < 0) {
                ~~~~~~~^~~~~~~~
bulldozer.cpp: In constructor 'Line::Line(P&, P&, int, int)':
bulldozer.cpp:84:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   if (a < 0 || a == 0 && b < 0) {
                ~~~~~~~^~~~~~~~
bulldozer.cpp: In function 'int main()':
bulldozer.cpp:238:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < ev.size(); i++) {
                  ~~^~~~~~~~~~~
bulldozer.cpp:250:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i == ev.size() - 1 || ev[i + 1].first != ev[i].first)
       ~~^~~~~~~~~~~~~~~~
bulldozer.cpp:209:6: warning: unused variable 'ctd' [-Wunused-variable]
  int ctd = 0, ctl = 0;
      ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...