Submission #792446

# Submission time Handle Problem Language Result Execution time Memory
792446 2023-07-25T05:04:01 Z GEN 이지후(#10086) Real Mountains (CCO23_day1problem2) C++17
16 / 25
5000 ms 88560 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;
const int MAXT = 2100000;

// 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; }
};

struct seg {
	int tree[MAXT], lim;
	void init(vector<int> &a) {
		memset(tree, 0x3f, sizeof(tree));
		for (lim = 1; lim <= sz(a); lim <<= 1)
			;
		for (int i = 0; i < sz(a); i++) {
			tree[i + lim] = a[i];
		}
		for (int i = lim - 1; i; i--)
			tree[i] = min(tree[2 * i], tree[2 * i + 1]);
	}
	int query(int s, int e) {
		s += lim;
		e += lim;
		int ret = 1e9;
		while (s < e) {
			if (s % 2 == 1)
				ret = min(ret, tree[s++]);
			if (e % 2 == 0)
				ret = min(ret, tree[e--]);
			s >>= 1;
			e >>= 1;
		}
		if (s == e)
			ret = min(ret, tree[s]);
	}
} seg;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	vector<int> a(n);
	vector<pi> byval;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		byval.push_back({a[i], i});
	}
	seg.init(a);
	sort(all(byval));
	mint tot = 0;
	auto sum = [&](int l, int r) { return mint(1ll * (r - l + 1) * (r + l) / 2); };
	int l = 0, r = n;
	set<int> s;
	for (int i = 0; i < sz(byval) - 1; i++) {
		s.insert(byval[i][1]);
		if (byval[i][0] == byval[i + 1][0])
			continue;
		int C = byval[i][0];
		int D = byval[i + 1][0];
		while (l < n && a[l] <= C)
			l++;
		while (r > l && a[r - 1] <= C)
			r--;
		if (l == r)
			break;
		auto it = s.lower_bound(l);
		if (it == s.end() || *it >= r)
			continue;

		vector<int> pos;
		vector<pi> mswp;
		int mx = 1e9;
		for (int j = l; j < r; j++) {
			if (a[j] <= C) {
				pos.push_back(j);
				mswp.push_back({mx, 0});
			} else
				mx = min(mx, a[j]);
		}
		assert(sz(pos));
		int ptr = sz(pos);
		mx = 1e9;
		for (int j = r - 1; j >= l; j--) {
			if (a[j] <= C) {
				mswp[--ptr][1] = mx;
			} else
				mx = min(mx, a[j]);
		}
		if (sz(pos) == 1) {
			tot += mint(1ll * (D - C) * (mswp[0][0] + mswp[0][1])) + sum(C, D - 1);
			continue;
		}
		tot += sum(C, D - 1) * mint(sz(pos));
		tot += sum(C + 1, D) * mint(2 * sz(pos) - 2);
		tot += mint(1ll * mswp[0][0] * (D - C));
		tot += mint(1ll * mswp.back()[1] * (D - C));
		tot += mint(1ll * (D - C) * min(mswp[0][1], mswp.back()[0])) - sum(C + 1, D);
	}
	cout << tot << "\n";
}

Compilation message

Main.cpp: In member function 'int seg::query(int, int)':
Main.cpp:89:2: warning: no return statement in function returning non-void [-Wreturn-type]
   89 |  }
      |  ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 172 ms 8940 KB Output is correct
36 Correct 172 ms 8940 KB Output is correct
37 Correct 172 ms 8944 KB Output is correct
38 Correct 171 ms 8948 KB Output is correct
39 Correct 173 ms 8944 KB Output is correct
40 Correct 5 ms 8888 KB Output is correct
41 Correct 5 ms 8916 KB Output is correct
42 Correct 5 ms 8916 KB Output is correct
43 Correct 171 ms 8940 KB Output is correct
44 Correct 168 ms 8948 KB Output is correct
45 Correct 167 ms 8944 KB Output is correct
46 Correct 157 ms 8936 KB Output is correct
47 Correct 161 ms 9188 KB Output is correct
48 Correct 157 ms 8936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 172 ms 8940 KB Output is correct
36 Correct 172 ms 8940 KB Output is correct
37 Correct 172 ms 8944 KB Output is correct
38 Correct 171 ms 8948 KB Output is correct
39 Correct 173 ms 8944 KB Output is correct
40 Correct 5 ms 8888 KB Output is correct
41 Correct 5 ms 8916 KB Output is correct
42 Correct 5 ms 8916 KB Output is correct
43 Correct 171 ms 8940 KB Output is correct
44 Correct 168 ms 8948 KB Output is correct
45 Correct 167 ms 8944 KB Output is correct
46 Correct 157 ms 8936 KB Output is correct
47 Correct 161 ms 9188 KB Output is correct
48 Correct 157 ms 8936 KB Output is correct
49 Correct 172 ms 8944 KB Output is correct
50 Correct 194 ms 8940 KB Output is correct
51 Correct 184 ms 8944 KB Output is correct
52 Correct 171 ms 8940 KB Output is correct
53 Correct 178 ms 8940 KB Output is correct
54 Correct 5 ms 8916 KB Output is correct
55 Correct 4 ms 8916 KB Output is correct
56 Correct 5 ms 8916 KB Output is correct
57 Correct 169 ms 8948 KB Output is correct
58 Correct 169 ms 8940 KB Output is correct
59 Correct 179 ms 8944 KB Output is correct
60 Correct 164 ms 8948 KB Output is correct
61 Correct 163 ms 8948 KB Output is correct
62 Correct 166 ms 8940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 1936 ms 87864 KB Output is correct
36 Correct 1973 ms 87960 KB Output is correct
37 Correct 1927 ms 87968 KB Output is correct
38 Correct 1897 ms 87976 KB Output is correct
39 Correct 1992 ms 88016 KB Output is correct
40 Correct 4 ms 8532 KB Output is correct
41 Correct 4 ms 8532 KB Output is correct
42 Correct 457 ms 88468 KB Output is correct
43 Correct 463 ms 88492 KB Output is correct
44 Correct 478 ms 88560 KB Output is correct
45 Correct 1276 ms 88020 KB Output is correct
46 Correct 1246 ms 88016 KB Output is correct
47 Correct 1285 ms 87996 KB Output is correct
48 Correct 1485 ms 88012 KB Output is correct
49 Correct 1402 ms 87984 KB Output is correct
50 Correct 1443 ms 87972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 172 ms 8940 KB Output is correct
36 Correct 172 ms 8940 KB Output is correct
37 Correct 172 ms 8944 KB Output is correct
38 Correct 171 ms 8948 KB Output is correct
39 Correct 173 ms 8944 KB Output is correct
40 Correct 5 ms 8888 KB Output is correct
41 Correct 5 ms 8916 KB Output is correct
42 Correct 5 ms 8916 KB Output is correct
43 Correct 171 ms 8940 KB Output is correct
44 Correct 168 ms 8948 KB Output is correct
45 Correct 167 ms 8944 KB Output is correct
46 Correct 157 ms 8936 KB Output is correct
47 Correct 161 ms 9188 KB Output is correct
48 Correct 157 ms 8936 KB Output is correct
49 Correct 1936 ms 87864 KB Output is correct
50 Correct 1973 ms 87960 KB Output is correct
51 Correct 1927 ms 87968 KB Output is correct
52 Correct 1897 ms 87976 KB Output is correct
53 Correct 1992 ms 88016 KB Output is correct
54 Correct 4 ms 8532 KB Output is correct
55 Correct 4 ms 8532 KB Output is correct
56 Correct 457 ms 88468 KB Output is correct
57 Correct 463 ms 88492 KB Output is correct
58 Correct 478 ms 88560 KB Output is correct
59 Correct 1276 ms 88020 KB Output is correct
60 Correct 1246 ms 88016 KB Output is correct
61 Correct 1285 ms 87996 KB Output is correct
62 Correct 1485 ms 88012 KB Output is correct
63 Correct 1402 ms 87984 KB Output is correct
64 Correct 1443 ms 87972 KB Output is correct
65 Execution timed out 5077 ms 20400 KB Time limit exceeded
66 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 3 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 7 ms 8916 KB Output is correct
5 Correct 4 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8768 KB Output is correct
8 Correct 5 ms 8876 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 9 ms 8912 KB Output is correct
12 Correct 7 ms 8952 KB Output is correct
13 Correct 8 ms 8916 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 3 ms 8476 KB Output is correct
16 Correct 9 ms 8912 KB Output is correct
17 Correct 8 ms 8916 KB Output is correct
18 Correct 8 ms 8916 KB Output is correct
19 Correct 8 ms 8916 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 8 ms 8964 KB Output is correct
24 Correct 7 ms 8916 KB Output is correct
25 Correct 7 ms 8916 KB Output is correct
26 Correct 7 ms 8916 KB Output is correct
27 Correct 8 ms 8932 KB Output is correct
28 Correct 7 ms 8948 KB Output is correct
29 Correct 4 ms 8532 KB Output is correct
30 Correct 4 ms 8532 KB Output is correct
31 Correct 4 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 172 ms 8940 KB Output is correct
36 Correct 172 ms 8940 KB Output is correct
37 Correct 172 ms 8944 KB Output is correct
38 Correct 171 ms 8948 KB Output is correct
39 Correct 173 ms 8944 KB Output is correct
40 Correct 5 ms 8888 KB Output is correct
41 Correct 5 ms 8916 KB Output is correct
42 Correct 5 ms 8916 KB Output is correct
43 Correct 171 ms 8940 KB Output is correct
44 Correct 168 ms 8948 KB Output is correct
45 Correct 167 ms 8944 KB Output is correct
46 Correct 157 ms 8936 KB Output is correct
47 Correct 161 ms 9188 KB Output is correct
48 Correct 157 ms 8936 KB Output is correct
49 Correct 172 ms 8944 KB Output is correct
50 Correct 194 ms 8940 KB Output is correct
51 Correct 184 ms 8944 KB Output is correct
52 Correct 171 ms 8940 KB Output is correct
53 Correct 178 ms 8940 KB Output is correct
54 Correct 5 ms 8916 KB Output is correct
55 Correct 4 ms 8916 KB Output is correct
56 Correct 5 ms 8916 KB Output is correct
57 Correct 169 ms 8948 KB Output is correct
58 Correct 169 ms 8940 KB Output is correct
59 Correct 179 ms 8944 KB Output is correct
60 Correct 164 ms 8948 KB Output is correct
61 Correct 163 ms 8948 KB Output is correct
62 Correct 166 ms 8940 KB Output is correct
63 Correct 1936 ms 87864 KB Output is correct
64 Correct 1973 ms 87960 KB Output is correct
65 Correct 1927 ms 87968 KB Output is correct
66 Correct 1897 ms 87976 KB Output is correct
67 Correct 1992 ms 88016 KB Output is correct
68 Correct 4 ms 8532 KB Output is correct
69 Correct 4 ms 8532 KB Output is correct
70 Correct 457 ms 88468 KB Output is correct
71 Correct 463 ms 88492 KB Output is correct
72 Correct 478 ms 88560 KB Output is correct
73 Correct 1276 ms 88020 KB Output is correct
74 Correct 1246 ms 88016 KB Output is correct
75 Correct 1285 ms 87996 KB Output is correct
76 Correct 1485 ms 88012 KB Output is correct
77 Correct 1402 ms 87984 KB Output is correct
78 Correct 1443 ms 87972 KB Output is correct
79 Execution timed out 5077 ms 20400 KB Time limit exceeded
80 Halted 0 ms 0 KB -