#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. linux中设置TAB键的宽度

    对于编程的人员来说,常常须要排版代码,这时候就须要TAB键,但TAB键的宽度太大,非常有可能代码太长,延伸到下一行,这个时候你就须要设置TAB键的宽度了. linux下设置TAB键的宽度,做法例如以下 ...

  2. Java之旅--Web.xml解析

    Windows的IIS,是用UI界面进行网站的配置.Linux以下的差点儿全部系统,都是使用配置文件来进行配置,Java容器(JBoss/Tomcat/Jetty/WebSphere/WebLogic ...

  3. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  4. Gretna2.0 使用过程中遇到的问题

    在做Normalize的时候,报错"Cannot find T1 image (e.g. *.dcm in T1 Directory), Please Check again!", ...

  5. ASP.NET Core-组件:目录

    ylbtech-ASP.NET Core-组件:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http ...

  6. TP5异常处理

    TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...

  7. 21.QT二进制文件

    #include "mainwindow.h" #include <QApplication> #include <QFile> #include < ...

  8. 16. 3Sum Closest[M]最接近的三数之和

    题目 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

  9. github结合TortoiseGit使用sshkey,无需每次输入账号和密码

    首先需要明确,github上支持三种方式进行项目的clone    https,ssh,subversion ssh的方式 git@github.com:用户名/版本库t.git            ...

  10. css实战笔记(一):写网页前的reset工作

    reset.css是每个html必备的样式,其中有各种元素属性清零的代码. 为什么要有reset.css 让各个浏览器的CSS样式有一个统一的基准,比如清除各个浏览器为元素自带的margin.padd ...