SCUT - 321 - Tobby's magic - 线段树
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 - 线段树的更多相关文章
- 【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 ...
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- SCUT - 337 - 岩殿居蟹 - 线段树 - 树状数组
https://scut.online/p/337 这个东西是个阶梯状的.那么可以考虑存两棵树,一棵树是阶梯的,另一棵树的平的,随便一减就是需要的阶梯. 优化之后貌似速度比树状数组还惊人. #incl ...
- SCUT - 77 - 哈利波特与他的魔法杖 - 线段树
https://scut.online/p/77 线段树的一种奇怪的应用,暴力区间更新,每次update直接pushdown到底部,然后从维护底部.这样下次update的时候假如提前遇到底部就很快返回 ...
- SCUT - 153 - 小马哥和他的山脉 - 线段树
https://scut.online/p/153 其实不需要用线段树,只关心相邻元素的差,像神仙那样用差分就可以O1维护的. 但是我偏要用. 交之前写的那个,注意没有st本身的线段树只有lazy标记 ...
- HDU 5125 magic balls(线段树+DP)
magic balls Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...
- 【线段树】【3-21个人赛】【同样的problemB】
同一道题 http://blog.csdn.net/zy691357966/article/details/44680121 区间查询最大值 用线段树 比单调队列慢 #include <cst ...
随机推荐
- CSS基础语法和CSS经常用到的知识点总结
1. [代码]css基础教程 CSS基础语法CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.每条声明由一个属性和一个值组成.每个属性有一个值.属性和值被冒号分开.例如:下面这行代 ...
- Luogu P1196 [NOI2002]银河英雄传说:带权并查集
题目链接:https://www.luogu.org/problemnew/show/P1196 题意: 有30000个战舰队列,编号1...30000. 有30000艘战舰,编号1...30000, ...
- eclipse自动提示功能没了的解决办法
由于重新配置了环境,并且eclipse也是装的4.2的,今天用的时候发现了,居然没有自动提示功能,也就是当一个对象居然点不出他的相关方法.后来网上搜索了下,成功的 办法是. 1.我window-> ...
- unity的一些重要技巧(转)【整理他人的东西】
刚开始学习Unity3D时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇文章,觉得还不错,于是翻译过来和大家共享.原文地址: http://devmag.org ...
- jQuery插件--图片文字向上向左循环滚动
需要引用jquery 调用非常简单: 一. 向上滚动 $(".scroll_two").jScroll({vertical: true}); <div class=" ...
- BEC listen and translation exercise 36
你所持的护照可使你享有免费医疗.Your passport qualifies you to receive free medical treatment.公司指使其职员挖对手的客户.The comp ...
- bison 编译sql的基本知识
一.bison是干什么的? bison 是一个语法分析器,把用户输入的内容,根绝在.y文件中事先定义好的规则,构建一课语法分析树.(所谓的规则就是,匹配上对应字符之后,执行相应的动作.) 1.一个简单 ...
- 06 - Django应用第三步
知识点 1) 编写urls 配合include()的URL查找过程 获取正则抓取的值并命名, 给url取名 2) 模板的编写 for循环的遍历 用点的方式执行函数, 不带括号 3) 视图函数的编写 H ...
- HNOI2008玩具装箱 斜率优化
题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...
- Gym - 100801D:Distribution in Metagonia (数学)
题意:给定一个N,让你把它拆成若干个只含素因子2和3的数之和,且两两之间没有倍数关系,比如10=4+6. 思路:即是2因子的幂递增,3因子的幂递减:或者反之. 对于当前N,我们拆分出的数为num=2^ ...