Submission #102723

#TimeUsernameProblemLanguageResultExecution timeMemory
102723MinnakhmetovBulldozer (JOI17_bulldozer)C++14
100 / 100
1182 ms16476 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;
	ll operator * (const P &oth) const {
		return x * oth.y - y * oth.x;
	}
	ll operator / (const P &oth) const {
		return x * oth.x + y * oth.y;
	}
};

P operator - (const P &a, const P &b) {
	return { a.x - b.x, a.y - b.y };
}

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;
pair<int, int> z[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);
	}
}

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++) {
			z[ct] = { i, j };
			if (a[j].x > a[i].x || a[j].x == a[i].x && a[j].y < a[i].y) {
				swap(z[ct].first, z[ct].second);
			}
			ct++;
		}
	}

	build();

	sort(z, z + ct, [](pair<int, int> &p, pair<int, int> &q) {
		P vp = a[p.second] - a[p.first], vq = a[q.second] - a[q.first];
		ll prod = vp * vq;
		if (prod != 0)
			return prod > 0;
		prod = (a[p.second] - a[p.first]) / (a[q.first] - a[p.first]);
		if (prod != 0)
			return prod > 0;
		return abs(vp.x) < abs(vq.x) || abs(vp.y) < abs(vq.y);
	});;


	ll ans = t[1];

	for (int i = 0; i < ct; i++) {
		int x = z[i].first,
			y = z[i].second;
		if (pos[x] > pos[y]) {
			swap(x, y);
		}

		upd(pos[x], pos[y] - 1, -a[x].w);
		upd(pos[x], pos[y] - 1, a[y].w);

		swap(pos[x], pos[y]);

		if (i == ct - 1 || (a[z[i + 1].second] - a[z[i + 1].first]) *
			(a[y] - a[x]) != 0) {
			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 function 'int main()':
bulldozer.cpp:142:44: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
    if (a[j].x > a[i].x || a[j].x == a[i].x && a[j].y < a[i].y) {
                           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
#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...