poj_3468,线段树成段更新
参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=111111;
long long sum[maxn<<2];
long long col[maxn<<2];
void pushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushDown(int rt,int m)
{
if(col[rt])
{
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
sum[rt<<1]+=col[rt]*(m-(m>>1));
sum[rt<<1|1]+=col[rt]*(m>>1);
col[rt]=0;
}
}
void build(int l,int r,int rt)
{
col[rt]=0;
if(r==l)
{
scanf("%lld",&sum[rt]);
return ;
}
int m=(r+l)>>1;
build(lson);
build(rson);
pushUp(rt);
}
void update(int L,int R,int d,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
sum[rt]+=d*(r-l+1);
col[rt]+=d;
return;
}
pushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update(L,R,d,lson);
if(m<R) update(L,R,d,rson);
pushUp(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
long long ret=0;
if(L<=l&&r<=R)
{
return sum[rt];
}
pushDown(rt,r-l+1);
int m=(r+l)>>1;
if(L<=m) ret+=query(L,R,lson);
if(m<R) ret+=query(L,R,rson);
return ret;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
char c;
long long a,b,d;
for(int i=0;i<m;i++)
{
getchar();
scanf("%c%lld%lld",&c,&a,&b);
if(c=='Q')
printf("%lld\n",query(a,b,1,n,1));
else
{
scanf("%lld",&d);
update(a,b,d,1,n,1);
}
}
}
return 0;
}
poj_3468,线段树成段更新的更多相关文章
- poj_3468线段树成段更新求区间和
#include<iostream> #include<string.h> #include<cstdio> long long num[100010]; usin ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- 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. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
随机推荐
- php获取当前url地址的方法小结
js 获取: top.location.href //顶级窗口的地址 this.location.href //当前窗口的地址 php获取当前url地址: #测试网址: http://localhos ...
- CSS学习(四)
伪类(Pseudo-classes) CSS伪类是用来添加一些选择器的特殊效果. 伪类的语法: selector:pseudo-class {property:value;} CSS类也可以使用伪类: ...
- 【剑指offer】Q31:连续子数组的组大和
简短的分析见:http://blog.csdn.net/shiquxinkong/article/details/17934747 def FindGreatestSumOfSubArray(arra ...
- 50个Android开发技巧(12 为控件加入圆角边框)
控件的圆角边框能够使你的App看起来更美观,事实上实现起来也非常easy. (原文地址:http://blog.csdn.net/vector_yi/article/details/24463025) ...
- (一)Eureka 服务的注册与发现
(一)服务的注册于发现(eureka); Eureka Server: 服务注册中心,负责服务列表的注册.维护和查询等功能 在Idea里,新建项目,选择Spring initializer. 下面的p ...
- eclipse启动错误:An internal error occurred during: "reload maven project".
解决方案一: 简单粗暴!直接删除E:\eclipseWorkspace\.metadata目录!然后再单个导入eclipseWorkspace中的项目. 解决方案二: 打开这个配置文件:E:\ecli ...
- 【算法】第二类斯特林数Stirling
第二类Stirling数实际上是集合的一个拆分,表示将n个不同的元素拆分成m个集合的方案数,记为 或者 . 第二类Stirling数的推导和第一类Stirling数类似,可以从定义出发考虑第n+1个元 ...
- SpringBoot学习笔记(5)----SpringBoot中异常处理的三种方法
对于异常的处理,Spring Boot中提供默认的一个异常处理界面,如下图: 但是在实际的运用开发中,这样的页面显然是不友好的,Spring Boot也提供了自定义异常处理的方式,如下总结三种一场处理 ...
- MVC 设计模式
MVC 设计模式: 最早由 Trygve Teenskaug 在 1978 年提出,上世纪 80 年代是程序语言 Smalltalk 的一种内部架构.后来 MVC 被其他领域借鉴,成为了软件工程中的一 ...
- 解决css兼容性
关于CSS对各个浏览器兼容已经是老生常谈的问题了, 网络上的教程遍地都是.以下内容没有太多新颖, 纯属个人总结, 希望能对初学者有一定的帮助. 一.CSS HACK 以下两种方法几乎能解决现今所有HA ...