POJ 3468 A Simple Problem with Integers【线段树】

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 123941 Accepted: 38439
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.

#include<iostream>
#include<cstring>
using namespace std;
#define ll long long
const int MAX = 5e5;
ll a[MAX], lazy[MAX];
void Update(int rt, int l, int r, int x, int y, int val)
{
    if(x <= l && y >= r)
    {
        a[rt] += val * (r - l + 1);
        lazy[rt] += val;
        return;
    }
    if(lazy[rt])
    {
        lazy[rt << 1] += lazy[rt];
        lazy[rt << 1 | 1] += lazy[rt];
        a[rt << 1] += ((l + r) / 2 - l + 1) * lazy[rt];
        a[rt << 1 | 1] += (r - (l + r) / 2) * lazy[rt];
        lazy[rt] = 0;
    }
    int mid = (l + r) >> 1;
    if(mid >= x)
        Update(rt << 1, l, mid, x, y, val);
    if(mid < y)
        Update(rt << 1 | 1, mid + 1, r, x, y, val);
    a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
ll Query(int rt, int l, int r, int x, int y)
{
    if(x <= l && y >= r)
        return a[rt];
    if(lazy[rt])
    {
        lazy[rt << 1] += lazy[rt];
        lazy[rt << 1 | 1] += lazy[rt];
        a[rt << 1] += ((l + r) / 2 - l + 1) * lazy[rt];
        a[rt << 1 | 1] += (r - (l + r) / 2) * lazy[rt];
        lazy[rt] = 0;
    }
    int mid = (l + r) >> 1;
    ll sum = 0;
    if(mid >= x)
        sum += Query(rt << 1, l, mid, x, y);
    if(y > mid)
        sum += Query(rt << 1 | 1, mid + 1, r, x, y);
    return sum;
}
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    while(cin >> n >> m)
    {
        memset(a, 0, sizeof a);
        memset(lazy, 0, sizeof lazy);
        for(int i = 1, x; i <= n; i++)
        {
            cin >> x;
            Update(1, 1, n, i, i, x);
        }
        char s[2];
        for(int i = 0, x, y, z; i < m; i++)
        {
            cin >> s;
            if(s[0] == 'Q')
            {
                cin >> x >> y;
                cout << Query(1, 1, n, x, y) << endl;
            }
            else
            {
                cin >> x >> y >> z;
                Update(1, 1, n, x, y, z);
            }
        }
    }
    return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
#define mms(x) memset(x, 0, sizeof x)
#define ll long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define MAX 100005
ll tree[MAX << 2], lazy[MAX << 2];
int n, m;
void PushDown(int rt, int l, int r)
{
    if(lazy[rt])
    {
        int mid = (l + r) >> 1;
        lazy[rt << 1] += lazy[rt];
        lazy[rt << 1 | 1] += lazy[rt];
        tree[rt << 1] += (mid - l + 1) * lazy[rt];
        tree[rt << 1 | 1] += (r - mid) * lazy[rt];
        lazy[rt] = 0;
    }
}
void PushUp(int rt)
{
    tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void BuildTree(int rt, int l, int r)
{
    lazy[rt] = 0;
    if(l == r)
    {
        cin >> tree[rt];
        return;
    }
    int mid = (l + r) >> 1;
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}

ll Query(int rt, int l, int r, int x, int y)
{
    if(x <= l && r <= y)
        return tree[rt];
    PushDown(rt, l, r);
    int mid = (l + r) >> 1;
    ll ans = 0;
    if(x <= mid)
        ans += Query(lson, x, y);
    if(mid < y)
        ans += Query(rson, x, y);
    PushUp(rt);
    return ans;
}
void Update(int rt, int l, int r, int x, int y, int val)
{
    if(x <= l && r <= y)
    {
        tree[rt] += val * (r - l + 1);
        lazy[rt] += val;
        return;
    }
    PushDown(rt, l, r);
    int mid = (l + r) >> 1;
    if(x <= mid)
        Update(lson, x, y, val);
    if(mid < y)
        Update(rson, x, y, val);
    PushUp(rt);
}
void init()
{
    mms(lazy);
    mms(tree);
    BuildTree(1, 1, n);
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n >> m)
    {
        init();
        for(int i = 0, xx, yy, zz; i < m; i++)
        {
            char s[5];
            cin >> s;
            if(s[0] == 'Q')
            {
                cin >> xx >> yy;
                cout << Query(1, 1, n, xx, yy) << endl;
            }
            else
            {
                cin >> xx >> yy >> zz;
                Update(1, 1, n, xx, yy, zz);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值