poj 3468 A Simple Problem with Integers 线段树加延迟标记
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
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
ll l,r;
ll num;
ll lazy;
}tree[*];
void build_tree(ll l,ll r,ll pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].lazy=;
if(l==r)
{
//tree[pos].num=1;
scanf("%lld",&tree[pos].num);
return;
}
ll mid=(l+r)/;
build_tree(l,mid,pos*);
build_tree(mid+,r,pos*+);
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
void update(ll l,ll r,ll change,ll pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
tree[pos].lazy+=change;
tree[pos].num+=(tree[pos].r-tree[pos].l+)*change;
return;
}
if(tree[pos].lazy)
{
tree[pos*].num+=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
tree[pos*+].num+=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
tree[pos*].lazy+=tree[pos].lazy;
tree[pos*+].lazy+=tree[pos].lazy;
tree[pos].lazy=;
}
ll mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
update(l,r,change,pos*);
else if(l>mid)
update(l,r,change,pos*+);
else
{
update(l,mid,change,pos*);
update(mid+,r,change,pos*+);
}
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
ll query(ll l,ll r,ll pos)
{
//cout<<l<<" "<<r<<" "<<pos<<endl;
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].num;
if(tree[pos].lazy)
{
tree[pos*].num+=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
tree[pos*+].num+=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
tree[pos*].lazy+=tree[pos].lazy;
tree[pos*+].lazy+=tree[pos].lazy;
tree[pos].lazy=;
}
ll mid=(tree[pos].l+tree[pos].r)/;
if(l>mid)
return query(l,r,pos*+);
else if(r<=mid)
return query(l,r,pos*);
else
return query(l,mid,pos*)+query(mid+,r,pos*+);
}
int main()
{
ll x,q,i,t;
while(~scanf("%lld",&x))
{
scanf("%lld",&q);
build_tree(,x,);
while(q--)
{
char a[];
ll l,r;
ll change;
scanf("%s%lld%lld",a,&l,&r);
if(a[]=='C')
{
scanf("%lld",&change);
update(l,r,change,);
}
else
printf("%lld\n",query(l,r,));
}
}
return ;
}
poj 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 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 (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- 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 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- 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: 67511 ...
随机推荐
- 循环结构 for
for格式:for(初始化表达式;循环条件表达式;循环后的操作表达式) { 执行语句:循环体 } ------------------------------------ -------------- ...
- IP追踪
cmd里输入:tracert www.baidu.com 上图箭头方框中就是对应公司的总网IP
- [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode] 258. Add Digits_Easy tag: Math
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- testNG入门详解
TestNG 的注释: @DataProvider @ExpectedExceptions @Factory @Test @Parameters <suite name="Parame ...
- linux常用命令:crontab 命令
前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...
- 【百度统计】设置页面元素点击事件转化pv、uv
html元素点击事件内添加代码:_hmt.push(['_trackEvent', category, action, opt_label, opt_value]); 1. '_trackEvent' ...
- python进程join()函数理解
Join()是主程序等我这个进程执行完毕了,程序才往下走
- 前端路由以及浏览器回退,hash & history & location
一.前言 其实不止一次想监听浏览器的回退方法,比如 在 list.html 页滚动加载了几页列表,点到 detail.html 看详情,反回来时又得重新加载几页 H5 有背景音乐的,跳页就得重新放,体 ...
- Python之CMDB资产管理系统
最近正好在给公司做CMDB资产管理系统,现在做的也差不多了,现在回头吧思路整理下. CMDB介绍 CMDB --Configuration Management Database 配置管理数据库, C ...