結果

問題 No.1435 Mmm......
ユーザー 👑 emthrmemthrm
提出日時 2021-03-19 23:30:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 748 ms / 2,000 ms
コード長 2,960 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 221,348 KB
実行使用メモリ 51,480 KB
最終ジャッジ日時 2024-04-29 19:21:39
合計ジャッジ時間 16,794 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 473 ms
36,256 KB
testcase_07 AC 581 ms
42,824 KB
testcase_08 AC 678 ms
48,984 KB
testcase_09 AC 658 ms
45,492 KB
testcase_10 AC 556 ms
40,048 KB
testcase_11 AC 524 ms
39,620 KB
testcase_12 AC 481 ms
37,180 KB
testcase_13 AC 467 ms
36,584 KB
testcase_14 AC 351 ms
26,636 KB
testcase_15 AC 691 ms
50,412 KB
testcase_16 AC 595 ms
42,652 KB
testcase_17 AC 389 ms
29,492 KB
testcase_18 AC 697 ms
51,152 KB
testcase_19 AC 508 ms
38,880 KB
testcase_20 AC 674 ms
46,968 KB
testcase_21 AC 692 ms
51,356 KB
testcase_22 AC 665 ms
51,356 KB
testcase_23 AC 717 ms
51,356 KB
testcase_24 AC 742 ms
51,356 KB
testcase_25 AC 740 ms
51,352 KB
testcase_26 AC 745 ms
51,480 KB
testcase_27 AC 748 ms
51,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Semigroup, typename Fn>
struct DisjointSparseTable {
  DisjointSparseTable(const std::vector<Semigroup> &a, Fn fn) : fn(fn) {
    int n = a.size(), table_h = 1;
    while ((1 << table_h) < n) ++table_h;
    lg.assign(1 << table_h, 0);
    for (int i = 2; i < (1 << table_h); ++i) lg[i] = lg[i >> 1] + 1;
    dat.assign(table_h, std::vector<Semigroup>(n));
    for (int j = 0; j < n; ++j) dat[0][j] = a[j];
    for (int i = 1; i < table_h; ++i) {
      int shift = 1 << i;
      for (int left = 0; left < n; left += shift << 1) {
        int mid = std::min(left + shift, n);
        dat[i][mid - 1] = a[mid - 1];
        for (int j = mid - 2; j >= left; --j) dat[i][j] = fn(a[j], dat[i][j + 1]);
        if (n <= mid) break;
        int right = std::min(mid + shift, n);
        dat[i][mid] = a[mid];
        for (int j = mid + 1; j < right; ++j) dat[i][j] = fn(dat[i][j - 1], a[j]);
      }
    }
  }

  Semigroup query(int left, int right) const {
    assert(left < right);
    if (left == --right) return dat[0][left];
    int h = lg[left ^ right];
    return fn(dat[h][left], dat[h][right]);
  }

private:
  Fn fn;
  std::vector<int> lg;
  std::vector<std::vector<Semigroup>> dat;
};

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  DisjointSparseTable mx(a, [](int a, int b) { return max(a, b); });
  struct Node {
    int m1 = -1, m2 = -1;
  };
  vector<Node> b;
  REP(i, n) b.emplace_back(Node{a[i], -1});
  DisjointSparseTable mn(b, [](const Node &a, const Node &b) {
    vector<int> v;
    if (a.m1 != -1) v.emplace_back(a.m1);
    if (a.m2 != -1) v.emplace_back(a.m2);
    if (b.m1 != -1) v.emplace_back(b.m1);
    if (b.m2 != -1) v.emplace_back(b.m2);
    sort(ALL(v));
    assert(v.size() >= 2);
    return Node{v[0], v[1]};
  });
  ll ans = 0;
  REP(i, n) {
    int l = i, r = n;
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      auto m1m2 = mn.query(i, mid + 1);
      (mx.query(i, mid + 1) <= m1m2.m1 + m1m2.m2 ? l : r) = mid;
    }
    ans += l - i;
  }
  cout << ans << '\n';
  return 0;
}
0