/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 400.0 KiB
#3 Accepted 1ms 564.0 KiB
#4 Accepted 163ms 6.543 MiB
#5 Wrong Answer 152ms 47.426 MiB
#6 Accepted 343ms 24.027 MiB
#7 Accepted 73ms 1.02 MiB
#8 Accepted 240ms 23.508 MiB
#9 Wrong Answer 171ms 23.586 MiB
#10 Wrong Answer 134ms 24.34 MiB
#11 Accepted 20ms 624.0 KiB
#12 Accepted 42ms 612.0 KiB
#13 Accepted 1ms 532.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;


template<typename T, typename U = T>
struct SegmentTree {
  int n;
  vector<T> tree;

  // ----------

  template<typename VAL_T>
  T get_tree_val(VAL_T& val) {
    return {val};
  }

  T merge(T a, T b) {
    T c;
    std::merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));
    return c;
  }

  // ----------

  SegmentTree(int n = 0) : n(n) {
    tree.resize(n<<2);
  }

  template<typename VAL_T>
  SegmentTree(vector<VAL_T>& data) : SegmentTree((int)data.size()) {
    __build(0, 0, n-1, data);
  }

  template<typename VAL_T>
  void __build(int ti, int left, int right, vector<VAL_T>& data) {
    if (left == right) {
      tree[ti] = get_tree_val(data[left]);
      return;
    }

    int tl, tr, tm;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;
    tm = (left+right)>>1;

    __build(tl, left, tm, data);
    __build(tr, tm+1, right, data);
    tree[ti] = merge(tree[tl], tree[tr]);
  }

  int __query_l(int ti, int left, int right, int l, int r, int x) {
    if ((l <= left) && (right <= r)) {
      int cnt = lower_bound(tree[ti].begin(), tree[ti].end(), x) - tree[ti].begin();
      return cnt;
    }

    int tl, tr, tm;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;
    tm = (left+right)>>1;

    if (l > tm) return __query_l(tr, tm+1, right, l, r, x);
    if (r <= tm) return __query_l(tl, left, tm, l, r, x);
    return (
      __query_l(tl, left, tm, l, r, x) +
      __query_l(tr, tm+1, right, l, r, x)
    );
  }

  int query_l(int l, int r, int x) { return __query_l(0, 0, n-1, l, r, x); }
};

int main() {
  FAST;
  
  int tc = 1, ti;
  cin >> tc;

  for (ti = 1; ti <= tc; ++ti) {
    int n, q, i, l, r, l0, l1, r0, r1, ans;
    cin >> n;

    vector<int> a(n);
    for (i = 0; i < n; ++i) cin >> a[i];

    SegmentTree<vector<int>> seg(a);

    cin >> q; while (q--) {
      cin >> i; --i;
      l = 0; r = n-1;

      l0 = l1 = r0 = r1 = 0;
      l0 = seg.query_l(l, i, a[i]);
      l1 = i-l+1 - seg.query_l(l, i, a[i]+1);
      r0 = seg.query_l(i, r, a[i]);
      r1 = r-i+1 - seg.query_l(i, r, a[i]+1);

      ans = l0*r1 + r0*l1;
      cout << ans << "\n";
    }
  }

  return 0;
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 15:49:06
Judged At
2024-10-03 15:49:06
Judged By
Score
76
Total Time
343ms
Peak Memory
47.426 MiB