bzoj2653 middle

bzoj2653 middle

给定一个序列,每次询问左端点在 \([a,\ b]\) 之间,右端点在 \([c,\ d]\) 之间的子序列中最大的中位数

强制在线

\(n\leq2\times10^4,\ m\leq2.5\times10^4\)

主席树,二分答案


先离散化……考虑二分答案,假设当前二分值为 \(mid\)

考虑求中位数常用的 \(trick\) ,将小于 \(x\) 的数看作 \(-1\) ,大于等于 \(x\) 的数看作 \(1\)

将原序列这样转化后,可以发现存在至少为 \(mid\) 的答案的条件是左端点在 \([a,\ b]\) ,右端点在 \([c,\ d]\) 的最大子段和大于等于 \(0\)于是现在就有一个优秀的 \(O(nm\log n)\) 的做法了

由于二分时原序列每个值对应一个 \(1,-1\) 序列,可以考虑预处理出所有 \(1,-1\) 序列

由于值域相邻的两个数所对应的序列的改动总数不大于 \(O(n)\) ,因此可以用主席树维护

时间复杂度 \(O(n\log n)\) ,空间复杂度 \(O(n\log n)\)

代码

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

typedef pair <int, int> pii;
const int maxn = 2e4 + 10;
int tot, rt[maxn], pos[maxn];
int n, m, len, lastans, a[maxn], data[maxn];

struct node {
  int l, r, sum, lef, rig;
} tree[maxn << 4];

node operator + (node a, node b) {
  static node res;
  res.sum = a.sum + b.sum;
  res.lef = max(a.lef, a.sum + b.lef);
  res.rig = max(b.rig, b.sum + a.rig);
  return res;
}

#define mid ((l + r) >> 1)
#define lson tree[k].l, l, mid
#define rson tree[k].r, mid + 1, r

void build(int &k, int l, int r) {
  k = ++tot, tree[k].sum = tree[k].lef = tree[k].rig = r - l + 1;
  if (l == r) return;
  build(lson), build(rson);
}

void ins(int &k, int rt, int l, int r, int x) {
  tree[k = ++tot] = tree[rt];
  if (l == r) {
    tree[k].sum = -1;
    tree[k].lef = tree[k].rig = 0;
    return;
  }
  x <= mid ? ins(tree[k].l, tree[rt].l, l, mid, x) : ins(tree[k].r, tree[rt].r, mid + 1, r, x);
  int L = tree[k].l, R = tree[k].r;
  tree[k] = tree[L] + tree[R], tree[k].l = L, tree[k].r = R;
}

node query(int k, int l, int r, int ql, int qr) {
  if (l == ql && r == qr) {
    return tree[k];
  }
  if (qr <= mid) {
    return query(lson, ql, qr);
  } else if (ql > mid) {
    return query(rson, ql, qr);
  } else {
    return query(lson, ql, mid) + query(rson, mid + 1, qr);
  }
}

#undef mid
#undef lson
#undef rson

void rebuild() {
  static pii b[maxn];
  for (int i = 1; i <= n; i++) {
    data[i] = a[i];
  }
  sort(data + 1, data + n + 1);
  len = unique(data + 1, data + n + 1) - data - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(data + 1, data + len + 1, a[i]) - data;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = pii(a[i], i);
  }
  sort(b + 1, b + n + 1);
  build(rt[0], 1, n);
  for (int i = 1, j = 1; i <= len; i++) {
    pos[i] = j - 1;
    for (; j <= n && b[j].first == i; j++) {
      ins(rt[j], rt[j - 1], 1, n, b[j].second);
    }
  }
}

int query(int l1, int r1, int l2, int r2) {
  int l = 1, r = len, mid;
  while (l < r) {
    mid = (l + r + 1) >> 1;
    int k = rt[pos[mid]];
    int s = query(k, 1, n, r1, l2).sum;
    if (l1 < r1) s += query(k, 1, n, l1, r1 - 1).rig;
    if (l2 < r2) s += query(k, 1, n, l2 + 1, r2).lef;
    s < 0 ? r = mid - 1 : l = mid;
  }
  return data[r];
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  rebuild();
  scanf("%d", &m);
  while (m--) {
    int q[4];
    for (int i = 0; i < 4; i++) {
      scanf("%d", q + i);
      q[i] = (q[i] + lastans) % n + 1;
    }
    sort(q, q + 4);
    printf("%d\n", lastans = query(q[0], q[1], q[2], q[3]));
  }
  return 0;
}
posted @ 2019-06-05 17:03  cnJuanzhang  阅读(89)  评论(0编辑  收藏  举报