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[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 (树状数组差分)的更多相关文章
- 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(树状数组)题解
题意:给你数组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 ...
- 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 ...
随机推荐
- python + PyQt5 实现 简易计算器
忽然想起之前一直想写个简单的计算器,今天就写了一下,界面有些简陋,但是基本功能实现没有问题 以下是源码: # --*-- coding:utf-8 --*-- import sys from PyQt ...
- elasticsearch系列一:elasticsearch(ES简介、安装&配置、集成Ikanalyzer)
一.ES简介 1. ES是什么? Elasticsearch 是一个开源的搜索引擎,建立在全文搜索引擎库 Apache Lucene 基础之上 用 Java 编写的,它的内部使用 Lucene 做索引 ...
- tomcat报异常Invalid character found in method name. HTTP method names must be tokens
最近监控了一下测试环境的日志,突然出现如下一个异常 由Error parsing HTTP request header可以看出是由于解析请求头出错导致的,但是它属于DEBUG级别的异常,虽然不影响系 ...
- nuxt cdn等
https://blog.csdn.net/xuelang532777032/article/details/78398960
- C. Songs Compression(简单贪心)
水题 #include<iostream> #include<algorithm> using namespace std; #define LL long long ; st ...
- js修改父子json格式成树状结构格式
js修改父子json成树状结构 var json = [ { "id" : "01", "pId":"" } , { & ...
- Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
SpringBoot启动时的异常信息如下: "C:\Program Files\Java\jdk1.8.0_161\bin\java" ......... com.fangxing ...
- H5 百度一下,你就知道
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- @deprecated 的方法处理
因为需要用到poi,偷懒不太想看官方文档,同时自己的github账号忘记密码了.所以直接在别人博客那拷贝一段代码来模仿修改创建HSSF的xsl文件. 虽然能运行,但发现代码太多横线,可以知道方法被标注 ...
- zabbix异常信息修改已确认,为未确认
问题知悉只能知悉一次知悉了之后就不能再次知悉了,但又不想再创建新的异常怎么办呢.....直接改数据库数据.首先找到acknowledges表这里边存放的全是已经知悉的异常然后找events表,even ...