抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

题目

传送门
一个长度为nn的序列aa,设其排过序之后为bb,其中位数定义为b[n/2]b[n/2],其中a,ba,b00开始标号,除法取下整。
给你一个长度为nn的序列ss
回答QQ个这样的询问:ss的左端点在[a,b][a,b]之间,右端点在[c,d][c,d]之间的子序列中,最大的中位数。
其中a<b<c<da<b<c<d
位置也从00开始标号。
我会使用一些方式强制你在线。

题解

寻找中位数有一个通常的套路:
考虑二分中位数,设xx是现在二分的数,midmid是序列的中位数:
将大于xx的数设为11,小于xx的数设为1-1
将整个1/11/-1序列求和
Sum>0Sum>0,说明11的数量比1-1多,也就是大于xx的数比小于xx的数多
说明x<=midx<=mid
反之x>midx>mid


回到本题,[b+1,c1][b+1,c-1]为必选区间,[a,b][a,b]选后缀,[c,d][c,d]选前缀。
要使中位数尽可能大,我们要使SumSum尽量大。
因为[b+1,c1][b+1,c-1]固定且[a,b][a,b]后缀[c,d][c,d]前缀互不影响,所以在[a,b][a,b]区间我们选择最大后缀,在[c,d][c,d]区间我们选择最大前缀。

至此,我们已经有具体思路了:
线段树维护区间最大前缀,最大后缀,区间和,每次二分,用线段树判断可不可行。


但是,若使用普通线段树,每次查询都要重置区间为1/11/-1,所以查询时间复杂度为O(nlogn)O(nlogn)
TLETLE才奇怪。

考虑使用可持久化线段树,把每次二分后的1/11/-1序列预处理下来,每次查询就是查一个历史版本
这样每次查询时间复杂度为O(log2n)O(log^2n),预处理复杂度为O(nlogn)O(nlogn),总复杂度为O(nlogn+qlog2n)O(nlogn+qlog^2n)可以ACAC

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
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
#define MAXN 20005
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^'0');
ch=getchar();
}
return x*f;
}
namespace SegmentTree{
struct node{
int l,r;
int lmax,rmax,sum;
}tree[MAXN*20];
int tot;
#define lc tree[i].l
#define rc tree[i].r
node operator + (node A,node B){
node C;
C.sum=A.sum+B.sum;
C.lmax=max(A.lmax,A.sum+B.lmax);
C.rmax=max(B.rmax,A.rmax+B.sum);
return C;
}
void pushup(int i){
tree[i].sum=tree[lc].sum+tree[rc].sum;
tree[i].lmax=max(tree[lc].lmax,tree[lc].sum+tree[rc].lmax);
tree[i].rmax=max(tree[rc].rmax,tree[lc].rmax+tree[rc].sum);
}
inline void Value(int i,int val){
tree[i].sum=tree[i].lmax=tree[i].rmax=val;
}
void build(int &i,int L,int R){//一开始都是1
if (!i) i=++tot;
Value(i,R-L+1);
if (L==R){
return ;
}
int mid=(L+R)>>1;
build(lc,L,mid);
build(rc,mid+1,R);
pushup(i);
}
void update(int &i,int L,int R,int index){//修改成-1
tree[++tot]=tree[i],i=tot;//新建节点
if (L==R) {
Value(i,-1);
return ;
}
int mid=(L+R)>>1;
if (index<=mid) update(lc,L,mid,index);
else update(rc,mid+1,R,index);
pushup(i);
}
node query(int i,int L,int R,int ql,int qr){
if (ql<=L&&R<=qr){
return tree[i];
}
int mid=(L+R)>>1;
if (ql>mid) return query(rc,mid+1,R,ql,qr);
else if (qr<=mid) return query(lc,L,mid,ql,qr);
else return query(lc,L,mid,ql,qr)+query(rc,mid+1,R,ql,qr);
}
}
using namespace SegmentTree;
int rt[MAXN];
int a[MAXN],b[MAXN],c[MAXN];//b:id a:原数组
inline bool cmp(int A,int B){
return a[A]<a[B];
}
inline void discrete(int n){//离散化
for (register int i=1;i<=n;++i) b[i]=i;
sort(b+1,b+1+n,cmp);
}
int n;
#define VAR rt[mid],1,n
inline int Check(int mid,int A,int B,int C,int D){//[A,B] [B+1,C-1] [C,D]
int sum=0;
if (B+1<=C-1) sum+=query(VAR,B+1,C-1).sum;//这个特判容易漏掉
sum+=query(VAR,A,B).rmax;
sum+=query(VAR,C,D).lmax;
return sum;
}
int p[4];
int main(){
n=read();
for (register int i=1;i<=n;++i) a[i]=read();
discrete(n);
build(rt[1],1,n);
for (register int i=2;i<=n;++i){//预处理历史版本
rt[i]=rt[i-1];
update(rt[i],1,n,b[i-1]);
}
int last=0;
int q=read();
while (q--){
for (register int i=0;i<4;++i){
p[i]=(read()+last)%n+1;
}
sort(p,p+4);
int A=p[0],B=p[1],C=p[2],D=p[3];
int l=1,r=n;
int ans=0;
while (l<=r){
int mid=(l+r)>>1;
if (Check(mid,A,B,C,D)>=0) {
ans=a[b[mid]];
l=mid+1;
}
else {
r=mid-1;
}
}
last=ans;
printf("%d\n",ans);
}
}

评论