题目地址http://poj.org/problem?id=3468

题目大意很简单,有两个操作,一个

Q a, b 查询区间[a, b]的和

C a, b, c让区间[a, b] 的每一个数+c

第一次线段树的延时标记,花了好大的功夫才写好==!

很容易看出来使用使用线段树记录区间的和,但是难点在于每次修改的是一个区间而不是一个点

所以采用的方法就是每次做修改操作时,只将区间[a,b]的标记+c,而不是真正意义上的将区间[a, b] 的每一个值+c。

而当我们做查询操作时,就只需要将区间[a, b]在从[1, N]开始查找到查找到时所经过的区间的标记往下传递就可以了(同时记得更新当前节点的值)

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct Node { LL sum, add; } tree[MAXN<<];
int a, b, N, Q;
LL c; void updataChild(int k, int L, int R)//将编号为k的标记传到他的子节点去
{
tree[k].sum += (R-L+) * tree[k].add;//先更新它自己的sum值
tree[k<<].add += tree[k].add;//将左右子节点的add值更新
tree[k<<|].add += tree[k].add;
tree[k].add = ;//去掉标记
} void update(int k, int L, int R)//更新区间的值
{
if(R<a || b<L) return ;//不再区间内 if(a<=L && R<=b) { tree[k].add += c; return; }//实际上只更新这个区间的add值 int mid = (L+R)>>; update(lson); update(rson); //往左和往右更新 tree[k].sum = tree[k<<].sum + tree[(k<<)+].sum//更新当前节点的sum
+ (mid-L+)*tree[k<<].add + (R-mid)*tree[k<<|].add;
} LL query(int k, int L, int R)
{
if(R<a || b<L) return ; if(a<=L && R<=b) return tree[k].sum + (R-L+)*tree[k].add;//注意加上当前节点的标记 int mid = (L+R)>>; updataChild(k, L, R);//吧标记传到子节点 return query(lson) + query(rson);
} int main()
{
while(~scanf("%d %d", &N, &Q))
{
mem0(tree); char ch;
for(a=;a<=N;a++)
{
scanf("%lld%*c", &c); b = a;//区间是[a, a]
update(, , N);
}
for(int i=;i<Q;i++)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d %d%*c", &a, &b);
printf("%lld\n", query(,,N));
}
else
{
scanf("%d %d %lld%*c", &a, &b, &c);
update(,,N);
}
}
}
return ;
}

POJ3468 A Simple Problem with Integers(线段树延时标记)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  2. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  3. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

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

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

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  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 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

随机推荐

  1. ORACLE 修改日志大小及增加日志成员

    日志文件能不能resize,直接扩大日志文件的大小?10g是不能的. 网上的一般方法就是新建两个临时日志组(oracle至少要求两个日志组),切换到这两个临时日志组后,删掉重建扩大或缩小,再添加日志组 ...

  2. eclipse环境下tomcat远程调试方法

    前提:Windows环境tomcat是以catalina.bat方式而非Windows服务形式启动(两者所使用的JAVA配置及JVM参数会有差异). 服务器段设置 方法1:修改CATALINA_OPT ...

  3. 使用rsync+inotify+apache做分布式图片服务器的部署方法

    图片服务器一般是做成分布式的,但要使得所有的图片服务器的文件一致,可以由一个主服务器将文件推送到各个备份服务器上. rsync:文件差异检查及文件推送 inotify:事件触发,实时检测到添加.删除. ...

  4. 一篇关于apache commons类库的详解

    1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...

  5. XRPictureBox z

    XRPictureBox 大小加入是40x40  我绑定的图片好比是60X50 , 在不自己写代码的情况下,XRPictureBox 有没有什么属性可以调整,比如像SizeMode那种? // Set ...

  6. html --- canvas --- javascript --- 绘图方法

    Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像. 如有疑问请访问链接:http://javascript.ruanyifeng.com/htmlapi/canvas.html < ...

  7. 自学Java过程

    由于之前判断失误,其实也不应该说失误吧,自己脱产花了几个月来啃C,现在基本上算是啃完了吧,之所以说失误是因为:没有找到跟C有关的适合我的工作!!! 本来的打算是先把基础搞定然后去找找看有没有肯收留打杂 ...

  8. Package inputenc Error: Unicode char \u8: not set up for use with LaTeX.

    用TexStudio编辑文档时,不知是多加了空格还是啥,总是提示如下错误: Package inputenc Error: Unicode char \u8: not set up for use w ...

  9. 我的日常工具——gdb篇

    我的日常工具——gdb篇 03 Apr 2014 1.gdb的原理 熟悉linux的同学面试官会问你用过gdb么?那好用过,知道gdb是怎么工作的么?然后直接傻眼... gdb是怎么接管一个进程?并且 ...

  10. Chapter7:类

    关于this指针 成员函数通过一个名为this的额外的隐式参数来访问调用它的对象.当我们调用一个成员函数时,用请求该函数的对象初始化this. total.isbn(); //等价于编译器重写为 Sa ...