#include<iostream>
#include<string.h>
#include<cstdio>
long long num[100010];
using namespace std;
struct st
{
int l;
int r;
long long sum;
long long a;
} p[400100];
void build(int t,int l,int r)
{
p[t].l=l;
p[t].r=r;
if(l==r-1)
{
p[t].sum=num[l];
return;
}
int mid=(l+r)/2;
build(t*2+1,mid,r);
build(t*2,l,mid);
p[t].sum=p[t*2].sum+p[t*2+1].sum;
}
void update(int t,int l,int r,int v)
{
if(p[t].l==l&&p[t].r==r)
{
p[t].a += v;
p[t].sum += v*(r -l);
return ;
}
if( p[t].a )
{
p[2*t+1].a += p[t].a;
p[2*t].a += p[t].a;
p[2*t+1].sum += p[t].a*(p[2*t+1].r -p[2*t+1].l);
p[2*t].sum += p[t].a*(p[2*t].r -p[2*t].l);
p[t].a = 0;
}
int mid = (p[t].l+p[t].r)/2;
if( l >= mid )
update(2*t+1,l,r,v);
else
if( r <= mid )
update(2*t,l,r,v);
else
{
update(2*t,l,mid,v);
update(2*t+1,mid,r,v);
}
p[t].sum = p[2*t].sum + p[2*t+1].sum;
}
long long query(int t,int l,int r)
{
if(p[t].l == l && p[t].r == r )
return p[t].sum;
if( p[t].a )
{
p[2*t+1].a += p[t].a;
p[2*t].a += p[t].a;
p[2*t+1].sum +=p[t].a*(p[2*t+1].r - p[2*t+1].l);
p[2*t].sum += p[t].a*(p[2*t].r - p[2*t].l);
p[t].a = 0;
}
int mid =(p[t].l+p[t].r)/2;
if( l >= mid )
return query(2*t+1,l,r);
else
if( r <= mid )
return query(2*t,l,r);
else
return query(2*t,l,mid) + query(2*t+1,mid,r);
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=0; i<n; i++)
scanf("%lld",&num[i]);
memset(p,0,sizeof(p));
build(1,0,n+1);
getchar();
for(int i=0; i<q; i++)
{
char c;
scanf("%c",&c);
if(c=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
getchar();
printf("%lld\n",query(1,x-1,y));
}
else if(c=='C')
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
getchar();
update(1,x-1,y,z);
}
} }
return 0;
}

poj_3468线段树成段更新求区间和的更多相关文章

  1. poj_3468,线段树成段更新

    参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include& ...

  2. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

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

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

  4. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  5. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

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

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

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

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

  8. HDU-1698-Just a Hook-区间更新+线段树成段更新

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...

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

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

随机推荐

  1. 开源ext2read代码走读之-在windows下怎样推断有几个硬盘设备?

    int get_ndisks() {     HANDLE hDevice;               // handle to the drive to be examined     int n ...

  2. ComponentName

    ComponentName,顾名思义,就是组件名称,通过调用Intent中的setComponent方法,我们可以打开另外一个应用中的Activity或者服务. 实例化一个ComponentName需 ...

  3. uninstall OpenJDK9

    sudo apt--jre openjdk--jdk ///要慎用auto命令,会把所有的软件包删掉 https://www.linuxidc.com/Linux/2017-11/148941.htm ...

  4. scikit-learn系列之如何存储和导入机器学习模型

    scikit-learn系列之如何存储和导入机器学习模型   如何存储和导入机器学习模型 找到一个准确的机器学习模型,你的项目并没有完成.本文中你将学习如何使用scikit-learn来存储和导入机器 ...

  5. 【POJ 2828】Buy Tickets

    [题目链接] http://poj.org/problem?id=2828 [算法] 离线用线段树维护序列即可 [代码] #include <algorithm> #include < ...

  6. 20.QT文本文件读写

    #include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #inclu ...

  7. (转)用Lottie制作动画,我的月薪翻了一番!!

    Lottie是Airbnb发布的开源动画库. 帮助动效落地.学会使用Lottie,会极大地提高工作效率. Lottie是一种新的开发动画的方式. 学会使用Lottie,会极大改善你和开发小哥哥撕逼的情 ...

  8. Redis学习笔记(二) Redis 数据类型

    Redis 支持五种数据类型:string(字符串).list(列表).hash(哈希).set(集合)和 zset(有序集合),接下来我们讲解分别讲解一下这五种类型的的使用. String(字符串) ...

  9. Spark SQL 编程API入门系列之Spark SQL的作用与使用方式

    不多说,直接上干货! Spark程序中使用SparkSQL 轻松读取数据并使用SQL 查询,同时还能把这一过程和普通的Python/Java/Scala 程序代码结合在一起. CLI---Spark ...

  10. Unity3D项目

    Input.mousePosition //鼠标点击的位置 0 左键  1右键  2滚轮 Input.GetMouseButton(0) //当鼠标左键处于按下状态的时候返回True Input.Ge ...