A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 54012   Accepted: 16223
Case Time Limit: 2000MS

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

Hint

The sums may exceed the range of 32-bit integers.
 
#include<iostream>
#include<cstdio>
using namespace std; #define lson left,mid,i<<1
#define rson mid+1,right,i<<1|1 typedef __int64 LL;
int a[]; struct IntervalTree
{
int left,right;
LL sum,add;
}f[]; void pushdown(int i)
{
if(f[i].add != )
{
f[i<<].add+=f[i].add;
f[i<<|].add+=f[i].add;
f[i<<].sum+=(f[i<<].right-f[i<<].left+)*f[i].add;
f[i<<|].sum+=(f[i<<|].right-f[i<<|].left+)*f[i].add;
f[i].add=;
}
} void pushup(int i)
{
f[i].sum=f[i<<].sum+f[i<<|].sum;
} void bulid(int left,int right,int i)
{
f[i].left=left,f[i].right=right,f[i].add=;
if(left==right)
{
f[i].sum=a[left];return ;
}
int mid=(left+right)>>;
bulid(lson);
bulid(rson);
pushup(i);
return ;
} void update(int left,int right,int add,int i)
{
if(f[i].left==left && f[i].right==right)
{
f[i].add+=add;
f[i].sum+=(right-left+)*add;
return ;
}
pushdown(i);
if(f[i<<].right>=right) update(left,right,add,i<<);
else if(f[i<<|].left<=left) update(left,right,add,i<<|);
else { update(left,f[i<<].right,add,i<<);update(f[i<<|].left,right,add,i<<|);}
pushup(i);
return ;
} LL query(int left,int right,int i)
{
if(f[i].left==left && f[i].right==right) return f[i].sum;
pushdown(i);
if(f[i<<].right>=right) return query(left,right,i<<);
else if (f[i<<|].left<=left) return query(left,right,i<<|);
else return query(left,f[i<<].right,i<<)+query(f[i<<|].left,right,i<<|);
} int main()
{
int i,n,m,ai,bi,d;
char ch[];
while(~scanf("%d %d",&n,&m))
{
for(i=;i<=n;i++) scanf("%d",a+i);
bulid(,n,);
while(m--)
{
scanf("%s",ch);
if(strcmp(ch,"C")==)
{
scanf("%d %d %d",&ai,&bi,&d);
update(ai,bi,d,);
}
else
{
scanf("%d %d",&ai,&bi);
printf("%I64d\n",query(ai,bi,));
}
}
}
return ;
}

poj 3468 线段树成段更新的更多相关文章

  1. POJ 3468 线段树 成段更新 懒惰标记

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

  2. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

  3. poj 3468 线段树 成段增减 区间求和

    题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...

  4. poj 3669 线段树成段更新+区间合并

    添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...

  5. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

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

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  7. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  8. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  9. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. Bootstrap历练实例:带徽章的列表组

    向列表组添加徽章 我们可以向任意的列表项添加徽章组件,它会自动定位到右边.只需要在 <li> 元素中添加 <span class="badge"> 即可.下 ...

  2. shell脚本,awk在需要的行上打打印空行。

    注解: 判断每行中是否包含字母a,包含了,就将$1的值赋值给变量a,然后判断变量a是否存在,存在打印一个空行,在将变量的值使用空变量b赋值,最后在打印输出. 结果就是在包含有字符a的行上打印一个空行.

  3. 洛谷 P3601 签到题

    https://www.luogu.org/problemnew/show/P3601 一道关于欧拉函数的题. 读完题目以后我们知道所谓的$aindao(x)=x- \phi (x) $. 对于x小的 ...

  4. 二叉搜索树详解(Java实现)

    1.二叉搜索树定义 二叉搜索树,是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值: 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根 ...

  5. 【dp】守望者的逃离

    妙 题目描述 恶魔猎手尤迪安野心勃勃,他背着了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快 ...

  6. mysql 备份 常用脚本

    全备: innobackupex --defaults-file=/data/mysql3316/my3316.cnf --user=root --password=mysqlpass /data/b ...

  7. 在 Ubuntu 环境下实现插入鼠标自动关闭触摸板

    Ubuntu 以及其他衍生版本,如 Linux Mint 等等都可以用官方的 PPA 来安装"触摸板指示"应用程序.打开一个终端,运行以下命令: sudo add-apt-repo ...

  8. 移动端H5前端性能优化指南:

    分享地址:https://isux.tencent.com/h5-performance.html

  9. Mac远程访问Ubuntu

    MacOS和Ubuntu连接到同一个网络使用ping命令可以通信即可.SSH使用SSH可以很方便的在MacOS上访问Ubuntu,不过只能用命令行操作,相当于连接了Ubuntu的终端. 1. Ubun ...

  10. Servlet注意事项

    注意事项 1.对于Servlet的应用程序的目录结构来说,若想让有些文件Servlet可以访问,而用户不能访问的时候,可以将其放置在WEB-INF目录下 2.ServletResponse中getwr ...