Submission #792442

# Submission time Handle Problem Language Result Execution time Memory
792442 2023-07-25T04:58:27 Z GEN 이지후(#10086) Real Mountains (CCO23_day1problem2) C++17
16 / 25
5000 ms 29504 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<int, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using llf = long double;
const int mod = 1e6 + 3; // 1e9 + 7;//993244853;

// I don't remember the credit of modint, but it's not mine.
// I don't remember the credit of FFT, but it's probably mine.
// Polynomial library is due to adamant:
// https://github.com/cp-algorithms/cp-algorithms-aux/blob/master/src/polynomial.cpp
// To use polynomial sqrt, need to amend sqrt for modint.

struct mint {
	int val;
	mint() { val = 0; }
	mint(const lint &v) {
		val = (-mod <= v && v < mod) ? v : v % mod;
		if (val < 0)
			val += mod;
	}

	friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; }
	friend bool operator==(const mint &a, const mint &b) { return a.val == b.val; }
	friend bool operator!=(const mint &a, const mint &b) { return !(a == b); }
	friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; }

	mint operator-() const { return mint(-val); }
	mint &operator+=(const mint &m) {
		if ((val += m.val) >= mod)
			val -= mod;
		return *this;
	}
	mint &operator-=(const mint &m) {
		if ((val -= m.val) < 0)
			val += mod;
		return *this;
	}
	mint &operator*=(const mint &m) {
		val = (lint)val * m.val % mod;
		return *this;
	}
	friend mint ipow(mint a, lint p) {
		mint ans = 1;
		for (; p; p /= 2, a *= a)
			if (p & 1)
				ans *= a;
		return ans;
	}
	mint inv() const { return ipow(*this, mod - 2); }
	mint &operator/=(const mint &m) { return (*this) *= m.inv(); }

	friend mint operator+(mint a, const mint &b) { return a += b; }
	friend mint operator-(mint a, const mint &b) { return a -= b; }
	friend mint operator*(mint a, const mint &b) { return a *= b; }
	friend mint operator/(mint a, const mint &b) { return a /= b; }
	operator int64_t() const { return val; }
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> coords;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		coords.push_back(a[i]);
	}
	sort(all(coords));
	coords.resize(unique(all(coords)) - coords.begin());
	mint tot = 0;
	auto sum = [&](int l, int r) { return mint(1ll * (r - l + 1) * (r + l) / 2); };
	int l = 0, r = n;
	for (int i = 0; i < sz(coords) - 1; i++) {
		while (l < n && a[l] <= coords[i])
			l++;
		while (r > l && a[r - 1] <= coords[i])
			r--;
		vector<int> pos;
		vector<pi> mswp;
		int mx = 1e9;
		for (int j = l; j < r; j++) {
			if (a[j] <= coords[i]) {
				pos.push_back(j);
				mswp.push_back({mx, 0});
			} else
				mx = min(mx, a[j]);
		}
		if (sz(pos) == 0)
			continue;
		int ptr = sz(pos);
		mx = 1e9;
		for (int j = r - 1; j >= l; j--) {
			if (a[j] <= coords[i]) {
				mswp[--ptr][1] = mx;
			} else
				mx = min(mx, a[j]);
		}
		if (sz(pos) == 1) {
			tot += mint(1ll * (coords[i + 1] - coords[i]) * (mswp[0][0] + mswp[0][1])) + sum(coords[i], coords[i + 1] - 1);
			continue;
		}
		tot += sum(coords[i], coords[i + 1] - 1) * mint(sz(pos));
		tot += sum(coords[i] + 1, coords[i + 1]) * mint(2 * sz(pos) - 2);
		tot += mint(1ll * mswp[0][0] * (coords[i + 1] - coords[i]));
		tot += mint(1ll * mswp.back()[1] * (coords[i + 1] - coords[i]));
		tot += mint(1ll * (coords[i + 1] - coords[i]) * min(mswp[0][1], mswp.back()[0])) - sum(coords[i] + 1, coords[i + 1]);
	}
	cout << tot << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 192 ms 2844 KB Output is correct
36 Correct 196 ms 572 KB Output is correct
37 Correct 195 ms 484 KB Output is correct
38 Correct 192 ms 484 KB Output is correct
39 Correct 193 ms 480 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 163 ms 492 KB Output is correct
44 Correct 164 ms 488 KB Output is correct
45 Correct 164 ms 488 KB Output is correct
46 Correct 172 ms 588 KB Output is correct
47 Correct 166 ms 480 KB Output is correct
48 Correct 164 ms 480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 192 ms 2844 KB Output is correct
36 Correct 196 ms 572 KB Output is correct
37 Correct 195 ms 484 KB Output is correct
38 Correct 192 ms 484 KB Output is correct
39 Correct 193 ms 480 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 163 ms 492 KB Output is correct
44 Correct 164 ms 488 KB Output is correct
45 Correct 164 ms 488 KB Output is correct
46 Correct 172 ms 588 KB Output is correct
47 Correct 166 ms 480 KB Output is correct
48 Correct 164 ms 480 KB Output is correct
49 Correct 196 ms 616 KB Output is correct
50 Correct 190 ms 484 KB Output is correct
51 Correct 193 ms 488 KB Output is correct
52 Correct 190 ms 488 KB Output is correct
53 Correct 193 ms 484 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 164 ms 488 KB Output is correct
58 Correct 165 ms 492 KB Output is correct
59 Correct 166 ms 608 KB Output is correct
60 Correct 171 ms 480 KB Output is correct
61 Correct 168 ms 484 KB Output is correct
62 Correct 170 ms 480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1296 ms 29308 KB Output is correct
36 Correct 1314 ms 29292 KB Output is correct
37 Correct 1338 ms 29348 KB Output is correct
38 Correct 1359 ms 29468 KB Output is correct
39 Correct 1324 ms 29388 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 199 ms 29504 KB Output is correct
43 Correct 202 ms 29440 KB Output is correct
44 Correct 205 ms 29412 KB Output is correct
45 Correct 1062 ms 29372 KB Output is correct
46 Correct 1092 ms 29388 KB Output is correct
47 Correct 1071 ms 29356 KB Output is correct
48 Correct 1162 ms 29380 KB Output is correct
49 Correct 1142 ms 29276 KB Output is correct
50 Correct 1100 ms 29332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 192 ms 2844 KB Output is correct
36 Correct 196 ms 572 KB Output is correct
37 Correct 195 ms 484 KB Output is correct
38 Correct 192 ms 484 KB Output is correct
39 Correct 193 ms 480 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 163 ms 492 KB Output is correct
44 Correct 164 ms 488 KB Output is correct
45 Correct 164 ms 488 KB Output is correct
46 Correct 172 ms 588 KB Output is correct
47 Correct 166 ms 480 KB Output is correct
48 Correct 164 ms 480 KB Output is correct
49 Correct 1296 ms 29308 KB Output is correct
50 Correct 1314 ms 29292 KB Output is correct
51 Correct 1338 ms 29348 KB Output is correct
52 Correct 1359 ms 29468 KB Output is correct
53 Correct 1324 ms 29388 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 199 ms 29504 KB Output is correct
57 Correct 202 ms 29440 KB Output is correct
58 Correct 205 ms 29412 KB Output is correct
59 Correct 1062 ms 29372 KB Output is correct
60 Correct 1092 ms 29388 KB Output is correct
61 Correct 1071 ms 29356 KB Output is correct
62 Correct 1162 ms 29380 KB Output is correct
63 Correct 1142 ms 29276 KB Output is correct
64 Correct 1100 ms 29332 KB Output is correct
65 Execution timed out 5063 ms 8388 KB Time limit exceeded
66 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 492 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 5 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 5 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 412 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 496 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 192 ms 2844 KB Output is correct
36 Correct 196 ms 572 KB Output is correct
37 Correct 195 ms 484 KB Output is correct
38 Correct 192 ms 484 KB Output is correct
39 Correct 193 ms 480 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 163 ms 492 KB Output is correct
44 Correct 164 ms 488 KB Output is correct
45 Correct 164 ms 488 KB Output is correct
46 Correct 172 ms 588 KB Output is correct
47 Correct 166 ms 480 KB Output is correct
48 Correct 164 ms 480 KB Output is correct
49 Correct 196 ms 616 KB Output is correct
50 Correct 190 ms 484 KB Output is correct
51 Correct 193 ms 488 KB Output is correct
52 Correct 190 ms 488 KB Output is correct
53 Correct 193 ms 484 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 164 ms 488 KB Output is correct
58 Correct 165 ms 492 KB Output is correct
59 Correct 166 ms 608 KB Output is correct
60 Correct 171 ms 480 KB Output is correct
61 Correct 168 ms 484 KB Output is correct
62 Correct 170 ms 480 KB Output is correct
63 Correct 1296 ms 29308 KB Output is correct
64 Correct 1314 ms 29292 KB Output is correct
65 Correct 1338 ms 29348 KB Output is correct
66 Correct 1359 ms 29468 KB Output is correct
67 Correct 1324 ms 29388 KB Output is correct
68 Correct 1 ms 212 KB Output is correct
69 Correct 1 ms 212 KB Output is correct
70 Correct 199 ms 29504 KB Output is correct
71 Correct 202 ms 29440 KB Output is correct
72 Correct 205 ms 29412 KB Output is correct
73 Correct 1062 ms 29372 KB Output is correct
74 Correct 1092 ms 29388 KB Output is correct
75 Correct 1071 ms 29356 KB Output is correct
76 Correct 1162 ms 29380 KB Output is correct
77 Correct 1142 ms 29276 KB Output is correct
78 Correct 1100 ms 29332 KB Output is correct
79 Execution timed out 5063 ms 8388 KB Time limit exceeded
80 Halted 0 ms 0 KB -