首页 > 其他分享 > 序列查询新解

序列查询新解

时间:2022-08-25 20:25:49浏览次数:60  
标签:get int res LL mid 查询 新解 abs 序列

https://www.acwing.com/problem/content/4284/

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100010;

int n, m;
int a[N];
int R;

LL get(int l, int r)  // 求g[l] + g[l + 1] + ... + g[r]
{
    if (l / R == r / R) return (LL)(r - l + 1) * (l / R);
    int a = l / R + 1, b = r / R - 1;
    LL res = (a + b) * (LL)(b - a + 1) / 2 * R;  // 中间部分
    res += (a - 1) * (LL)(a * R - l);  // 左边界
    res += (b + 1) * (LL)(r - (b * R + R) + 1);  // 右边界
    return res;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
    a[n + 1] = m;
    R = m / (n + 1);

    LL res = 0;
    for (int i = 0; i <= n; i ++ )
    {
        int l = a[i], r = a[i + 1] - 1;
        int x = l / R, y = r / R;
        if (y <= i || x >= i)
        {
            res += abs((LL)i * (r - l + 1) - get(l, r));
        }
        else
        {
            int mid = i * R;
            res += abs((LL)i * (mid - l + 1) - get(l, mid));  // 左半边
            res += abs((LL)i * (r - mid) - get(mid + 1, r));  // 右半边
        }
    }

    printf("%lld\n", res);
    return 0;
}

标签:get,int,res,LL,mid,查询,新解,abs,序列
From: https://www.cnblogs.com/xjtfate/p/16625587.html

相关文章

  • 使用mybatis的Criteria 查询、条件过滤用法
     借鉴博客:https://cloud.tencent.com/developer/article/1979972 1、如果业务查询中,有的条件要用括号()括起来达到想要的效果,如:第2个and后面的条件要括起来【此业......
  • DRF当中序列化器中通过重写create()来实现保护登录保护
    在DRF原来源码框架中,我们知道保存的用户信息时,用户的密码是被明文保存到数据库中。代码实classUserRegisterModelSerializer(serializers.ModelSerializer)   """......
  • redis 慢查询
    redis慢查询慢查询,顾名思义就是比较慢的查询,但是究竟是哪里慢呢?首先,我们了解一下Redis命令执行的整个过程在慢查询的定义中,统计比较慢的时间段指的是命令执行这个步骤......
  • DNS 查询原理详解
    通过DNS查询,得到域名的IP地址,才能访问网站。那么,DNS查询到底是怎么完成的?本文通过实例,详细介绍背后的步骤。   一、DNS服务器 域名对应的IP地址,都保......
  • MySQL查询性能优化七种武器之索引下推
    前面已经讲了MySQL的其他查询性能优化方式,没看过可以去了解一下:MySQL查询性能优化七种武器之索引潜水MySQL查询性能优化七种武器之链路追踪今天要讲的是MySQL的另一种查......
  • 【elasticsearch】elasticSearch数据库配置慢查询日志
    给elasticSearch引擎配置慢查询日志,可以实时监控搜索过慢的日志。虽然ElasticSearch以快速搜索而出名,但随着数据量的进一步增大或是服务器的一些性能问题,会有可能出现慢查......
  • 在线安全清空慢查询日志slow.log
    setglobalslow_query_log=0;showvariableslike'%slow_query%';setglobalslow_query.log_file='/app/data01/mysql/db/slow.log';setglo......
  • SQL Server查询优化
    从上至下优化看过一篇文章,印象深刻,里面将数据库查询优化分为四个大的方向使用钞能力——给DB服务器加物理配置,内存啊,CPU啊,硬盘啊,全上顶配替换存储系统——根据实际的......
  • C++地铁线路查询
    C++地铁线路查询问题描述:当一个用户从甲地到乙地时,由于不同需求,就有不同的交通路线,有人希望以最短距离到达,有人希望用最少的换乘次数等。请编写一北京地铁线路查询系统,......
  • leetcode 594. Longest Harmonious Subsequence 最长和谐子序列(简单).md
    一、题目大意https://leetcode.cn/problems/longest-harmonious-subsequence和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。现在,给你一个整数数组......