POJ(3468):A Simple Problem with Integers

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

POJ Monthly--2007.11.25, Yang Yi

题意:

给定Q (1 ≤ Q ≤ 100,000) 个数A 1 ,A 2 … A Q , ,
以及可能多次进行的两个操作:
1)  对某个区间A i  … A j 的 每个数 都加n(n 可变)
2) 求某个区间A i  … A j 的数的和

解题思路:

只存和,会导致每次加数的时候都要更新到叶子节点,速度太慢(O(nlogn)) ,这是必须要避免的。

在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc 值,不再往下走,否则要更新nSum( 加上本次增量), 再将增
量往下传。这样更新的复杂度就是O(log(n))在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc 往下带,然后将Inc
代表的所有增量累加到nSum 上后将Inc 清0,接下来再往下查询。 一边查询,一边 Inc 往下带的过程也是区间分解的过程,复杂度也是 logn

#include<iostream>
#include<cstdio>
using namespace std;
struct CNode{
    int L,R;
    long long nSum;
    long long lnc; //存增量

};
CNode tree[400010];

void build_tree(int root,int L,int R){
    tree[root].L = L;
    tree[root].R = R;
    tree[root].nSum = 0;
    tree[root].lnc = 0;
    if(L == R)
        return;
    build_tree(2*root+1,L,(L+R)/2);
    build_tree(2*root+2,(L+R)/2+1,R);
}

void Insert (int root,int i,int v){
    if(tree[root].L == i && tree[root].R ==i){
        tree[root].nSum = v;
        return;
    }
    tree[root].nSum += v;
    if(i <= (tree[root].L+tree[root].R)/2)
        Insert(2*root+1,i,v);
    else
        Insert(2*root+2,i,v);
}

void Add(int root,int a,int b,long long c){
    if(tree[root].L == a && tree[root].R == b){
        tree[root].lnc += c;
        return;
    }
    tree[root].nSum += c*(b-a+1);
    if(b <= (tree[root].R + tree[root].L)/2)
        Add(2*root+1,a,b,c);
    else if(a >= (tree[root].R + tree[root].L)/2+1)
        Add(2*root+2,a,b,c);
    else{
        Add(2*root+1,a,(tree[root].R + tree[root].L)/2,c);
        Add(2*root+2,(tree[root].R + tree[root].L)/2+1,b,c);
    }
}

long long query(int root,int a,int b){
    if(tree[root].L == a&& tree[root].R == b)
        return tree[root].nSum + (tree[root].R-tree[root].L+1)*tree[root].lnc;
    tree[root].nSum += (tree[root].R - tree[root].L + 1)*tree[root].lnc;
    Add(2*root+1,tree[root].L,(tree[root].L+tree[root].R)/2,tree[root].lnc);
    Add(2*root+2,(tree[root].L+tree[root].R)/2+1,tree[root].R,tree[root].lnc);
    tree[root].lnc = 0;
    if(b <= (tree[root].L+tree[root].R)/2)
        return query(2*root+1,a,b);
    else if(a >= (tree[root].L+tree[root].R)/2+1)
        return  query(2*root+2,a,b);
    else{
        return query(2*root+1,a,(tree[root].L+tree[root].R)/2)+
                query(2*root+2,(tree[root].L+tree[root].R)/2+1,b);
    }
}
int main()
{
    int n,q,a,b,c;
    char s;
    scanf("%d%d",&n,&q);
    getchar();
    int i,j,k;
    build_tree(0,1,n);
    for( i = 1;i <= n;i ++ ) {
        scanf("%d",&a);
        getchar();
        Insert(0,i,a);
    }
    for( i = 0;i < q;i ++ ) {
        scanf("%s",&s);
    if ( s == 'C' ) {
        scanf("%d%d%d",&a,&b,&c);
        getchar();
        Add( 0,a,b,c);
    }
    else {
        scanf("%d%d",&a,&b);
        getchar();
        printf("%I64d\n",query(0,a,b));
    }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值