这题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. C# 使用 AutoResetEvent 或 ManualResetEvent 同步两个线程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 提权后获取linux root密码

    提权后获取linux root密码 2011-09-09 10:45:25     我来说两句      收藏    我要投稿 在webbackdoor本身是root(可能性小的可怜)或通过某漏洞溢出 ...

  3. 条件随机场CRF简介

    http://blog.csdn.net/xmdxcsj/article/details/48790317 Crf模型 1.   定义 一阶(只考虑y前面的一个)线性条件随机场: 相比于最大熵模型的输 ...

  4. GridControl Find/Clear 添加图标

    public static void ControlFind(GridControl grid) { FindControl fControl = null; foreach (Control ite ...

  5. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  6. zoj 3591 Nim 博弈论

    思路:先生成序列再求异或,最多的可能为n*(n+1)/2: 在去掉其中必败的序列,也就是a[i]=a[j]之间的序列. 代码如下: #include<iostream> #include& ...

  7. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource

    一.概述 1.Spring offers several options for configuring data-source beans in your Spring application, i ...

  8. JLINK固件丢失或升级固件后提示Clone的解决办法

    J-LINK V8固件烧录指导 J-LINK 是使用过程中,如果内部固件意外损坏或丢失,请参考下面操作步骤说明,重新烧录JLINK固件. 安装固件烧录软件 请ATMEL官方网址下载AT91-ISP下载 ...

  9. [iOS]SourceTree+oschina实现代码远程托管

    在iOS开发, 涉及到多人协同开发的时候, 这个时候, 我们就得利用版本控制系统(例如GIT), 来合并和管理代码了, 今天我们来讲一下, 利用 SourceTree+oschina进行版本控制 先来 ...

  10. http://www.ruanyifeng.com/blog/2007/03/metadata.html

    http://www.ruanyifeng.com/blog/2007/03/metadata.html