1 条题解
-
0
区间加一,(所有)单点查询,显然可以用经典的树状数组解决。操作完毕后查询每块地砖的数目,排序后输出中位数即可。树状数组 ,排序 ,总时间复杂度为 。
#include <cstdio> #include <algorithm> using namespace std; int tr[1000005], a[1000005], n, k; inline int lowbit(int x) { return x & -x; } void add(int x, int y) { while (x <= n) { tr[x] += y; x += lowbit(x); } } int ask(int x) { int ans = 0; while (x) { ans += tr[x]; x -= lowbit(x); } return ans; } int main() { scanf("%d%d", &n, &k); while (k--) { int a, b; scanf("%d%d", &a, &b); add(a, 1); add(b + 1, -1); } for (int i = 1; i <= n; i++) a[i] = ask(i); sort(a + 1, a + n + 1); printf("%d", a[n / 2 + 1]); return 0; }
- 1
信息
- ID
- 14
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者