結果

問題 No.1079 まお
ユーザー ei1333333ei1333333
提出日時 2020-06-12 21:53:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 4,641 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 217,252 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-09-06 10:19:26
合計ジャッジ時間 6,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 89 ms
5,484 KB
testcase_13 AC 19 ms
4,376 KB
testcase_14 AC 157 ms
6,872 KB
testcase_15 AC 84 ms
5,196 KB
testcase_16 AC 35 ms
4,376 KB
testcase_17 AC 151 ms
5,864 KB
testcase_18 AC 18 ms
4,376 KB
testcase_19 AC 260 ms
8,032 KB
testcase_20 AC 176 ms
6,484 KB
testcase_21 AC 129 ms
5,664 KB
testcase_22 AC 145 ms
6,420 KB
testcase_23 AC 134 ms
6,164 KB
testcase_24 AC 188 ms
7,216 KB
testcase_25 AC 242 ms
8,384 KB
testcase_26 AC 266 ms
8,940 KB
testcase_27 AC 56 ms
5,004 KB
testcase_28 AC 120 ms
6,452 KB
testcase_29 AC 135 ms
8,976 KB
testcase_30 AC 132 ms
8,832 KB
testcase_31 AC 59 ms
4,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, K;
  cin >> N >> K;
  vector< int > A(N);
  cin >> A;

  int64 ans = 0;
  auto rec = MFP([&](auto rec, int l, int r) -> void {
    if(l + 1 >= r) {
      if(A[l] + A[l] == K) ++ans;
      return;
    }
    int m = (l + r) / 2;
    rec(l, m);
    rec(m, r);
    // [l, m) -> [m, r)

    int ret = inf, cnt = 0;
    map< int, vector< pair< int, int64 > > > latte, malta;
    for(int i = m - 1; i >= l; i--) {
      if(chmin(ret, A[i])) cnt = 0;
      if(ret == A[i]) ++cnt;
      int rv = K - A[i];
      if(rv < 0) continue;
      if(cnt >= 2) latte[A[i]].emplace_back(ret, m - i);
      else malta[A[i]].emplace_back(ret, m - i);
    }
    for(auto &v : latte) {
      sort(begin(v.second), end(v.second));
      for(int k = 0; k + 1 < v.second.size(); k++) {
        v.second[k + 1].second += v.second[k].second;
      }
    }
    for(auto &v : malta) {
      sort(begin(v.second), end(v.second));
      for(int k = 0; k + 1 < v.second.size(); k++) {
        v.second[k + 1].second += v.second[k].second;
      }
    }

    // latte:= second未満の値が1個含まれる
    // malta:= second未満の値が1個含まれるまたは,すべての要素がsecond+1以上
    ret = inf, cnt = 0;
    for(int i = m; i < r; i++) {
      if(chmin(ret, A[i])) cnt = 0;
      if(ret == A[i]) ++cnt;
      int lv = K - A[i];
      if(lv < 0) continue;
      if(latte.count(lv)) {
        auto &vs = latte[lv];
        if(cnt == 1) { //secondがretよりも大きいものの個数
          int pos = upper_bound(begin(vs), end(vs), make_pair(ret, infll)) - begin(vs);
          int64 sz = vs.size() - pos;
          // pos+1,pos+2,....
          if(sz) {
            ans += (i - m + 1) * sz;
            ans += vs.back().second;
            if(pos) ans -= vs[pos - 1].second;
          }
        }
      }
      if(malta.count(lv)) {
        auto &vs = malta[lv];
        if(cnt == 1) { //secondがretよりも大きいものの個数
          int pos = upper_bound(begin(vs), end(vs), make_pair(ret, infll)) - begin(vs);
          int64 sz = vs.size() - pos;
          if(sz) {
            ans += (i - m + 1) * sz;
            ans += vs.back().second;
            if(pos) ans -= vs[pos - 1].second;
          }
        }
        //secondがretよりも小さいものの個数もOK
        {
          int pos = lower_bound(begin(vs), end(vs), make_pair(ret, -1LL)) - begin(vs);
          int64 sz = pos;
          if(sz) {
            ans += (i - m + 1) * sz;
            ans += vs[pos - 1].second;
          }
        }
      }
    }
  });
  rec(0, N);
  cout << ans << "\n";
}

0