Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C 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.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
在段更新和查找时要注意,当父节点的下方和子节点的下方同时须要更新时不能直接覆盖了子节点的num,而是要加起来。
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 100100
struct Tree
{
__int64 sum,num;//分别表示当前段的总和,子点的每个点所要加的值
bool b;//判断子节点是否要更新
}tree[4*N];
__int64 init[N+5];//初始时每个点的值
void builde(int l,int r,int k)
{
int m=(l+r)/2;
tree[k].b=false; tree[k].num=0;
if(l==r)
{
tree[k].sum=init[r]; return ;
}
builde(l,m,k*2);
builde(m+1,r,k*2+1);
tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
}
void set_child(int l,int r,int k)
{
int m=(l+r)/2;
if(tree[k*2].b==true)//注意这里,不能直接覆盖了
tree[k*2].num+=tree[k].num;
else
tree[k*2].num=tree[k].num;
tree[k*2].sum+=(m-l+1)*tree[k].num; if(tree[k*2+1].b==true)
tree[k*2+1].num+=tree[k].num;
else
tree[k*2+1].num=tree[k].num;
tree[k*2+1].sum+=(r-m)*tree[k].num;
tree[k*2].b=tree[k*2+1].b=true;
}
void updata(int l,int r,int k,int L,int R,__int64 num)
{
int m=(l+r)/2;
if(L<=l&&r<=R)
{
if(tree[k].b==true)
tree[k].num+=num;
else
tree[k].num=num;
tree[k].sum+=(r-l+1)*num;
tree[k].b=true;
return ;
}
if(tree[k].b==true)
set_child(l,r,k);
if(L<=m) updata(l,m,k*2,L,R,num);
if(R>m) updata(m+1,r,k*2+1,L,R,num);
tree[k].b=false;
tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
}
__int64 SUM;
void calculate(int l,int r,int k,int L,int R)
{
int m=(l+r)/2;
if(L<=l&&r<=R)
{
SUM+=tree[k].sum; return ;
}
if(tree[k].b==true)
set_child(l,r,k);
if(L<=m) calculate(l,m,k*2,L,R);
if(R>m) calculate(m+1,r,k*2+1,L,R);
tree[k].b=false;
}
int main()
{
int n,m,L,R,i;
__int64 num;
char ch[2];
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%I64d",&init[i]);
getchar();
builde(1,n,1);
while(m--)
{
scanf("%s%d%d",ch,&L,&R);
if(ch[0]=='Q')
{
SUM=0; calculate(1,n,1,L,R);
printf("%I64d\n",SUM);
}
else
{
scanf("%I64d",&num);
updata(1,n,1,L,R,num);
}
}
}
												

poj3468A Simple Problem with Integers(线段树,在段更新时要注意)的更多相关文章

  1. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

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

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

  3. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

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

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

  5. A Simple Problem with Integers(线段树,区间更新)

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

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

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

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

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

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

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

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

随机推荐

  1. vim 大小写转化命令

    vim中大小写转化的命令是<blockquote>gu或者gU</blockquote>形象一点的解释就是小u意味着转为小写:大U意味着转为大写. 剩下的就是对这两个命令的限定 ...

  2. [转]vi与vim的区别

    一直用着vi,有朋友劝我用vim,那么它们有什么区别呢? 简单点来说,它们都是多模式编辑器, 不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令, 而且还有一些新的特性在里面. vim的这些优 ...

  3. 《Windows核心编程》第5版 学习进度备忘

    学习资源:<Windows核心编程>第5版 知识基础支持: 本书与<Windows程序设计>第5版珍藏版结合很好,二者重叠内容不多,二者互补性强,而且相关方面的优秀书籍 跳过的 ...

  4. 【leetcode】155 - Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 使用SpringMVC ...

  6. 利用BlazeDS的AMF3数据封装与Flash 进行Socket通讯

    前几天看到了Adobe有个开源项目BlazeDS,里面提供了Java封装AMF3格式的方法.这个项目貌似主要是利用Flex来Remoting的,不过我们可以利用他来与Flash中的Socket通讯. ...

  7. ionic 相关

     基本操作 $cordova platform update android@5.0.0 $ npm install -g cordova ionic $ ionic start myApp tabs ...

  8. 【原创译文】基于Docker和Rancher的超融合容器云架构

    基于Docker和Rancher的超融合容器云架构 ---来自Rancher和Redapt 超融合架构在现代数据中心是一项巨大的变革.Nutanix公司发明了超融合架构理论,自从我听说他们的“iPho ...

  9. Strider SSH Deploy配置

    登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令

  10. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...