POJ 3468 (线段树 区间增减) A Simple Problem with Integers
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好。
因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递。更新和查询都要pushdown。
#include <cstdio> typedef long long LL; const int maxn = + ; int n, m, qL, qR, v;
LL sum[maxn << ], add[maxn << ]; inline void maintain(int o)
{ sum[o] = sum[o*] + sum[o*+]; } void build(int o, int L, int R)
{
if(L == R) { scanf("%I64d", &sum[o]); return; }
int M = (L + R) / ;
build(o*, L, M);
build(o*+, M+, R);
maintain(o);
} void pushdown(int o, int L, int R)
{
if(add[o])
{
int lc = o*, rc = o*+;
int M = (L + R) / ;
add[lc] += add[o];
add[rc] += add[o];
sum[lc] += (LL) add[o] * (M - L + );
sum[rc] += (LL) add[o] * (R - M);
add[o] = ;
}
} void update(int o, int L, int R)
{
if(qL <= L && qR >= R)
{
add[o] += v;
sum[o] += (LL) v * (R - L + );
return;
}
int M = (L + R) / ;
pushdown(o, L, R);
if(qL <= M) update(o*, L, M);
if(qR > M) update(o*+, M+, R);
maintain(o);
} LL query(int o, int L, int R)
{
if(qL <= L && qR >= R) return sum[o];
pushdown(o, L, R);
int M = (L + R) / ;
LL ans = ;
if(qL <= M) ans += query(o*, L, M);
if(qR > M) ans += query(o*+, M+, R);
return ans;
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d%d", &n, &m);
build(, , n);
char op[];
while(m--)
{
scanf("%s", op);
if(op[] == 'Q')
{
scanf("%d%d", &qL, &qR);
printf("%I64d\n", query(, , n));
}
else
{
scanf("%d%d%d", &qL, &qR, &v);
update(, , n);
}
} return ;
}
代码君
POJ 3468 (线段树 区间增减) A Simple Problem with Integers的更多相关文章
- 线段树专题 POJ3468 A Simple Problem with Integers
题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- poj 3468 线段树区间更新/查询
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- POJ - 3468 线段树区间修改,区间求和
由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...
- 线段树的区间更新---A Simple Problem with Integers
POJ 3468 Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 100 ...
随机推荐
- win7修改hosts文件方法
因权限的关系,导致无法修改,解决如下: hosts属性 --> 安全 --> 编辑权限 --> 完全控制,就可以修改该文件了.win7默认不能修改hosts文件.
- Codeforces Round #260 (Div. 2) A~C
题目链接 A. Laptops time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputo ...
- SOLID 原则
世纪的前几年里,“ Uncle Bob”Robert Martin 引入了用OOP 开发软件的五条原 则,其目的是设计出更易于维护的高质量系统.无论是设计新应用程序,还是重构现有基 本代码,这些 S ...
- Java集合框架(四)
Collections 集合框架的工具类 着重讲解以下方法: 1.sort(): 1º根据元素的自然顺序对指定列表按升序进行排序,列表中的所有元素都必须实现comparable接口. pu ...
- 23种设计模式学习一(单列模式)singleton
单列模式的类(单线程下的) class Singleton { private static Singleton instance; private Singleton() { } public st ...
- AC自动机总结
AC自动机的模板 void buildAC() { while(!q.empty()) q.pop(); q.push(); while(!q.empty()) { int x=q.front();q ...
- PushBackInputStream与PushBackInputStreamReader的用法
举个例子:获取XX内容 PushBackInputStream pb=new PushBackInputStream(in,4);//4制定缓冲区大小 byte[] buf=new byte[4]; ...
- Android核心分析之二十Android应用程序框架之无边界设计意图
Android应用程序框架1 无边界设计理念 Android的应用框架的外特性空间的描述在SDK文档(http://androidappdocs.appspot.com/guide/topics/fu ...
- iOS tintColor解析
在UIView中一个相对来说比较小的属性,tintColor属性是相当的强大.今天我们就来看看如何使用他,包含使用tint color进行着色标准控件.我们自定义控件甚至重新着色图像. 本章的实例程序 ...
- Data Flow ->> Term Lookup
Term Lookup是和Term Extraction相反的一个组件.Term Extraction是为了找出term,Term Lookup是利用我们关心的term list来找出某个文本里面包含 ...