<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>二分 on Zirnc's Blog</title><link>https://blog.chungzh.cn/blog/%E4%BA%8C%E5%88%86/</link><description>Recent content in 二分 on Zirnc's Blog</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sun, 23 Oct 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.chungzh.cn/blog/%E4%BA%8C%E5%88%86/index.xml" rel="self" type="application/rss+xml"/><item><title>ARC101B Median of Medians - 中位数</title><link>https://blog.chungzh.cn/oi-history/arc-101b/</link><pubDate>Sun, 23 Oct 2022 00:00:00 +0000</pubDate><guid>https://blog.chungzh.cn/oi-history/arc-101b/</guid><description>D - Median of Medians
题意 给定一个整数序列 $a[1],a[2],&amp;hellip;.a[n]$，那么对于 $a$ 序列的任意一个连续子序列 $a[L],a[L+1],&amp;hellip;&amp;hellip;a[R]$，其中 $1&amp;lt;=L&amp;lt;=R&amp;lt;=n$, 求出该连续子序列的中位数，记为 $b[L][R]$。
显然 $b$ 数组共有 $n*(n+1)/2$ 个整数。
输出 $b$ 数组的中位数。
分析 关于中位数有一个 Trick：
我们二分一个数 $mid$，对于原序列中 $\ge mid$ 的数，我们标记为 $1$；反之，对于 $&amp;lt; mid$ 的数，我们标记为 $−1$。 标记结束后，如果一个区间内的标记和大于等于 $0$，说明中位数大于等于 $mid$，那么向右二分；反之向左。 对于本题，我们对 $b$ 数组二分它的中位数 $mid$，并按 $mid$ 对 $a$ 数组进行 $+1,-1$ 标记。然后问题就变为了：统计有多少个区间的标记和 $\ge 0$。
记这个区间数为 $cnt$，若 $cnt\ge \lfloor \frac{n(n+1)/2+1}{2} \rfloor$，说明 $b$ 数组实际中位数 $\ge mid$，向右二分。否则向左二分。
怎么求有多少个区间的标记和 $\ge 0$ 呢？我们可以做一个前缀和 $s$，统计 $i &amp;lt; j$ 且 $s[i] \le s[j]$ 的个数。这是一个二维偏序问题，可以搭配树状数组解决。</description></item></channel></rss>