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 ...
随机推荐
- ThinkPad.E440_安装固态硬盘
1.ThinkPad(E440) 加装SSD固态硬盘,并改装双硬盘_百度经验.html(https://jingyan.baidu.com/article/9f63fb91856ec7c8400f0e ...
- 一段神奇的代码,解决form表单背景偏黄问题
一段神奇的代码,解决form表框背景偏黄问题 最近在做项目时,发现自己做的挺好看的表单,背景变成了黄色,所以这次折腾了好久终于找到了符合我的决绝办法,现在来分享给大家 一般解决这种input表框偏黄问 ...
- java 中的拦截器和过滤器
区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几 ...
- python3反射解析
python反射解析 一. 简介 python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某 ...
- JavaUtil_02_二维码的生成与解析
1.引入jar包 zxing-core-1.7.jar : http://viralpatel.net/blogs/download/jar/zxing-core-1.7.jar zxing-j ...
- 百度编辑器UEditor配置toolbars工具条功能按钮
两种方式: 1.代码中定义 <script id="container" name="content" type="text/plain&quo ...
- BEC listen and translation exercise 31
听力练习: All societies have ways of encouraging and enforcing what they view as appropriate behaviour w ...
- IP通信中音频编解码技术与抗丢包技术概要
此文较长,建议收藏起来看. 一.一个典型的IP通信模型 二.Server2Server技术分类 Server2Server这块也是一个专门的领域,这里只简单分个类. 1.同一国家相同运营商之间: 同一 ...
- ACM学习历程—HDU5637 Transform(数论 && 最短路)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...
- bzoj 3727: Final Zadanie 思维题
题目: Description 吉丽YY了一道神题,题面是这样的: "一棵n个点的树,每条边长度为1,第i个结点居住着a[i]个人.假设在i结点举行会议,所有人都从原住址沿着最短路径来到i结 ...