3468-A Simple Problem with Integers 线段树(区间增减,区间求和)
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 110077 | Accepted: 34272 | |
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
题意:n个数字,q次操作,每次可以区间增减,或者查询区间的和
#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXN 100100
#define LL long long
LL sum[MAXN<<];
LL add[MAXN<<];
int n,q;
char s[];
void putup(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void putdown(int rt,int m)
{
if (add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
sum[rt<<] += (m-(m>>))*add[rt];
sum[rt<<|] += (m>>)*add[rt];
add[rt] = ;
}
}
void build(int l,int r,int rt)
{
add[rt] = ;
if (l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
putup(rt);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if (L<=l && r<=R)
{
add[rt] += c;
sum[rt] += (LL)c*(r-l+);
return ;
}
putdown(rt,r-l+);
int m = (l+r)>>;
if (L<=m) update(lson,L,R,c);
if (R>m) update(rson,L,R,c);
putup(rt);
}
LL query(int l,int r,int rt,int L,int R)
{
if (L<=l && r<=R)
{
return sum[rt];
}
putdown(rt,r-l+);
LL ret = ;
int m = (l+r)>>;
if (L<=m) ret += query(lson,L,R);
if (R>m) ret += query(rson,L,R);
return ret;
}
int main()
{
scanf("%d%d",&n,&q);
build(,n,);
while (q--)
{
int x,y,z;
scanf("%s",s);
if (s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(,n,,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(,n,,x,y));
}
}
return ;
}
3468-A Simple Problem with Integers 线段树(区间增减,区间求和)的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 [线段树区间更新求和]
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 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- python核心编程中网络爬虫的例子
#!/usr/bin/env python import cStringIO # import formatter # from htmllib import HTMLParser # We use ...
- vue.js--基础 数据的双向绑定
所谓双向绑定:就是改变modle,就会改变view,改变view,也会改变modle 下面案例,点击getMthod(),获取msg的内容,在点击setMthod()改变msg的内容,你会发现H1的值 ...
- 线程pthread_cleanup_push的简单例程.
http://www.cnblogs.com/hnrainll/archive/2011/04/20/2022149.html #include<stdlib.h> #include< ...
- 【BZOJ1171】大sz的游戏(线段树+单调队列)
点此看题面 大致题意: 有\(n\)个点,两点间最大通讯距离为\(L\).已知除\(1\)号点外第\(i\)个点能够发出和接收的信号区间\([l_i,r_i]\)以及到\(1\)号点的距离\(dis_ ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
- PHP设计模式——装饰器模式
<?php /** * 装饰器模式 * 如果已有对象的部分内容或功能发生变化,但是不需要修改原始对象的结构,应使用装饰器模式 * * 为了在不修改对象结构的前提下对现有对象的内容或功能稍加修改, ...
- LeetCode15.三数之和 JavaScript
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- CSS 实战1
1.CSS 初始化 @charset "UTF-8"; /*css 初始化 */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3 ...
- CSS&JS定位器
一.CssSelector定位器 1.概述 CssSelector是效率很高的元素定位方法,Selenium官网的Document里极力推荐使用CSS locator,而不是XPath来定位元素,原因 ...
- 调用微信JS上传照片接口上传图片
public ActionResult UploadImge(string serverId) { var headPath = "/UploadImage/" + DateTim ...