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. 这题 ...
随机推荐
- 解决TS报错Property 'style' does not exist on type 'Element'
在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...
- Mysql 8.0 新特性
转载:https://www.jianshu.com/p/be29467c2b0c
- css实现页面文字不换行、自动换行、强制换行
强制不换行 div{ white-space:nowrap; } 自动换行 div{ word-wrap: break-word; word-break: normal; } 强制英文单词断行 div ...
- NSString 使用 copy、strong
// 首先定义2个属性 @property (nonatomic, strong) NSString *stStr; @property (nonatomic, copy) NSString *coS ...
- React入门教程(二)
前言 距离上次我写 React 入门教程已经快2个月了,年头年尾总是比较忙哈,在React 入门教程(一)我大概介绍了 React 的使用和一些注意事项,这次让我们来继续学习 React 一. Rea ...
- Codevs1033 蚯蚓的游戏
题目描述 Description 在一块梯形田地上,一群蚯蚓在做收集食物游戏.蚯蚓们把梯形田地上的食物堆积整理如下: a(1,1) a(1,2)…a(1,m) a(2,1) a(2,2) a(2 ...
- Verilog之语句位置
1.if语句.case语句必须放在always过程语句块中. 2.verilog的系统函数比如:\(display/\)monitor必须放在initial 过程语句块中.这点尚为理解为何,但必须这样 ...
- loc与iloc函数的使用
Pandas中loc和iloc函数用法详解(源码+实例) https://blog.csdn.net/w_weiying/article/details/81411257 Pandas中loc,il ...
- Applied Nonparametric Statistics-lec1
参考网址: https://onlinecourses.science.psu.edu/stat464/node/2 Binomial Distribution Normal Distribution ...
- POJ:3160-Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...