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 equals to r - l + 1),或者 2 i b:把第i个换成b
思路:用一个树状数组存i的前缀和,再用一个树状数组存(n - i + 1)*a[ i ]的前缀和,这样算出后面那个的区间差减去前一个的区间差的某个倍数就会成为答案。
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
ll sum[][maxn], a[maxn];
ll n;
int lowbit(int x){
return x&(-x);
}
void update(int x, ll v, int id){
for(int i = x; i <= n; i += lowbit(i)){
sum[id][i] += v;
}
}
ll query(int x, int id){
ll cnt = ;
for(int i = x; i > ; i -= lowbit(i)){
cnt += sum[id][i];
}
return cnt;
}
int main(){
ll q;
while(~scanf("%lld%lld", &n, &q)){
memset(sum, , sizeof(sum));
for(int i = ; i <= n; i++){
scanf("%lld", &a[i]);
update(i, a[i], );
update(i, a[i] * (n - i + ), );
}
while(q--){
ll u, v, w;
scanf("%lld%lld%lld", &u, &v, &w);
if(u == ){
printf("%lld\n", query(w, ) - query(v - , ) - (n - w) * (query(w, ) - query(v - , )));
}
else{
update(v, -a[v] + w, );
update(v, (-a[v] + w) * (n - v + ), );
a[v] = w;
}
}
}
return ;
}
ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解的更多相关文章
- ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study (树状数组差分)
https://nanti.jisuanke.com/t/31460 题意 两个操作.1:查询区间[l,r]的和,设长度为L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+...+a[ ...
- 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 ...
- 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 ...
- 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 ...
- 计蒜客 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 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)
https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...
- ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)
传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...
- ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和
链接 https://nanti.jisuanke.com/t/31456 参考题解 https://blog.csdn.net/ftx456789/article/details/82590044 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
随机推荐
- 07_Linux 终端命令格式
Linux 终端命令格式 目标 了解终端命令格式 知道如何查阅终端命令帮助信息 01. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相应 ...
- a文件.o文件和.so文件有什么区别?
.o类似于windows的.obj .a是多个.o合在一起,用于静态连接. .so文件(shared object)类似于.dll文件.,用于动态连接.
- hdu1505City Game(扫描线)
http://acm.hdu.edu.cn/showproblem.php?pid=1505 题意:R为被占位置,F为空位,求出最大子空矩阵大小*3. 思路:1.悬线法,记录每个位置的悬线能到达的左边 ...
- Map<String, String>循环遍历的方法
Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 下 ...
- 软件包管理:rpm命令管理-查询
yum没有rpm查询的多. 使用包名时无所谓使用位置.因为查询是在后台数据库中查询的. 记忆是只需知道安装和升级时要加包全名,其余加包名.其实未安装的加包全名. rpm包可以手工指定安装路径,但不建议 ...
- BCB 按钮添加背景图
使用控件:TBitBtn 位于 Additional分类 属性:GlyPh
- UVM中factory机制的使用
UVM中的factory机制一般用在sequence的重载,尤其是virtual sequence.当Test_case变化时,通过virtual sequence的重载,可以很容易构建新的测试. 因 ...
- 5select的运用
四.select的运用 --汇总函数 max()最大值,min()最小值,avg()平均值select max(age),min(age),avg(age) from tablename; --算出表 ...
- Masonry 适配label多行
设置属性后,然后根据文本自动多行显示,无需设置标签高度约束 1 属性preferredMaxLayoutWidth,如:label.preferredMaxLayoutWidth = (WidthSc ...
- pandas练习(二)------ 数据过滤与排序
数据过滤与排序------探索2012欧洲杯数据 相关数据见(github) 步骤1 - 导入pandas库 import pandas as pd 步骤2 - 数据集 path2 = ". ...