yang_cm's Blog

瞻彼洛矣、維水泱泱

0%

Luogu P2839 Record

ヽ(≧□≦)ノ

真是十分有趣的题目。

二分答案

看到中位数当然要想到二分答案啦~

我们二分中位数 xx,将大于等于 xx 的数标记为 +1+1,小于 xx 的数标为 −1-1,若对于一段区间,标记之和大于等于 00,则意味着 xx 小于等于中位数,否则意味着 xx 大于等于中位数。所以可以二分答案。

基于主席树实现的 check 函数

那我们如何实现 check 函数呢,就是 主席树 啦(这谁能想到啊

在 xx 上建立主席树,对于主席树的一个点,我们需要维护三个值:

  1. sumsum:这个区间标记的和;
  2. lmaxlmax:当前区间标记的最大前缀;
  3. rmaxrmax:当前区间标记的最大后缀。

这样,对于一个 check,其中 [b+1,c−1][b+1,c-1] 的区间是必须要取的,直接求 sumsum 即可;[a,b][a,b] 区间可以求最大后缀,即 rmaxrmax;[c,d][c,d] 区间可以求最大前缀,即 lmaxlmax。三者相加即可,最后判断这个和是否大于等于 00。完成 check 函数!

like this~

1
2
3
4
5
6
7
8
bool check(int x, int a, int b, int c, int d) {
int total = 0;
if (b + 1 <= c - 1)
total += T.query(T.head[x], 1, n, b + 1, c - 1).sum;
total += T.query(T.head[x], 1, n, a, b).rmax;
total += T.query(T.head[x], 1, n, c, d).lmax;
return total >= 0;
}

剩余细节以及代码

然后我们施展传统手艺——进行离散化。

放上码风十分良好的代码:

参考实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 2e4 + 10;

int n, m;

struct PST {
struct Node {
int sum, lmax, rmax;
int lc, rc;
} tree[MAXN << 5];
int tot, head[MAXN];

void pushUp(int id) {
tree[id].sum = tree[tree[id].lc].sum + tree[tree[id].rc].sum;
tree[id].lmax = max(tree[tree[id].lc].lmax,
tree[tree[id].lc].sum + tree[tree[id].rc].lmax);
tree[id].rmax = max(tree[tree[id].rc].rmax,
tree[tree[id].rc].sum + tree[tree[id].lc].rmax);
}

int build(int l, int r) {
int id = ++tot;
if (l == r) {
tree[id].sum = tree[id].lmax = tree[id].rmax = 1;
return id;
}
int mid = (l + r) >> 1;
tree[id].lc = build(l, mid);
tree[id].rc = build(mid + 1, r);
pushUp(id);
return id;
}

int update(int id, int l, int r, int x) {
tree[++tot] = tree[id];
id = tot;
if (l == r) {
tree[id].sum = tree[id].lmax = tree[id].rmax = -1;
return id;
}
int mid = (l + r) >> 1;
if (x <= mid) tree[id].lc = update(tree[id].lc, l, mid, x);
else tree[id].rc = update(tree[id].rc, mid + 1, r, x);
pushUp(id);
return id;
}

Node query(int id, int l, int r, int x, int y) {
if (l >= x && r <= y) return tree[id];
int mid = (l + r) >> 1;
if (y <= mid) return query(tree[id].lc, l, mid, x, y);
if (x > mid) return query(tree[id].rc, mid + 1, r, x, y);

Node res, left = query(tree[id].lc, l, mid, x, y), right =
query(tree[id].rc, mid + 1, r, x, y);
res.sum = left.sum + right.sum;
res.lmax = max(left.lmax, left.sum + right.lmax);
res.rmax = max(right.rmax, right.sum + left.rmax);
return res;
}
} T;

vector<pair<int,int> > nums;
int pre = 0;

bool check(int x, int a, int b, int c, int d) {
int total = 0;
if (b + 1 <= c - 1)
total += T.query(T.head[x], 1, n, b + 1, c - 1).sum;
total += T.query(T.head[x], 1, n, a, b).rmax;
total += T.query(T.head[x], 1, n, c, d).lmax;
return total >= 0;
}

signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1, v; i <= n; i++) {
cin >> v;
nums.push_back({v, i});
}
sort(nums.begin(), nums.end());
T.head[0] = T.build(1, n);
for (int i = 1; i < n; i++)
T.head[i] = T.update(T.head[i-1], 1, n, nums[i - 1].second);
cin >> m;
while (m --) {
int q[4];
for (int i = 0; i < 4; i++) {
cin >> q[i];
q[i] = (q[i] + pre) % n;
}
sort(q, q + 4);
int a = q[0] + 1, b = q[1] + 1, c = q[2] + 1, d = q[3] + 1;
int l = 0, r = n - 1, res = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid, a, b, c, d)) {
res = mid;
l = mid + 1;
}else r = mid - 1;
}
pre = nums[res].first;
cout << pre << '\n';
}
return 0;
}