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. Mac 上Sublime Text 2配置lua环境

    1,首先下载最新版lua  然后解压到你想解压到的位置     http://www.lua.org/ftp/ 2,运行终端,cd  进入该文件夹src目录下. 3,在终端输入 make macosx ...

  2. jquery的ajax(err)

    load()方法 load()方法是jquery中最为简单和常用的ajax方法. 直接使用ajax技术的流程 1.创建xmlhttprequest对象 2.调用open函数("提交方式&qu ...

  3. 十 Django框架,Cookie

    注意:获取Cookie是在请求对象里处理,设置Cookie是在响应对象里处理 普通Cookieset_cookie()设置普通cookie 参数: key, 键 value='', 值 max_age ...

  4. HTML5中Modernizr类库的作用和使用

    Modernizr 是一个用来检测浏览器功能支持情况的JavaScript 库.通过这个库我们可以检测不同的浏览器对于HTML5特性的支持情况. 使用Modernizr类库和使用其他第三方类库的方法是 ...

  5. java支付宝开发-00-资源帖

    一.一些重要的官方文档 1.沙箱登录 2.沙箱环境使用说明 3.如何使用沙箱环境 4.当面付产品介绍 5.扫码支付接入指引 6.当面付快速接入 7.当面付接入必读 8.当面付进阶功能 9.当面付异步通 ...

  6. jQuery 参考手册 - 选择器

    jQuery 选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元 ...

  7. Failed to install xxxx.apk on device 'emulator-5554!

    异常信息:[HelloAndroid] Performing com.example.helloandroid.HelloAndroid activity launch[HelloAndroid] U ...

  8. C易位构词(华师网络赛)(错排)

    Time limit per test: 2.0 seconds Memory limit: 256 megabytes 易位构词 (anagram),指将一个单词中的字母重新排列,原单词中的每个字母 ...

  9. BZOJ1720:[Usaco2006 Jan]Corral the Cows 奶牛围栏

    我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  10. CentOS 6.X 安装 EPEL 源

    CentOS 6.X 自带的软件源可选的并不多,有时候要找到一个偏门一些的软件,用命令一搜怎么都没有源,考虑到使用软件源配合 yum 命令安装可以自动安装依赖,所以加一个新的软件源迫在眉睫. 考虑到同 ...