A Simple Problem with Integers

Time Limit: 5000MS  Memory Limit: 131072K
Total Submissions: 108903   Accepted: 33919
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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.

Source

 
带lazy的线段树
 //2017-05-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((st[id].l+st[id].r)>>1)
#define lson (id<<1)
#define rson ((id<<1)|1) using namespace std; const ll N = ;
ll arr[N];
struct Node{
ll l, r, sum, lazy;
}st[N<<]; void build(int id, int l, int r)
{
st[id].l = l; st[id].r = r; st[id].lazy = ;
if(l == r){
st[id].sum = arr[l];
return;
}
build(lson, l, mid);
build(rson, mid+, r);
st[id].sum = st[lson].sum+st[rson].sum;
} void push_down(int id)
{
if(st[id].lazy != ){
st[lson].lazy += st[id].lazy;
st[rson].lazy += st[id].lazy;
st[id].sum += (st[id].r-st[id].l+)*st[id].lazy;
st[id].lazy = ;
}
return;
} ll query(int id, int l, int r)
{
if(st[id].l == l && st[id].r == r)return st[id].sum+(r-l+)*st[id].lazy;
push_down(id);
if(r <= mid)return query(lson, l, r);
else if(l > mid)return query(rson, l, r);
else return query(lson, l, mid)+query(rson, mid+, r);
} void update(int id, int l, int r, int w)
{
if(st[id].l == l && st[id].r == r){
st[id].lazy += w;
return;
}
st[id].sum += (r-l+)*w;
if(r <= mid)update(lson, l, r, w);
else if(l > mid)update(rson, l, r, w);
else{
update(lson, l, mid, w);
update(rson, mid+, r, w);
}
} int main()
{
ll n, q;
while(scanf("%lld%lld", &n, &q)!=EOF){
for(int i = ; i <= n; i++)
scanf("%lld", &arr[i]);
build(, , n);
char op[];
ll a, b, c;
while(q--){
scanf("%s", op);
if(op[] == 'Q'){
scanf("%lld%lld", &a, &b);
printf("%lld\n", query(, a, b));
}else if(op[] == 'C'){
scanf("%lld%lld%lld", &a, &b, &c);
update(, a, b, c);
}
}
} return ;
}
 //2018-03-28

 import java.util.*;

 public class Main {

     public static void main(String[] args) {
Scanner cin = new Scanner(System.in); int n, q;
while(cin.hasNext()) {
n = cin.nextInt();
q = cin.nextInt();
SegmentTree st = new SegmentTree(n);
for(int i = 1; i <= n; i++)
st.arr[i] = cin.nextLong();
st.build(1, 1, n);
char op;
int a, b;
long c;
while(q-- > 0) {
op = cin.next().charAt(0);
if(op == 'Q') {
a = cin.nextInt();
b = cin.nextInt();
System.out.println(st.query(1, a, b));
}else if(op == 'C') {
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextLong();
st.updata(1, a, b, c);
}
}
}
}
} class SegmentTree{
static class Node{
public int l, r;
long sum, lazy;
Node(int _l, int _r, long _sum, long _lazy){
this.l = _l;
this.r = _r;
this.sum = _sum;
this.lazy = _lazy;
}
} public int n;
public long [] arr;
public Node [] nodes; SegmentTree(int _n){
this.n = _n;
arr = new long[n+1];
nodes = new Node[n<<2];
} int lson(int id) {
return (id<<1);
} int rson(int id) {
return ((id<<1)|1);
} int mid(int id) {
return (nodes[id].l + nodes[id].r)>>1;
}
void build(int id, int l, int r) {
nodes[id] = new Node(l, r, 0, 0);
if(l == r) {
nodes[id].sum = arr[l];
return;
}
build(lson(id), l, mid(id));
build(rson(id), mid(id)+1, r);
nodes[id].sum = nodes[lson(id)].sum + nodes[rson(id)].sum;
} void pushDown(int id) {
if(nodes[id].lazy != 0) {
nodes[lson(id)].lazy += nodes[id].lazy;
nodes[rson(id)].lazy += nodes[id].lazy;
nodes[id].sum += (nodes[id].r-nodes[id].l+1)*nodes[id].lazy;
nodes[id].lazy = 0;
}
} long query(int id, int l, int r) {
if(nodes[id].l == l && nodes[id].r == r)
return nodes[id].sum+(r-l+1)*nodes[id].lazy;
pushDown(id);
if(r <= mid(id))return query(lson(id), l, r);
else if(l > mid(id))return query(rson(id), l, r);
else return query(lson(id), l, mid(id))+query(rson(id), mid(id)+1, r);
} void updata(int id, int l, int r, long w) {
if(nodes[id] == null)return;
if(nodes[id].l == l && nodes[id].r == r) {
nodes[id].lazy += w;
return;
}
nodes[id].sum += (r-l+1)*w;
if(r <= mid(id))updata(lson(id), l, r, w);
else if(l > mid(id))updata(rson(id), l, r, w);
else {
updata(lson(id), l, mid(id), w);
updata(rson(id), mid(id)+1, r, w);
}
}
}

POJ3468(KB7-C 线段树)的更多相关文章

  1. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

  2. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  3. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  4. 洛谷P3372/poj3468(线段树lazy_tag)(询问区间和,支持区间修改)

    洛谷P3372 //线段树 询问区间和,支持区间修改 #include <cstdio> using namespace std; struct treetype { int l,r; l ...

  5. POJ3468 线段树(区间更新,区间求和,延迟标记)

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

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

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

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

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

  8. 【POJ3468】【zkw线段树】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  9. poj3468 线段树+lazy标记

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

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

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

随机推荐

  1. Python 关于 encode与decode 中文乱码问题

    字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...

  2. 使用 kafkat 在线扩缩容 kafka replicas

    本文档应用环境为 kafka-0.8.2.0, 其余版本请先行测试 场景 线上很多 kafka 的 topic 的副本数为1,这样的设置丧失了 kafka 高可用的特性,所以我们需要把 topic 的 ...

  3. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  4. cglib invoke 和 invokeSuper 可用的组合

    在深入字节码理解invokeSuper无限循环的原因中,我们理解的cglib的原理和其中一个合理的调用方式.但是这个调用方式是基于类的,对所有实例生效.实际场景中,我们可能只是希望代理某个具体的实例, ...

  5. 【Spark调优】:RDD持久化策略

    [场景] Spark对RDD执行一系列算子操作时,都会重新从头到尾计算一遍.如果中间结果RDD后续需要被被调用多次,可以显式调用 cache()和 persist(),以告知 Spark,临时保存之前 ...

  6. Git使用、Git配置、Git提交代码、Git上传

    非教程,只是自己的一个简单笔记.建议没有入门的朋友,直接看git的官方help文档: https://help.github.com/articles/set-up-git 1.注册一个git账号,超 ...

  7. mysql基础知识(3)

    十六.组合查询 使用 union 来组合查询,如果第一个查询返回M行,第二个查询返回N行,那么组合查询的结果一般为 M+N 行. 注意:每个查询必须包含相同的行.表达式的聚集函数:默认会去除相同行.表 ...

  8. js便签笔记(7)——style、currentStyle、getComputedStyle区别介绍【转载】

    转者语: 今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里 ...

  9. 自然语言处理--LDA主题聚类模型

    LDA模型算法简介: 算法 的输入是一个文档的集合D={d1, d2, d3, ... , dn},同时还需要聚类的类别数量m:然后会算法会将每一篇文档 di 在 所有Topic上的一个概率值p:这样 ...

  10. MySQL调研笔记1:MySQL调研清单

    0x00 背景 最近公司正在去微软化,之前使用的SQL Server.Oracle将逐步切换到MySQL,所以部门也会跟随公司步伐,一步步将现有业务从SQL Server切换到MySQL,当然上MyS ...