結果

問題 No.3801 Split Pairs
コンテスト
ユーザー hiro1729
提出日時 2026-11-13 11:57:05
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 151 ms / 2,000 ms
+ 309µs
コード長 1,618 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,888 ms
コンパイル使用メモリ 345,268 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2026-11-13 22:08:38
合計ジャッジ時間 7,101 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int N; cin >> N;
	vector<ll> A(N);
	for (int i = 0; i < N; i++) cin >> A[i];
	ll ans = 0;
	set<pair<ll, ll>> intv;
	auto add = [&] (ll x) -> void {
		auto it = intv.lower_bound({x, inf});
		if (intv.empty() || it == intv.begin()) {
			it = intv.insert({x, x + 1}).first;
		} else {
			--it;
			if (it->second == x) {
				pair<ll, ll> v = *it;
				v.second++;
				intv.erase(it);
				it = intv.insert(v).first;
			} else {
				it = intv.insert({x, x + 1}).first;
			}
		}
		if (next(it) != intv.end() && it->second == next(it)->first) {
			pair<ll, ll> v = *it;
			v.second = next(it)->second;
			intv.erase(next(it));
			intv.erase(it);
			intv.insert(v);
		}
	};
	auto del = [&] (ll x) -> void {
		auto it = intv.lower_bound({x, inf}); --it;
		if (x == it->first) {
			pair<ll, ll> v = *it;
			v.first++;
			intv.erase(it);
			if (v.first != v.second) {
				intv.insert(v);
			}
		} else if (x == it->second - 1) {
			pair<ll, ll> v = *it;
			v.second--;
			intv.erase(it);
			intv.insert(v);
		} else {
			pair<ll, ll> v = *it;
			intv.erase(it);
			intv.insert({v.first, x});
			intv.insert({x + 1, v.second});
		}
	};
	for (ll a: A) {
		auto it = intv.lower_bound({a + 1, -inf});
		if (it == intv.begin()) {
			add(a);
		} else {
			it--;
			if (a < (*it).second) {
				ll L = (*it).first, R = (*it).second - 1;
				ans += (a - L + 1) * (R - a + 1);
				del(L + R - a);
				add(L - 1);
				add(R + 1);
			} else {
				add(a);
			}
		}
	}
	cout << ans << '\n';
}
0