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. 这题 ...
随机推荐
- beta版和alpha版
外部测试版的意思. 软件会出现三种版本 1.alpha内部测试版本,极不稳定,一般也不会出现的公众视线,仅供内部测试人员测试用. 2.beta公共测试版,就是对外发布软件的测试版,收集公众的意见和建议 ...
- module.exports exports 和export export default
首先可以知道的是这是两组不同模块规范. module.exports 是CommonJS模块规范,通过require 导入 a.js: var x = 'hello' module.exports.x ...
- 【转】Windows 邮件槽(MailSlot)
Windows 邮件槽(MailSlot) 来自<Windows网络编程第二版 中文版> 优点:通过网络,将一条消息广播给一台或多台计算机. 缺点:只允许从客户机到服务器,建立一种不可 ...
- 转载:将画布(canvas)图像保存成本地图片的方法
之前我曾介绍过如何将HTML5画布(canvas)内容转变成图片形式,方法十分简单.但后来我发现只将canvas内容转变成图片输出还不够,如何能将转变后的图片保存到本地呢? 其实,这个方法也是非常简单 ...
- Anaconda安装和环境的搭建
Anaconda安装 在官网上下载最新的Anaconda https://www.anaconda.com/distribution/ 我使用的是2018.12,Python 3.7这个版本的. 安装 ...
- proc_info_list
内核中每种处理器架构抽象为一个proc_info_list结构体,在arch/arm/include/asm/procinfo.h中定义, struct proc_info_list { unsign ...
- POJ:2632-Crashing Robots
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Description In a modernized warehouse, robot ...
- PAT Basic 1074
1074 宇宙无敌加法器 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星 ...
- BZOJ 5313: 新Fib数列
打表找规律 #include<cstdio> using namespace std; int F[20]={0,1,1,2,3,0,3,3,1,4,0,4,4,3,2,0,2,2,4,1 ...
- UVa 11552 DP Fewest Flops
题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...