https://nanti.jisuanke.com/t/31460

题意

两个操作。1:查询区间[l,r]的和,设长度为L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+...+a[r]。2:将第a个位置修改为b。

分析

变形一下,sum=a[l]*(r-l+1)+a[l+1]*(r-(l+1)-1)+...+a[r](r-r+1)=(r+1)*a[l]-a[l]*l。因此,可维护两个树状数组计算。至于更新操作,实质就是看变化前后的差值,变大就相当与加上差值,否则就是减。注意用longlong

#include<bits/stdc++.h>
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + ;
typedef long long ll;
ll c1[maxn],c2[maxn];
int a[maxn];
int lb(int x){ return x&-x; }
void add(ll c[],int x,ll d){
while(x<maxn){
c[x]+=d;
x+=lb(x);
}
}
ll sum(ll c[],int x){
ll res=;
while(x){
res+=c[x];
x-=lb(x);
}
return res;
}
ll query(int l,int r){
return (r+)*(sum(c1,r)-sum(c1,l-))-(sum(c2,r)-sum(c2,l-));
}
int main(){
int n,q;
int op,x,y;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++) {
scanf("%d",&x);
add(c1,i,x);
add(c2,i,1ll*i*x);
a[i]=x;
}
while(q--){
scanf("%d%d%d",&op,&x,&y);
if(op==){
printf("%lld\n",query(x,y));
}else{
add(c1,x,y-a[x]);
add(c2,x,1ll*x*(y-a[x]));
a[x]=y;
}
}
return ;
}

ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study (树状数组差分)的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...

  2. 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, ea ...

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

    题意:给你数组a,有两个操作 1 l 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 ...

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

    Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...

  5. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)

    https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和

    链接 https://nanti.jisuanke.com/t/31456 参考题解  https://blog.csdn.net/ftx456789/article/details/82590044 ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

随机推荐

  1. 【技术文章】《快速上手nodejs》

    本文地址:http://www.cnblogs.com/aiweixiao/p/8294814.html 原文地址: 扫码关注微信公众号 1.写在前面   nodejs快速上手   nodejs使ja ...

  2. Python开发【前端篇】HTML

    1.html概述和基本结构 html概述 HTML是 HyperText Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的 ...

  3. Eclipse安装Gradle插件

    1.window下安装Gradle见https://www.cnblogs.com/felixzh/p/9203271.html2.eclipse中依次打开help>Install new so ...

  4. SQL NULL 值

    NULL 值是遗漏的未知数据. 默认地,表的列可以存放 NULL 值. 本章讲解 IS NULL 和 IS NOT NULL 操作符. SQL NULL 值 如果表中的某个列是可选的,那么我们可以在不 ...

  5. 虚拟DOM和react中的diff算法总结

    https://blog.csdn.net/qq_26708777/article/details/78107577 一.虚拟DOM 1.什么是虚拟DOM及原理        把真实DOM树,变成js ...

  6. 在Linux命令行中以图形化窗口打开文件夹

    Linux 系统中也有类似的命令.Ubuntu 发行版的命令行中,我们可以使用 nautilus 命令来打开指定目录的图形化窗口界面.类似下面命令这样使用: nautilus /home/testPr ...

  7. 好程序员分享ApacheSpark常见的三大误解

    误解一:Spark是一种内存技术 大家对Spark最大的误解就是其是一种内存技术(in-memorytechnology).其实不是这样的!没有一个Spark开发者正式说明这个,这是对Spark计算过 ...

  8. 理解OSI参考模型(转)

    文章转自 https://www.cnblogs.com/evablogs/p/6709707.html 一个视频网站上不小心搜到网络知识的视频,突然以前大学的没有真正接受的知识点,一下子豁然开朗,赶 ...

  9. go笔记-pprof使用

    go tool pprof http://localhost:6060/debug/pprof/profile go tool pprof http://localhost:6060/debug/pp ...

  10. Vue-router的三种传参方式

    第一种传递参数:name传参 两步完成name传参并显示在模板中: 第一在router/index.js中配置name属性, routes: [ { path: '/', name: 'HelloWo ...