传送门

线段树入门操作。

直接把题目给的(r−i+1)∗a[i](r-i+1)*a[i](r−i+1)∗a[i]拆开变成(r+1)∗1∗a[i]−i∗a[i](r+1)*1*a[i]-i*a[i](r+1)∗1∗a[i]−i∗a[i]因此只需要维护∑i=lra[i]\sum _{i=l} ^r a[i]∑i=lr​a[i]和∑i=lri∗a[i]\sum _{i=l} ^r i*a[i]∑i=lr​i∗a[i]就行了。

代码:

#include<bits/stdc++.h>
#define N 100005
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define ll long long
using namespace std;
inline ll read(){
	ll ans=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans*w;
}
int n,m;
ll sum[N],a[N];
struct Node{int l,r;ll sum1,sum2;}T[N<<1];
inline void pushup(int p){T[p].sum1=T[lc].sum1+T[rc].sum1,T[p].sum2=T[lc].sum2+T[rc].sum2;}
inline void build(int p,int l,int r){
	T[p].l=l,T[p].r=r;
	if(l==r){T[p].sum1=a[l],T[p].sum2=T[p].l*a[l];return;}
	build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int k,ll v){
	if(T[p].l==T[p].r){
		T[p].sum1+=v;
		T[p].sum2+=T[p].l*v;
		return;
	}
	if(k<=mid)update(lc,k,v);
	else update(rc,k,v);
	pushup(p);
}
inline pair<ll,ll>query(int p,int ql,int qr){
	if(ql<=T[p].l&&T[p].r<=qr)return make_pair(T[p].sum1,T[p].sum2);
	if(qr<=mid)return query(lc,ql,qr);
	if(ql>mid)return query(rc,ql,qr);
	pair<ll,ll>ret1=query(lc,ql,mid),ret2=query(rc,mid+1,qr);
	return make_pair(ret1.first+ret2.first,ret1.second+ret2.second);
}
int main(){
	freopen("overwatch.in","r",stdin);
	freopen("overwatch.out","w",stdout);
	n=read(),m=read();
	for(int i=1;i<=n;++i)sum[i]=sum[i-1]+i,a[i]=read();
	build(1,1,n);
	while(m--){
		int op=read(),x=read();
		if(op==1)update(1,x,read());
		else{
			int y=read();
			pair<ll,ll>ret=query(1,x,y);
			ll ans=ret.first*(y+1)-ret.second;
			printf("%lld\n",ans);
		}
	}
	return 0;
}

2018.10.22 cogs2471. [EZOI 2016]源氏的数学课(线段树)的更多相关文章

  1. 2018.07.30 cogs2632. [HZOI 2016] 数列操作d(线段树)

    传送门 线段树基本操作 区间加等差数列,维护区间和. 对于每个区间维护等差数列首项和公差,易证这两个东西都是可合并的,然后使用小学奥数的知识就可以切掉这题. 代码: #include<bits/ ...

  2. 2018.10.18 NOIP训练 ZUA球困难综合征(线段树)

    传送门 考虑到模数等于7 * 13 * 17 * 19. 那么只需要维护四棵线段树求出每个数处理之后模7,13,17,197,13,17,197,13,17,19的值再用crtcrtcrt合并就行了. ...

  3. 2018.08.04 cogs2633. [HZOI 2016]数列操作e(线段树)

    传送门 支持区间加w(i−ql+1)2" role="presentation" style="position: relative;">w(i ...

  4. Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 128[Submit][Status ...

  5. BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树

    4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...

  6. 2018.07.23 codeforces 438D. The Child and Sequence(线段树)

    传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...

  7. 2018.07.23 洛谷P4097 [HEOI2013]Segment(李超线段树)

    传送门 给出一个二维平面,给出若干根线段,求出x" role="presentation" style="position: relative;"&g ...

  8. 8VC Venture Cup 2016 - Elimination Round G. Raffles 线段树

    G. Raffles 题目连接: http://www.codeforces.com/contest/626/problem/G Description Johnny is at a carnival ...

  9. NOIP模拟赛-2018.10.22

    模拟赛 今天第一节课是历史,当然是不可能上的,一来到机房发现今天高二考试... 老师说以后可能还要给高一考...那还不如现在跟着做好了,毕竟在学长学姐中垫底显得没那么丢人 这套题风格挺奇怪的...为什 ...

随机推荐

  1. maven 在pom.xml 中指定仓库位置

    ...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...

  2. eclipse中build path与Web Deployment Assembly的作用,区别

    转自:https://blog.csdn.net/heart_mine/article/details/79402792 以下内容只为做个笔记记录已下,有问题可以留言,欢迎补充. 今天在eclipse ...

  3. 2.登录linun 输入密码登录不进去 进入单用户模式 修改 然后reboot

    centos进入单用户模式 单用户模式,就是你现在站在这台机器面前能干的活,再通俗点就是你能够接触到这个物理设备. 一般干这个活的话,基本上是系统出现严重故障或者其他的root密码忘记等等,单用户模式 ...

  4. UI5-文档-4.24-Filtering

    在此步骤中,我们为产品列表添加一个搜索字段,并定义一个表示搜索项的过滤器.搜索时,列表会自动更新,只显示与搜索项匹配的项. Preview A search field is displayed ab ...

  5. 从底层谈WebGIS 原理设计与实现(五):WebGIS中通过行列号来换算出多种瓦片的URL 之在线地图

    从底层谈WebGIS 原理设计与实现(五):WebGIS中通过行列号来换算出多种瓦片的URL 之在线地图 作者:naaoveGI…    文章来源:naaoveGIS    点击数:2063    更 ...

  6. spring boot 测试类

    import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.Logge ...

  7. mydumper使用

    一.下载安装,打开https://launchpad.net/mydumper #wget https://launchpadlibrarian.net/185032423/mydumper-0.6. ...

  8. How to Pronounce UMBRELLA

    How to Pronounce UMBRELLA Share Tweet Share Tagged With: 3-Syllable When the weather is bad, you’ll ...

  9. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 解析JSON

    视频地址:https://www.cctalk.com/v/15114923886141 JSON 数据 我颠倒了整个世界,只为摆正你的倒影. 前面的文章中,我们已经完成了项目中常见的问题,比如 路由 ...

  10. oracle 查看被锁的表和解锁

    相关视图 SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$process ;S ...