poj3511--A Simple Problem with Integers(线段树求和)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 60441 | Accepted: 18421 | |
| 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
将一段的值添加c 求一段的和
将线段树的每一段表示它代表的那一段的和。统计结果时,要记录一段的全部的父节点的和。对该段会有影响
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 100000
#define LL __int64
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now LL l,LL r,LL rt
LL cl[maxn<<2] , lazy[maxn<<2];
void push_up(int_now)
{
cl[rt] = cl[rt<<1] + cl[rt<<1|1] + (r-l+1)*lazy[rt] ;
}
void push_down(int_now)
{ }
void creat(int_now)
{
cl[rt] = lazy[rt] = 0 ;
if(l != r)
{
creat(lson);
creat(rson);
push_up(now);
}
else
scanf("%I64d", &cl[rt]);
}
void update(LL ll,LL rr,LL x,int_now)
{
if( ll > r || rr < l )
return ;
if( ll <= l && r <= rr )
{
lazy[rt] += x ;
cl[rt] += (r-l+1)*x ;
return ;
}
update(ll,rr,x,lson);
update(ll,rr,x,rson);
push_up(now);
}
LL query(LL ll,LL rr,int_now,LL add)
{
if( ll > r || rr < l )
return 0;
if( ll <= l && r <= rr )
return cl[rt] + ( r-l+1 )*add ;
push_down(now);
return query(ll,rr,lson,add+lazy[rt]) + query(ll,rr,rson,add+lazy[rt]) ;
}
int main()
{
LL i , j , x , n , m ;
char str[10] ;
while(scanf("%I64d %I64d", &n, &m) !=EOF)
{
creat(root);
while(m--)
{
scanf("%s", str);
if(str[0] == 'C')
{
scanf("%I64d %I64d %I64d", &i, &j, &x);
update(i,j,x,root);
}
else
{
scanf("%I64d %I64d", &i, &j);
printf("%I64d\n", query(i,j,root,0));
}
}
}
return 0;
}
poj3511--A Simple Problem with Integers(线段树求和)的更多相关文章
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
随机推荐
- Vuex的全面用法总结
1. vuex简介 vuex是专门用来管理vue.js应用程序中状态的一个插件.他的作用是将应用中的所有状态都放在一起,集中式来管理.需要声明的是,这里所说的状态指的是vue组件中data里面的属性. ...
- 获取windows版本号
原文:https://blog.csdn.net/justFWD/article/details/44856277 内容整理如下,点击跳至指定内容: manifest文件加上compatibility ...
- java正则表达式的进阶使用20180912
package org.jimmy.autosearch20180821.test; import java.util.regex.Matcher; import java.util.regex.Pa ...
- Django中使用多线程发送邮件
1.settings.py 增加Email设置 #mail EMAIL_HOST = ‘smtp.gmail.com’ #邮件smtp服务器 EMAIL_POR ...
- vue开发 - 根据vue-router的meta动态设置html里标签的内容
路由文件 :router/index.js import Vue from 'vue'import Router from 'vue-router'import index '@/view/index ...
- 瀑布流布局js
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)
出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...
- centos passwo文件被删除
错误提示 该问题一般由/etc/passwd被清空,删除,移动,改名等造成,需要通过救援模式恢复,操作步骤如下 真实环境已经解决,这里使用vmware模拟.光盘启动,选择救援模式: 语言选择,键盘布局 ...
- 配置python3 项目环境
安装python3 安装仓库软件 sudo apt-get install software-properties-common python-software-properties 添加仓库 sud ...
- 第十七节:Scrapy爬虫框架之item.py文件以及spider中使用item
Scrapy原理图: item位于原理图的最左边 item.py文件是报存爬取数据的容器,他使用的方法和字典很相似,但是相比字典item多了额外的保护机制,可以避免拼写错误或者定义错误. 1.创建it ...