题意:
C:对区间[l,r]每一个数+c;
Q:查询区间[l,r]的所有元素的总和。

线段树修改和查找的时间复杂度都是O(logn)。
线段树基本思想:分治。
线段树基本操作:建树、区间查询(最值;和)、区间修改(更新)、单点修改、单点查询。

注意这题,输入说是一行 N、q 单组输入,但是会TLE,多组输入才可以AC。

AC代码:

 //题意:
//C:对区间[l,r]每一个数+c;
// Q:查询区间[l,r]的所有元素的总和。 //线段树修改和查找的时间复杂度都是O(logn)。
//线段树基本思想:分治。
//线段树基本操作:建树、区间查询(最值;和)、区间修改(更新)、单点修改、单点查询。 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath> using namespace std;
#define inf 0x3f3f3f3f;
#define pi acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
typedef long long ll; const int N=1e5+;
ll a[N<<],lazy[N<<];//需要开到节点的四倍大小 void build(int L,int R,int i)
{
if(L==R)//当左右结点相同的时候,说明该节点可以建树,输入即可。
{
scanf("%lld",&a[i]);//即为叶子结点
return;//因为已经确定这个点可以输入了,也就类似叶结点,返回函数上次调用的地方即可。
} //否则往下继续找
int mid=(L+R)>>;
build(L,mid,i<<);//递归建立左子树
build(mid+,R,i<<|);//递归建立右子树
a[i]=a[i<<]+a[i<<|];//统计该点(i)的左子树和右子树之和
//a这个操作也可以另外写到一个函数pushup中(即pushup(i)),这个看自己怎么写代码
//节点数据向上更新 //根据题意写,这一题是求区间和,之前左区间和右区间相加即可
//例如如果求区间内最大值,则写成:a[i]=max(a[i<<1],a[i<<1|1]);
} void pushdown(int i,int len)//节点懒惰标记下推
{
if(lazy[i])//如果懒惰标记为真,说明之前有过懒惰标记,现在需要进行更新
{
lazy[i<<]+=lazy[i];//懒惰标记往左结点传
lazy[i<<|]+=lazy[i];//懒惰标记往右结点传
//左右用 |1 区分
//因为求区间和,所以当区间内每个元素加上一个值时,区间的和也加上这个值
//对于区间求和, 原数组值需要加上lazy标记*子树所统计的区间长度
a[i<<]+=lazy[i]*(len-(len>>));//(len-(len>>1)是左区间的长度
a[i<<|]+=lazy[i]*(len>>);//(len>>1)是右区间的长度
lazy[i]=;//由于懒惰标记向下传递,所以当前节点的懒惰标记取消
}
//对于区间求最大值, 子树的值不需要乘以长度, 所以不需要传递参数区间长度len。
} //注意:
// 1、单点更新, 不需要用到lazy标记
// 2、成段(区间)更新, 需要用到lazy标记来提高时间效率
void update(int x,int y,int L,int R,int i,int pluss)
{
if(L>=x&&R<=y)//当前节点区间包含在查询区间内
//范围缩小到left和right之间
{
a[i]+=pluss*(R-L+);
lazy[i]+=pluss;
return;
}
pushdown(i,R-L+);
int mid=(L+R)>>; //更新区间
if(x<=mid)//更新左区间
update(x,y,L,mid,i<<,pluss);
if(y>mid)//更新右区间
update(x,y,mid+,R,i<<|,pluss); //更新结点值
a[i]=a[i<<]+a[i<<|];
} ll query(int x,int y,int L,int R,int i)//查询操作
{
if(L>=x&&R<=y)//当前节点区间包含在查询区间内
return a[i];//返回当前值
pushdown(i,R-L+);
int mid=(L+R)>>;
ll ans=;
if(x<=mid)//递归查询左子树内部的的区间值
ans+=query(x,y,L,mid,i<<);
if(y>mid)//递归查询右子树内部的的区间值
ans+=query(x,y,mid+,R,i<<|);
return ans;//返回题目所需的区间和(左+右)
} int main()
{
int n,q;
while(~scanf("%d %d",&n,&q))
{
mem(lazy,);//如果多组数据lazy数组需要进行清空
mem(a,);
build(,n,);//开始建树,传入树的总区间(传入最左端点,最右端点)和树的根节点
//建树的过程中输入每一个节点
for(int i=;i<=q;i++)
{
char ch;
getchar();//吸收每次读入的空格
scanf("%c",&ch);
if(ch=='Q')//询问区间内的和
{
int x,y;
scanf("%d %d",&x,&y);
ll ans=query(x,y,,n,);
printf("%lld\n",ans);
}else if(ch=='C')//往区间内每一个数上都插入pluss
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
update(x,y,,n,,z);
}
}
}
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 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. 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 ...

  4. 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 ...

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

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

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

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

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

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

  8. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

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

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

  10. 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. (转载)openwrt nginx

    ln -s ../../../feeds/packages/net/fcgiwrap/ fcgiwrap 本帖最后由 cjd6568358 于 2018-6-4 11:21 编辑 刚又把路由器重置重新 ...

  2. 单向连通图 Going from u to v or from v to u? poj2762

    http://poj.org/problem?id=2762 强连通求子图和子图关系 + 子图关系是链式结构 #include <cstdio> #include <cstdlib& ...

  3. leetcode-52-N皇后②

    题目描述: 方法一:回溯 class Solution: def totalNQueens(self, n: int) -> int: def backtrack(i,tmp,col,z_dia ...

  4. Java——子类对象实例化的全过程

    2.4子类对象实例化的全过程 public class TestDog { public static void main(String[] args) { Dog d = new Dog(); d. ...

  5. 后缀自动机求字典序第k小的串——p3975

    又领悟到了一点新的东西,后缀自动机其实可以分为两个数据结构,一个是后缀树,还有一个是自动机 后缀树用来划分endpos集合,并且维护后缀之间的关系,此时每个结点代表的是一些后缀相同且长度连续的子串 自 ...

  6. NX二次开发-UFUN工程图表格注释section转tag函数UF_TABNOT_ask_tabular_note_of_section

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  7. <router-link :to="...">

    一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串<router-link to=& ...

  8. 20140319 const sizeof define 编译时分配内存

    1.面试宝典预处理,const,sizeof Define作用定义函数: //用一个宏定义FIND求一个结构体struc里某个变量相对于struc的偏移量,如FIND(student,a)//等于0 ...

  9. adostoredproc用法 因为用的少每次还得看一下代码,记下来

    {1.关闭2.清除参数(固定的可省略)3.参数赋值4.打开(或执行)如果有感知控件的话 就会显示出结果} ADOStoredProc1.close; //关闭 ADOStoredProc1.param ...

  10. python执行环境

    转自 http://www.cnblogs.com/BeginMan/p/3191856.html 一.python特定的执行环境 在当前脚本继续进行 创建和管理子进程 执行外部命令或程序 执行需要输 ...