Submission #108544

# Submission time Handle Problem Language Result Execution time Memory
108544 2019-04-30T07:09:07 Z E869120 Bulldozer (JOI17_bulldozer) C++14
100 / 100
1126 ms 50104 KB
#include <iostream>
#include <vector>
#include <tuple>
#include <cmath>
#include <algorithm>
using namespace std;

class SegmentTree {
public:
	vector<long long>dat1, dat2, dat3; int size_ = 1;

	void init(int sz) {
		while (size_ <= sz) size_ *= 2;
		dat1.resize(size_ * 2, 0);
		dat2.resize(size_ * 2, 0);
		dat3.resize(size_ * 2, 0);
	}

	void update(int pos, long long x) {
		pos += size_;
		dat1[pos] = x; dat2[pos] = x;
		while (pos >= 2) {
			pos >>= 1;
			dat1[pos] = min(dat1[pos * 2], dat1[pos * 2 + 1]);
			dat2[pos] = max(dat2[pos * 2], dat2[pos * 2 + 1]);
			dat3[pos] = max({ dat3[pos * 2], dat3[pos * 2 + 1], dat2[pos * 2 + 1] - dat1[pos * 2] });
		}
	}

	long long get_val() {
		return dat3[1];
	}
};

struct Fraction {
public:
	long long cx, cy;
};

bool operator<(const Fraction &a1, const Fraction &a2) {
	// a1 < a2
	if (a1.cx * a2.cy > a1.cy * a2.cx) return true;
	return false;
}

bool operator==(const Fraction &a1, const Fraction &a2) {
	// a1 == a2
	if (a1.cx * a2.cy == a1.cy * a2.cx) return true;
	return false;
}

long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

Fraction teisei(Fraction A) {
	if (A.cx < 0) { A.cx *= -1; A.cy *= -1; }
	long long E = gcd(A.cx, A.cy); E = abs(E);
	A.cx /= E; A.cy /= E;
	return A;
}

long long N, X[2009], Y[2009], W[2009], order[2009], ichi[2009], ruiseki[2009], ans; bool ok = false;
vector<tuple<Fraction, int, int>>E;
SegmentTree Z;

long long solve_1() {
	long long res = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (X[i] > X[j]) continue;
			long long sm = 0; for (int k = 0; k < N; k++) { if (X[i] <= X[k] && X[k] <= X[j]) sm += W[k]; }
			res = max(res, sm);
		}
	}
	return res;
}

int main() {
	// 入力部分・偏角ソート
	vector<tuple<long long, long long, long long>> vec1;
	cin >> N;
	for (int i = 0; i < N; i++) { cin >> X[i] >> Y[i] >> W[i]; vec1.push_back(make_tuple(X[i], Y[i], W[i])); if (Y[i] != 0) ok = true; }
	sort(vec1.begin(), vec1.end());
	for (int i = 0; i < N; i++) { X[i] = get<0>(vec1[i]); Y[i] = get<1>(vec1[i]); W[i] = get<2>(vec1[i]); }

	ok = true;
	if (ok == false) {
		cout << solve_1() << endl;
		return 0;
	}

	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) E.push_back(make_tuple(teisei(Fraction{ X[j] - X[i],Y[j] - Y[i] }), i, j));
	}
	sort(E.begin(), E.end());

	// 初期値を計算 ① ~ Y 座標編 ~
	vector<pair<long long, long long>>vec;
	for (int i = 0; i < N; i++) vec.push_back(make_pair(Y[i], i));
	sort(vec.begin(), vec.end());
	for (int i = 0; i < vec.size(); i++) { ichi[vec[i].second] = i; order[i] = vec[i].second; }

	Z.init(N + 2);
	for (int i = 0; i < N; i++) ruiseki[i + 1] = ruiseki[i] + W[order[i]];
	for (int i = 0; i <= N; i++) Z.update(i, ruiseki[i]);
	for (int i = N + 1; i < Z.size_; i++) Z.update(i, -(1LL << 60));

	ans = max(ans, Z.get_val());

	// 初期値を計算 ② ~ X 座標編 ~
	vec.clear();
	for (int i = 0; i < N; i++) vec.push_back(make_pair(X[i], i));
	sort(vec.begin(), vec.end());
	for (int i = 0; i < vec.size(); i++) { ichi[vec[i].second] = i; order[i] = vec[i].second; }
	
	Z.init(N + 2);
	for (int i = 0; i < N; i++) ruiseki[i + 1] = ruiseki[i] + W[order[i]];
	for (int i = 0; i <= N; i++) Z.update(i, ruiseki[i]);
	for (int i = N + 1; i < Z.size_; i++) Z.update(i, -(1LL << 60));

	// 値を更新
	ans = max(ans, Z.get_val());
	for (int i = 0; i < E.size(); i++) {
		int u1 = ichi[get<1>(E[i])], u2 = ichi[get<2>(E[i])]; if (u1 > u2) swap(u1, u2);
		if (X[order[u1]] < X[order[u2]]) {
			swap(order[u1], order[u2]);
			swap(ichi[get<1>(E[i])], ichi[get<2>(E[i])]);
			ruiseki[u1 + 1] = ruiseki[u1] + W[order[u1]];
			Z.update(u1 + 1, ruiseki[u1 + 1]);
		}

		if (i == E.size() - 1 || !(get<0>(E[i]) == get<0>(E[i + 1]))) ans = max(ans, Z.get_val());
	}

	cout << ans << endl;
	return 0;
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:103:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < vec.size(); i++) { ichi[vec[i].second] = i; order[i] = vec[i].second; }
                  ~~^~~~~~~~~~~~
bulldozer.cpp:116:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < vec.size(); i++) { ichi[vec[i].second] = i; order[i] = vec[i].second; }
                  ~~^~~~~~~~~~~~
bulldozer.cpp:125:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < E.size(); i++) {
                  ~~^~~~~~~~~~
bulldozer.cpp:134:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i == E.size() - 1 || !(get<0>(E[i]) == get<0>(E[i + 1]))) ans = max(ans, Z.get_val());
       ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 4 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 4 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 1039 ms 49808 KB Output is correct
34 Correct 1030 ms 49824 KB Output is correct
35 Correct 1037 ms 49884 KB Output is correct
36 Correct 1022 ms 49780 KB Output is correct
37 Correct 1041 ms 49812 KB Output is correct
38 Correct 1047 ms 49840 KB Output is correct
39 Correct 1081 ms 50012 KB Output is correct
40 Correct 1061 ms 49820 KB Output is correct
41 Correct 1054 ms 49820 KB Output is correct
42 Correct 1025 ms 49948 KB Output is correct
43 Correct 944 ms 49856 KB Output is correct
44 Correct 963 ms 49780 KB Output is correct
45 Correct 921 ms 49844 KB Output is correct
46 Correct 984 ms 49864 KB Output is correct
47 Correct 962 ms 49800 KB Output is correct
48 Correct 952 ms 49884 KB Output is correct
49 Correct 950 ms 49848 KB Output is correct
50 Correct 959 ms 49788 KB Output is correct
51 Correct 952 ms 49848 KB Output is correct
52 Correct 930 ms 49884 KB Output is correct
53 Correct 925 ms 49924 KB Output is correct
54 Correct 943 ms 50016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 4 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 1039 ms 49808 KB Output is correct
34 Correct 1030 ms 49824 KB Output is correct
35 Correct 1037 ms 49884 KB Output is correct
36 Correct 1022 ms 49780 KB Output is correct
37 Correct 1041 ms 49812 KB Output is correct
38 Correct 1047 ms 49840 KB Output is correct
39 Correct 1081 ms 50012 KB Output is correct
40 Correct 1061 ms 49820 KB Output is correct
41 Correct 1054 ms 49820 KB Output is correct
42 Correct 1025 ms 49948 KB Output is correct
43 Correct 944 ms 49856 KB Output is correct
44 Correct 963 ms 49780 KB Output is correct
45 Correct 921 ms 49844 KB Output is correct
46 Correct 984 ms 49864 KB Output is correct
47 Correct 962 ms 49800 KB Output is correct
48 Correct 952 ms 49884 KB Output is correct
49 Correct 950 ms 49848 KB Output is correct
50 Correct 959 ms 49788 KB Output is correct
51 Correct 952 ms 49848 KB Output is correct
52 Correct 930 ms 49884 KB Output is correct
53 Correct 925 ms 49924 KB Output is correct
54 Correct 943 ms 50016 KB Output is correct
55 Correct 1071 ms 49780 KB Output is correct
56 Correct 1029 ms 49976 KB Output is correct
57 Correct 1062 ms 49852 KB Output is correct
58 Correct 1030 ms 49800 KB Output is correct
59 Correct 1084 ms 49896 KB Output is correct
60 Correct 1071 ms 49868 KB Output is correct
61 Correct 1066 ms 49924 KB Output is correct
62 Correct 1052 ms 50012 KB Output is correct
63 Correct 1051 ms 50004 KB Output is correct
64 Correct 1066 ms 50104 KB Output is correct
65 Correct 1126 ms 49972 KB Output is correct
66 Correct 1033 ms 49884 KB Output is correct
67 Correct 1024 ms 49884 KB Output is correct
68 Correct 1051 ms 49800 KB Output is correct
69 Correct 1035 ms 49872 KB Output is correct
70 Correct 1036 ms 49920 KB Output is correct
71 Correct 1036 ms 49828 KB Output is correct
72 Correct 1068 ms 49908 KB Output is correct
73 Correct 1074 ms 49936 KB Output is correct
74 Correct 1024 ms 49896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 4 ms 640 KB Output is correct
17 Correct 4 ms 640 KB Output is correct
18 Correct 4 ms 640 KB Output is correct
19 Correct 4 ms 640 KB Output is correct
20 Correct 4 ms 640 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 4 ms 640 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 4 ms 640 KB Output is correct
42 Correct 4 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 4 ms 640 KB Output is correct
45 Correct 4 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 1039 ms 49808 KB Output is correct
49 Correct 1030 ms 49824 KB Output is correct
50 Correct 1037 ms 49884 KB Output is correct
51 Correct 1022 ms 49780 KB Output is correct
52 Correct 1041 ms 49812 KB Output is correct
53 Correct 1047 ms 49840 KB Output is correct
54 Correct 1081 ms 50012 KB Output is correct
55 Correct 1061 ms 49820 KB Output is correct
56 Correct 1054 ms 49820 KB Output is correct
57 Correct 1025 ms 49948 KB Output is correct
58 Correct 944 ms 49856 KB Output is correct
59 Correct 963 ms 49780 KB Output is correct
60 Correct 921 ms 49844 KB Output is correct
61 Correct 984 ms 49864 KB Output is correct
62 Correct 962 ms 49800 KB Output is correct
63 Correct 952 ms 49884 KB Output is correct
64 Correct 950 ms 49848 KB Output is correct
65 Correct 959 ms 49788 KB Output is correct
66 Correct 952 ms 49848 KB Output is correct
67 Correct 930 ms 49884 KB Output is correct
68 Correct 925 ms 49924 KB Output is correct
69 Correct 943 ms 50016 KB Output is correct
70 Correct 1071 ms 49780 KB Output is correct
71 Correct 1029 ms 49976 KB Output is correct
72 Correct 1062 ms 49852 KB Output is correct
73 Correct 1030 ms 49800 KB Output is correct
74 Correct 1084 ms 49896 KB Output is correct
75 Correct 1071 ms 49868 KB Output is correct
76 Correct 1066 ms 49924 KB Output is correct
77 Correct 1052 ms 50012 KB Output is correct
78 Correct 1051 ms 50004 KB Output is correct
79 Correct 1066 ms 50104 KB Output is correct
80 Correct 1126 ms 49972 KB Output is correct
81 Correct 1033 ms 49884 KB Output is correct
82 Correct 1024 ms 49884 KB Output is correct
83 Correct 1051 ms 49800 KB Output is correct
84 Correct 1035 ms 49872 KB Output is correct
85 Correct 1036 ms 49920 KB Output is correct
86 Correct 1036 ms 49828 KB Output is correct
87 Correct 1068 ms 49908 KB Output is correct
88 Correct 1074 ms 49936 KB Output is correct
89 Correct 1024 ms 49896 KB Output is correct
90 Correct 1062 ms 49788 KB Output is correct
91 Correct 1036 ms 49872 KB Output is correct
92 Correct 1018 ms 49864 KB Output is correct
93 Correct 1042 ms 49844 KB Output is correct
94 Correct 1025 ms 49852 KB Output is correct
95 Correct 1018 ms 49884 KB Output is correct
96 Correct 1051 ms 49848 KB Output is correct
97 Correct 1039 ms 49820 KB Output is correct
98 Correct 1066 ms 49852 KB Output is correct
99 Correct 1046 ms 49904 KB Output is correct
100 Correct 372 ms 49884 KB Output is correct
101 Correct 369 ms 49888 KB Output is correct
102 Correct 371 ms 49856 KB Output is correct
103 Correct 373 ms 49884 KB Output is correct
104 Correct 366 ms 49860 KB Output is correct
105 Correct 441 ms 49888 KB Output is correct
106 Correct 428 ms 49828 KB Output is correct
107 Correct 418 ms 49788 KB Output is correct
108 Correct 425 ms 49884 KB Output is correct
109 Correct 412 ms 49884 KB Output is correct
110 Correct 710 ms 49776 KB Output is correct
111 Correct 722 ms 49840 KB Output is correct
112 Correct 715 ms 49888 KB Output is correct
113 Correct 727 ms 49844 KB Output is correct
114 Correct 751 ms 49944 KB Output is correct
115 Correct 728 ms 49836 KB Output is correct
116 Correct 720 ms 49912 KB Output is correct
117 Correct 729 ms 49792 KB Output is correct
118 Correct 726 ms 49856 KB Output is correct
119 Correct 714 ms 49844 KB Output is correct
120 Correct 2 ms 384 KB Output is correct
121 Correct 2 ms 384 KB Output is correct
122 Correct 746 ms 49856 KB Output is correct
123 Correct 750 ms 49856 KB Output is correct
124 Correct 757 ms 49884 KB Output is correct
125 Correct 740 ms 49820 KB Output is correct
126 Correct 746 ms 49840 KB Output is correct
127 Correct 753 ms 49864 KB Output is correct
128 Correct 719 ms 49872 KB Output is correct
129 Correct 722 ms 49844 KB Output is correct
130 Correct 715 ms 49884 KB Output is correct
131 Correct 756 ms 49848 KB Output is correct
132 Correct 743 ms 50012 KB Output is correct
133 Correct 747 ms 49888 KB Output is correct