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 ...
随机推荐
- VLFeat图像库在VS2012下的配置
近期做课题所需,開始使用VLFeat图像库. 库下载链接: http://download.csdn.net/detail/sunboyiris/7500097 ...
- java上传文件,下载文件
1.上传文件 1 protected int doTask(BaseForm form) throws AppException, FatalException, NoExistsException, ...
- python基础5 ---python文件处理
python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...
- rails数据验证
@user1 = :name => "zhou" 与 @user2 = :name=> "ZHOU" 在为保存之前都有可能通过第一关validate ...
- 转 Hadoop傻瓜化:Datameer大数据收入翻三番
淘薛奎发布到 <数据极客> 06-28 16:04 随着分析正在成为企业IT的核心,昔日的BI- ETL-EDW分析范型已经完全落伍,不再适用.而力推“大数据傻瓜化”的Datameer ...
- 【leetcode刷题笔记】Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- poj 1065 Wooden Sticks 【贪心 新思维】
题目地址:http://poj.org/problem?id=1065 Sample Input 3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1 ...
- 中国剩余定理的应用:猪的安家 ->福州大学 OJ
Problem 1402 猪的安家 Accept: 984 Su ...
- Codeforces 180C Letter:dp
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小 ...
- npm-install once
Once 是我最习惯的模块,它展示了几乎所有的我书写的通过issac Schlueter创建的应用. 原理很简单,Once使用各类一个函数且返回了一个函数,你可以调用这个函数,但是只能调用一次.如果你 ...