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 ...
随机推荐
- 08day2
引爆炸弹 贪心 [问题描述] 有 n 个炸弹,有些炸弹牵了一根单向引线(也就是说引线只有在这一端能被炸弹点燃),只要引爆了这个炸弹,用引线连接的下一个炸弹也会爆炸.每个炸弹还有个得分,当这个炸弹被引爆 ...
- 【英语】Bingo口语笔记(48) - 关于春节的表达
- 部署在IIS服务器的asp.net 网站,禁止访问指定类型文件
网站上的一些文件不希望用户访问,可以通过下面的方式简单实现.不需写代码(在IIS6下试验过). 第一步,在IIS中实现映射. 哪些文件需要特殊处理. 通俗的将就是将哪种类型的文件交给特定的工厂来处理. ...
- 查询mysql数据库表的信息(表大小、数据大小、索引大小)
select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA='databasename' ...
- mysql 错误代码汇总
1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除 ...
- java监测方法运行时间/效率方法
前言: 这周在写一个小项目,虽然小但是是纯调外部接口的,调完了接口还不停的循环接口返回的数据(已转换JSONArray),然后再判断值,再做不同处理,关键是数据量还比较大,这刚做完还没开始上线,测试也 ...
- s:iterator标签的使用
1.在说明s:iterator标签的使用前,先了解下struts2中的Value Stack. 这里参考了webwork中对Value Stack的描述,由于struts2是在webwork的基础上进 ...
- js实现密码加密
http://www.cnblogs.com/mofish/archive/2012/02/25/2367858.html 1.base64加密 在页面中引入base64.js文件,调用方法为: &l ...
- .net core学习
http://www.cnblogs.com/artech/ http://www.blogs8.cn/posts/AA0E630 http://pan.baidu.com/s/1bo4fJ47 ht ...
- [转]linux之date命令
转自:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用 ...