poj 3468 线段树成段更新
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 54012 | Accepted: 16223 | |
Case Time Limit: 2000MS |
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
Hint
#include<iostream>
#include<cstdio>
using namespace std; #define lson left,mid,i<<1
#define rson mid+1,right,i<<1|1 typedef __int64 LL;
int a[]; struct IntervalTree
{
int left,right;
LL sum,add;
}f[]; void pushdown(int i)
{
if(f[i].add != )
{
f[i<<].add+=f[i].add;
f[i<<|].add+=f[i].add;
f[i<<].sum+=(f[i<<].right-f[i<<].left+)*f[i].add;
f[i<<|].sum+=(f[i<<|].right-f[i<<|].left+)*f[i].add;
f[i].add=;
}
} void pushup(int i)
{
f[i].sum=f[i<<].sum+f[i<<|].sum;
} void bulid(int left,int right,int i)
{
f[i].left=left,f[i].right=right,f[i].add=;
if(left==right)
{
f[i].sum=a[left];return ;
}
int mid=(left+right)>>;
bulid(lson);
bulid(rson);
pushup(i);
return ;
} void update(int left,int right,int add,int i)
{
if(f[i].left==left && f[i].right==right)
{
f[i].add+=add;
f[i].sum+=(right-left+)*add;
return ;
}
pushdown(i);
if(f[i<<].right>=right) update(left,right,add,i<<);
else if(f[i<<|].left<=left) update(left,right,add,i<<|);
else { update(left,f[i<<].right,add,i<<);update(f[i<<|].left,right,add,i<<|);}
pushup(i);
return ;
} LL query(int left,int right,int i)
{
if(f[i].left==left && f[i].right==right) return f[i].sum;
pushdown(i);
if(f[i<<].right>=right) return query(left,right,i<<);
else if (f[i<<|].left<=left) return query(left,right,i<<|);
else return query(left,f[i<<].right,i<<)+query(f[i<<|].left,right,i<<|);
} int main()
{
int i,n,m,ai,bi,d;
char ch[];
while(~scanf("%d %d",&n,&m))
{
for(i=;i<=n;i++) scanf("%d",a+i);
bulid(,n,);
while(m--)
{
scanf("%s",ch);
if(strcmp(ch,"C")==)
{
scanf("%d %d %d",&ai,&bi,&d);
update(ai,bi,d,);
}
else
{
scanf("%d %d",&ai,&bi);
printf("%I64d\n",query(ai,bi,));
}
}
}
return ;
}
poj 3468 线段树成段更新的更多相关文章
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
随机推荐
- 《队长说得队》【Alpha】Scrum meeting 4
项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 ...
- ovx openVirtex安装
搞了好久的pox实验,中途一个星期没更新了吧, 今天继续... 新手第一次搞还是在虚拟机上最安全~ ovx参照上面的做吧,注意必须在联网的情况下,否则很多控件都连不上(第一次做的时候虚拟机没配ip 坑 ...
- C#数组协方差
对于任意两个“引用类型”A和B,如果存在从A到B的隐式引用转换或显式引用转换,则也一定存在从数组类型A[R]到数组类型B[R]的相同的引用转换,其中R可以是任何给定的“秩说明符”,但是这两个数组类型必 ...
- 采用maven 对tomcat 进行自动部署
在工作过程中经常会遇到项目频繁发不到额过程,而且在这个过程中会一直进行一些简单但是繁琐的重复性工程 1.打war 包 2.停掉tomcat 3.copy war 包 4.启动tomcat 听说mave ...
- ES6 -- 模板字符串(反单引号)
1)直接使用变量 // before var str = 'test'; console.log(str + "123"); // now var str = 'test'; co ...
- (71)Received empty response from Zabbix Agent问题解决
刚接触zabbix新手少部分会出现如下错误: Received empty response from Zabbix Agent at [192.168.1.2]. Assuming that age ...
- logback写日志
https://blog.csdn.net/u010128608/article/details/76618263 https://blog.csdn.net/zhuyucheng123/articl ...
- 解析Vue.js中的computed工作原理
我们通过实现一个简单版的和Vue中computed具有相同功能的函数来了解computed是如何工作的.写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指 ...
- Python 基本数据类型 (二) - 字符串1
# ----------- 首字母大写 ---------- test = "alex is a man" v = test.capitalize() print(v): Alex ...
- SQL_1_简介
了解一门语言,还是应该从名字开始.SQL中的S即Structured(结构),L即Language(语言),Q即Query(查询),但不仅仅只是查询,还可以建立数据库,添加和删除数据,对数据作联合,当 ...