2018.10.22 cogs2471. [EZOI 2016]源氏的数学课(线段树)
传送门
线段树入门操作。
直接把题目给的(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=lra[i]和∑i=lri∗a[i]\sum _{i=l} ^r i*a[i]∑i=lri∗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]源氏的数学课(线段树)的更多相关文章
- 2018.07.30 cogs2632. [HZOI 2016] 数列操作d(线段树)
传送门 线段树基本操作 区间加等差数列,维护区间和. 对于每个区间维护等差数列首项和公差,易证这两个东西都是可合并的,然后使用小学奥数的知识就可以切掉这题. 代码: #include<bits/ ...
- 2018.10.18 NOIP训练 ZUA球困难综合征(线段树)
传送门 考虑到模数等于7 * 13 * 17 * 19. 那么只需要维护四棵线段树求出每个数处理之后模7,13,17,197,13,17,197,13,17,19的值再用crtcrtcrt合并就行了. ...
- 2018.08.04 cogs2633. [HZOI 2016]数列操作e(线段树)
传送门 支持区间加w(i−ql+1)2" role="presentation" style="position: relative;">w(i ...
- Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 128[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...
- 2018.07.23 codeforces 438D. The Child and Sequence(线段树)
传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...
- 2018.07.23 洛谷P4097 [HEOI2013]Segment(李超线段树)
传送门 给出一个二维平面,给出若干根线段,求出x" role="presentation" style="position: relative;"&g ...
- 8VC Venture Cup 2016 - Elimination Round G. Raffles 线段树
G. Raffles 题目连接: http://www.codeforces.com/contest/626/problem/G Description Johnny is at a carnival ...
- NOIP模拟赛-2018.10.22
模拟赛 今天第一节课是历史,当然是不可能上的,一来到机房发现今天高二考试... 老师说以后可能还要给高一考...那还不如现在跟着做好了,毕竟在学长学姐中垫底显得没那么丢人 这套题风格挺奇怪的...为什 ...
随机推荐
- Alpha版本检测报告
1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...
- jpa summary
JPA Prepared by: John Tan March, Contents what Where to use JPA Difference between JPA and Mybatis 1 ...
- Posting JSON to Spring MVC Controller
Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensu ...
- 常用html设置:
省略 居中 1. 省略 ellipsis: text-overflow:ellipsis: 要求容器必须是固定的,要不然无法做省略. table的省略 table{ table_layout:fixe ...
- 在Eclipes中查看源代码和大纲快速定位
1 在Eclipes中查看源代码,快捷键使用clrl+光标,选择你要查看的方法和属性查看源代码.例如你想看StringBuilder这个类源代码 StringBuilder allow = new S ...
- Hibernate DetachedCriteria实现
前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单从结构上来说, 底层的数据访问层依赖于业务层 ...
- C# 窗口页面卡的处理方案-异步编程委托
今天用winform做了一个小程序,主要是用于远程数据的登录采集,因为数据量非常大,到时每次点击按钮执行程序的时候界面都会出现假死状态,具体表现是无法拖动窗口,无法最小化或关闭等,只能任务管理进程结束 ...
- bed文件格式解读
1)BED文件 BED 文件(Browser Extensible Data)格式是ucsc 的genome browser的一个格式 ,提供了一种灵活的方式来定义的数据行,以用来描述注释信息.BED ...
- U盘做启动盘后,恢复原始容量
借助u盘进行系统安装时,可能会对u盘进行分区.u盘分区后,再连接至电脑,就有很大程度的可能是一部分区域不能显示.u盘原本的大小被占据,显示的大小是比之前少了的,并且这些少掉了的内存也无法再使用.只有对 ...
- 122. Best Time to Buy and Sell Stock II (Array;Greedy)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...