题意:
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. python_django_urls基础配置

    url配置:请求地址与views函数的匹配 首先,指定根级url配置文件,默认为setting.py中的ROOT_URLCONF='项目名.urls'(俺们也不用去修改啥) 我们urls有两个,一个是 ...

  2. CentOS 7 編譯qBittorrent Web UI安裝指南

    前言 以下是安装qBittorrent教学. 适用于CentOS7或更新版本 适用于qBittorrent4.1.5或更新版本 安裝須知 qBittorrent基于libtorrent,所以必须先安装 ...

  3. 文档 所有空格变为Tab

    遗憾的是记事本.word没有这个功能... 可以生成exe #include <cstdio> #include <cstdlib> #include <cmath> ...

  4. quartz的使用(三)

    1.在数据源数据库中执行下载的quartz的sql语句(创建11张表),其中表头qrtz_可以在在配置文件中更改,对应表创建时更改org.quartz.jobStore.tablePrefix=qrt ...

  5. Android中的SrollView滚动详解

    今天开发遇到一个需求就是ScrollView中嵌套一个ListView,同时需要实现滑动到底部自动加载更多,我们知道ListView滑动到底部简单实现onScrollListener()监听器即可,但 ...

  6. Java中JNI的使用详解第四篇:C/C++中创建Java对象和String字符串对象及对字符串的操作方法

    首先来看一下C/C++中怎么创建Java对象:在JNIEnv中有两种方法是用来创建Java对象的: 第一种方法: jobject  NewObject(jclass clazz  , jmethodI ...

  7. 暴力模拟——cf988E

    很简单的题,就是模拟一下 #include<bits/stdc++.h> using namespace std; #define ll long long ll n,a[],len; i ...

  8. weblogic重置控制台密码

    1.备份文件如下文件 %DOMAIN_HOME%/security/DefaultAuthenticatorInit.ldift 2.进入%DOMAIN_HOME%/security目录,执行下列命令 ...

  9. Delphi中任务栏状态区的编程

    在Windows桌面的任务栏上有一个凹陷的区域,其中显示着系统时钟以及一些图标,这个长方形的区域便是Windows的任务栏状态区(taskbar status area).本文将介绍使用Borland ...

  10. git rm --cached (解决:modified: .idea/workspace.xml,git idea 操作完之后不能pull)

    错误解决:modified: .idea/workspace.xml 这个错误是本地的.idea提交到了远端. 解决办法: .gitignore文件加上: .dea/workspace.xml 可是. ...