結果

問題 No.924 紲星
ユーザー 👑 emthrmemthrm
提出日時 2019-11-08 23:19:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,834 bytes
コンパイル時間 1,872 ms
コンパイル使用メモリ 136,892 KB
実行使用メモリ 18,776 KB
最終ジャッジ日時 2023-10-13 04:58:17
合計ジャッジ時間 7,997 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,492 KB
testcase_01 AC 3 ms
5,204 KB
testcase_02 AC 3 ms
5,428 KB
testcase_03 AC 5 ms
5,412 KB
testcase_04 AC 4 ms
5,316 KB
testcase_05 AC 6 ms
5,160 KB
testcase_06 AC 5 ms
5,320 KB
testcase_07 AC 4 ms
5,488 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

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; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 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;
};

struct Mo {
  Mo(const vector<int> &left, const vector<int> &right) : left(left), right(right) {
    n = left.size();
    int width = sqrt(n);
    order.resize(n);
    iota(ALL(order), 0);
    sort(ALL(order), [&](int a, int b) {
      return left[a] / width != left[b] / width ? left[a] < left[b] : ((left[a] / width) & 1 ? right[a] < right[b] : right[a] > right[b]);
    });
  }

  int process() {
    if (ptr == n) return -1;
    int idx = order[ptr];
    while (left[idx] < nl) add(--nl);
    while (nr < right[idx]) add(nr++);
    while (nl < left[idx]) del(nl++);
    while (right[idx] < nr) del(--nr);
    ++ptr;
    return idx;
  }

  void add(int idx);

  void del(int idx);

private:
  vector<int> left, right, order;
  int n, ptr = 0, nl = 0, nr = 0;
};

const int N = 200000;

int b[N];
vector<long long> comp;
BIT<long long> val(N);
BIT<int> cnt(N);

void Mo::add(int idx) {
  val.add(b[idx], comp[b[idx]]);
  cnt.add(b[idx], 1);
}

void Mo::del(int idx) {
  val.add(b[idx], -comp[b[idx]]);
  cnt.add(b[idx], -1);
}

int main() {
  int n, q; cin >> n >> q;
  vector<int> a(n);
  comp.resize(n);
  REP(i, n) {
    cin >> a[i];
    comp[i] = a[i];
  }
  sort(ALL(comp));
  comp.erase(unique(ALL(comp)), comp.end());
  REP(i, n) b[i] = lower_bound(ALL(comp), a[i]) - comp.begin();
  vector<long long> ans(q, 0);
  vector<int> left(q), right(q);
  REP(i, q) {
    int l, r; cin >> l >> r; --l; --r;
    left[i] = l;
    right[i] = r + 1;
  }
  Mo mo(left, right);
  REP(_, q) {
    int idx = mo.process();
    // if (idx == -1) assert(false);
    int kosu = (cnt.sum(N - 1) + 1) / 2;
    int mid = cnt.lower_bound(kosu);
    ans[idx] = -val.sum(mid - 1) + val.sum(mid, N - 1);
    ans[idx] += comp[mid] * cnt.sum(mid - 1) - comp[mid] * cnt.sum(mid, N);
  }
  REP(i, q) cout << ans[i] << '\n';
  return 0;
}
0