結果

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

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T> ostream &operator<<(ostream &os, const vector<T> &as);
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


#define l first
#define r second

constexpr Int INF = 1001001001001001001LL;

int N;
vector<Int> A;

int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) scanf("%lld", &A[i]);
    sort(A.begin(), A.end());
    
    set<pair<Int, Int>> fs;
    fs.emplace(-INF, -INF);
    fs.emplace(+INF, +INF);
    auto add = [&](Int l, Int r) -> void {
      auto it = fs.lower_bound(make_pair(l, r));
      if (r + 1 == it->first) {
        r = it->second;
        it = fs.erase(it);
      }
      --it;
      if (it->second == l - 1) {
        l = it->first;
        it = fs.erase(it);
      }
      fs.emplace(l, r);
    };
    for (const Int a : A) {
      auto it = fs.lower_bound(make_pair(a, +INF));
      --it;
      const Int l = it->first;
      const Int r = it->second;
      if (l <= a && a <= r) {
        const Int b = l + r - a;
        fs.erase(it);
        add(l - 1, b - 1);
        add(b + 1, r + 1);
      } else {
        add(a, a);
      }
// cerr<<a<<"; ";pv(fs.begin(),fs.end());
    }
    
    __int128 s = 0, t = 0;
    for (const Int a : A) s += a*a;
    for (const auto &f : fs) {
      const Int l = f.first;
      const Int r = f.second;
      if (-INF < l && r < +INF) {
        t += ((__int128)r*(r+1)*(2*r+1)/6 - (__int128)l*(l-1)*(2*l-1)/6);
      }
    }
    const Int ans = (t - s) / 2;
    printf("%lld\n", ans);
  }
  return 0;
}
0