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. matlab函数之imresize()

    B = imresize(A,scale) B = imresize(A,scale) 返回图像 B,它是将 A 的长宽大小缩放 scale 倍之后的图像.输入图像 A 可以是灰度.RGB 或二值图像 ...

  2. cocos2d-x 3.9 android studio项目命令行打包

    进入创建的项目的 proj.android-studio目录 cocos run/compile -p android --android-studio 这样就可以打包了

  3. BI入门经典(转载)

    原帖地址:http://blog.csdn.net/sgtzzc/archive/2009/10/10/4649770.aspx [前言] 昨天论坛的SQL Server大版新增了一个BI板块,大家讨 ...

  4. asp.net中关于《%=》《%#》《%》 的用法——(转帖)

    1:在.aspx页面,<% %>标签相当于在.cs页面的代码,也就是说你在.cs文件里面怎样写,就可以在.aspx文件里面的<% %>标签里面怎样写. 2:在.aspx页面,& ...

  5. Python习题-一个函数实现读写功能

    def new_op_file(filename,content=None): f = open(filename,'a+') f.seek(0) if content: #非空即真,如果有内容就往下 ...

  6. Git_学习_05_ 解决冲突

    二.参考资料 1.使用git pull文件时和本地文件冲突怎么办? 2.git 冲突解决

  7. Mysql总结_03_mysql常用命令

    一.MySQL服务的启动和停止 net stop mysql net start mysql 二.登陆mysql mysql -u用户名 -p用户密码 键入命令mysql -uroot -p, 回车后 ...

  8. JavaWEB - 请求的转发和重定向

    JavaWEB - Servlet

  9. Mybatis学习--Java API

    学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/java-api.html#directoryStructure 既然你已经知道如何配 ...

  10. 图的Tarjan算法

    “Tarjan有三种算法 你们知道吗”——Tar乙己 void tarjan(int x) { low[x]=dfn[x]=++ind; q[++top]=x;mark[x]=; for(int i= ...