\(\text{Problem}\)

给定一个整数序列 \(a[1..N]\),定义 \(sum[i][j]=a[i]+a[i+1]+...+a[j]\),将所有的 \(sum[i][j]\) 从小到大排序(其中 \(i,j\) 满足 \(1<=i<=j<=N\) ),得到一个长为 \(N*(N+1)/2\) 的序列,求该序列中的第 \(K\) 个元素。

\(\text{Solution}\)

非常经典的题

二分答案然后 \(check\)

\(\text{Code}\)

#include <cstdio>
#include <algorithm>
#include <iostream>
#define ls (p << 1)
#define rs (ls | 1)
#define RE register
#define IN inline
using namespace std;
typedef long long LL; const int N = 2e4 + 5;
const LL INF = 1e18;
int n, m, len;
LL a[N], b[N], sum[N * 4]; void build(int p, int l, int r)
{
sum[p] = 0;
if (l == r) return;
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
void Modify(int p, int l, int r, int x, int v)
{
if (x > r || x < l) return;
if (l == r) return sum[p] += v, void();
int mid = l + r >> 1;
if (x <= mid) Modify(ls, l, mid, x, v);
else Modify(rs, mid + 1, r, x, v);
sum[p] = sum[ls] + sum[rs];
}
LL Query(int p, int l, int r, int x)
{
if (x > r || x < l) return 0;
if (l == r) return sum[p];
int mid = l + r >> 1;
if (x <= mid) return Query(ls, l, mid, x) + sum[rs];
return Query(rs, mid + 1, r, x);
} IN int check(LL mid)
{
int res = 0;
build(1, 1, len), Modify(1, 1, len, lower_bound(b + 1, b + len + 1, 0) - b, 1);
for(RE int i = 1; i <= n; i++)
{
int x = lower_bound(b + 1, b + len + 1, a[i] - mid) - b;
res += Query(1, 1, len, x);
Modify(1, 1, len, lower_bound(b + 1, b + len + 1, a[i]) - b, 1);
}
return res >= m;
} int main()
{
scanf("%d%d", &n, &m);
for(RE int i = 1; i <= n; i++) scanf("%lld", &a[i]), a[i] += a[i - 1], b[i] = a[i];
b[n + 1] = 0, sort(b + 1, b + n + 2), len = unique(b + 1, b + n + 2) - b - 1;
LL l = -INF, r = INF, mid, ans;
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%lld\n", ans);
}

JZOJ 1078. 【GDOI2006】The Kth Element的更多相关文章

  1. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  2. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  3. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

  4. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  5. JZOJ 3223. 【HBOI2013】Ede的新背包问题

    3223. [HBOI2013]Ede的新背包问题 (Standard IO) Time Limits: 2000 ms  Memory Limits: 262144 KB  Detailed Lim ...

  6. JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)

    2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms  Memory Limits: 128000 KB  Detailed Limits  ...

  7. JZOJ 2136. 【GDKOI2004】汉诺塔

    2136. [GDKOI2004]汉诺塔 (Standard IO) Time Limits: 3000 ms  Memory Limits: 128000 KB  Detailed Limits   ...

  8. JZOJ 1154. 【GDOI2003】购物

    1154. [GDOI2003]购物 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description GDOI商场推出优惠 ...

  9. 【Leetcode】378. Kth Smallest Element in a Sorted Matrix

    Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...

  10. 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

随机推荐

  1. 更换linux的开机启动图片, 启动主题

    简述 之前就想更改开机的启动图片,但是后来简单查了一下,说要重新编译内核,听到编译我就望而却步了,今天发现只是个命令而已,注意这里我用的是 linux mint .这里更改不是 grub 主题, 是 ...

  2. audio解决不能自动播放问题

    问题描述 无法实现打开网页就能自动播放音乐 正常情况下使用autoplay即可实现自动播放,但是现在打开网页该参数无效 原因分析: 根据最新的规范,Chrome系浏览器,没有交互过的网站默认禁止自动播 ...

  3. JUC源码学习笔记6——ReentrantReadWriteLock

    系列文章目录和关于我 阅读此文需要有AQS独占和AQS共享的源码功底,推荐阅读: 1.JUC源码学习笔记1--AQS独占模式和ReentrantLock 2.JUC源码学习笔记2--AQS共享和Sem ...

  4. 【Shell案例】【小数点scale&bc】14、求平均值

    描述写一个bash脚本以实现一个需求,求输入的一个的数组的平均值 第1行为输入的数组长度N第2~N行为数组的元素,如以下为:数组长度为4,数组元素为1 2 9 8示例:41298 那么平均值为:5.0 ...

  5. 【Java EE】Day02 MySQL概念、软件、语句

    〇.总结 1. 一.数据库的基本概念 1.概念 用于存储和管理数据的仓库 特点: 持久化存储,本质是文件系统 方便存储和管理数据 使用统一方式(MySQL)操作 常见的数据库软件: MySQL:Ora ...

  6. WPF 自定义附加事件

    我们都知道路由事件,而附加事件用的比较少. 但如果是通用的场景,类似附加属性,附加事件就很有必要的. 举个例子,输入设备有很多种,WPF中输入事件主要分为鼠标.触摸.触笔:WPF 屏幕点击的设备类型 ...

  7. C/S UDP通信实践踩坑记录与对于ICMP的进一步认识

    背景 最近有个业务场景需要服务端(简称S)与客户端(简称C)设计一套基于UDP的通信协议--要求尽可能快的前提下可容忍一定丢包率,得以比较深入地学习和了解UDP通信和实践,在开发调试期间先后碰到了C端 ...

  8. ABP Framework 手动升级指南:从6.0.1升级到7.0.0

    ABP 7.0.0 正式版已经发布,ABP-Framework-All-In-One 项目同步升级. LeptonX Lite Theme 目前还没有包含在源码解决方案中,还是以 Nuget 包提供, ...

  9. 【问题解决】Tomcat启动服务时提示Filter初始化或销毁出现java.lang.AbstractMethodError错误

    问题背景 最近在开发项目接口,基于SpringBoot 2.6.8,最终部署到外置Tomcat 8.5.85 下,开发过程中写了一个CookieFilter,实现javax.servlet.Filte ...

  10. 刷题笔记——1112:C语言考试练习题_一元二次方程

    题目 1112:C语言考试练习题_一元二次方程 代码 import math while True: try: a,b,c=map(float,input().strip().split()) del ...