POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 67511   Accepted: 20818
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

Hint

The sums may exceed the range of 32-bit integers.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-6 ;
const int N = 100010;
const int mod = 2333333;
int n , m ;
LL sum[N<<2] , lazy[N<<2] , e[N] , tot ;

void Up( int rt ) {
    sum[rt] = sum[lr] + sum[rr];
}
void Down( int l , int r , int rt ) {
    if( lazy[rt] != 0 ) {
        int mid = (l+r)>>1;
        sum[lr] += lazy[rt]*(mid-l+1) , sum[rr] += lazy[rt]*(r-mid);
        lazy[lr] += lazy[rt] , lazy[rr] += lazy[rt];
        lazy[rt] = 0 ;
    }
}
void build( int l , int r , int rt ){
    lazy[rt] = 0 ;
    if( l == r ) {
        sum[rt] = e[tot++];
        return ;
    }
    int mid = (l+r)>>1;
    build(lson),build(rson);
    Up(rt);
}
void update( int l , int r , int rt , int L , int R , LL val ) {
    if( L == l && r == R ) {
        sum[rt] += val*(r-l+1) ;
        lazy[rt] += val;
        return ;
    }
    Down( l , r , rt );
    int mid = (l+r)>>1;
    if( R <= mid ) update(lson,L,R,val);
    else if( L > mid ) update(rson,L,R,val);
    else update(lson,L,mid,val) , update(rson,mid+1,R,val);
    Up(rt);
}
LL query( int l , int r , int rt , int L , int R ) {
    if( L == l && r == R ) {
        return sum[rt];
    }
    Down(l,r,rt);
    int mid = (l+r)>>1;
    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+1,R);
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
//        freopen("out.txt","w",stdout);
    #endif // LOCAL
    char s[10]; int x , y ; LL c ;
    while( ~scanf("%d%d",&n,&m ) ) {
        tot = 0 ;
        for( int i = 0 ; i < n ; ++i ) {
            scanf("%I64d",&e[i]);
        }
        build(root);
        while(m--) {
            scanf("%s",s);
            if( s[0] == ‘Q‘ ) {
                scanf("%d%d",&x,&y);
                printf("%I64d\n",query(root,x,y));
            }
            else {
                scanf("%d%d%I64d",&x,&y,&c);
                update(root,x,y,c);
            }
        }
    }
}

时间: 2024-12-29 07:21:51

POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)的相关文章

POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 62431   Accepted: 19141 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

A Simple Problem with Integers 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

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers 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

POJ 3468 A Simple Problem with Integers //线段树的成段更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   Accepted: 17974 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers 线段树加延迟标记

A Simple Problem with Integers 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

poj 3468 A Simple Problem with Integers 线段树区间更新

点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 63565   Accepted: 19546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers 线段树 区间更新 区间查询

题目链接: http://poj.org/problem?id=3468 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map