题目链接

题意:

只有这两种操作

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. iOS10和Xcode8适配

    1 Xib文件的注意事项 使用Xcode8打开xib文件后,会出现下图的提示. 大家选择Choose Device即可. 之后大家会发现布局啊,frame乱了,只需要更新一下frame即可.如下图 注 ...

  2. 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB

    最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子     1.使用springmvc代替strutsMVC     2.使用请求json数据串的方式代替传统 ...

  3. 修改tomcat 启动45秒

    当我们需要增加Tomcat的启动时间,修改方法如下:

  4. 【POJ】【2348】Euclid‘s Game

    博弈论 题解:http://blog.sina.com.cn/s/blog_7cb4384d0100qs7f.html 感觉本题关键是要想到[当a-b>b时先手必胜],后面的就只跟奇偶性有关了 ...

  5. 一些css效果积累

    悬浮效果: ul li a{text-decoration: none;color: black}  ul li a:hover{color: blue}   鼠标变小手 span:hover{cur ...

  6. Unity3d Detect NetState

    public static bool HasConnection() { System.Net.WebClient client; System.IO.Stream stream; try { usi ...

  7. c++ 虚继承与继承的差异 (转)

    转自:CSDN dqjyong 原文链接:http://blog.csdn.net/dqjyong/article/details/8029527 前面一篇文章,说明了在C++ 虚继承对基类构造函数调 ...

  8. js和jquery获取文档对象以及滚动条位置

    <div style="width:120px;height:120px;border:1px solid red; position:absolute; left:800px; to ...

  9. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

  10. POJ 1191 棋盘分割(DP)

    题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...