题目地址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. STL set容器的一点总结

    整理了一下set常用语句,参看这篇http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html -------------- ...

  2. UIScrollView 期本使用方法

    UIScrollView 1.   contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView ...

  3. 【C#学习笔记】Hello World

    using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { Conso ...

  4. ubuntu下Rhythmbox播放器乱码问题解决方案

    (注:本文部分内容转自互联网)<a href="http://riden001.com/wp-content/uploads/2014/11/45.jpg"><i ...

  5. 剑指offer—第二章算法之二分查找(旋转数组的最小值)

    旋转数组的最小数字 题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如:数组{3,4,5,1,2}为{1,2,3,4, ...

  6. Java 中的二维数组

    所谓二维数组,可以简单的理解为是一种“特殊”的一维数组,它的每个数组空间中保存的是一个一维数组. 那么如何使用二维数组呢,步骤如下: 1. 声明数组并分配空间 或者 如: 2. 赋值 二维数组的赋值, ...

  7. hdu 2177(威佐夫博奕)

    题意:容易理解,在威佐夫博奕的基础上新增加了一条要求:就是如果在赢得条件下,输出第一步怎么走. 分析:使用暴力判断,详细见代码. 代码: #include<stdio.h> #includ ...

  8. Android 动画深入解析

    http://blog.csdn.net/rain_butterfly/article/details/39642613

  9. linux umask使用详解

    转自:http://blog.csdn.net/lmh12506/article/details/7281910 umask使用方法 A 什么是umask?   当我们登录系统之后创建一个文件总是有一 ...

  10. [转]Linux中文件权限目录权限的意义及权限对文件目录的意义

    转自:http://www.jb51.net/article/77458.htm linux中目录与文件权限的意义 一.文件权限的意义 r:可以读这个文件的具体内容: w:可以编辑这个文件的内容,包括 ...