結果

問題 No.956 Number of Unbalanced
ユーザー ei1333333ei1333333
提出日時 2019-12-19 02:10:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 7,144 bytes
コンパイル時間 2,658 ms
コンパイル使用メモリ 225,760 KB
実行使用メモリ 26,532 KB
最終ジャッジ日時 2024-07-06 23:19:53
合計ジャッジ時間 7,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
21,988 KB
testcase_01 AC 11 ms
21,888 KB
testcase_02 AC 12 ms
21,992 KB
testcase_03 AC 12 ms
21,976 KB
testcase_04 AC 12 ms
21,976 KB
testcase_05 AC 12 ms
21,992 KB
testcase_06 AC 186 ms
23,596 KB
testcase_07 AC 235 ms
23,320 KB
testcase_08 AC 168 ms
23,392 KB
testcase_09 AC 112 ms
23,068 KB
testcase_10 AC 185 ms
22,996 KB
testcase_11 AC 271 ms
24,004 KB
testcase_12 AC 191 ms
23,492 KB
testcase_13 AC 70 ms
25,896 KB
testcase_14 AC 331 ms
24,300 KB
testcase_15 AC 96 ms
26,024 KB
testcase_16 AC 185 ms
23,888 KB
testcase_17 AC 114 ms
26,532 KB
testcase_18 AC 333 ms
24,304 KB
testcase_19 AC 105 ms
25,960 KB
testcase_20 AC 316 ms
24,308 KB
testcase_21 AC 69 ms
25,772 KB
testcase_22 AC 112 ms
26,492 KB
testcase_23 AC 187 ms
23,928 KB
testcase_24 AC 110 ms
25,772 KB
testcase_25 AC 189 ms
23,076 KB
testcase_26 AC 78 ms
23,004 KB
testcase_27 AC 178 ms
23,088 KB
testcase_28 AC 192 ms
23,096 KB
testcase_29 AC 324 ms
24,332 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)};
}

const int MAX_A = 100000;

template< typename Monoid, typename OperatorMonoid, typename F, typename G, typename H >
struct LazySegmentTree {

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

int main() {
  int N;
  cin >> N;
  vector< int > A(N);
  cin >> A;
  for(auto &t : A) --t;

  vector< vector< int > > be(MAX_A);
  for(int i = 0; i < N; i++) {
    be[A[i]].emplace_back(i + 1);
  }

  int64 ret = 0;


  struct Node {
    int64 sum1, sum2, len;

    Node(int len) : sum1(0), sum2(0), len(len) {}
  };

  auto f = [](const Node &a, const Node &b) {
    Node c(a.len + b.len);
    c.sum1 = a.sum1 + b.sum1;
    c.sum2 = a.sum1 * b.len + a.sum2 + b.sum2;
    return c;
  };

  auto g = [](Node a, int64 x) {
    a.sum1 += x * a.len;
    a.sum2 += x * a.len * (a.len + 1) / 2;
    return a;
  };

  auto h = [](int64 x, int64 y) {
    return x + y;
  };

  LazySegmentTree< Node, int64, decltype(f), decltype(g), decltype(h) > seg(200001, f, g, h, Node(0), 0);
  for(int j = 0; j < 200001; j++) seg.set(j, Node(1));
  seg.build();
  seg.update(100000, 100001, 1);


  for(int i = 0; i < MAX_A; i++) {
    if(be[i].empty()) continue;
    vector< pair< int, int > > st;
    for(int j = 0; j <= be[i].size(); j++) {
      int left = 1;
      if(j > 0) left = be[i][j - 1] + 1;
      int right = N;
      if(j < be[i].size()) right = be[i][j] - 1;
      if(left <= right) st.emplace_back(-1, right - left + 1);
      if(j < be[i].size()) st.emplace_back(1, 1);
    }

    int cur = 100000;
    vector< tuple< int, int, int > > op;
    for(auto &p : st) {
      if(p.first == 1) {
        cur += p.first;
        ret += seg.query(0, cur).sum1;
        seg.update(cur, cur + 1, 1);
        op.emplace_back(cur, cur + 1, 1);
      } else {
        ret -= seg.query(0, cur - p.second - 1).sum2;
        ret += seg.query(0, cur - 1).sum2;
        seg.update(cur - p.second, cur, 1);
        op.emplace_back(cur - p.second, cur, 1);
        cur -= p.second;
      }
    }
    for(auto &p : op) {
      int x, y, z;
      tie(x, y, z) = p;
      seg.update(x, y, -z);
    }
  }
  cout << ret << endl;
}
0