結果

問題 No.738 平らな農地
ユーザー risujirohrisujiroh
提出日時 2019-04-29 00:58:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 477 ms / 2,000 ms
コード長 4,631 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 186,488 KB
実行使用メモリ 30,264 KB
最終ジャッジ日時 2024-06-01 10:32:05
合計ジャッジ時間 20,201 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,940 KB
testcase_05 AC 9 ms
6,944 KB
testcase_06 AC 9 ms
6,940 KB
testcase_07 AC 8 ms
6,944 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 228 ms
27,768 KB
testcase_16 AC 235 ms
28,176 KB
testcase_17 AC 266 ms
27,908 KB
testcase_18 AC 261 ms
27,468 KB
testcase_19 AC 314 ms
29,476 KB
testcase_20 AC 240 ms
28,168 KB
testcase_21 AC 300 ms
29,736 KB
testcase_22 AC 247 ms
28,400 KB
testcase_23 AC 287 ms
28,708 KB
testcase_24 AC 287 ms
28,404 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 4 ms
6,944 KB
testcase_28 AC 4 ms
6,940 KB
testcase_29 AC 4 ms
6,940 KB
testcase_30 AC 4 ms
6,940 KB
testcase_31 AC 4 ms
6,944 KB
testcase_32 AC 4 ms
6,940 KB
testcase_33 AC 4 ms
6,940 KB
testcase_34 AC 4 ms
6,944 KB
testcase_35 AC 4 ms
6,944 KB
testcase_36 AC 4 ms
6,944 KB
testcase_37 AC 4 ms
6,944 KB
testcase_38 AC 4 ms
6,940 KB
testcase_39 AC 4 ms
6,940 KB
testcase_40 AC 4 ms
6,944 KB
testcase_41 AC 4 ms
6,940 KB
testcase_42 AC 4 ms
6,940 KB
testcase_43 AC 4 ms
6,944 KB
testcase_44 AC 3 ms
6,944 KB
testcase_45 AC 230 ms
30,136 KB
testcase_46 AC 228 ms
27,768 KB
testcase_47 AC 226 ms
29,240 KB
testcase_48 AC 206 ms
28,176 KB
testcase_49 AC 203 ms
28,036 KB
testcase_50 AC 205 ms
29,180 KB
testcase_51 AC 242 ms
29,248 KB
testcase_52 AC 222 ms
27,908 KB
testcase_53 AC 227 ms
28,572 KB
testcase_54 AC 237 ms
29,468 KB
testcase_55 AC 241 ms
29,376 KB
testcase_56 AC 235 ms
28,976 KB
testcase_57 AC 216 ms
27,636 KB
testcase_58 AC 223 ms
28,844 KB
testcase_59 AC 224 ms
30,128 KB
testcase_60 AC 218 ms
29,872 KB
testcase_61 AC 220 ms
29,512 KB
testcase_62 AC 211 ms
27,736 KB
testcase_63 AC 249 ms
30,132 KB
testcase_64 AC 237 ms
29,596 KB
testcase_65 AC 377 ms
28,132 KB
testcase_66 AC 396 ms
28,940 KB
testcase_67 AC 269 ms
27,764 KB
testcase_68 AC 272 ms
28,572 KB
testcase_69 AC 396 ms
28,944 KB
testcase_70 AC 322 ms
30,000 KB
testcase_71 AC 18 ms
6,940 KB
testcase_72 AC 205 ms
27,732 KB
testcase_73 AC 182 ms
26,076 KB
testcase_74 AC 253 ms
28,972 KB
testcase_75 AC 363 ms
27,604 KB
testcase_76 AC 289 ms
28,532 KB
testcase_77 AC 405 ms
27,600 KB
testcase_78 AC 445 ms
29,872 KB
testcase_79 AC 419 ms
29,876 KB
testcase_80 AC 327 ms
28,712 KB
testcase_81 AC 373 ms
28,040 KB
testcase_82 AC 373 ms
29,068 KB
testcase_83 AC 231 ms
30,264 KB
testcase_84 AC 210 ms
29,112 KB
testcase_85 AC 477 ms
29,868 KB
testcase_86 AC 398 ms
27,504 KB
testcase_87 AC 141 ms
28,804 KB
testcase_88 AC 133 ms
27,636 KB
testcase_89 AC 2 ms
6,940 KB
testcase_90 AC 2 ms
6,940 KB
testcase_91 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

#pragma GCC target ("avx")
struct Bitvector {
  using T = uint32_t;
  static constexpr int W = 32;
  static int popcnt(T x) { return __builtin_popcount(x); }

  const int n;
  V<T> b;
  V<> c;
  Bitvector(int n) : n(n), b(n / W + 1), c(n / W + 1) {}
  void set(int i) { b[i / W] |= (T)1 << i % W; }
  void build() { for (int i = 0; i < n / W; ++i) c[i + 1] = c[i] + popcnt(b[i]); }
  bool operator[](int i) const { return b[i / W] >> i % W & 1; }
  int rank(int i) const { return c[i / W] + popcnt(b[i / W] & ~(~(T)0 << i % W)); }
  int rank(bool v, int i) const { return v ? rank(i) : i - rank(i); }
};
template<class T> struct WaveletMatrix {
  const int n, h;
  V<Bitvector> b;
  V<> z;
  template<class Itr> WaveletMatrix(Itr first, Itr last) : n(distance(first, last)), h(__lg(2 * *max_element(first, last) + 1)), b(h, Bitvector(n)), z(h) {
    assert(*min_element(first, last) >= 0);
    V<T> a(first, last), na(n);
    for (int j = 0; j < h; ++j) {
      for (int i = 0; i < n; ++i) {
        if (a[i] >> h + ~j & 1) b[j].set(i);
        else ++z[j];
      }
      b[j].build();
      int p = 0, q = z[j];
      for (int i = 0; i < n; ++i) (b[j][i] ? na[q++] : na[p++]) = a[i];
      swap(a, na);
    }
  }
  T operator[](int i) const {
    T res = 0;
    for (int j = 0; j < h; ++j) {
      T v = b[j][i];
      res |= v << h + ~j;
      i = v * z[j] + b[j].rank(v, i);
    }
    return res;
  }
  int rank(int l, int r, T x) const {
    if (x < 0 or x >= (T)1 << h) return 0;
    for (int j = 0; j < h; ++j) {
      int v = x >> h + ~j & 1;
      l = v * z[j] + b[j].rank(v, l);
      r = v * z[j] + b[j].rank(v, r);
    }
    return r - l;
  }
  T kth(int l, int r, int k) const {
    assert(0 <= k and k < r - l);
    T res = 0;
    for (int j = 0; j < h; ++j) {
      int p = b[j].rank(0, l), q = b[j].rank(0, r);
      T v = k >= q - p;
      k -= v * (q - p);
      res |= v << h + ~j;
      l = v * z[j] + (v ? l - p : p);
      r = v * z[j] + (v ? r - q : q);
    }
    return res;
  }
  T rkth(int l, int r, int k) const { return kth(l, r, r - l + ~k); }
  int cnt(int l, int r, T x) const {
    if ((x = max<T>(x, 0)) >= (T)1 << h) return 0;
    int res = 0;
    for (int j = 0; j < h; ++j) {
      int p = b[j].rank(1, l), q = b[j].rank(1, r);
      int v = x >> h + ~j & 1;
      res += !v * (q - p);
      l = v * z[j] + (v ? p : l - p);
      r = v * z[j] + (v ? q : r - q);
    }
    return res + r - l;
  }
  int cnt(int l, int r, T x, T y) const { return cnt(l, r, x) - cnt(l, r, y); }
};

struct WaveletSum {
  using T = lint;
  const int n, h;
  V<Bitvector> b;
  V<> z;
  VV<T> c;
  template<class Itr> WaveletSum(Itr first, Itr last) :
    n(distance(first, last)), h(__lg(2 * *max_element(first, last) + 1)), b(h, Bitvector(n)), z(h), c(h + 1, V<T>(n + 1)) {
    assert(*min_element(first, last) >= 0);
    V<> w(first, last), nw(n);
    for (int j = 0; j < h; ++j) {
      for (int i = 0; i < n; ++i) {
        if (w[i] >> h + ~j & 1) b[j].set(i);
        else ++z[j];
      }
      b[j].build();
      int p = 0, q = z[j];
      for (int i = 0; i < n; ++i) (b[j][i] ? nw[q++] : nw[p++]) = w[i];
      swap(w, nw);
    }
  }
  T acc(int l, int r, int x, int y, int j, int s, int t) const {
    if (y <= s or t <= x) return 0;
    if (x <= s and t <= y) return c[j][r] - c[j][l];
    int m = s + t >> 1, p = b[j].rank(0, l), q = b[j].rank(0, r);
    return acc(p, q, x, y, j + 1, s, m) + acc(z[j] + l - p, z[j] + r - q, x, y, j + 1, m, t);
  }
  T acc(int l, int r, int x, int y) const { return acc(l, r, x, y, 0, 0, 1 << h); }
  void set(int i, T x) {
    for (int j = 0; j < h; ++j) {
      c[j][i + 1] = x;
      int v = b[j][i];
      i = v * z[j] + b[j].rank(v, i);
    }
    c[h][i + 1] = x;
  }
  void build() {
    for (int j = 0; j <= h; ++j) {
      for (int i = 0; i < n; ++i) c[j][i + 1] += c[j][i];
    }
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, k; cin >> n >> k;
  V<> a(n); for (auto&& e : a) cin >> e;
  WaveletMatrix<int> wm(begin(a), end(a));
  WaveletSum ws(begin(a), end(a));
  for (int i = 0; i < n; ++i) {
    ws.set(i, a[i]);
  }
  ws.build();
  lint res = 9e18;
  for (int l = 0; l + k <= n; ++l) {
    int r = l + k;
    lint m = wm.kth(l, r, k / 2);
    int cl = wm.cnt(l, r, 0, m), cu = wm.cnt(l, r, m, 1 << 30);
    lint curr = 0;
    curr += cl * m - ws.acc(l, r, 0, m);
    curr += ws.acc(l, r, m, 1 << 30) - cu * m;
    res = min(res, curr);
  }
  cout << res << '\n';
}
0