답안 #792452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792452 2023-07-25T05:07:22 Z GEN 이지후(#10086) Real Mountains (CCO23_day1problem2) C++17
16 / 25
5000 ms 67296 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]);
	}
	void upd(int x, int v) {
		x += lim;
		tree[x] = v;
		while (x > 1) {
			x >>= 1;
			tree[x] = min(tree[2 * x], tree[2 * x + 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]);
		return ret;
	}
} 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]);
		seg.upd(byval[i][1], 1e9);
		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;

		int fp = *it;
		int gp = *--s.lower_bound(r);
		if (fp == gp) {
			tot += mint(1ll * (D - C) * (seg.query(l, fp - 1) + seg.query(fp + 1, r))) + sum(C, D - 1);
			continue;
		}
		int pcnt = 0;
		for (int j = l; j < r; j++) {
			if (a[j] <= C) {
				pcnt++;
			}
		}
		tot += sum(C, D - 1) * mint(pcnt);
		tot += sum(C + 1, D) * mint(2 * pcnt - 2);
		tot += mint(1ll * seg.query(l, fp - 1) * (D - C));
		tot += mint(1ll * seg.query(gp + 1, r - 1) * (D - C));
		tot += mint(1ll * (D - C) * min(seg.query(fp + 1, r - 1), seg.query(l, gp - 1))) - sum(C + 1, D);
	}
	cout << tot << "\n";
}
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 20 ms 8788 KB Output is correct
36 Correct 23 ms 8800 KB Output is correct
37 Correct 22 ms 8788 KB Output is correct
38 Correct 20 ms 8776 KB Output is correct
39 Correct 20 ms 8840 KB Output is correct
40 Correct 4 ms 8788 KB Output is correct
41 Correct 4 ms 8788 KB Output is correct
42 Correct 4 ms 8832 KB Output is correct
43 Correct 21 ms 8760 KB Output is correct
44 Correct 21 ms 8788 KB Output is correct
45 Correct 21 ms 8792 KB Output is correct
46 Correct 21 ms 8796 KB Output is correct
47 Correct 20 ms 8812 KB Output is correct
48 Correct 20 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 20 ms 8788 KB Output is correct
36 Correct 23 ms 8800 KB Output is correct
37 Correct 22 ms 8788 KB Output is correct
38 Correct 20 ms 8776 KB Output is correct
39 Correct 20 ms 8840 KB Output is correct
40 Correct 4 ms 8788 KB Output is correct
41 Correct 4 ms 8788 KB Output is correct
42 Correct 4 ms 8832 KB Output is correct
43 Correct 21 ms 8760 KB Output is correct
44 Correct 21 ms 8788 KB Output is correct
45 Correct 21 ms 8792 KB Output is correct
46 Correct 21 ms 8796 KB Output is correct
47 Correct 20 ms 8812 KB Output is correct
48 Correct 20 ms 8788 KB Output is correct
49 Correct 21 ms 8728 KB Output is correct
50 Correct 24 ms 8828 KB Output is correct
51 Correct 28 ms 8812 KB Output is correct
52 Correct 31 ms 8820 KB Output is correct
53 Correct 21 ms 8788 KB Output is correct
54 Correct 5 ms 8788 KB Output is correct
55 Correct 4 ms 8788 KB Output is correct
56 Correct 4 ms 8788 KB Output is correct
57 Correct 21 ms 8820 KB Output is correct
58 Correct 23 ms 8752 KB Output is correct
59 Correct 21 ms 8788 KB Output is correct
60 Correct 20 ms 8788 KB Output is correct
61 Correct 20 ms 8776 KB Output is correct
62 Correct 21 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 854 ms 67140 KB Output is correct
36 Correct 896 ms 67296 KB Output is correct
37 Correct 873 ms 67124 KB Output is correct
38 Correct 906 ms 67136 KB Output is correct
39 Correct 884 ms 67172 KB Output is correct
40 Correct 3 ms 8532 KB Output is correct
41 Correct 3 ms 8532 KB Output is correct
42 Correct 383 ms 67180 KB Output is correct
43 Correct 406 ms 67208 KB Output is correct
44 Correct 387 ms 67264 KB Output is correct
45 Correct 376 ms 67264 KB Output is correct
46 Correct 371 ms 67184 KB Output is correct
47 Correct 418 ms 67244 KB Output is correct
48 Correct 499 ms 67180 KB Output is correct
49 Correct 475 ms 67152 KB Output is correct
50 Correct 481 ms 67256 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 20 ms 8788 KB Output is correct
36 Correct 23 ms 8800 KB Output is correct
37 Correct 22 ms 8788 KB Output is correct
38 Correct 20 ms 8776 KB Output is correct
39 Correct 20 ms 8840 KB Output is correct
40 Correct 4 ms 8788 KB Output is correct
41 Correct 4 ms 8788 KB Output is correct
42 Correct 4 ms 8832 KB Output is correct
43 Correct 21 ms 8760 KB Output is correct
44 Correct 21 ms 8788 KB Output is correct
45 Correct 21 ms 8792 KB Output is correct
46 Correct 21 ms 8796 KB Output is correct
47 Correct 20 ms 8812 KB Output is correct
48 Correct 20 ms 8788 KB Output is correct
49 Correct 854 ms 67140 KB Output is correct
50 Correct 896 ms 67296 KB Output is correct
51 Correct 873 ms 67124 KB Output is correct
52 Correct 906 ms 67136 KB Output is correct
53 Correct 884 ms 67172 KB Output is correct
54 Correct 3 ms 8532 KB Output is correct
55 Correct 3 ms 8532 KB Output is correct
56 Correct 383 ms 67180 KB Output is correct
57 Correct 406 ms 67208 KB Output is correct
58 Correct 387 ms 67264 KB Output is correct
59 Correct 376 ms 67264 KB Output is correct
60 Correct 371 ms 67184 KB Output is correct
61 Correct 418 ms 67244 KB Output is correct
62 Correct 499 ms 67180 KB Output is correct
63 Correct 475 ms 67152 KB Output is correct
64 Correct 481 ms 67256 KB Output is correct
65 Execution timed out 5061 ms 20800 KB Time limit exceeded
66 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 6 ms 8768 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 4 ms 8788 KB Output is correct
7 Correct 4 ms 8788 KB Output is correct
8 Correct 4 ms 8788 KB Output is correct
9 Correct 4 ms 8788 KB Output is correct
10 Correct 4 ms 8788 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 4 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 3 ms 8432 KB Output is correct
15 Correct 3 ms 8532 KB Output is correct
16 Correct 4 ms 8788 KB Output is correct
17 Correct 6 ms 8788 KB Output is correct
18 Correct 5 ms 8788 KB Output is correct
19 Correct 5 ms 8712 KB Output is correct
20 Correct 5 ms 8796 KB Output is correct
21 Correct 4 ms 8788 KB Output is correct
22 Correct 4 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8788 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 5 ms 8788 KB Output is correct
28 Correct 4 ms 8788 KB Output is correct
29 Correct 3 ms 8532 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 8532 KB Output is correct
32 Correct 3 ms 8532 KB Output is correct
33 Correct 3 ms 8484 KB Output is correct
34 Correct 3 ms 8532 KB Output is correct
35 Correct 20 ms 8788 KB Output is correct
36 Correct 23 ms 8800 KB Output is correct
37 Correct 22 ms 8788 KB Output is correct
38 Correct 20 ms 8776 KB Output is correct
39 Correct 20 ms 8840 KB Output is correct
40 Correct 4 ms 8788 KB Output is correct
41 Correct 4 ms 8788 KB Output is correct
42 Correct 4 ms 8832 KB Output is correct
43 Correct 21 ms 8760 KB Output is correct
44 Correct 21 ms 8788 KB Output is correct
45 Correct 21 ms 8792 KB Output is correct
46 Correct 21 ms 8796 KB Output is correct
47 Correct 20 ms 8812 KB Output is correct
48 Correct 20 ms 8788 KB Output is correct
49 Correct 21 ms 8728 KB Output is correct
50 Correct 24 ms 8828 KB Output is correct
51 Correct 28 ms 8812 KB Output is correct
52 Correct 31 ms 8820 KB Output is correct
53 Correct 21 ms 8788 KB Output is correct
54 Correct 5 ms 8788 KB Output is correct
55 Correct 4 ms 8788 KB Output is correct
56 Correct 4 ms 8788 KB Output is correct
57 Correct 21 ms 8820 KB Output is correct
58 Correct 23 ms 8752 KB Output is correct
59 Correct 21 ms 8788 KB Output is correct
60 Correct 20 ms 8788 KB Output is correct
61 Correct 20 ms 8776 KB Output is correct
62 Correct 21 ms 8796 KB Output is correct
63 Correct 854 ms 67140 KB Output is correct
64 Correct 896 ms 67296 KB Output is correct
65 Correct 873 ms 67124 KB Output is correct
66 Correct 906 ms 67136 KB Output is correct
67 Correct 884 ms 67172 KB Output is correct
68 Correct 3 ms 8532 KB Output is correct
69 Correct 3 ms 8532 KB Output is correct
70 Correct 383 ms 67180 KB Output is correct
71 Correct 406 ms 67208 KB Output is correct
72 Correct 387 ms 67264 KB Output is correct
73 Correct 376 ms 67264 KB Output is correct
74 Correct 371 ms 67184 KB Output is correct
75 Correct 418 ms 67244 KB Output is correct
76 Correct 499 ms 67180 KB Output is correct
77 Correct 475 ms 67152 KB Output is correct
78 Correct 481 ms 67256 KB Output is correct
79 Execution timed out 5061 ms 20800 KB Time limit exceeded
80 Halted 0 ms 0 KB -