https://codeforces.com/contest/1119/problem/D

题意

有n个数组,每个数组大小为\(10^{18}+1\)且为等差数列,给出n个数组的\(s[i]\),q次询问,每次询问一个区间[l,r],问所有数组的[l,r]区间一共有多少不同的数

题解

  • 结果只与选择的区间长度len有关,还有和两个数组s[i]的差cha有关

    • 若len<=cha,则没有重复,即\(2*len\)
    • 若len>cha,则前面一个数组可以取满len个,后面的数组只能cha个,即\(cha+len\)
  • 根据以上规律,将a数组进行排序后差分,对差分进行排序,然后做前缀和,每次二分查找出最大的p满足b[p]<=x
  • 这里二分查找需要特判l>r的情况

代码

#include<bits/stdc++.h>
#define ll long long
#define MAXN 100005
using namespace std;
ll a[MAXN],b[MAXN],s[MAXN],q,n,x,p,ans,l,r,sz; int fd(ll x){
int l=1,r=sz-1,res=0;
while(l<r){
int mid=(l+r)/2;
if(b[mid]<=x)l=mid+1;
else r=mid;
}
if(l>r||b[l]>x)l--; //特判
return l;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+n+1);
sz=unique(a+1,a+n+1)-(a+1);
for(int i=2;i<=sz;i++)b[i-1]=a[i]-a[i-1];
sort(b+1,b+sz);
for(int i=1;i<sz;i++)s[i]=s[i-1]+b[i];
cin>>q;
while(q--){
scanf("%lld%lld",&l,&r);
x=r-l+1;
p=fd(x);
ans=s[p]+(sz-p)*x;
printf("%lld ",ans);
}
}

Codeforces Global Round 2 D 差分 + 前缀和 + 二分的更多相关文章

  1. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  2. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  3. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  4. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  5. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  6. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  7. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  8. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  9. D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)

    题目传送门:http://codeforces.com/contest/1119/problem/D D. Frets On Fire time limit per test 1.5 seconds ...

随机推荐

  1. spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings

    项目启动报错: Connected to the target VM, address: '127.0.0.1:59412', transport: 'socket' SLF4J: Class pat ...

  2. mybatis批处理数据

    批处理数据主要有三种方式: 1.传统jdbc处理    2.mybatis批处理插入    3.使用executortype处理 jdbc 处理 1.通过 for循环插入 main方法如下所示: Co ...

  3. 应用层内存溢出/越界/重复释放等问题检查工具(ASan)

    https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/AddressSanitizer ...

  4. docker容器的端口映射

    1.创建一个Nginx 容器,先不映射端口 [root@localhost ~]# docker run --name my_nginx -d nginx 7be3673a4c0f8f7ffe79a7 ...

  5. kmv 学习笔记 工具

    qemu:kmv的文本管理工具,包括qemu-kvm.qemu-img libvirt:是一套免费.开源的支持Linux下主流虚拟化工具的C函数库,libvirtd是运行的守护进程的名称.包括GUI: ...

  6. JavaScript的__proto__、prototype和继承

    JavaScript也是可以“继承”的! 各位看官或是好奇,或是一知半解.什么是prototype,__proto__,constructor.哪种继承方式好.今天就在这交流交流. 什么是protot ...

  7. 服务器部署Laravel

    安装lnmp环境 参考:简书 - Centos 7 下安装LNMP官方最新版 安装redis 参考:简书 - Centos 7下使用yum安装redis 安装nodejs npm nodejs分8.x ...

  8. C#中使用WCF创建面向网络的服务程序

    如题. 这种东西基于微软的一整套东西,在.NET内使用特别方便.利弊自行衡量,是否使用自行决定. 步骤1.创建一组在网上发布的方法 新建项目,类型选择“WCF服务应用程序”  在项目里,你可以补充任意 ...

  9. python网络编程-2

    1.理解相关概念 #浅显理解下 对比cpu与io的差距如:io从硬盘读取一条数据9ms ,cpu在9ms可以做450万次指令 cpu切换上下文的方式:1.遇到io操作切换cpu 2.cpu时间片分配 ...

  10. css去掉button点击后的蓝框

    转自:http://www.inbeijing.org/archives/1139 css控制Button 按钮的点击时候出现蓝色边框的问题 添加css属性,这样在点击安按钮的时候就不会有蓝色边框了. ...