poj 3468 A Simple Problem with Integers(线段树区间更新)
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
#include <iostream>
#include <cstdio>
using namespace std;
#define LL long long
#define lson rt<<1,first,mid
#define rson rt<<1 | 1,mid+1,end
const int maxn=;
LL tree[maxn<<];
LL add[maxn<<];
void push_up(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<< | ];
}
void push_down(int rt,int m) //更新子节点的数值
{
if(add[rt])
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
tree[rt<<]+=add[rt]*(m-(m>>));
tree[rt<<|]+=add[rt]*(m>>);
add[rt]=; //更新后需要还原
}
}
void build(int rt,int first,int end)
{
add[rt]=;
if(first==end)
{
scanf("%lld",&tree[rt]);
//cin>>tree[rt];
return ;
}
int mid=(first+end)>>;
build(lson);
build(rson);
push_up(rt);
return ;
}
LL query(int rt,int first,int end,int a,int b) //求区间和
{
if(first>=a&&end<=b)
return tree[rt];
push_down(rt,end-first+);
LL sum=;
int mid=(first+end)>>;
if(a<=mid)
sum+=query(lson,a,b);
if(b>mid)
sum+=query(rson,a,b);
return sum;
}
void update(int rt,int first,int end,int a,int b,int c)
{
if(first>=a&&end<=b)
{
add[rt]+=c;
tree[rt]+=(LL)c*(end-first+);
return ;
}
push_down(rt,end-first+);
int mid=(first+end)>>;
if(a<=mid)
update(lson,a,b,c);
if(b>mid)
update(rson,a,b,c);
push_up(rt);
return ;
}
int main()
{
int n,q;
char ch[];
int a,b,c;
scanf("%d%d",&n,&q);
//cin>>n>>q;
build(,,n);
while(q--)
{
scanf("%s",ch);
//cin>>ch;
if(ch[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(,,n,a,b));
//cin>>a>>b;
//cout<<query(1,1,n,a,b)<<endl;
}
if(ch[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
//cin>>a>>b>>c;
update(,,n,a,b,c);
}
}
return ;
}
poj 3468 A Simple Problem with Integers(线段树区间更新)的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #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 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- Java框架spring Boot学习笔记(三):Controller的使用
Controller注解介绍 @Controller:处理http请求 @RestController: Spirng4之后新加的注解,其实是一个组合注解等同于@ResponseBody和@Contr ...
- flask中缓存cache导入时引发的错误
安装 pip install flask-cache 初始化 from flask_cache import Cache cache = Cache(config={ 'CACHE_TYPE': 's ...
- Web用户登录---验证码的设计与实现
在开发中验证码是比较常用到有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试的方式. 验证码生成步骤:1.获得随机生成的颜色,画出随机线条生成背景底.2.String cod ...
- Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- Cpython解释器下实现并发编程
一 背景知识 二 python并发编程之多进程 三 python并发编程之多线程 四 python并发编程之协程 五 python并发编程之IO模型 六 补充:paramiko模块 七 作业 一 背景 ...
- scikit_learn,NLTK导入分类器相关流程命令
- margin-top / padding-top 的百分比 到底是多少?
之前,我以为 margin-top/padding-top 如果设置成%,得到的是 基于父对象总高度的百分比. 但是,这种想法是错的.因为在CSS标准里,是基于父对象宽度的百分比. <style ...
- scrapy项目对接Docker
1.项目根目录生成requirements.txt文件 命令pip freeze > requirements.txt 2.项目根目录新建Dockerfile文件,文件不加任何后缀名 内容如下 ...
- 周一要做的事情Sd31y5YE
问实习生活动是否有费用 调查结果更新,提醒各个pm 修改模板,把我们追加的东西体现在模板里
- Module 的语法
模块功能主要由两个命令构成:export和import.export命令用于规定模块的对外接口,import命令用于输入其他模块提供的功能. 一个模块就是一个独立的文件.该文件内部的所有变量,外部无法 ...