Submission #292000

# Submission time Handle Problem Language Result Execution time Memory
292000 2020-09-06T07:27:29 Z Diuven Robogolf (ROI19_golf) C++11
50 / 100
5000 ms 389352 KB
// Created by Nikolay Budin

#ifdef LOCAL
#  define _GLIBCXX_DEBUG
#else
#  define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 998244353;

struct cells_order {
	bool operator()(pii p1, pii p2) {
		return p1.ff - p1.ss < p2.ff - p2.ss || (p1.ff - p1.ss == p2.ff - p2.ss && p1.ff + p1.ss < p2.ff + p2.ss);
	}
};

void solve() {
	int h, w, n;
	cin >> h >> w >> n;
	int m = max(h, w);
	vector<set<pii, cells_order>> intr_cells(2);
	map<pii, int> inp;
	for (int i = 0; i < n; ++i) {
		int x, y, v;
		cin >> x >> y >> v;
		--y;
		x = m - x;
		inp[{x, y}] = v;
		for (int dx = 0; dx <= 2; ++dx) {
			for (int dy = -2; dy <= 0; ++dy) {
				if (abs(dx) + abs(dy) <= 2) {
					int nx = x + dx;
					int ny = y + dy;
					if (0 <= nx && nx < m && 0 <= ny && ny < m) {
						intr_cells[(nx + ny) & 1].insert({nx, ny});
					}
				}
			}
		}
	}

	int ans = 0;
	vector<pii> change;
	for (int q = 0; q < 2; ++q) {
		// cerr << "q: " << q << endl;
		auto it = intr_cells[q].begin();
		ll sum = 0;
		map<int, int> arr;
		int prev = -INF;
		while (it != intr_cells[q].end()) {
			int i = it->ff - it->ss;
			// cerr << i << " " << prev << " " << sum << endl;
			ans = (ans + (ll) (i - prev) / 2 * (sum % MOD)) % MOD;
			int l = abs(i);
			int r = m * 2 - 2 - abs(i);

			if (i <= 0) {
				sum += arr[l];
				if (l != r)	{
					sum += arr[r];
				}
			} else if (i > 1) {
				sum -= arr[l - 2];
				sum -= arr[r + 2];
			}

			change.clear();

			while (it != intr_cells[q].end() && it->ff - it->ss == i) {
				int x, y;
				tie(x, y) = *it;
				
				if (inp.count({x, y})) {
					//arr[x + y] = inp[{x, y}];
					change.push_back({x + y, inp[{x, y}]});
				} else {
					int val1;
					if (x == 0) {
						val1 = 0;
					} else if (inp.count({x - 1, y})) {
						val1 = inp[{x - 1, y}];
					} else {
						val1 = max(arr[x + y], x + y - 2 >= 0 ? arr[x + y - 2] : 0);
					}
					int val2;
					if (y == m - 1) {
						val2 = 0;
					} else if (inp.count({x, y + 1})) {
						val2 = inp[{x, y + 1}];
					} else {
						val2 = max(arr[x + y], x + y + 2 < m * 2 ? arr[x + y + 2] : 0);
					}

					// arr[x + y] = min(val1, val2);
					change.push_back({x + y, min(val1, val2)});
					if (x + y < m - 2) {
						intr_cells[q].insert({x + y + 2, 0});
					} else if (x + y >= m + 1) {
						intr_cells[q].insert({m - 1, x + y - (m - 1) - 2});
					}
				}

				++it;
			}

			for (pii p : change) {
				if (l <= p.ff && p.ff <= r) {
					sum -= arr[p.ff];
				}

				arr[p.ff] = p.ss;

				if (l <= p.ff && p.ff <= r) {
					sum += arr[p.ff];
				}
			}


			prev = i;
			// cerr << sum << endl;
			// for (int j = l; j <= r; j += 2) {
			// 	cerr << arr[j] << " ";
			// }
			// cerr << endl;
		}

		// cerr << ((m & 1) == q ? m : m + 1) << " " << prev << " " << sum << endl;
		ans = (ans + (ll) (((m & 1) == q ? m : m + 1) - prev) / 2 * (sum % MOD)) % MOD;
	}

	cout << (ans + MOD) % MOD << "\n";
}


int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}
	
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1183 ms 28924 KB Output is correct
11 Correct 1204 ms 28768 KB Output is correct
12 Correct 1161 ms 28536 KB Output is correct
13 Correct 1150 ms 28192 KB Output is correct
14 Correct 1000 ms 24064 KB Output is correct
15 Correct 950 ms 24120 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 16 ms 1152 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 971 ms 24696 KB Output is correct
23 Correct 703 ms 18680 KB Output is correct
24 Correct 764 ms 20216 KB Output is correct
25 Correct 709 ms 19568 KB Output is correct
26 Correct 1046 ms 25976 KB Output is correct
27 Correct 958 ms 23800 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 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 663 ms 24312 KB Output is correct
33 Correct 854 ms 28924 KB Output is correct
34 Correct 375 ms 16632 KB Output is correct
35 Correct 349 ms 15992 KB Output is correct
36 Correct 332 ms 15352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 79 ms 4216 KB Output is correct
4 Correct 812 ms 28400 KB Output is correct
5 Correct 2774 ms 118688 KB Output is correct
6 Execution timed out 5085 ms 389352 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 352 ms 15992 KB Output is correct
2 Correct 344 ms 15992 KB Output is correct
3 Correct 871 ms 27792 KB Output is correct
4 Correct 849 ms 29640 KB Output is correct
5 Correct 796 ms 30072 KB Output is correct
6 Correct 531 ms 20728 KB Output is correct
7 Correct 706 ms 25720 KB Output is correct
8 Correct 874 ms 30072 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1183 ms 28924 KB Output is correct
11 Correct 1204 ms 28768 KB Output is correct
12 Correct 1161 ms 28536 KB Output is correct
13 Correct 1150 ms 28192 KB Output is correct
14 Correct 1000 ms 24064 KB Output is correct
15 Correct 950 ms 24120 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 16 ms 1152 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 971 ms 24696 KB Output is correct
23 Correct 703 ms 18680 KB Output is correct
24 Correct 764 ms 20216 KB Output is correct
25 Correct 709 ms 19568 KB Output is correct
26 Correct 1046 ms 25976 KB Output is correct
27 Correct 958 ms 23800 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 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 663 ms 24312 KB Output is correct
33 Correct 854 ms 28924 KB Output is correct
34 Correct 375 ms 16632 KB Output is correct
35 Correct 349 ms 15992 KB Output is correct
36 Correct 332 ms 15352 KB Output is correct
37 Correct 1237 ms 28944 KB Output is correct
38 Correct 4120 ms 153600 KB Output is correct
39 Correct 1203 ms 35328 KB Output is correct
40 Correct 1223 ms 34864 KB Output is correct
41 Correct 933 ms 24640 KB Output is correct
42 Correct 709 ms 19156 KB Output is correct
43 Correct 751 ms 21084 KB Output is correct
44 Correct 720 ms 19968 KB Output is correct
45 Correct 1123 ms 33524 KB Output is correct
46 Correct 909 ms 23800 KB Output is correct
47 Execution timed out 5038 ms 296796 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2402 ms 48920 KB Output is correct
2 Correct 1892 ms 44444 KB Output is correct
3 Correct 1732 ms 41264 KB Output is correct
4 Correct 1556 ms 41280 KB Output is correct
5 Correct 162 ms 10360 KB Output is correct
6 Correct 225 ms 11640 KB Output is correct
7 Correct 1071 ms 34936 KB Output is correct
8 Correct 971 ms 34680 KB Output is correct
9 Correct 1473 ms 43052 KB Output is correct
10 Correct 1272 ms 43044 KB Output is correct
11 Correct 1886 ms 44168 KB Output is correct
12 Correct 1477 ms 44216 KB Output is correct
13 Correct 2250 ms 44684 KB Output is correct
14 Correct 1910 ms 44568 KB Output is correct
15 Correct 1109 ms 41840 KB Output is correct
16 Correct 869 ms 32828 KB Output is correct
17 Correct 1250 ms 33968 KB Output is correct
18 Correct 959 ms 34992 KB Output is correct
19 Correct 1124 ms 44140 KB Output is correct
20 Correct 902 ms 37884 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 678 ms 24184 KB Output is correct
23 Correct 878 ms 28824 KB Output is correct
24 Correct 1435 ms 34088 KB Output is correct
25 Correct 147 ms 6000 KB Output is correct
26 Correct 171 ms 7416 KB Output is correct
27 Correct 193 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2402 ms 48920 KB Output is correct
2 Correct 1892 ms 44444 KB Output is correct
3 Correct 1732 ms 41264 KB Output is correct
4 Correct 1556 ms 41280 KB Output is correct
5 Correct 162 ms 10360 KB Output is correct
6 Correct 225 ms 11640 KB Output is correct
7 Correct 1071 ms 34936 KB Output is correct
8 Correct 971 ms 34680 KB Output is correct
9 Correct 1473 ms 43052 KB Output is correct
10 Correct 1272 ms 43044 KB Output is correct
11 Correct 1886 ms 44168 KB Output is correct
12 Correct 1477 ms 44216 KB Output is correct
13 Correct 2250 ms 44684 KB Output is correct
14 Correct 1910 ms 44568 KB Output is correct
15 Correct 1109 ms 41840 KB Output is correct
16 Correct 869 ms 32828 KB Output is correct
17 Correct 1250 ms 33968 KB Output is correct
18 Correct 959 ms 34992 KB Output is correct
19 Correct 1124 ms 44140 KB Output is correct
20 Correct 902 ms 37884 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 678 ms 24184 KB Output is correct
23 Correct 878 ms 28824 KB Output is correct
24 Correct 1435 ms 34088 KB Output is correct
25 Correct 147 ms 6000 KB Output is correct
26 Correct 171 ms 7416 KB Output is correct
27 Correct 193 ms 8312 KB Output is correct
28 Execution timed out 5053 ms 332120 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1183 ms 28924 KB Output is correct
11 Correct 1204 ms 28768 KB Output is correct
12 Correct 1161 ms 28536 KB Output is correct
13 Correct 1150 ms 28192 KB Output is correct
14 Correct 1000 ms 24064 KB Output is correct
15 Correct 950 ms 24120 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 16 ms 1152 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 971 ms 24696 KB Output is correct
23 Correct 703 ms 18680 KB Output is correct
24 Correct 764 ms 20216 KB Output is correct
25 Correct 709 ms 19568 KB Output is correct
26 Correct 1046 ms 25976 KB Output is correct
27 Correct 958 ms 23800 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 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 663 ms 24312 KB Output is correct
33 Correct 854 ms 28924 KB Output is correct
34 Correct 375 ms 16632 KB Output is correct
35 Correct 349 ms 15992 KB Output is correct
36 Correct 332 ms 15352 KB Output is correct
37 Correct 352 ms 15992 KB Output is correct
38 Correct 344 ms 15992 KB Output is correct
39 Correct 871 ms 27792 KB Output is correct
40 Correct 849 ms 29640 KB Output is correct
41 Correct 796 ms 30072 KB Output is correct
42 Correct 531 ms 20728 KB Output is correct
43 Correct 706 ms 25720 KB Output is correct
44 Correct 874 ms 30072 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 2402 ms 48920 KB Output is correct
48 Correct 1892 ms 44444 KB Output is correct
49 Correct 1732 ms 41264 KB Output is correct
50 Correct 1556 ms 41280 KB Output is correct
51 Correct 162 ms 10360 KB Output is correct
52 Correct 225 ms 11640 KB Output is correct
53 Correct 1071 ms 34936 KB Output is correct
54 Correct 971 ms 34680 KB Output is correct
55 Correct 1473 ms 43052 KB Output is correct
56 Correct 1272 ms 43044 KB Output is correct
57 Correct 1886 ms 44168 KB Output is correct
58 Correct 1477 ms 44216 KB Output is correct
59 Correct 2250 ms 44684 KB Output is correct
60 Correct 1910 ms 44568 KB Output is correct
61 Correct 1109 ms 41840 KB Output is correct
62 Correct 869 ms 32828 KB Output is correct
63 Correct 1250 ms 33968 KB Output is correct
64 Correct 959 ms 34992 KB Output is correct
65 Correct 1124 ms 44140 KB Output is correct
66 Correct 902 ms 37884 KB Output is correct
67 Correct 0 ms 384 KB Output is correct
68 Correct 678 ms 24184 KB Output is correct
69 Correct 878 ms 28824 KB Output is correct
70 Correct 1435 ms 34088 KB Output is correct
71 Correct 147 ms 6000 KB Output is correct
72 Correct 171 ms 7416 KB Output is correct
73 Correct 193 ms 8312 KB Output is correct
74 Correct 2421 ms 48792 KB Output is correct
75 Correct 1992 ms 44480 KB Output is correct
76 Correct 1658 ms 41428 KB Output is correct
77 Correct 1666 ms 41248 KB Output is correct
78 Correct 186 ms 10492 KB Output is correct
79 Correct 213 ms 11564 KB Output is correct
80 Correct 1066 ms 34648 KB Output is correct
81 Correct 922 ms 34684 KB Output is correct
82 Correct 1631 ms 43108 KB Output is correct
83 Correct 1279 ms 43056 KB Output is correct
84 Correct 2033 ms 44280 KB Output is correct
85 Correct 1548 ms 44104 KB Output is correct
86 Correct 2459 ms 44612 KB Output is correct
87 Correct 2063 ms 44772 KB Output is correct
88 Correct 1164 ms 40952 KB Output is correct
89 Correct 890 ms 31864 KB Output is correct
90 Correct 1004 ms 32016 KB Output is correct
91 Correct 914 ms 34040 KB Output is correct
92 Correct 1120 ms 44152 KB Output is correct
93 Correct 905 ms 37588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5091 ms 300016 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1183 ms 28924 KB Output is correct
11 Correct 1204 ms 28768 KB Output is correct
12 Correct 1161 ms 28536 KB Output is correct
13 Correct 1150 ms 28192 KB Output is correct
14 Correct 1000 ms 24064 KB Output is correct
15 Correct 950 ms 24120 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 16 ms 1152 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 971 ms 24696 KB Output is correct
23 Correct 703 ms 18680 KB Output is correct
24 Correct 764 ms 20216 KB Output is correct
25 Correct 709 ms 19568 KB Output is correct
26 Correct 1046 ms 25976 KB Output is correct
27 Correct 958 ms 23800 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 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 663 ms 24312 KB Output is correct
33 Correct 854 ms 28924 KB Output is correct
34 Correct 375 ms 16632 KB Output is correct
35 Correct 349 ms 15992 KB Output is correct
36 Correct 332 ms 15352 KB Output is correct
37 Correct 352 ms 15992 KB Output is correct
38 Correct 344 ms 15992 KB Output is correct
39 Correct 871 ms 27792 KB Output is correct
40 Correct 849 ms 29640 KB Output is correct
41 Correct 796 ms 30072 KB Output is correct
42 Correct 531 ms 20728 KB Output is correct
43 Correct 706 ms 25720 KB Output is correct
44 Correct 874 ms 30072 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 79 ms 4216 KB Output is correct
50 Correct 812 ms 28400 KB Output is correct
51 Correct 2774 ms 118688 KB Output is correct
52 Execution timed out 5085 ms 389352 KB Time limit exceeded
53 Halted 0 ms 0 KB -