답안 #446873

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446873 2021-07-23T15:46:38 Z flappybird Bulldozer (JOI17_bulldozer) C++14
100 / 100
1985 ms 33584 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 101010
#define ln '\n'
struct Point {
	ll x, y;
	ll c;
	Point() :x(0), y(0) {}
	Point(ll x, ll y) :x(x), y(y) {}
	Point(ll x, ll y, ll c) :x(x), y(y), c(c) {}
};
bool operator<(const Point& p1, const Point& p2) {
	if (p1.x == p2.x) return p1.y < p2.y;
	return p1.x < p2.x;
}
bool operator==(const Point& p1, const Point& p2) {
	return p1.x == p2.x && p1.y == p2.y;
}
struct Node {
	ll lmx, rmx;
	ll sum, mx;
	Node(ll lmx, ll rmx, ll sum, ll mx) :lmx(lmx), rmx(rmx), sum(sum), mx(mx) {}
	Node() {
		lmx = rmx = mx = sum = 0;
	}
};
Node operator+(const Node& n1, const Node& n2) {
	Node ret;
	ret.lmx = max(n1.lmx, n1.sum + n2.lmx);
	ret.rmx = max(n2.rmx, n2.sum + n1.rmx);
	ret.sum = n1.sum + n2.sum;
	ret.mx = max(max(n1.mx, n2.mx), n1.rmx + n2.lmx);
	return ret;
}
struct segtree {
	vector<Node> tree;
	vector<ll> l, r;
	ll N, s;
	void init(ll x = 1) {
		if (x >= s) {
			l[x] = r[x] = x - s;
			return;
		}
		init(x * 2);
		init(x * 2 + 1);
		l[x] = l[x * 2];
		r[x] = r[x * 2 + 1];
		tree[x] = tree[x * 2] + tree[x * 2 + 1];
	}
	void update(ll x, ll v) {
		x += s;
		tree[x]= Node(max(0LL, v), max(0LL, v), v, max(0LL, v));
		x /= 2;
		while (x) {
			tree[x] = tree[x * 2] + tree[x * 2 + 1];
			x /= 2;
		}
	}
	ll maxall() {
		return tree[1].mx;
	}
	segtree(const vector<ll>& v) {
		N = v.size();
		s = 1LL << (ll)ceil(log2(N));
		l.resize(2 * s + 1);
		r.resize(2 * s + 1);
		tree.resize(2 * s + 1);
		ll i;
		for (i = 0; i < N; i++) tree[i + s] = Node(max(0LL, v[i]), max(0LL, v[i]), v[i], max(0LL, v[i]));
		init();
	}
	segtree() {}
};
vector<Point> point;
vector<pll> v;
vector<ll> val;
segtree seg;
bool cmp(const pll &p1, const pll &p2) {
	if ((point[p1.first].y - point[p1.second].y) * (point[p2.first].x - point[p2.second].x) == (point[p2.first].y - point[p2.second].y) * (point[p1.first].x - point[p1.second].x)) {
		if (point[p1.first] == point[p2.first]) return point[p1.second] < point[p2.second];
		return point[p1.first] < point[p2.first];
	}
	if (point[p1.first].y == point[p1.second].y) return (point[p2.first].y - point[p2.second].y) * (point[p2.first].x - point[p2.second].x) > 0;
	if (point[p2.first].y == point[p2.second].y) return (point[p1.first].y - point[p1.second].y) * (point[p1.first].x - point[p1.second].x) < 0;
	return (point[p1.first].y - point[p1.second].y) * (point[p2.first].x - point[p2.second].x) < (point[p2.first].y - point[p2.second].y) * (point[p1.first].x - point[p1.second].x);
}
bool eq(const pll& p1, const pll& p2) {
	return (point[p1.first].y - point[p1.second].y) * (point[p2.first].x - point[p2.second].x) == (point[p2.first].y - point[p2.second].y) * (point[p1.first].x - point[p1.second].x);
}
ll f(ll x) {
	if (!x) return 0;
	if (x > 0) return 1;
	else return -1;
}
ll ccw(Point p1, Point p2, Point p3) {
	return f((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y));
}
ll N;
ll calc() {
	ll i, j;
	if (N == 1) {
		val[0] = max(val[0], 0LL);
		return val[0];
	}
	v.clear();
	for (i = 0; i < N; i++) {
		for (j = i + 1; j < N; j++) {
			if (point[i].x != point[j].x) {
				if (point[i].x < point[j].x) v.push_back({ i, j });
				else v.push_back({ j, i });
			}
		}
	}
	if (v.empty()) return 0;
	sort(v.begin(), v.end(), cmp);
	Point p1, p2;
	p1 = point[v[0].second];
	p2 = point[v[0].first];
	vector<ll> ord, rev;
	ord.resize(N);
	rev.resize(N);
	for (i = 0; i < N; i++) ord[i] = i;
	ll a, b, c;
	a = (p2.y - p1.y);
	b = (p2.x - p1.x);
	c = (p2.x * p1.y - p1.x * p2.y);
	for (i = 0; i < N; i++) {
		for (j = i + 1; j < N; j++) {
			ll x1, x2, y1, y2;
			x1 = point[ord[i]].x;
			x2 = point[ord[j]].x;
			y1 = point[ord[i]].y;
			y2 = point[ord[j]].y;
			ll c1 = (-ccw(p1, p2, point[ord[i]]) * abs(a * x1 - b * y1 + c));
			ll c2 = (-ccw(p1, p2, point[ord[j]]) * abs(a * x2 - b * y2 + c));
			if (c1 > c2) swap(ord[i], ord[j]);
			else if (c1 == c2) {
				if (a * b > 0) {
					if (x2 > x1) swap(ord[i], ord[j]);
				}
				else if (a * b == 0) {
					if (x2 < x1) swap(ord[i], ord[j]);
				}
				else {
					if (x2 < x1) swap(ord[i], ord[j]);
				}
			}
		}
	}
	for (i = 0; i < N; i++) rev[ord[i]] = i;
	vector<ll> nv;
	nv.resize(N);
	for (i = 0; i < N; i++) nv[i] = val[ord[i]];
	seg = segtree(nv);
	ll ans = 0;
	for (i = 0; i < v.size(); i++) {
		pll x = v[i];
		if ((i == 0) || (!eq(v[i], v[i - 1]))) ans = max(ans, seg.maxall());
		swap(ord[rev[x.first]], ord[rev[x.second]]);
		swap(rev[x.first], rev[x.second]);
		seg.update(rev[x.first], val[x.first]);
		seg.update(rev[x.second], val[x.second]);
	}
	return ans;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> N;
	ll i, j;
	for (i = 0; i < N; i++) {
		ll x, y, c;
		cin >> x >> y >> c;
		point.push_back(Point(x, y));
		val.push_back(c);
	}
	ll ans = 0;
	ans = calc();
	for (i = 0; i < N; i++) swap(point[i].x, point[i].y);
	ans = max(ans, calc());
	cout << ans << ln;
}

Compilation message

bulldozer.cpp: In function 'll calc()':
bulldozer.cpp:162:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |  for (i = 0; i < v.size(); i++) {
      |              ~~^~~~~~~~~~
bulldozer.cpp: In function 'int main()':
bulldozer.cpp:175:8: warning: unused variable 'j' [-Wunused-variable]
  175 |  ll i, j;
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 3 ms 448 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 532 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 308 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 3 ms 460 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 3 ms 460 KB Output is correct
26 Correct 5 ms 460 KB Output is correct
27 Correct 4 ms 460 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 4 ms 460 KB Output is correct
32 Correct 4 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 3 ms 448 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 532 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 308 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 3 ms 460 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 3 ms 460 KB Output is correct
26 Correct 5 ms 460 KB Output is correct
27 Correct 4 ms 460 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 4 ms 460 KB Output is correct
32 Correct 4 ms 460 KB Output is correct
33 Correct 1937 ms 33408 KB Output is correct
34 Correct 1945 ms 33392 KB Output is correct
35 Correct 1972 ms 33456 KB Output is correct
36 Correct 1932 ms 33448 KB Output is correct
37 Correct 1899 ms 33456 KB Output is correct
38 Correct 1917 ms 33448 KB Output is correct
39 Correct 1985 ms 33304 KB Output is correct
40 Correct 1939 ms 33464 KB Output is correct
41 Correct 1973 ms 33444 KB Output is correct
42 Correct 1970 ms 33424 KB Output is correct
43 Correct 1921 ms 33388 KB Output is correct
44 Correct 1884 ms 33456 KB Output is correct
45 Correct 1942 ms 33456 KB Output is correct
46 Correct 1920 ms 33432 KB Output is correct
47 Correct 1914 ms 33448 KB Output is correct
48 Correct 1910 ms 33456 KB Output is correct
49 Correct 1915 ms 33456 KB Output is correct
50 Correct 1950 ms 33364 KB Output is correct
51 Correct 1910 ms 33456 KB Output is correct
52 Correct 1918 ms 33456 KB Output is correct
53 Correct 1895 ms 33432 KB Output is correct
54 Correct 1894 ms 33448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 3 ms 448 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 532 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 308 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 3 ms 460 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 3 ms 460 KB Output is correct
26 Correct 5 ms 460 KB Output is correct
27 Correct 4 ms 460 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 4 ms 460 KB Output is correct
32 Correct 4 ms 460 KB Output is correct
33 Correct 1937 ms 33408 KB Output is correct
34 Correct 1945 ms 33392 KB Output is correct
35 Correct 1972 ms 33456 KB Output is correct
36 Correct 1932 ms 33448 KB Output is correct
37 Correct 1899 ms 33456 KB Output is correct
38 Correct 1917 ms 33448 KB Output is correct
39 Correct 1985 ms 33304 KB Output is correct
40 Correct 1939 ms 33464 KB Output is correct
41 Correct 1973 ms 33444 KB Output is correct
42 Correct 1970 ms 33424 KB Output is correct
43 Correct 1921 ms 33388 KB Output is correct
44 Correct 1884 ms 33456 KB Output is correct
45 Correct 1942 ms 33456 KB Output is correct
46 Correct 1920 ms 33432 KB Output is correct
47 Correct 1914 ms 33448 KB Output is correct
48 Correct 1910 ms 33456 KB Output is correct
49 Correct 1915 ms 33456 KB Output is correct
50 Correct 1950 ms 33364 KB Output is correct
51 Correct 1910 ms 33456 KB Output is correct
52 Correct 1918 ms 33456 KB Output is correct
53 Correct 1895 ms 33432 KB Output is correct
54 Correct 1894 ms 33448 KB Output is correct
55 Correct 1940 ms 33352 KB Output is correct
56 Correct 1944 ms 33320 KB Output is correct
57 Correct 1928 ms 33432 KB Output is correct
58 Correct 1915 ms 33456 KB Output is correct
59 Correct 1910 ms 33456 KB Output is correct
60 Correct 1914 ms 33456 KB Output is correct
61 Correct 1890 ms 33444 KB Output is correct
62 Correct 1897 ms 33456 KB Output is correct
63 Correct 1911 ms 33444 KB Output is correct
64 Correct 1934 ms 33464 KB Output is correct
65 Correct 1893 ms 33352 KB Output is correct
66 Correct 1891 ms 33444 KB Output is correct
67 Correct 1917 ms 33416 KB Output is correct
68 Correct 1891 ms 33368 KB Output is correct
69 Correct 1912 ms 33444 KB Output is correct
70 Correct 1898 ms 33444 KB Output is correct
71 Correct 1900 ms 33456 KB Output is correct
72 Correct 1931 ms 33320 KB Output is correct
73 Correct 1887 ms 33432 KB Output is correct
74 Correct 1892 ms 33324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 3 ms 460 KB Output is correct
17 Correct 3 ms 460 KB Output is correct
18 Correct 4 ms 460 KB Output is correct
19 Correct 4 ms 460 KB Output is correct
20 Correct 3 ms 444 KB Output is correct
21 Correct 3 ms 448 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 4 ms 532 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 3 ms 444 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 308 KB Output is correct
36 Correct 3 ms 460 KB Output is correct
37 Correct 3 ms 460 KB Output is correct
38 Correct 3 ms 460 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 5 ms 460 KB Output is correct
42 Correct 4 ms 460 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 3 ms 460 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 4 ms 460 KB Output is correct
48 Correct 1937 ms 33408 KB Output is correct
49 Correct 1945 ms 33392 KB Output is correct
50 Correct 1972 ms 33456 KB Output is correct
51 Correct 1932 ms 33448 KB Output is correct
52 Correct 1899 ms 33456 KB Output is correct
53 Correct 1917 ms 33448 KB Output is correct
54 Correct 1985 ms 33304 KB Output is correct
55 Correct 1939 ms 33464 KB Output is correct
56 Correct 1973 ms 33444 KB Output is correct
57 Correct 1970 ms 33424 KB Output is correct
58 Correct 1921 ms 33388 KB Output is correct
59 Correct 1884 ms 33456 KB Output is correct
60 Correct 1942 ms 33456 KB Output is correct
61 Correct 1920 ms 33432 KB Output is correct
62 Correct 1914 ms 33448 KB Output is correct
63 Correct 1910 ms 33456 KB Output is correct
64 Correct 1915 ms 33456 KB Output is correct
65 Correct 1950 ms 33364 KB Output is correct
66 Correct 1910 ms 33456 KB Output is correct
67 Correct 1918 ms 33456 KB Output is correct
68 Correct 1895 ms 33432 KB Output is correct
69 Correct 1894 ms 33448 KB Output is correct
70 Correct 1940 ms 33352 KB Output is correct
71 Correct 1944 ms 33320 KB Output is correct
72 Correct 1928 ms 33432 KB Output is correct
73 Correct 1915 ms 33456 KB Output is correct
74 Correct 1910 ms 33456 KB Output is correct
75 Correct 1914 ms 33456 KB Output is correct
76 Correct 1890 ms 33444 KB Output is correct
77 Correct 1897 ms 33456 KB Output is correct
78 Correct 1911 ms 33444 KB Output is correct
79 Correct 1934 ms 33464 KB Output is correct
80 Correct 1893 ms 33352 KB Output is correct
81 Correct 1891 ms 33444 KB Output is correct
82 Correct 1917 ms 33416 KB Output is correct
83 Correct 1891 ms 33368 KB Output is correct
84 Correct 1912 ms 33444 KB Output is correct
85 Correct 1898 ms 33444 KB Output is correct
86 Correct 1900 ms 33456 KB Output is correct
87 Correct 1931 ms 33320 KB Output is correct
88 Correct 1887 ms 33432 KB Output is correct
89 Correct 1892 ms 33324 KB Output is correct
90 Correct 1918 ms 33436 KB Output is correct
91 Correct 1912 ms 33328 KB Output is correct
92 Correct 1905 ms 33456 KB Output is correct
93 Correct 1907 ms 33432 KB Output is correct
94 Correct 1917 ms 33448 KB Output is correct
95 Correct 1911 ms 33584 KB Output is correct
96 Correct 1923 ms 33516 KB Output is correct
97 Correct 1917 ms 33460 KB Output is correct
98 Correct 1912 ms 33440 KB Output is correct
99 Correct 1898 ms 33340 KB Output is correct
100 Correct 1433 ms 33492 KB Output is correct
101 Correct 1433 ms 33492 KB Output is correct
102 Correct 1457 ms 33464 KB Output is correct
103 Correct 1433 ms 33572 KB Output is correct
104 Correct 1432 ms 33500 KB Output is correct
105 Correct 1575 ms 33416 KB Output is correct
106 Correct 1590 ms 33328 KB Output is correct
107 Correct 1611 ms 33456 KB Output is correct
108 Correct 1589 ms 33320 KB Output is correct
109 Correct 1596 ms 33456 KB Output is correct
110 Correct 1785 ms 33424 KB Output is correct
111 Correct 1829 ms 33312 KB Output is correct
112 Correct 1794 ms 33452 KB Output is correct
113 Correct 1798 ms 33328 KB Output is correct
114 Correct 1823 ms 33328 KB Output is correct
115 Correct 1801 ms 33328 KB Output is correct
116 Correct 1821 ms 33428 KB Output is correct
117 Correct 1795 ms 33456 KB Output is correct
118 Correct 1799 ms 33328 KB Output is correct
119 Correct 1787 ms 33376 KB Output is correct
120 Correct 0 ms 204 KB Output is correct
121 Correct 1 ms 204 KB Output is correct
122 Correct 1928 ms 33360 KB Output is correct
123 Correct 1895 ms 33456 KB Output is correct
124 Correct 1898 ms 33316 KB Output is correct
125 Correct 1883 ms 33448 KB Output is correct
126 Correct 1915 ms 33320 KB Output is correct
127 Correct 1892 ms 33448 KB Output is correct
128 Correct 1904 ms 33428 KB Output is correct
129 Correct 1885 ms 33456 KB Output is correct
130 Correct 1898 ms 33436 KB Output is correct
131 Correct 1922 ms 33460 KB Output is correct
132 Correct 1885 ms 33316 KB Output is correct
133 Correct 1878 ms 33356 KB Output is correct