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

The sums may exceed the range of 32-bit integers.
 
这是对区间所有点增固定值类的线段树。
 
代码:
#include <iostream>
#include <cstdio>
#define LL long long using namespace std; int n, q; //线段树
const int maxn = 100000;
struct node
{
int lt, rt;
LL val, add;
}tree[4*maxn]; //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = 0;//每段的初值,根据题目要求
tree[id].add = 0;
if (lt == rt)
{
scanf("%I64d", &tree[id].val);
//tree[id].add = ??;
return;
}
int mid = (lt + rt) >> 1;
Build(lt, mid, id << 1);
Build(mid + 1, rt, id << 1 | 1);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} void PushDown(int id, int pls)
{
tree[id<<1].add += tree[id].add;
//tree[id<<1].val += (pls-(pls>>1))*tree[id].add;
tree[id<<1].val += (tree[id<<1].rt-tree[id<<1].lt+1)*tree[id].add;
tree[id<<1|1].add += tree[id].add;
//tree[id<<1|1].val += (pls>>1)*tree[id].add;
tree[id<<1|1].val += (tree[id<<1|1].rt-tree[id<<1|1].lt+1)*tree[id].add;
tree[id].add = 0;
} //增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
tree[id].add += pls;
tree[id].val += pls * (tree[id].rt-tree[id].lt+1);
return;
}
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
if (lt <= mid)
Add(lt, rt, id<<1, pls);
if (rt > mid)
Add(lt, rt, id<<1|1, pls);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} LL Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
LL ans = 0;
if (lt <= mid)
ans += Query(lt, rt, id<<1);
if (rt > mid)
ans += Query(lt, rt, id<<1|1);
return ans; } int main()
{
//freopen("in.txt", "r", stdin);
char op;
int a, b, k;
while (scanf("%d%d", &n, &q) != EOF)
{
Build(1, n, 1);
for (int i = 0; i < q; ++i)
{
getchar();
op = getchar();
getchar();
scanf("%d%d", &a, &b);
if (op == 'Q')
printf("%I64d\n", Query(a, b, 1));
else
{
scanf("%d", &k);
Add(a, b, 1, k);
}
}
}
return 0;
}

ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  3. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  4. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. [poj3468]A Simple Problem with Integers_线段树

    A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...

  7. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. 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 ...

  9. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

随机推荐

  1. java 开发环境安装

    一.在mac上安装jdk 1. 下载Mac版本的JDK并安装      http://www.oracle.com/technetwork/java/javase/downloads/index.ht ...

  2. 跟我学AngularJs:Controller数据共享、继承、通信使用具体解释

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主讲了AngularJs中的Controller中数据共享.继承.通信的具体使用 本 ...

  3. DataView中的 Sort 排序

    using System.Data; using System; public class A { static void Main(string[] args) { DataTable locati ...

  4. java自定义before和after

    package com.ada.wuliu.worker.web.cooperation.worker; public class TestOne { abstract class Father{ p ...

  5. linearLayout 和 relativeLayout的属性区别

    LinearLayout和RelativeLayout 共有属性: java代码中通过btn1关联次控件 android:id="@+id/btn1" 控件宽度 android:l ...

  6. 开源大数据引擎:Greenplum 数据库架构分析

    Greenplum 数据库是最先进的分布式开源数据库技术,主要用来处理大规模的数据分析任务,包括数据仓库.商务智能(OLAP)和数据挖掘等.自2015年10月正式开源以来,受到国内外业内人士的广泛关注 ...

  7. 《Programming WPF》翻译 第4章 5.主从复合(Master-Detail)绑定

    我们已经看到绑定一个单独的对象,还看到绑定一个单独的对象列表.另一种非常流行的方式是绑定多个对象列表,尤其是相关的列表.例如,如果你向用户显示一个客户列表,当他们选中其中一个客户,就会显示客户的相关订 ...

  8. ASP.NET动态网站制作(12)-- JQ(4)

    前言:这节课接着上次课的继续讲. 内容:接上--> 1.jq元素样式设置:  (4)某个元素中是否含有某个css类别,返回布尔型:$("li:last").hasClass( ...

  9. iOS 逆向 - Class-dump 安装和使用方法

    1.下载安装包 http://stevenygard.com/projects/class-dump/,这里我下载的是 class-dump-3.5.dmp.然后把下载下来的 dmg 打开,复制文件里 ...

  10. 【BZOJ3319】黑白树 并查集

    [BZOJ3319]黑白树 Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作:1.查询u到根路径上的第一条黑色边的标号.2.将u到v    路径上的所有边的颜色设为 ...