POJ3468(线段树区间维护)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 85502 | Accepted: 26556 | |
| 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 线段树入门题。
#include <cstdio>
using namespace std;
const int MAXN = ;
typedef long long LL;
struct Node{
int l, r;
LL sum, lazy;
}a[MAXN*];
int n, m;
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
a[rt].lazy = ;
if(l == r)
{
scanf("%I64d", &a[rt].sum);
return ;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build((rt << ) | , mid + , r);
a[rt].sum = a[rt<<].sum + a[(rt<<)|].sum;
}
void pushDown(int rt)
{
int mid = (a[rt].l + a[rt].r) >> ;
a[rt<<].sum += a[rt].lazy * (mid - a[rt].l + );
a[(rt<<)|].sum += a[rt].lazy * (a[rt].r - mid);
a[rt<<].lazy += a[rt].lazy;
a[(rt<<)|].lazy += a[rt].lazy;
a[rt].lazy = ;
}
void update(int rt, int l, int r, int val)
{
if(a[rt].l == l && a[rt].r == r)
{
a[rt].sum += (LL)val * (r - l + );
a[rt].lazy += (LL)val;
return ;
}
if(a[rt].lazy != )
{
pushDown(rt);
}
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
update(rt << , l, r, val);
}
else if(mid < l)
{
update((rt << ) | , l, r, val);
}
else
{
update(rt << , l, mid, val);
update((rt << ) | , mid + , r, val);
}
a[rt].sum = a[rt<<].sum + a[(rt<<)|].sum;
}
LL query(int rt, int l, int r)
{
if(a[rt].l == l && a[rt].r == r)
{
return a[rt].sum;
}
if(a[rt].lazy != )
{
pushDown(rt);
}
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
return query(rt << , l, r);
}
else if(mid < l)
{
return query((rt << ) | , l, r);
}
else
{
return query(rt << , l, mid) + query((rt << ) | , mid + , r);
}
}
int main()
{
while(scanf("%d %d",&n, &m) != EOF)
{
build(, , n);
while(m--)
{
scanf("%*c");
char op;
scanf("%c", &op);
if(op == 'Q')
{
int l, r;
scanf("%d %d", &l, &r);
printf("%I64d\n", query(, l, r));
}
else
{
int l, r, val;
scanf("%d %d %d", &l, &r ,&val);
update(, l, r, val);
}
}
}
return ;
}
POJ3468(线段树区间维护)的更多相关文章
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
- HDU 6315 Naive Operations(线段树+区间维护)多校题解
题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...
- POJ3468(线段树 区间修改 lazy-tag)
我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 线段树(区间维护):HDU 3308 LCIS
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
随机推荐
- ElasticSearch(十七)初识倒排索引
现在有两条document: doc1:I really liked my small dogs, and I think my mom also liked them. doc2:He never ...
- 洛谷 P1558 色板游戏
洛谷 题解里面好像都是压位什么的, 身为蒟蒻的我真的不会, 所以就来谈谈我的30颗线段树蠢方法吧! 这题初看没有头绪. 然后发现颜色范围好像只有30: 所以,我就想到一种\(sao\)操作,搞30颗线 ...
- 我的Android进阶之旅------>如何获取Android控件的宽和高
本文转载于:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我 ...
- activiti基础--2----------------------(流程定义)
Deployment 部署对象 1.一次部署的多个文件信息,对于不需要的流程可以删除和修改 2.对应的表 act_re_deployment #部署对象表 act_re_procdef #流程定义表 ...
- 关于ionic开发中遇到的坑与总结
这次是第二次使用ionic开发混合app,今天算是对这个框架做一个总结,基础的我就不再重复了,网上都有教程.我就说说自己的心得和遇见的各种坑, 之后会陆续补充,想到什么说什么吧. 1.关于ionic效 ...
- 牛客小白月赛1 C 分元宵【快速幂】
题目链接 https://www.nowcoder.com/acm/contest/85/C 思路 有 A 种 元宵馅,B 种元宵皮 所以 我们可以认为 有Q = A * B 种 元宵 有 C 张桌子 ...
- 加深Java基础,做了20道题选择题!简答题没做
2015-03-16 17:13 269人阅读 评论(1) 收藏 举报 分类: 笔试(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. 1,下列说法正确的是( A ) A )Jav ...
- bzoj 1008: [HNOI2008]越狱 数学
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 监狱有连 ...
- javascript作用域与闭包
Javasript作用域概要 在javascript中,作用域是执行代码的上下文,作用域有三种类型: 1) 全局作用域 2) 局部作用域(函数作用域) 3) eval作用域 var foo = ...
- DELPHI 动态 创建和释放 多个 EDIT 控件
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...