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. 小猴打架(luogu4430)(数论+生成树计数)

    一开始森林里面有\(N\)只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友.每次打完架后,打架的双方以及它们的好朋友就会互相认识,成为好朋友.经过\(N-1\)次打架之后,整个森林的小猴 ...

  2. Swift5 语言指南(二十六) 内存安全

    默认情况下,Swift可以防止代码中发生不安全行为.例如,Swift确保变量在使用之前进行初始化,在取消分配后不访问内存,并检查数组索引是否存在越界错误. Swift还确保对同一内存区域的多次访问不会 ...

  3. 坑爹的Sun JDK

    Sun的这个java.lang.Throwable 源码 设计非常糟糕,完全没有扩展性, 我在IBM 的Java JDK下,继承java.lang.Throwable重新定义了一个ExceptionW ...

  4. ubuntu升级pip后, ImportError: cannot import name ‘main‘

    场景描述: 原先pip安装完成之后,一直没有更新版本,原pip版本为8.1.1,今天安装python 包pysftp的时候,提示需要升级pip到(pip 10.0.1); 于是乎,直接手到擒来,终端命 ...

  5. 解决 在 WINDOWS 下 同时安装 python2 python3 后 pip 错误

    再之前同时安装 python 后 只需把环境变量PATH 里面改为 PATH=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Python27\;C:\Pytho ...

  6. MapReduce中的partitioner

    1.日志源文件: 1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 ...

  7. python多线程--Condition(条件对象)

    Condition class threading.Condition(lock=None 这个类实现条件变量对象.条件变量允许一个或多个线程等待,知道它们被另一个线程唤醒. 如果给出了lock参数而 ...

  8. NIO基础之同步、异步、阻塞、非阻塞

    这里区分几个概念,也是常见但是容易混淆的概念,就是标题中的同步.异步.阻塞.非阻塞. 一.同步与异步 同步与异步,关心的是消息通信的机制.也就是调用者和被调用者之间,消息是如何进行通知的.如果是调用者 ...

  9. Android系统架构及启动流程

  10. 如何用Python来处理数据表的长宽转换(图文详解)

    不多说,直接上干货! 很多地方都需用到这个知识点,比如Tableau里.   通常可以采取如python 和 r来作为数据处理的前期. Tableau学习系列之Tableau如何通过数据透视表方式读取 ...