2023河南萌新联赛第(一)场:河南农业大学 L - 迷宫探险
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
某天cbyyx突发奇想给lyt出了一道题:给定序列a,b,保证|a|+|b|为奇数且小于1e6,给定q组询问,每次将a序列其中一个数修改或将b序列其中一个数修改,问每次操作后两序列合并后的中位数是多少,lyt觉得这个问题太简单并把它秒了,但lyt觉得这个题对新生来说有点困难,于是他简化问题如下:给定两个正整数n,m,再给定长度为n的正整数序列a, 保证n为奇数。接下来m行,每行两个正整数p, x。表示把a[p]修改为x。对于每次操作输出修改后的中位数。
输入描述:
第一行输入两个正整数
n
n
n,
m
m
m
第二行给定
n
n
n 个正整数表示序列
a
1
a_1
a1 ~
a
n
a_n
an
接下来
m
m
m 行每行给定两个数
p
,
x
,
p,x,
p,x, 表示将
a
[
p
]
a[p]
a[p] 修改为
x
x
x 。
1
<
=
n
<
=
1
e
6
,
1
<
=
m
<
=
1
e
5
1<=n<=1e6,1<=m<=1e5
1<=n<=1e6,1<=m<=1e5
∀
1
<
=
i
<
=
n
,
1
<
=
a
[
i
]
<
=
1
e
6
∀1<=i<=n,1<=a[i]<=1e6
∀1<=i<=n,1<=a[i]<=1e6
1
<
=
x
<
=
1
e
6
1<=x<=1e6
1<=x<=1e6
输出描述:
对于每次操作输出每次操作后序列的中位数的值。
示例1
输入
7 3
1 2 3 4 5 6 7
2 3
4 4
7 1
输出
4
4
3
import java.io.*;
public class Main {
static int N = 1000010;
static int[] tr = new int[N << 2];
static int[] a = new int[N];
public static void pushup(int u) {
tr[u] = tr[u << 1] + tr[u << 1 | 1];
}
public static void update(int u, int l, int r, int v, int y) {
if (l == r) tr[u] += y;
else {
int mid = l + r >> 1;
if (v <= mid) update(u << 1, l, mid, v, y);
else update(u << 1 | 1, mid + 1, r, v, y);
pushup(u);
}
}
public static int query(int u, int l, int r, int k) {
if (l == r) return l;
int mid = l + r >> 1;
if (k <= tr[u << 1]) return query(u << 1, l, mid, k);
return query(u << 1 | 1, mid + 1, r, k - tr[u << 1]);
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] s = bf.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
s = bf.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
update(1, 1, N, a[i], 1);
}
for (int i = 1; i <= m; i++) {
s = bf.readLine().split(" ");
int x = Integer.parseInt(s[0]);
int y = Integer.parseInt(s[1]);
update(1, 1, N, a[x], -1);
a[x] = y;
update(1, 1, N, y, 1);
bw.write(query(1, 1, N, (n + 1) / 2) + "\n");
}
bw.close();
}
}