답안 #792522

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792522 2023-07-25T06:20:46 Z 박영우(#10055) Real Mountains (CCO23_day1problem2) C++17
0 / 25
5000 ms 79412 KB
#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 1010101
#define MAXS 500
#define INF 1000000020
#define bb ' '
#define ln '\n'
#define Ln '\n'
struct node {
	int mn;
	int mn2;
	int cnt;
	int mnl, mnr;
	node(int x = 0, int ind = 0) {
		mnl = mnr = ind;
		if (!~x) {
			mn = mn2 = cnt = -1;
			return;
		}
		mn = x;
		mn2 = INF;
		cnt = 1;
	}
};
node operator+(node n1, node n2) {
	if (!~n1.cnt) return n2;
	if (!~n2.cnt) return n1;
	node ret;
	ret.mn = min(n1.mn, n2.mn);
	ret.cnt = 0;
	ret.mnl = INF;
	ret.mnr = -INF;
	if (ret.mn == n1.mn) ret.cnt += n1.cnt, ret.mnl = min(ret.mnl, n1.mnl), ret.mnr = max(ret.mnr, n1.mnr);
	if (ret.mn == n2.mn) ret.cnt += n2.cnt, ret.mnl = min(ret.mnl, n2.mnl), ret.mnr = max(ret.mnr, n2.mnr);
	if (n1.mn != ret.mn) ret.mn2 = min(ret.mn2, n1.mn);
	if (n2.mn != ret.mn) ret.mn2 = min(ret.mn2, n2.mn);
	ret.mn2 = min(ret.mn2, n1.mn2);
	ret.mn2 = min(ret.mn2, n2.mn2);
	return ret;
}
int H[MAX];
int ch[MAX];
namespace segtree {
	int N;
	node tree[MAX * 4];
	int lazy[MAX * 4];
	void init(int s, int e, int loc = 1) {
		lazy[loc] = -1;
		if (s == e) {
			tree[loc] = node(H[s], s);
			return;
		}
		int m = s + e >> 1;
		init(s, m, loc * 2);
		init(m + 1, e, loc * 2 + 1);
		tree[loc] = tree[loc * 2] + tree[loc * 2 + 1];
	}
	void prop(int loc) {
		for (auto c : { loc * 2, loc * 2 + 1 }) {
			tree[c].mn = max(tree[c].mn, lazy[loc]);
			lazy[c] = max(lazy[c], lazy[loc]);
		}
		lazy[loc] = -1;
	}
	void upd(int s, int e, int l, int r, int v, int loc = 1) {
		if (s != e) prop(loc);
		if (e < l || r < s) return;
		if (l <= s && e <= r) {
			if (v < tree[loc].mn2) {
				if (v > tree[loc].mn) {
					tree[loc].mn = v;
					lazy[loc] = max(lazy[loc], v);
					return;
				}
				else return;
			}
		}
		int m = s + e >> 1;
		upd(s, m, l, r, v, loc * 2);
		upd(m + 1, e, l, r, v, loc * 2 + 1);
		tree[loc] = tree[loc * 2], tree[loc * 2 + 1];
	}
	void upd(int low, int r, int v) { upd(1, N, low, r, v); }
	node query(int s, int e, int l, int r, int loc = 1) {
		if (s != e) prop(loc);
		if (e < l || r < s) return node(-1);
		if (l <= s && e <= r) return tree[loc];
		int m = s + e >> 1;
		return query(s, m, l, r, loc * 2) + query(m + 1, e, l, r, loc * 2 + 1);
	}
	node query(int low, int r) { return query(1, N, low, r); }
}
inline ll rsum(ll n) { return n * (n + 1) / 2; }
inline ll rsum(ll low, ll r) { return rsum(r) - rsum(low - 1); }
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	segtree::N = N;
	int i;
	for (i = 1; i <= N; i++) cin >> H[i];
	segtree::init(1, N);
	int low, high;
	low = -1;
	for (i = 1; i < N; i++) if (H[i] > H[i + 1]) {
		low = i;
		break;
	}
	if (!~low) {
		cout << 0 << ln;
		return 0;
	}
	for (i = N; i > 1; i--) if (H[i] > H[i - 1]) {
		high = i;
		break;
	}
	if (low == high) {
		cout << 0 << ln;
		return 0;
	}
	set<int> lst, rst;
	for (i = 1; i <= low; i++) lst.insert(H[i]);
	for (i = high; i <= N; i++) rst.insert(H[i]);
	ll ans = 0;
	while (low < high) {
		while (low < N && segtree::query(low, low).mn <= segtree::query(low + 1, low + 1).mn) low++, lst.insert(H[low]);
		while (high > 1 && segtree::query(high, high).mn <= segtree::query(high - 1, high - 1).mn) high--, rst.insert(H[high]);
		if (low == high) break;
		auto res = segtree::query(low + 1, high - 1);
		int r = res.mn2;
		int mn = res.mn;
		int n = res.cnt;
		auto itl = lst.upper_bound(mn);
		auto itr = rst.upper_bound(mn);
		int up = min(res.mn2, min(*itl, *itr));
		int ml = min(*itl, segtree::query(low, res.mnl).mn2);
		int mr = min(*itr, segtree::query(res.mnr, high).mn2);
		if (res.cnt == 1) {
			ans += 1ll * (ml + mr) * (up - mn);
			segtree::upd(low, high, up);
			continue;
		}
		ans += rsum(mn + 1, up) * (n * 2 - 3);
		ans += 1ll * (up + ml + mr) * (up - mn);
		segtree::upd(low, high, up);
	}
	int mv = 1;
	for (i = 1; i <= N; i++) if (H[mv] < H[i]) mv = i;
	for (i = 1; i <= N; i++) ch[i] = H[i];
	for (i = 1; i <= mv; i++) ch[i] = max(ch[i], ch[i - 1]);
	for (i = N; i >= mv; i--) ch[i] = max(ch[i], ch[i + 1]);
	for (i = 1; i <= N; i++) ans += rsum(H[i], ch[i] - 1);
	cout << ans << ln;
}

Compilation message

Main.cpp: In function 'void segtree::init(int, int, int)':
Main.cpp:61:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |   int m = s + e >> 1;
      |           ~~^~~
Main.cpp: In function 'void segtree::upd(int, int, int, int, int, int)':
Main.cpp:86:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int m = s + e >> 1;
      |           ~~^~~
Main.cpp:89:46: warning: right operand of comma operator has no effect [-Wunused-value]
   89 |   tree[loc] = tree[loc * 2], tree[loc * 2 + 1];
      |                              ~~~~~~~~~~~~~~~~^
Main.cpp: In function 'node segtree::query(int, int, int, int, int)':
Main.cpp:96:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   96 |   int m = s + e >> 1;
      |           ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:138:7: warning: unused variable 'r' [-Wunused-variable]
  138 |   int r = res.mn2;
      |       ^
Main.cpp:111:11: warning: 'high' may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |  int low, high;
      |           ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 79340 KB Output is correct
2 Correct 31 ms 79412 KB Output is correct
3 Execution timed out 5101 ms 79332 KB Time limit exceeded
4 Halted 0 ms 0 KB -