結果

問題 No.924 紲星
ユーザー 👑 emthrmemthrm
提出日時 2019-11-16 20:51:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 982 ms / 4,000 ms
コード長 3,468 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 134,852 KB
実行使用メモリ 28,016 KB
最終ジャッジ日時 2023-10-25 10:42:49
合計ジャッジ時間 12,693 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 941 ms
28,016 KB
testcase_09 AC 957 ms
28,016 KB
testcase_10 AC 982 ms
28,016 KB
testcase_11 AC 978 ms
28,016 KB
testcase_12 AC 980 ms
28,016 KB
testcase_13 AC 337 ms
13,660 KB
testcase_14 AC 270 ms
11,760 KB
testcase_15 AC 276 ms
12,244 KB
testcase_16 AC 600 ms
21,104 KB
testcase_17 AC 463 ms
16,092 KB
testcase_18 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
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()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
template <typename Abelian>
struct BIT {
  BIT(int n, const Abelian &UNITY = 0) : n(n), UNITY(UNITY), dat(n, UNITY) {}

  void add(int idx, const Abelian &value) {
    while (idx < n) {
      dat[idx] += value;
      idx |= idx + 1;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    while (idx >= 0) {
      res += dat[idx];
      idx = (idx & (idx + 1)) - 1;
    }
    return res;
  }

  Abelian sum(int left, int right) {
    if (right < left) return UNITY;
    return sum(right) - sum(left - 1);
  }

  Abelian operator[](const int idx) { return sum(idx, idx); }

  int lower_bound(Abelian value) {
    if (value <= UNITY) return 0;
    int res = 0, exponent = 1;
    while (exponent <= n) exponent <<= 1;
    for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
      if (res + mask - 1 < n && dat[res + mask - 1] < value) {
        value -= dat[res + mask - 1];
        res += mask;
      }
    }
    return res;
  }

private:
  int n;
  const Abelian UNITY;
  vector<Abelian> dat;
};

int main() {
  int n, q; cin >> n >> q;
  vector<int> a(n), comp(n);
  REP(i, n) {
    cin >> a[i];
    comp[i] = a[i];
  }
  sort(ALL(comp));
  comp.erase(unique(ALL(comp)), comp.end());
  int m = comp.size();
  vector<vector<int> > sorted(m);
  REP(i, n) sorted[lower_bound(ALL(comp), a[i]) - comp.begin()].emplace_back(i);
  vector<int> l(q), r(q);
  REP(i, q) {
    cin >> l[i] >> r[i]; --l[i]; --r[i];
  }
  vector<int> lb(q, -1), ub(q, m - 1);
  while (true) {
    bool is_updated = false;
    vector<vector<int> > mids(m);
    REP(i, q) {
      if (ub[i] - lb[i] > 1) {
        is_updated = true;
        mids[(lb[i] + ub[i]) / 2].emplace_back(i);
      }
    }
    if (!is_updated) break;
    BIT<int> bit(n);
    REP(i, m) {
      for (int e : sorted[i]) bit.add(e, 1);
      for (int e : mids[i]) {
        (bit.sum(l[e], r[e]) > (r[e] - l[e] + 1) / 2 ? ub[e] : lb[e]) = i;
      }
    }
  }
  vector<vector<int> > medians(m);
  REP(i, q) medians[ub[i]].emplace_back(i);
  vector<long long> ans(q, 0);
  REP(i, q) {
    if ((r[i] - l[i]) % 2 == 0) ans[i] -= comp[ub[i]];
  }
  BIT<long long> bit(n);
  REP(i, m) {
    for (int e : medians[i]) ans[e] -= bit.sum(l[e], r[e]) * 2;
    for (int e : sorted[i]) bit.add(e, comp[i]);
  }
  REP(i, q) {
    ans[i] += bit.sum(l[i], r[i]);
    cout << ans[i] << '\n';
  }
  return 0;
}
0