\(\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. .net如何优雅的使用EFCore

    EFCore是微软官方的一款ORM框架,主要是用于实体和数据库对象之间的操作.功能非常强大,在老版本的时候叫做EF,后来.net core问世,EFCore也随之问世. 本文我们将用一个控制台项目Ho ...

  2. [Kogel.Subscribe.Mssql]SQL Server增量订阅,数据库变更监听

    此框架是SQL Server增量订阅,用来监听增删改数据库数据变更 目前仅支持SQL Server,Nuget上可以下载安装 或者使用Nuget命令添加包 dotnet add package Kog ...

  3. 零基础学习python的第一天整理——python的安装以及pycharm安装

    ​ 一.python的安装 首先我们来谈一谈python的安装,python的官网地址:Welcome to Python.org​编辑 进入官网后点击Downloads,然后选择自己对应的系统,比如 ...

  4. #define 的神奇操作

    # define 的神奇操作 一.宏定义中的 #.## 符号的神奇用法 1.1 # 的用法 1.1.1 作用 #表示字符串化操作符(stringification),其作用是将宏定义中的传入参数名转换 ...

  5. 【Java EE】Day03 DQL、约束、数据库设计、范式、备份和还原

    〇.总结 1.DQL 聚合函数有空值需要使用ifnull函数 where不能使用聚合函数 分页开始索引的计算,及mysql和oracle的方言 2.约束 删除唯一约束DROP INDEX 列名; 3. ...

  6. 如何通过 C#/VB.NET 将 PDF 转为 Word

    众所周知,PDF 文档支持特长文件,集成度和安全可靠性都较高,可有效防止他人对 PDF 内容进行更改,所以在工作中深受大家喜爱.但是在工作中,我们不可避免的会对 PDF 文档进行修改或再编辑,这时我们 ...

  7. 用了这么多年的 SpringBoot 你知道什么是 SpringBoot 的 Web 类型推断吗?

    用了这么多年的 SpringBoot 那么你知道什么是 SpringBoot 的 web 类型推断吗? 估计很多小伙伴都不知道,毕竟平时开发做项目的时候做的都是普通的 web 项目并不需要什么特别的了 ...

  8. JSONObject 相关

    /** * 将json转为对应实体类 */ public static Object jsonToJavaObj(String json, Class cs) { return jsonToJavaO ...

  9. 求和【第十三届蓝桥杯省赛C++A/C组 , 第十三届蓝桥杯省赛JAVAA组】

    求和 给定 \(n\) 个整数 \(a1,a2,⋅⋅⋅,an\),求它们两两相乘再相加的和,即 \(S=a1⋅a2+a1⋅a3+⋅⋅⋅+a1⋅an+a2⋅a3+⋅⋅⋅+an−2⋅an−1+an−2⋅a ...

  10. [C++]default constructor默认构造函数

    例子: class A{ public: int a; char b; } A temp; cout<<temp.a<<endl; 问题1:什么时候会合成出一个default ...