首页 > 其他分享 >[ABC282Ex] Min + Sum

[ABC282Ex] Min + Sum

时间:2023-06-18 22:14:10浏览次数:40  
标签:ll Min int sum ABC282Ex Sum

[ABC282Ex] Min + Sum

一道分治题。比较新的地方在于,别的题都是按中点为M分治,而这道题是按最小值为M分治。记录b的前缀和sum。【L,R】最小值为M,则分为【L,M-1】,【M+1,R】。

 

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+66;
typedef long long ll;
const ll inf=2e18+1;
ll sum[N],s,a[N],b[N],ans,f[23][N];
int n;
ll Q(int l, int r)
{
    int k = __lg(r - l + 1);
    return a[f[k][l]] < a[f[k][r - (1 << k) + 1]] ? f[k][l] : f[k][r - (1 << k) + 1];
}
void solve(int l,int r){
    if(l>r) return;
    int m=Q(l,r);
    solve(l,m-1);solve(m+1,r);
    if(m-l<=r-m){
        for(int i=l;i<=m;i++){
            int j=upper_bound(sum+1,sum+r+1,s+sum[i-1]-a[m])-(sum+1);
            if(j>=m) ans+=j-m+1;
        }
    }else{
        for(int i=m;i<=r;i++){
            int j=lower_bound(sum+l-1,sum+n+1,sum[i]+a[m]-s)-sum+1;
            if(j<=m) ans+=m-j+1;
        }
    }
}
signed main(){
    cin>>n>>s;
    //a[0]=inf;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i],sum[i]=sum[i-1]+b[i];
    for(int i=1;i<=n;i++) f[0][i]=i;
    for (int i = 1; 1 << i <= n; ++i)
        for (int j = 1; j + (1 << i) - 1 <= n; ++j)
            f[i][j] = a[f[i - 1][j]] < a[f[i - 1][j + (1 << i - 1)]] ? f[i - 1][j] : f[i - 1][j + (1 << i - 1)];
    solve(1,n);
    cout<<ans;
    return 0;
}

 

标签:ll,Min,int,sum,ABC282Ex,Sum
From: https://www.cnblogs.com/DongPD/p/17489850.html

相关文章

  • git报错 failed: The TLS connection was non-properly terminated.
    问题现象:kali@kali:~$gitclonehttps://www.github.com/FluxionNetwork/fluxion.gitCloninginto'fluxion'...fatal:unabletoaccess'https://www.github.com/FluxionNetwork/fluxion.git/':gnutls_handshake()failed:TheTLSconnectionwasnon......
  • 随笔(二十)『docker 安装 minio』
    1、拉取镜像dockerpullminio/minio2、创建挂在目录mkdir-p/mydata/minio/configmkdir-p/mydata/minio/data3、创建容器并运行dockerrun-d-p9000:9000--nameminio\-e"MINIO_ACCESS_KEY=minioadmin"\-e"MINIO_SECRET_KEY=minioadmin"\-v/mydata......
  • PHP批量压缩图片,基于TP5,fastadmin
    <?php/***CreatedbyPhpStorm.*User:zhuo<[email protected]>*O(∩_∩)O*Date:2022-7-709:34:38*/namespaceapp\command;usethink\Image;usethink\image\Exception;usethink\console\{Command,Input,Output};//压缩图片classCom......
  • [Web] PerformanceNavigationTiming
     https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming DNSdomainLookupStartdomainLookupEndTCP/TLSconnectStartsecureConnectionStartconnectEndHTTPRequestrequestStartHTTPResponseresponseStartresponseEndP......
  • 2712.minimum Cost to Make All Characters Equal
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • 执行存储过程报错:User does not have access to metadata required to determine stor
    在执行存储过程中,报错详细信息如下:java.sql.SQLException:Userdoesnothaveaccesstometadatarequiredtodeterminestoredprocedureparametertypes.Ifrightscannotbegranted,configureconnectionwith"noAccessToProcedureBodies=true"tohavedrivergener......
  • Fastadmin会员管理-添加会员
    Fastadmin添加会员功能默认的框架没有这个功能,代码修改教程如下1.修改debug--/application/config.php修改成app_debug=true2.新增/application/admin/view/user/user/index.html文件修改,添加增加add按钮{:build_toolbar('refresh,add,edit,del')}3.新增/applicatio......
  • unity将安卓streamingAssetsPath文件复制到persistentDataPath
    privatevoidTestCopy(){stringfrom=Application.streamingAssetsPath+"/Test/test.txt";stringto=Application.persistentDataPath+"/Test/";CopyFile(from,to);}publicstaticvoidCopyFile(stringsourcePath,stringdesti......
  • 【C】专家编程 (Expert C Programming) 阅读笔记
      第一章C:穿越时空的迷雾  1p22~24 ANSIC有此问题。“安静”的类型转换原则:当执行算术运算时,操作数的类型如果不同,就会发生转换。数据类型一般朝着浮点精度更高,长度更长的方向转换,整形术如果转换为singed不会丢失信息,就转换为signed,否则转换为unsign......
  • 父元素min-height:100px 子元素 height:100%无效
    <divid="div1"><spanid="sp1">aaa</span></div>#div1{min-height:100px;background-color:yellow;position:relative;}#sp1{width:20%;height:100%;background-color:b......