POJ 3468 A Simple Problem with Integers(线段树)
题意 : 给你n个数,进行两种操作,第一种是将a到b上所有元素都加上c,第二种是查询a到b上所有元素之和输出。
思路 : 线段树,以前写过博客,但是现在在重刷,风格改变,,所以重新写一篇。。。。
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std ; LL lz[*],p[*] ; void pushup(int rt)
{
p[rt] = p[rt << ] + p[rt << | ] ;
}
void pushdown(int rt,int m)
{
if(lz[rt])
{
lz[rt << ] += lz[rt] ;
lz[rt << | ] += lz[rt] ;
p[rt << ] += (m-m/)*lz[rt] ;
p[rt << | ] += m/ * lz[rt] ;
lz[rt] = ;
}
}
void build(int l,int r,int rt)
{
lz[rt] = ;
if(l == r)
{
scanf("%I64d%*c",&p[rt]) ;
return ;
}
int mid = (l+r) >> ;
build(l,mid,rt << ) ;
build(mid+,r,rt << | ) ;
pushup(rt) ;
}
LL query(int L,int R,int l,int r,int rt)
{
LL sum = ;
if(l >= L && R >= r)
{
return p[rt] ;
}
pushdown(rt,r-l+) ;
int mid = (l+r) >> ;
if(mid >= L)
sum += query(L,R,l,mid,rt << ) ;
if(mid < R)
sum += query(L,R,mid+,r,rt << | ) ;
return sum ;
}
void update(int L,int R, int l,int r,int sc,int rt)
{
if(l >= L && r <= R)
{
lz[rt] += sc ;
p[rt] += (r-l+)*sc ;
return ;
}
pushdown(rt,r-l+) ;
int mid = (l+r) >> ;
if(mid >= L)
update(L,R,l,mid,sc,rt << ) ;
if(mid < R)
update(L,R,mid+,r,sc,rt << | ) ;
pushup(rt) ;
}
int main()
{
int N,Q ,a,b;
LL c ;
char ch[] ;
while(~scanf("%d %d",&N,&Q))
{
build(,N,) ;
for(int i = ; i < Q ; i++)
{
scanf("%s",ch) ;
if(ch[] == 'Q')
{
scanf("%d %d",&a,&b) ;
LL sum = query(a,b,,N,) ;
printf("%I64d\n",sum) ;
}
else
{
scanf("%d %d %I64d",&a,&b,&c) ;
update(a,b,,N,c,) ;
}
}
}
return ;
}
POJ 3468 A Simple Problem with Integers(线段树)的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
随机推荐
- iOS-CALayer
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- IOS判断网络环境
https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html 我下载的是vertio ...
- SQL Server数据库学习笔记-E-R模型
实体(Entities)联系(Relationships)模型简称E-R模型也称E-R方法,是由P.P.Chen于1976年首先提出的.还有一个关键元素Attributes-属性,它提供不受任何数据库 ...
- SQL-Server数据库学习笔记-表
1. 表及其属性 表(Table):也称实体,是存储同类型数据的集合. 列(Field):也称字段.域或属性,它构成表的架构,具体表示为一条信息中的一个属性. 行(Row):也称元组(Tuple),存 ...
- [转]vim常用命令
[转]vim常用命令 http://www.cnblogs.com/sunyubo/archive/2010/01/06/2282198.html http://blog.csdn.net/wooin ...
- 用cmd命令合并N个文件
今天早上朋友发我一篇小说(42个TXT文件),让我给他合并为一个文件.我首先想到的是“Copy”命令,它可以复制文件,也可以合并文件. 例如:合并1.txt和2.txt到12.txt(其为ASCII文 ...
- adb怎么判断是否有root权限,并更改system/app内容
一.首先判断root权限: adb root 结果: C:\signapp>adb root restarting adbd as root # 说明有root权限 ,若是adbd cannot ...
- 1566: [NOI2009]管道取珠 - BZOJ
Description Input第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. 第三行 ...
- UML 用例图,时序图,活动图的定义以及区别
1.用例图,时序图,活动图的定义 1.用例图: 用例图描述了系统提供的一个功能单元.用例图的主要目的是帮助开发团队以一种可视化的方式理解系统的功能需求,包括基于基本流程的"角色" ...
- 【CentOS】搭建Web服务器
参考资料: http://www.paipat.com/?post=24 http://www.cnblogs.com/xiaoluo501395377/archive/2013/04 ...