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 ...
随机推荐
- windows安装tf
https://www.cnblogs.com/lvsling/p/8672404.html
- 4、promise
es5 中 var obj = { ajax: function (callback) { console.log('执行') setTimeout(function () { callback &a ...
- python入门(十二):面向对象
1.场景:玩过游戏.主人公,进入了一个场景,有10个小怪物是一样的.有攻击力,血(100格).如果小怪物有多个数值需要管理,小怪物的血量.小怪物出现在屏幕的地点. 可以使用字典来进行记录: {&quo ...
- Tomcat的三种安装方式:解压版、安装版、配置成Windows服务版
https://blog.csdn.net/Jessica_XLF/article/details/81711429
- C++/CLI
[C++/CLI] A C++/CLI application or component uses extensions to C++ syntax (as allowed by the C++ Sp ...
- rsync镜像命令
rsync -e 'ssh -p 19809' -av wwwroot root@3.3.3.3:/home/download/ 参数详解 编辑 -v, --verbose 详细模式输出 -q, -- ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- 20175126《Java程序设计》第五周学习总结
# 20175126 2016-2017-2 <Java程序设计>第五周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲代码并理解内容学习. - 学习内容为教材第六章,本章内 ...
- Tableau可视化绘图教程
https://www.w3cschool.cn/tableau/tableau_environment_setup.html
- robotframework 连接mysql数据库
1.安装databaselibrary.pymysql 通过cmd命令执行:pip install robotframework-databaselibrary 通过cmd命令执行:pip insta ...