結果
問題 | No.924 紲星 |
ユーザー | risujiroh |
提出日時 | 2019-11-08 21:44:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,339 bytes |
コンパイル時間 | 2,136 ms |
コンパイル使用メモリ | 185,248 KB |
実行使用メモリ | 111,172 KB |
最終ジャッジ日時 | 2024-09-15 01:23:01 |
合計ジャッジ時間 | 17,667 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
ソースコード
#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 WeightedSegmentTree { using T = lint; static T op(const T& x, const T& y) { return x + y; } static constexpr T e() { return 0; } struct SegmentTree { const int n; V<T> t; SegmentTree(int _n) : n(_n), t(2 * n, e()) {} T& operator[](int i) { return t[i + n]; } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } T acc(int l, int r) const { T resl = e(), resr = e(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } void set(int i, const T& x) { t[i += n] = x; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } }; const int n, h; V<Bitvector> b; V<> z; V<SegmentTree> st; template<class Itr> WeightedSegmentTree(Itr first, Itr last) : n(distance(first, last)), h(__lg(2 * *max_element(first, last) + 1)), b(h, Bitvector(n)), z(h), st(h + 1, SegmentTree(n)) { 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 operator[](int i) const { return {st[0].t[i + n]}; } T acc(int l, int r, int x, int y, int j, int s, int t) const { if (y <= s or t <= x) return e(); if (x <= s and t <= y) return st[j].acc(l, r); int m = (s + t) / 2, p = b[j].rank(0, l), q = b[j].rank(0, r); return op(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) { st[j].set(i, x); int v = b[j][i]; i = v * z[j] + b[j].rank(v, i); } st[h].set(i, x); } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, q; cin >> n >> q; V<lint> a(n); for (auto&& e : a) cin >> e, e += 1e9; lint mx = *max_element(begin(a), end(a)); WaveletMatrix<lint> wm(begin(a), end(a)); WeightedSegmentTree wst(begin(a), end(a)); for (int i = 0; i < n; ++i) { wst.set(i, a[i]); } while (q--) { lint l, r; cin >> l >> r, --l; lint k = (r - l) / 2; lint m = wm.kth(l, r, k), res = 0; lint cl = wm.cnt(l, r, 0, m); lint cu = wm.cnt(l, r, m, mx + 1); res += cl * m - wst.acc(l, r, 0, m); res += wst.acc(l, r, m, mx + 1) - cu * m; cout << res << '\n'; } }