Ryuji doesn't want to study (树状数组)

IDEA中Maven工具栏与命令执行全攻略:高效开发必备技巧 IntelliJ IDEA 作为强大的 Java 开发工具,其 Maven 功能独具优势。Maven 工具栏位于工作区右侧,图标功能丰富,涵盖项目导入、源码处理、依赖操作等。执行 Maven 命令有多种途径,Run Anything 窗口可输入或选目标执行,上下文菜单能右键运行目标,还可创建运行配置,指定命令参数后运行,为开发者高效管理 Maven 项目、便捷执行命令提供有力支持。 阅读详情

Ryuji doesn’t want to study (树状数组)

Ryuji is not a good student, and he doesn’t want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]a[i].

Unfortunately, the longer he learns, the fewer he gets.

That means, if he reads books from lll to rrr, he will get a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LLL is the length of [ lll, rrr ] that equals to r−l+1r - l + 1r−l+1).

Now Ryuji has qqq questions, you should answer him:

  1. If the question type is 111, you should answer how much knowledge he will get after he reads books [ lll, rrr ].

  2. If the question type is 222, Ryuji will change the ith book’s knowledge to a new value.
    Input

First line contains two integers nnn and qqq (nnn, q≤100000q \le 100000q≤100000).

The next line contains n integers represent aia[i]( a[i] \le 1e9)ai .

Then in next qqq line each line contains three integers aaa, bbb, ccc, if a=1a = 1a=1, it means question type is 111, and bbb, ccc represents [ lll , rrr ]. if a=2a = 2a=2 , it means question type is 222 , and bbb, ccc means Ryuji changes the bth book’ knowledge to ccc
Output

For each question, output one line with one integer represent the answer.
样例输入

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出

10
8

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

题意:

给定一个数组 a[N] a [ N ] ,有两种操作,

操作1,给定 l,r,查询a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] l , r , 查 询 a [ l ] × L + a [ l + 1 ] × ( L − 1 ) + ⋯ + a [ r − 1 ] × 2 + a [ r ] 的值

(L is the length of [ l, r ] that equals to r - l + 1).

操作2, 给定 x,y,使a[x]=y x , y , 使 a [ x ] = y

有 N个数据,M N 个 数 据 , M 种操作,对于每个操作1输出计算结果。

分析:

我们将要求的式子变形

∑i=lrai∗(r−i+1)=∑i=lrai×[(r+1)−i]=(r+1)∑i=lrai−∑i=lnai×i ∑ i = l r a i ∗ ( r − i + 1 ) = ∑ i = l r a i × [ ( r + 1 ) − i ] = ( r + 1 ) ∑ i = l r a i − ∑ i = l n a i × i

这样我们可以构造并维护两个数组的树状数组,一个是a[i]的树状数组C1[i],一个是 a[i]*i的树状数组C2[i]。

注意这里是修改值而不是增加值,所以增加的是“新值与旧值得差”,并且要注意把原数组a[i]的值重新赋值。

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll a[maxn];
int n,m;
ll c1[maxn],c2[maxn];
void init(){
    memset(a,0,sizeof(a));
    memset(c1,0,sizeof(c1));
    memset(c2,0,sizeof(c2));
}
int lowbit(int x){
    return x & (-x);
}
void add1(int x,ll val){
    while(x <= n){
        c1[x] += val;
        x += lowbit(x);
    }
}
ll query1(int x){
    ll ans = 0;
    while(x > 0){
        ans += c1[x];
        x -= lowbit(x);
    }
    return ans;
}
void add2(int x,ll val){
    while(x <= n){
        c2[x] += val;
        x += lowbit(x);
    }
}
ll query2(int x){
    ll ans = 0;
    while(x > 0){
        ans += c2[x];
        x -= lowbit(x);
    }
    return ans;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i = 1; i <= n; i++){
            scanf("%lld",&a[i]);
            add1(i,a[i]);
            add2(i,i*a[i]);
        }
        ll op,l,r;
        for(int i = 1; i <= m; i++){
            scanf("%lld%lld%lld",&op,&l,&r);
            if(op == 1)
                printf("%lld\n",(r + 1) * (query1(r) - query1(l-1)) - (query2(r) - query2(l-1)));
            else{
                add1(l,r - a[l]);
                add2(l,l * r - l * a[l]);
                a[l] = r;
            }
        }
    }
    return 0;
}
泰森多面体Voronoi 3D-V5.0 功能介绍 Abaqus Voronoi3D 阅读详情

相关推荐

Nano Banana 2哪里使用?Nano Banana 2提示词合集和使用教程

收集最强的Nano Banana 2提示词合集,以及Nano Banana 2哪里使用的教程,并且有10个免费使用渠道。

qq_42927225的博客 1987

Ryuji doesn't want to study(线段树)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That m...

qq_37378303的博客 276

LVGL8.x 实体按键与Keypad驱动的无缝对接实战

本文详细解析了LVGL8.x中实体按键与Keypad驱动的两种对接方案:坐标映射的Button方式与事件驱动的Keypad方式。重点阐述了如何模块化裁剪lv_port_indev.c模板文件,并深入讲解了Keypad驱动与Group焦点管理系统的核心协作机制,为无触摸屏的嵌入式设备提供流畅的UI交互解决方案。

weixin_29164497的博客 446

2018 ACM-ICPC 徐州赛区 网络赛H题

(有任何问题欢迎留言或私聊 &amp;amp;amp;&amp;amp;amp; 欢迎交流讨论哦 Catalog Catalog Problem:Portal传送门 Solution: AC_Code: Problem Description: Problem:Portal传送门  原题目描述在最下面。  求a[l]∗len+a[l+1]∗(len−1)+...+a[r−1]...

打过ICPC/CCPC,学习ACM算法,Linux内核,Openharmony,加油努力开心珍惜 1129

Seedance2.5本地部署指南:从环境配置到生产优化的完整实践

在 AI 内容生成领域,本地部署方案正逐渐成为开发者和技术团队关注的焦点。Seedance2.5 作为一款支持图像和视频生成的 AI 工具,其完全离线运行的能力解决了云端服务在数据隐私、使用成本和访问稳定性方面的痛点。对于需要处理敏感内容、频繁生成素材或希望完全掌控生成流程的技术人员来说,掌握本地部署方法具有实际价值。 本文将以 Seedance2.5 的本地部署为例,详细介绍从环境准备、依赖配置到运行验证的完整流程,同时深入分析关键参数配置、常见问题排查方法以及生产环境下的优化建议。通过实际部署过程,理解

weixin_33139275的博客 724

基于Transformers库的BERT模型:一个文本情感分类的实例解析

简介 本文来讲述BERT应用的一个例子,采用预训练好的BERT模型来进行演示。BERT的库来源于Transformers,这是一个由PyTorch编写的库,其集成了多个NLP领域SOTA的模型,比如bert、gpt-2、transformer xl等,并且可以自由选择已经预训练好的模型参数,我们可以基于这些参数进行进一步的训练调试。 Part 1: 利用BERT基于特征的方式进行建模 1、任务与数...

蓝天白云梦的csdn博客 2万+

2018ICPC徐州站网络赛 Ryuji doesn't want to study 思维+前缀和+树状数组

做这道题让我对线段树和树状数组有了更深的理解。。 主要是维护两个前缀和,一个a[i],一个(n-i+1)*a[i],最后结果减一下。剩下就是注意一些树状数组的操作更改了。 附上通过代码: #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;cmath&gt; #include...

A confused programmer 856

ISCE+Mintpy进行SBASInSAR处理(持续更新)

通过调用pairs_misregistration方法,利用safe_dict中的数据信息,对每对SLC数据进行配准误差的计算和分析,以确定数据之间的精确差异,为后续的时间序列处理提供基础。给定同一地区的两景SAR影像,经过配准之后,对应像元进行共轭相乘得到相位差,并形成干涉图,但是从干涉图中得到的相位差实际上是主值,即在(-π,π)呈周期性变化,为了还原其真实的相位差,必须在主值的基础上加或减整数倍的相位周期2π,把被卷叠的相位展开,才能顺利地反演出地面目标的高程和形变。

Cyy2511250589的博客 3732

2018徐州模拟赛-H-Ryuji doesn't want to study

 1000ms  262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewe...

han_hhh的博客 388

【技能】OpenClaw Skill 操作钉钉(原理+20个实例)

OpenClaw Skill是遵循AgentSkills规范的自动化逻辑单元,用于指导AI助理调用钉钉工具。每个Skill包含SKILL.md文件,定义触发条件、执行逻辑和接口调用方式。Skill通过钉钉Connector插件实现API鉴权和请求转发,需配置ClientID/Secret等凭证。Skill加载路径包括工作目录、本地目录和内置目录,优先级由高到低。使用前需安装必备插件并配置钉钉权限,通过openclaw.json管理Skill开关和环境变量。ClawHub作为官方技能仓库支持Skill的安装和

海兰 1214

10、业务任务(service task)

业务任务通常是用来调用业务系统,camunda中可以调用JAVA代码或者rest api调用。 在camunda中,业务任务实现方式有5种,本节先用一个小例子显示前三种,External 外部任务,Connector连接器后面分别讲解。假设用户预约电器公司上门维修家电,然后师傅上门维修,完成后公司回访客户对师傅服务打分,师傅查询自己的评分。使用java class 模式实现业务任务。 com.forestlake.camunda.servicetask.ReserveRepair 使用Delegate ex

leyoliu的博客 3230

2018 ICPC徐州网络赛 H.Ryuji doesn't want to study (树状数组)

Ryuji is not a goodstudent, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]. Unfortunately, the longer he learns, the fewer he gets. That means,...

PleasantlY1的博客 312

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

ICPC网络赛 Ryuji doesn't want to study (线段树/树状数组)

Ryuji doesn’t want to study 题目链接: 计蒜客 - 题库链接 题意 给你N个数,M个询问,有两种操作 将一个数修改为另一个值 求区间和 a[l]∗L+a[l+1]∗(L−1)+...+a[r−1]∗2+a[r]a[l]∗L+a[l+1]∗(L−1)+...+a[r−1]∗2+a[r]a[l] * L + a[l+1]*(L-1) + ...+a[r-1]*2...

帅帅的博客 339

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study (树状数组)

题目链接https://nanti.jisuanke.com/t/A2007 题目 Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledgea[i]a[i]. Unfortunately, the...

Viator 217

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Ryuji doesn't want to study(树状数组)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. Th...

20164225的博客 196

ACM/ICPC 2018 徐州网络赛 H题 Ryuji doesn't want to study

题目链接:https://nanti.jisuanke.com/t/31460   Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the ...

WayJasy的博客 313

Ryuji doesn't want to study 2018 徐州赛区网络预赛

题意:  1、区间求   a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r](Lis the length of [ l,r] that equals tor - l + 1)  2、单点修改 解析: 只有这两个操作 并且区间求得值与前缀和极为相似   所以嘛 就要想到树状数组   树状数组能够维护前缀和 所以区间[l,r] 就是r...

babing2770的博客 150

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn’t want to study 题目 给一个 1e5 序列,有 1e5 次操作,每次操作有两种,1 单点修改,2 查询区间 (l, r) 值, 每个区间 【l, r】值计算过程(L 为区间长度):sum=a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]sum = a[l] \times L+a[l+1...

Stormjing的博客 197

2018徐州网络赛- H-Ryuji doesn't want to study(树状数组/线段树)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That m...

Harukaze 344

H. Ryuji doesn't want to study(ACM-ICPC 2018 徐州赛区网络预赛,树状数组)

描述 Ryuji is not a good student, and he doesn’t want to study. But there are n books he should learn, each book has its knowledge a[i]. Unfortunately, the longer he learns, the fewer he gets. ...

riba2534的博客 514

ACM-ICPC 2018 徐州赛区网络赛 H. Ryuji doesn't want to study(树状数组)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That m...

Timeclimber 305

【计蒜客】2018ICPC徐州赛区网络赛H Ryuji doesn't want to study【树状数组】

题目链接 Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]a[i]. Unfortunately, the longer he learns, the fewer he get...

Imagirl1的博客 224

徐州赛区网络赛 Ryuji doesn't want to study(树状数组)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That m...

柳予欣的三味书屋~ 373

ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash和H. Ryuji doesn’t want to study

Time Limit: 1000ms Memory Limit: 262144KB Description Mur loves hash algorithm, and he sometimes encrypt another one’s name, and call him with that encrypted value. For instance, he calls K...

庸医的博客 923

【计蒜客】2018ICPC徐州赛区网络赛H Ryuji doesn't want to study(树状数组)

题目链接   【题意】 Ryuji不想学习,但他会学习一段时间,但是每天的学习效率会下降,其实题目说了一堆就是一个式子。 [r,l]是Ryuji学习时间的一个区间,然后有q次查询,当a=1时,查询l-r的总知识量,当a=2时,更新第l本书的知识量为r。   【解题思路】 将上述式子化简后可得 所以只需要用树状数组或线段树维护a[i]和a[i]*(n-i+1)这两个前缀和...

よろしくお願いします 420

Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledgea[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That...

weixin_34310127的博客 122
上一篇: 2018ACM-ICPC徐州赛区网络赛: A. Hard to prepare(递推)
下一篇: Cow Marathon POJ - 1985(树的直径模板题)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值