poj3468A 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
在段更新和查找时要注意,当父节点的下方和子节点的下方同时须要更新时不能直接覆盖了子节点的num,而是要加起来。
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 100100
struct Tree
{
__int64 sum,num;//分别表示当前段的总和,子点的每个点所要加的值
bool b;//判断子节点是否要更新
}tree[4*N];
__int64 init[N+5];//初始时每个点的值
void builde(int l,int r,int k)
{
int m=(l+r)/2;
tree[k].b=false; tree[k].num=0;
if(l==r)
{
tree[k].sum=init[r]; return ;
}
builde(l,m,k*2);
builde(m+1,r,k*2+1);
tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
}
void set_child(int l,int r,int k)
{
int m=(l+r)/2;
if(tree[k*2].b==true)//注意这里,不能直接覆盖了
tree[k*2].num+=tree[k].num;
else
tree[k*2].num=tree[k].num;
tree[k*2].sum+=(m-l+1)*tree[k].num; if(tree[k*2+1].b==true)
tree[k*2+1].num+=tree[k].num;
else
tree[k*2+1].num=tree[k].num;
tree[k*2+1].sum+=(r-m)*tree[k].num;
tree[k*2].b=tree[k*2+1].b=true;
}
void updata(int l,int r,int k,int L,int R,__int64 num)
{
int m=(l+r)/2;
if(L<=l&&r<=R)
{
if(tree[k].b==true)
tree[k].num+=num;
else
tree[k].num=num;
tree[k].sum+=(r-l+1)*num;
tree[k].b=true;
return ;
}
if(tree[k].b==true)
set_child(l,r,k);
if(L<=m) updata(l,m,k*2,L,R,num);
if(R>m) updata(m+1,r,k*2+1,L,R,num);
tree[k].b=false;
tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
}
__int64 SUM;
void calculate(int l,int r,int k,int L,int R)
{
int m=(l+r)/2;
if(L<=l&&r<=R)
{
SUM+=tree[k].sum; return ;
}
if(tree[k].b==true)
set_child(l,r,k);
if(L<=m) calculate(l,m,k*2,L,R);
if(R>m) calculate(m+1,r,k*2+1,L,R);
tree[k].b=false;
}
int main()
{
int n,m,L,R,i;
__int64 num;
char ch[2];
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%I64d",&init[i]);
getchar();
builde(1,n,1);
while(m--)
{
scanf("%s%d%d",ch,&L,&R);
if(ch[0]=='Q')
{
SUM=0; calculate(1,n,1,L,R);
printf("%I64d\n",SUM);
}
else
{
scanf("%I64d",&num);
updata(1,n,1,L,R,num);
}
}
}
poj3468A Simple Problem with Integers(线段树,在段更新时要注意)的更多相关文章
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
随机推荐
- DESCryptoServiceProvider加密、解密
.net名称空间System.Security.Cryptography下DESCryptoServiceProvider类为我们提供了加密和解密方法,我们只需少许代码便可实现加密和解密. 稍感不托的 ...
- 15个必须知道的chrome开发者技巧(转)
15个必须知道的chrome开发者技巧 在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟 ...
- AIX 第2章 指令记录
root@db:/#mount node mounted mounted over vfs date options ------- ...
- mysql now() sysdate() curdate()区别
//mysql中now,curdate,curtime,sysdate区别 1.now 返回的时间,格式如下:2013-01-17 10:57:13 mysql> select now(),sl ...
- linux下ubuntu系统安装及开发环境配置
1.安装系统:别的没什么说的,就是安的时候把网线拔了,不然到 configure apt的时候会卡起很久不走的2.配置网络 编辑/etc/network/interface打开/etc/networt ...
- redmine 2.5.2 安装后邮件无法发送
找到redmine安装目录-apps->redmine->htdocs->conflg->configuration.yml email_delivery: delive ...
- [Papers]MHD, $\p_3\pi$, Lebesgue space [Zhang-Li-Yu, JMAA, 2013]
$$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{3}{2}\leq q\leq 3 ...
- js闭包用法
闭包 既保证了 内部函数的私有性 又可以向外公开 通过一个已有对象 向它注入属性 /** * 闭包 * 在函数中定义的函数,在外部使用 * 1.在函数内部定义的函数,在外部不能访问 */ functi ...
- Spring配置数据库固定代码
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > &l ...
- PHP.ini 配置文件解析
[PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini ;;;;;;;;;;;;;;;;;;;;; PHP's initialization file, generall ...