题目链接

题意:

只有这两种操作

a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

代码风格更新后:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
#define LL __int64
const int maxn = +;
using namespace std;
LL sum[*maxn], lz[*maxn]; void pushup(int rt)
{
sum[rt] = sum[*rt]+sum[*rt+];
}
void pushdown(int l, int r, int rt)
{
if(lz[rt]!=)
{
int mid = (l+r)/;
sum[*rt] += lz[rt]*(mid-l+);
sum[*rt+] += lz[rt]*(r-mid);
lz[*rt] += lz[rt];
lz[*rt+] += lz[rt];
lz[rt] = ;
}
}
void build(int l, int r, int rt)
{
if(l==r)
{
scanf("%I64d", &sum[rt]);
return;
}
int mid = (l+r)/;
build(lson);
build(rson);
pushup(rt);
}
void update(int ll, int rr, LL c, int l, int r, int rt)
{
if(ll>r) return;
if(rr<l) return;
if(ll<=l && rr>=r)
{
lz[rt] += c;
sum[rt] += (r-l+)*c;
return;
}
pushdown(l, r, rt);
int mid = (l+r)/;
update(ll, rr, c, lson);
update(ll, rr, c, rson);
pushup(rt);
}
LL query(int ll, int rr, int l, int r, int rt)
{
if(ll>r) return ;
if(rr<l) return ;
if(ll<=l && rr>=r)
return sum[rt];
pushdown(l, r, rt);
int mid = (l+r)/;
return query(ll, rr, lson)+query(ll, rr, rson);
}
int main()
{
int n, q, a, b;
LL c;
char ch;
while(~scanf("%d%d", &n, &q))
{
memset(sum, , sizeof(sum));
memset(lz, , sizeof(lz));
build(, n, );
while(q--)
{
getchar();
scanf("%c %d %d", &ch, &a, &b);
if(ch=='Q')
printf("%I64d\n", query(a, b, , n, ));
else
{
scanf("%I64d", &c);
update(a, b, c, , n, );
}
}
}
return ;
}

代码风格更新前:

分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久。

val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
int n, q;
__int64 a[maxn];
struct line
{
int l, r;
LL val, lazy;
}tr[*maxn]; void build(int o, int l, int r)
{
tr[o].l = l; tr[o].r = r;
tr[o].lazy = ;
if(l==r)
{
tr[o].val = a[l];
return;
}
int mid = (l+r)/;
build(*o, l, mid);
build(*o+, mid+, r);
tr[o].val = tr[*o].val+tr[*o+].val;
}
void update(int o, int l, int r, int add)
{
if(tr[o].l==l && tr[o].r==r)
{
tr[o].lazy += add;
return;
}
if(tr[o].lazy) //之前没向下更新的向下更新
{
tr[*o].lazy += tr[o].lazy;
tr[*o+].lazy += tr[o].lazy;
tr[o].val += (LL)(tr[o].lazy*(tr[o].r-tr[o].l+)); //同时把lazy存的加到和里
tr[o].lazy = ;
}
tr[o].val += (LL)(add*(r-l+)); //在这个过程中把新增加的加上,注意左右区间
int mid = (tr[o].l+tr[o].r)/;
if(r <= mid) update(*o, l, r, add);
else if(l > mid) update(*o+, l, r, add);
else
{
update(*o, l, mid, add);
update(*o+, mid+, r, add);
}
} LL query(int o, int l, int r)
{
if(tr[o].l==l && tr[o].r==r)
return tr[o].val + tr[o].lazy*(r-l+);
if(tr[o].lazy) //由于之前可能存在 没有更新的所以向下更新
{
tr[*o].lazy += tr[o].lazy;
tr[*o+].lazy += tr[o].lazy;
tr[o].val += (LL)(tr[o].lazy*(tr[o].r-tr[o].l+));
tr[o].lazy = ;
}
int mid = (tr[o].l+tr[o].r)/;
if(r<=mid) return query(*o, l, r);
else if(l > mid) return query(*o+, l, r);
else
{
return query(*o, l, mid) + query(*o+, mid+, r);
}
}
int main()
{
int i, l, r, add;
char ch;
while(~scanf("%d%d", &n, &q))
{
for(i = ; i <= n; i++)
scanf("%I64d", &a[i]);
build(, , n);
for(i = ; i <= q; i++)
{
getchar();
scanf("%c", &ch);
if(ch=='Q')
{
scanf("%d%d", &l, &r);
printf("%I64d\n", query(, l, r));
}
else
{
scanf("%d%d%d", &l, &r, &add);
update(, l, r, add);
}
}
}
return ;
}

poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  4. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  5. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  6. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  7. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  8. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. “Microsoft Visual Studio遇到了问题,需要关闭”解决办法

    运行VS2008,打开项目,弹出错误界面 . 解决办法:将项目中的所有设计窗体关闭并保存,重新打开就OK~

  2. Ubuntu 常用软件安装方法

    macubuntu 安裝方法: $wget https://github.com/downloads/ChinaLuo/Mac_Ubuntu/Mac_Ubuntu-12.04.tar.gz -O /t ...

  3. vim分屏快捷键使用/增大/减小字体使用

    问题描述: vim分屏快捷键使用 问题解决:         (1)vim 分屏快捷键           (2)vim高度改变          (3)vim中增加和减少字体大小  使用快捷键Ctr ...

  4. (ASP.NET)C#连接Oracle数据库示例(中文乱码问题解决)

    接手了一个遗留的ASP.NET系统,数据库用的是Oracle,以前没搞过.NET和Oracle数据库,数据库搞了半天才解决乱码问题,在此做个笔记备忘. 1.下载安装ODAC 1)请去Oracle官网下 ...

  5. oracle中行转列函数

    一.问题描述 有时在“相关子查询中”需要查询某个实体类对应的某个字段有多个值,如果不做行专列查询,会提示返回多个列的错误.例如: 如上图所示,一个组合包,可能对应多个产品,需要你将所对应的多个产品都放 ...

  6. 01-01-01【Nhibernate (版本3.3.1.4000) 出入江湖】配置文件

    默认配置文件名称是:hibernate.cfg.xml 放置在应用程序集的根目录下 <?xml version="1.0" encoding="utf-8" ...

  7. uva 11181

    直接枚举计算就行: #include<cstdio> #include<cstring> #include<algorithm> #define maxn 22 u ...

  8. Unity3d 一些 常见路径

    Application.persistentDataPath C:\Users\Administrator\AppData\LocalLow\Company Name\Product Name 如果改 ...

  9. hdoj 2202 最大三角形

    题目大意:给定n(3<=n<=50000)个点,求其中任意三个点组成的三角形面积最大,输出该面积. 题目传送:http://acm.hdu.edu.cn/showproblem.php?p ...

  10. Codeforces 337D Book of evil

    一道树形dp,写出来是因为最近也做了道类似的.这题是看了分析的思路才做出来的,但感觉很多这样的dp都是利用类似的性质.像这题的话distDown很好想,但distUp的时候就很难想了,其实只要抓住di ...