这题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的更多相关文章

  1. 线段树专题 POJ3468 A Simple Problem with Integers

    题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long ...

  2. hdu 1698+poj 3468 (线段树 区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

  3. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  4. POJ 3468 线段树区间修改查询(Java,c++实现)

    POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

  5. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

  6. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  7. POJ - 3468 线段树区间修改,区间求和

    由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...

  8. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  9. 线段树的区间更新---A Simple Problem with Integers

    POJ   3468 Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 100 ...

随机推荐

  1. 驱动笔记 - Makefile

    ifneq ($(KERNELRELEASE),) obj-m := hello.ohello-objs := main.o add.o else KDIR := /lib/modules/2.6.1 ...

  2. Sqli-labs less 58

    Less-58 执行sql语句后,并没有返回数据库当中的数据,所以我们这里不能使用union联合注入,这里使用报错注入. Payload:http://127.0.0.1/sqli-labs/Less ...

  3. ssh-copy-id(双机互信)

    最简单的2步骤: ssh-keygen -t rsa 需要输入的地方就回车 ssh-copy-id root@192.168.0.10 详细:ssh-keygen 创建公钥和密钥. ssh-copy- ...

  4. 理解Flight框架核心

    http://blog.csdn.net/sky_zhe/article/details/38906689 Flight 框架 Flight类 1.加载 autoload.php ,启动框架的自动加载 ...

  5. lintcode:最小编辑距离

    最小编辑距离 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=&quo ...

  6. 分布式 Key-Value 存储系统:Cassandra 入门

    Apache Cassandra 是一套开源分布式 Key-Value 存储系统.它最初由 Facebook 开发,用于储存特别大的数据. Cassandra 不是一个数据库,它是一个混合型的非关系的 ...

  7. iOS开发--项目内存优化

    在用非ARC模式编写iOS程序的时候,造成程序内存泄露在所难免,后期我们一般会进行内存优化.自己比较常用的内存优化方法有两种 1.Analyze,静态分析内存泄露的方法.很简单,在Xcode菜单栏中点 ...

  8. Java-马士兵设计模式学习笔记-桥接模式

    一.概述 1.桥接模式的应用情况:(1)两个维度扩展(2)排列组合 二.代码 1.Gift.java public class Gift { protected GiftImpl giftImpl; ...

  9. Java-马士兵设计模式学习笔记-代理模式-动态代理 修改成可以任意修改代理逻辑

    一.概述 1.目标:动态代理的代理逻辑可以任意修改 2.思路: (1)要把代理逻辑抽离,站在jvm的角度思考,应独立出InvocationHandler接口,并接收被代理的对象及方法作为参数invok ...

  10. [iOS问题归总]SourceTree+osChina版本管理出现的问题

    1.commit失败(1) 解决办法: 把爆红的路径的文件删除掉, 重新commit 2. push时候, 有100MB限制 解决办法: Git只允许上传最大100MB的文件,如果超过,则会被serv ...