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 ...
随机推荐
- jquery中的 deferred之 when (三)
先来看使用案例: var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); var def4 = $.Def ...
- js判断访问浏览器是安卓还是ios还是微信浏览器还是微博
var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > - ...
- angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?
1,错误如图所示 简单说下错误原因是:没有js没有注册进去. 解决方法: 1.看下index.html有没有引入你的js文件. 2.看下app.js有没有注册js,比如我这次就是这步没做好,合并代码时 ...
- gradle用户目录本地库移动设置
gradle被越来越多的程序开发人员使用来构件项目代码,使用gradle依赖的第三方jar包有时候非常占空间,默认这样的用户本地库目录(缓存目录)在系统盘上,我们可以修改用户目录到其它盘上 工具/原料 ...
- mybatis入门知识
概述 MyBatis 是一个优秀的基于 Java 的持久层框架,它内部封装了 JDBC,使开发者只需关注 SQL 语句本身,而不用再花费精力去处理诸如注册驱动.创建 Connection.配置 Sta ...
- day48 前端高级选择器优先级
复习 1. 基础选择器 标签选择器(div) | 类选择器(.div1) | id选择器(#div2) <div class="div1" id="div2&quo ...
- 2019-04(1)(Python学习)
9.1 迭代器 创建迭代器的3种方法: 方法一: 容器对象添加 __iter__() 和 __next__() 方法(Python 2.7 中是 next()):__iter__() 返回迭代器对象本 ...
- 【转】JRXlsExporter.setParameter常用参数说明
net.sf.jasperreports.engine.JRExporterParameter. JASPER_PRINT 这个参数是net.sf.jasperreports.engine.Jaspe ...
- Beyond Compare 4 提示错误“这个授权密钥已被吊销”的解决办法
错误提示: 这个授权密钥已被吊销. 解决方法: 删除以下目录中的所有文件即可. C:\Users\Administrator\AppData\Roaming\Scooter Software\Beyo ...
- JDK、CGlib动态代理详解
Java动态代理之JDK实现和CGlib实现(简单易懂) 一 JDK和CGLIB动态代理原理 1.JDK动态代理 利用拦截器(拦截器必须实现InvocationHanlder)加上反射机制生 ...