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 ...
随机推荐
- django 中的render和render_to_response()和locals()
1. django中的render context在Django里表现为 Context 类,在 django.template 模块里. 它的构造函数带有一个可选的参数: 一个字典映射变量和它们的值 ...
- poj3468A Simple Problem with Integers(线段树的区域更新)
http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...
- 使用Linux工作之Fedora KDE
小明拿着在Windows下不断蓝屏的T440和公司建议不使用云笔记的规定,心下想着,是时候回归linux了... 一.系统的获取与启动盘的制作 fedora20 KDE版 liveusb-creato ...
- 何为仿射变换(Affine Transformation)
http://www.cnblogs.com/ghj1976/p/5199086.html 变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可 ...
- mysql系统变量查询
mysql系统变量包括全局变量(global)和会话变量(session),global变量对所有session生效,session变量包括global变量.mysql调优必然会涉及这些系统变量的调整 ...
- ubuntu14.04无法安装Curl,需要先升级sudo apt-get update
ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...
- [转载]LinkButton跳转页面及传递参数
在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...
- pyDay6
内容来自廖雪峰的官方网站 1.在Python中,代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好,1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 2.取指 ...
- 简单的HTML5 canvas游戏工作原理
HTML5已经不是一个新名词.它看上去很cool,有很多feature,大多数人普遍看好它的发展.对于我来说,最感兴趣的是它的canvas标签,可以结合Javascript来绘制游戏画面. 我们可以在 ...
- Redis 如何正确实现分布式锁
前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...