結果

問題 No.924 紲星
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-11-08 22:19:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,409 ms / 4,000 ms
コード長 4,928 bytes
コンパイル時間 1,094 ms
コンパイル使用メモリ 122,832 KB
実行使用メモリ 113,472 KB
最終ジャッジ日時 2023-09-04 03:19:05
合計ジャッジ時間 15,199 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,504 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 1,388 ms
102,108 KB
testcase_09 AC 1,365 ms
101,980 KB
testcase_10 AC 1,406 ms
102,128 KB
testcase_11 AC 1,376 ms
103,192 KB
testcase_12 AC 1,409 ms
103,164 KB
testcase_13 AC 547 ms
60,492 KB
testcase_14 AC 430 ms
37,648 KB
testcase_15 AC 451 ms
38,072 KB
testcase_16 AC 856 ms
113,472 KB
testcase_17 AC 739 ms
70,360 KB
testcase_18 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T sum = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    sum += bit[x];
  }
  return sum;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const Q = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      auto L = new int[Q];
      auto R = new int[Q];
      foreach (q; 0 .. Q) {
        L[q] = readInt() - 1;
        R[q] = readInt();
      }
      
      
      // yukicoder 919
      
      const xs = A.dup.sort.uniq.array;
      const asLen = cast(int)(xs.length);
      
      int V;
      int[] l, r;
      int[] sum;
      int node() {
        l ~= 0;
        r ~= 0;
        sum ~= 0;
        return V++;
      }
      
      int segN;
      for (segN = 1; segN < asLen; segN <<= 1) {}
      auto seg = iota(segN << 1).array;
      V = segN << 1;
      l = new int[V];
      r = new int[V];
      sum = new int[V];
      foreach_reverse (a; 1 .. segN) {
        l[a] = a << 1;
        r[a] = a << 1 | 1;
      }
      
      // persistent
      auto roots = new int[N + 1];
      roots[0] = 0;
      foreach (i; 0 .. N) {
        // insert A[i]
        int a = xs.lowerBound(A[i]);
        a += segN;
        const old = seg[a];
        seg[a] = node();
        sum[seg[a]] = sum[old] + 1;
        for (; a >>= 1; ) {
          seg[a] = node();
          l[seg[a]] = seg[a << 1];
          r[seg[a]] = seg[a << 1 | 1];
          sum[seg[a]] = sum[l[seg[a]]] + sum[r[seg[a]]];
        }
        roots[i + 1] = seg[1];
      }
      debug {
        // writeln("l = ", l);
        // writeln("r = ", r);
        // writeln("sum = ", sum);
        // writeln("roots = ", roots);
      }
      
      long getMedian(int i, int j) {
        // min x s.t. # (< x) >= (j - i + 1) / 2
        int u = roots[i], v = roots[j];
        int pos;
        int s;
        for (int w = segN; w >>= 1; ) {
          if (s + (sum[l[v]] - sum[l[u]]) >= (j - i + 1) / 2) {
            u = l[u];
            v = l[v];
          } else {
            pos += w;
            s += (sum[l[v]] - sum[l[u]]);
            u = r[u];
            v = r[v];
          }
        }
        debug {
          // writefln("median [%s, %s) = xs[%s]", i, j, pos);
        }
        return xs[pos];
      }
      
      //
      
      
      Tuple!(long, int)[] es;
      foreach (i; 0 .. N) {
        es ~= tuple(A[i], i);
      }
      auto meds = new long[Q];
      foreach (q; 0 .. Q) {
        meds[q] = getMedian(L[q], R[q]);
        es ~= tuple(meds[q], N + q);
      }
      debug {
        writeln("meds = ", meds);
      }
      es.sort;
      auto bit0 = new long[N];
      auto bit1 = new long[N];
      auto ress0 = new long[Q];
      auto ress1 = new long[Q];
      foreach (ref e; es) {
        if (e[1] < N) {
          const i = e[1];
          bit0.bAdd(i, 1);
          bit1.bAdd(i, A[i]);
        } else {
          const q = e[1] - N;
          ress0[q] = bit0.bSum(R[q]) - bit0.bSum(L[q]);
          ress1[q] = bit1.bSum(R[q]) - bit1.bSum(L[q]);
        }
      }
      
      foreach (q; 0 .. Q) {
        /*
          (meds[q] - A[i]) for A[i] <= meds[q]
          (A[i] - meds[q]) for A[i] >  meds[q]
        */
        long ans;
        ans = (bit1.bSum(R[q]) - bit1.bSum(L[q])) - (bit0.bSum(R[q]) - bit0.bSum(L[q])) * meds[q];
        ans -= 2 * (ress1[q] - ress0[q] * meds[q]);
        writeln(ans);
      }
      
    }
  } catch (EOFException e) {
  }
}
0