题目描述 Description

给你N个数,有两种操作:

1:给区间[a,b]的所有数增加X

2:询问区间[a,b]的数的和。

输入描述 Input Description

第一行一个正整数n,接下来n行n个整数,

再接下来一个正整数Q,每行表示操作的个数,

如果第一个数是1,后接3个正整数,

表示在区间[a,b]内每个数增加X,如果是2,

表示操作2询问区间[a,b]的和是多少。

pascal选手请不要使用readln读入

输出描述 Output Description

对于每个询问输出一行一个答案

样例输入 Sample Input

3

1

2

3

2

1 2 3 2

2 2 3

样例输出 Sample Output

9

数据范围及提示 Data Size & Hint

数据范围

1<=n<=200000

1<=q<=200000

思路:线段树+延迟标记

#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
{
int l,r;
ll num;
int lazy;
}tree[*];
void build_tree(int l,int r,int 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;
}
int mid=(l+r)/;
build_tree(l,mid,pos*);
build_tree(mid+,r,pos*+);
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
void update(int l,int r,int change,int 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=;
}
int 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(int l,int r,int 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=;
}
int 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()
{
int x,q,i,t;
while(~scanf("%d",&x))
{
build_tree(,x,);
scanf("%d",&q);
while(q--)
{
int flag,change,l,r;
scanf("%d%d%d",&flag,&l,&r);
if(flag==)
{
scanf("%d",&change);
update(l,r,change,);
}
else
printf("%lld\n",query(l,r,));
}
}
return ;
}

codevs 1082 线段树练习 3 区间更新+延迟标记的更多相关文章

  1. codevs 1081 线段树练习 2 区间更新 单点查询 无lazy

    题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n ...

  2. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  3. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  4. codevs 1082 线段树练习3

    1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 给你N个数,有两种操作: 1: ...

  5. Codevs 1082 线段树练习 3

    1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Maste 传送门 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的 ...

  6. 【树状数组区间修改区间求和】codevs 1082 线段树练习 3

    http://codevs.cn/problem/1082/ [AC] #include<bits/stdc++.h> using namespace std; typedef long ...

  7. codevs 1082 线段树练习3 (线段树)

    题目: 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数 ...

  8. HDU4893【线段树单点、区间更新】

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=4893] 题意:输入n.q.表示有n个数,初始化默认这n个数都为零,有q次操作,操作种类分为三种:1.输 ...

  9. codevs 1082 线段树练习 3 --分块练习

    时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[ ...

随机推荐

  1. 人活着系列之开会(Floy)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2930 题意:所有点到Z点的最短距离.因为岛名由 ...

  2. Ng线性回归实现学习[转载]

    转自:https://github.com/huanting74/Coursera-ML-AndrewNg 1.可视化数据 import pandas as pd import seaborn as ...

  3. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  4. iOS 设计模式-委托模式

    委托是指给一个对象提供机会对另一对象中的变化做出反应或者相应另一个对象的行为.其基本思想是协同解决问题. Delegate的使用场合 对象A内部发生了一些事情,想通知对象B 对象B想监听对象A内部发生 ...

  5. BinarySearch

    今天看代码,看到这么一段,开始没有看明白,记录下来备忘 foreach (FinancialReport r3 in addAorList)            {                i ...

  6. xhtml 的三种 doctype

    {1}文档宣告 <!ODCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 [there]//EN" "http://www.w4org/TR/xhtml1/DTD/ ...

  7. transform 和 transition

    transform的属性包括:rotate() / skew() / scale() / translate() /matrix() 其中 rotate() 旋转度数,0-360 skew()  元素 ...

  8. 定位性能问题的18个linux命令

    1.TopTop命令是一个性能监控程序,它按一定的顺序显示所有正在运行而且处于活动状态的实时进程,而且会定期更新显示结果.这条命令显示了CPU的使用率.内存使用率.交换内存使用大小.高速缓存使用大小. ...

  9. Hive sql和Presto sql的一些对比

    最近由于工作上和生活上的一些事儿好久没来博客园了,但是写博客的习惯还是得坚持,新的一年需要更加努力,困知勉行,终身学习,每天都保持空杯心态.废话不说,写一些最近使用到的Presto SQL和Hive ...

  10. 数据仓库原理<4>:联机分析处理(OLAP)

    本文转载自:http://www.cnblogs.com/hbsygfz/p/4762085.html 1. 引言 本篇主要介绍数据仓库中的一项重要分析技术——联系分析处理(OLAP). 在第一篇笔者 ...