https://scut.online/p/321

第一次做区间线段树。

感觉和单点的一样啊。pushdown的时候要注意一些问题,st的值有可能是跟区间长度有关的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; inline int read() {
int x=0;
int f=0;
char c;
do {
c=getchar();
if(c=='-')
f=1;
} while(c<'0'||c>'9');
do {
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
} while(c>='0'&&c<='9');
return f?-x:x;
} inline void _write(int x) {
if(x>9)
_write(x/10);
putchar(x%10+'0');
} inline void write(int x) {
if(x<0) {
putchar('-');
x=-x;
}
_write(x);
putchar('\n');
} void TestCase(int ti); int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int T=1;
for(int ti=1; ti<=T; ti++)
TestCase(ti);
} /*--- ---*/ const int MAXM=140000;
int a[MAXM+5];
ll st[(MAXM<<2)+5],lazy[(MAXM<<2)+5]; inline void push_up(int o) {
st[o]=st[o<<1]+st[o<<1|1];
} inline void push_down(int o,int l,int r) {
if(lazy[o]) {
lazy[o<<1]+=lazy[o];
lazy[o<<1|1]+=lazy[o];
int m=(l+r)>>1;
st[o<<1]+=lazy[o]*(m-l+1);
st[o<<1|1]+=lazy[o]*(r-m);
lazy[o]=0;
}
} void build(int o,int l,int r) {
if(l==r)
st[o]=a[l];
else {
int m=(l+r)>>1;
build(o<<1,l,m);
build(o<<1|1,m+1,r);
push_up(o);
}
lazy[o]=0;
} void update(int o,int l,int r,int a,int b,ll v) {
if(a<=l&&r<=b) {
lazy[o]+=v;
st[o]+=v*(r-l+1);
return;
} else {
push_down(o,l,r);
int m=(l+r)>>1;
if(a<=m)
update(o<<1,l,m,a,b,v);
if(b>=m+1)
update(o<<1|1,m+1,r,a,b,v);
push_up(o);
}
} ll query(int o,int l,int r,int a,int b) {
if(a<=l&&r<=b) {
return st[o];
} else {
push_down(o,l,r);
int m=(l+r)>>1;
ll ans=0;
if(a<=m)
ans=ans+query(o<<1,l,m,a,b);
if(b>=m+1)
ans=ans+query(o<<1|1,m+1,r,a,b);
return ans;
}
} inline void TestCase(int ti) {
int n,m;
while(~scanf("%d%d",&n,&m)) {
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
build(1,1,n);
for(int i=1; i<=m; i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==1) {
printf("%lld\n",query(1,1,n,b,c));
} else {
int d;
scanf("%d",&d);
update(1,1,n,b,c,d);
}
}
}
}

SCUT - 321 - Tobby's magic - 线段树的更多相关文章

  1. 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律

    F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...

  2. HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. SCUT - 337 - 岩殿居蟹 - 线段树 - 树状数组

    https://scut.online/p/337 这个东西是个阶梯状的.那么可以考虑存两棵树,一棵树是阶梯的,另一棵树的平的,随便一减就是需要的阶梯. 优化之后貌似速度比树状数组还惊人. #incl ...

  4. SCUT - 77 - 哈利波特与他的魔法杖 - 线段树

    https://scut.online/p/77 线段树的一种奇怪的应用,暴力区间更新,每次update直接pushdown到底部,然后从维护底部.这样下次update的时候假如提前遇到底部就很快返回 ...

  5. SCUT - 153 - 小马哥和他的山脉 - 线段树

    https://scut.online/p/153 其实不需要用线段树,只关心相邻元素的差,像神仙那样用差分就可以O1维护的. 但是我偏要用. 交之前写的那个,注意没有st本身的线段树只有lazy标记 ...

  6. HDU 5125 magic balls(线段树+DP)

    magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  8. HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...

  9. 【线段树】【3-21个人赛】【同样的problemB】

    同一道题  http://blog.csdn.net/zy691357966/article/details/44680121 区间查询最大值 用线段树 比单调队列慢 #include <cst ...

随机推荐

  1. iis常见问题解决

    iis7以上版本部署4.0框架项目常见问题解决 配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overrideModeDefault=&quo ...

  2. Sqlte 知识点记录

    1.表存在 select count(*) from sqlite_master where type='table' and name='MyTable'; sql),path ))"; ...

  3. C++(三)— 二维容器

    1.二维bool向量 vector<vector<bool>> dp(len, vector<bool>(len, false));

  4. Unity3D之Mesh(五)绘制圆

    前言: Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的.那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径  以及分割数: 一.实现过程 基本过程与之前的类似,最 ...

  5. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  6. Spring MVC表单提交

    实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...

  7. 1103 Integer Factorization (30)(30 分)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  8. 判断iOS系统的Model

    获取iOS系统的Model   (参考网址:https://www.theiphonewiki.com/wiki/Models) + (NSString *)getModel{ struct utsn ...

  9. xml schema 中如何定义类似Map的结构

    利用xs:unique关键字.在xs:element里添加unique节点,任意命名,然后用xs:selector来选择需要唯一的域, xs:field 里指定特定的字段. 例如:定义所有Item里的 ...

  10. JSP介绍(4)--- JSP 过滤器

    过滤器是可用于 Servlet 编程的 Java 类,可以实现以下目的: 在客户端的请求访问后端资源之前,拦截这些请求. 在服务器的响应发送回客户端之前,处理这些响应. 过滤器通过 Web 部署描述符 ...