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课程之团队开发冲刺1.4
一.总结昨天进度 1.昨天任务全部完成 二.遇到的问题 1.对数据库的使用陌生 2.使用sqlite有些困难 3.对如何解决查询课程问题暂时没有找到好的解决方案 三.今日任务 1.由于周一的课程比较紧 ...
- std::remove_reference
[std::remove_reference] 用于移除类型的引用,返回原始类型. 1.可能的实现. 2.例子. #include <iostream> // std::cout #inc ...
- PhoenixFD插件流体模拟——UI布局【Foam】详解
流体泡沫 本文主要讲解Foam折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Foam 主要内容 Overview 综述 ...
- jmeter连接oracle数据库配置
1导入加载ojdbc.jar包(2种方法) 1)直接拷贝目录 2.
- Java并发编程之同步
1.synchronized 关键字 synchronized 锁什么?锁对象. 可能锁对象包括: this, 临界资源对象,Class 类对象. 1.1 同步方法 synchronized T me ...
- bhttpd
以前产品应用是用串口做控制台,写了一个带简单命令历史和命令补全功能的控制台Shell,用作程序的调试,包括查看系统状态和调试修改设定等等.确实非常好用,对很多现场简单问题的快速定位起到了很好的作用.系 ...
- 解决Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
1.在eclipse的workspace里面找到该项目.settings文件夹 2.编辑org.eclipse.wst.common.project.facet.core.xml文件 <?xml ...
- Subplot 分格显示
1.subplot2grid 使用import导入matplotlib.pyplot模块, 并简写成plt. 使用plt.figure()创建一个图像窗口 import matplotlib.pypl ...
- 自适应Web主页
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- NumPy学习_01 ndarray相关概念
1.NumPy库 NumPy = Numerical Python 是高性能科学计算和数据分析的基础库. pandas库充分借鉴了NumPy的相关概念,先行掌握NumPy库的用法,才能把pandas的 ...