码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3468(线段树区间增加,区间求和)

时间:2015-11-01 00:16:25      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 81519   Accepted: 25185
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标记的更新
/* 
* @Author: LinK
* @Date:   2015-10-31 18:34:32
* @Last Modified by:   LinK
* @Last Modified time: 2015-10-31 23:11:18
*/

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll;

const int maxn = 100010;

struct Node {
    ll sum, add;
} tree[maxn << 2];

int n, q;
ll num[maxn];

void build(int rt, int l, int r) {
    if (l == r) {
        tree[rt].sum = num[l];
        tree[rt].add = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
    tree[rt].add = 0;
}

void update(int rt, int l, int r, int L, int R, ll val) {
    if (L <= l && R >= r) {
        tree[rt].add += val;
        return;
    }
    tree[rt].sum += val * (min(R, r) - max(L, l) + 1);
    int mid = (l + r) >> 1;
    if (R <= mid) update(rt << 1, l, mid, L, R, val);
    else if (L > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
    else {
        update(rt << 1, l, mid, L, R, val);
        update(rt << 1 | 1, mid + 1, r, L, R, val);
    }
}

ll query(int rt, int l, int r, int L, int R) {
    if (L <= l && R >= r) {
        return tree[rt].sum + tree[rt].add * (r - l + 1);
    }
    int mid = (l + r) >> 1;
    if (tree[rt].add) {
        tree[rt].sum += (r - l + 1) * tree[rt].add;
        tree[rt << 1].add += tree[rt].add;
        tree[rt << 1 | 1].add += tree[rt].add;
        tree[rt].add = 0;
    }
    if (R <= mid) return query(rt << 1, l, mid, L, R);
    if (L > mid) return query(rt << 1 | 1, mid + 1, r, L, R);
    return query(rt << 1, l, mid, L, R) + query(rt << 1 | 1, mid + 1, r, L, R);
}

int main() {
    char op;
    int a, b;
    ll c;
    while (~scanf("%d %d", &n, &q)) {
        for (int i = 1; i <= n; i ++) scanf("%lld", &num[i]);
        build(1, 1, n);
        while (q --) {
            scanf(" %c %d %d", &op, &a, &b);
            if (op == Q) printf("%lld\n", query(1, 1, n, a, b));
            else {
                scanf("%lld", &c);
                update(1, 1, n, a, b, c);
            } 
        }
    }

    return 0;
}

 

POJ3468(线段树区间增加,区间求和)

标签:

原文地址:http://www.cnblogs.com/LinKArftc/p/4926523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!