結果

問題 No.3801 Split Pairs
コンテスト
ユーザー ぽえ
提出日時 2026-11-07 12:29:59
言語 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  
実行時間 171 ms / 2,000 ms
+ 108µs
コード長 823 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,654 ms
コンパイル使用メモリ 344,380 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2026-11-13 22:00:31
合計ジャッジ時間 7,917 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    int n; cin >> n;
	
    map<ll, ll> s;
    ll ans = 0;
    auto add = [&](ll l, ll r) {
        auto it = s.upper_bound(r+1);
        while (it != s.begin() && l-1 <= prev(it)->second) {
            it--;
            l = min(l, it->first), r = max(r, it->second);
            it = s.erase(it);
        }
        s[l] = r;
    };
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        auto it = s.upper_bound(x);
        if (it == s.begin() || prev(it)->second < x) { add(x, x); continue; }
        auto [l, r] = *prev(it);
        ans += (x-l+1) * (r-x+1);
        add(l-1, r+1);
        auto it2 = prev(s.upper_bound(l+r-x));
        s[l+r-x+1] = it2->second;
        it2->second = l+r-x-1;
    }
	
    cout << ans << endl;
}
0