2023河南萌新联赛第(一)场:河南农业大学 L - 中位数

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, px 表示将 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<=n1<=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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值