poj_3468线段树成段更新求区间和
#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线段树成段更新求区间和的更多相关文章
- poj_3468,线段树成段更新
参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include& ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- 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 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
随机推荐
- linux中设置TAB键的宽度
对于编程的人员来说,常常须要排版代码,这时候就须要TAB键,但TAB键的宽度太大,非常有可能代码太长,延伸到下一行,这个时候你就须要设置TAB键的宽度了. linux下设置TAB键的宽度,做法例如以下 ...
- HttpClient学习系列 -- 学习总结
jar包: HttpClient 4.x版本 简要介绍 HttpComponents 包括 HttpCore包和HttpClient包 HttpClient:Http的执行http请求 Default ...
- 机器学习 LR中的参数迭代公式推导——极大似然和梯度下降
Logistic本质上是一个基于条件概率的判别模型(DiscriminativeModel). 函数图像为: 通过sigma函数计算出最终结果,以0.5为分界线,最终结果大于0.5则属于正类(类别值为 ...
- 【转】Docker基础
一.简介 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的CGroup.Namespace.Union FS等技术实现的一种系统级虚拟化技术. 特性 更高效的利用系统资源: ...
- ZOJ 3019 Puzzle
解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS 因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可. 反思:一开始 ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- Reflection (computer programming) -反射-自身结构信息
n computer science, reflection is the ability of a computer program to examine, introspect, and modi ...
- 安装Oracle 12c及解决遇到的问题
一.[INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置) 原文链接:https://blog.csdn.net/u013388049/article/details/85 ...
- webpack——entry,output,plugins,loader,chunk知识
entry:打包入口 代码的入口,找到依赖模块 打包的入口,webpack去哪个文件找依赖,或是去那个文件依赖的依赖 可以是一个或者多个入口 例如: 1.module.exports={ entry: ...
- C语言基本语法——数组
一.一维数组 1.什么是数组 2.数组语法 3.下标 4.初始化 5.数组名和数组首地址 二.一维数组的应用 1.数组的赋值与拷贝 2.数组的正反遍历 3.随机数 4.数组乱序 5.数组的重复 三.二 ...