poj3468

A Simple Problem with Integers

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
Problem 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
 

#include <iostream>
#include<stdio.h>
#include<string.h> 
using namespace std; 
#define MAXN   
int N,Q; 
__int64 num[100010], ans ,x; 
struct data 

    __int64 sum,inc; 
    int l,r; 
}Tree[400040]; 
void CreateTree(int k,int l,int r)                             

    Tree[k].l = l; 
    Tree[k].r = r; 
    Tree[k].inc = 0; 
    if(l == r) 
 {
        Tree[k].sum = num[l]; 
  return;
 }
    else 
    { 
        int mid=(l+r)>>1; 
  CreateTree(k<<1,l,mid); 
        CreateTree(k<<1|1,mid+1,r); 
        Tree[k].sum = Tree[k<<1].sum + Tree[k<<1|1].sum; 
    } 

void Add(int k,int l,int r,__int64 inc)                              

    if(Tree[k].l == l && Tree[k].r == r) 
    { 
        Tree[k].inc += inc; 
    } 
 else 
    { 
        int mid = (Tree[k].l+Tree[k].r)>>1; 
  Tree[k].sum += (r-l+1)*inc; 
  if(l>mid) 
            Add(k<<1|1,l,r,inc); 
  else if(r<=mid) 
            Add(k<<1,l,r,inc); 
  else 
        { 
            Add(k<<1,l,mid,inc); 
            Add(k<<1|1 ,mid+1,r,inc); 
        } 
    } 

void Query(int k,int l,int r,__int64 temp) 

    temp += Tree[k].inc; 
    if(Tree[k].l == l &&Tree[k].r == r) 
    { 
        ans+= Tree[k].sum + temp * (r-l+1); 
    } 
    else 
    { 
  
        int mid = (Tree[k].l+Tree[k].r)>>1; 
        if(l>mid) 
            Query(k<<1|1,l,r,temp); 
        else if(r<=mid) 
            Query(k<<1,l,r,temp); 
        else 
        { 
            Query(k<<1,l,mid,temp); 
            Query(k<<1|1 ,mid+1,r,temp); 
        } 
    } 

int main() 

    int i,j; 
    char ch; 
    while(scanf("%d%d",&N,&Q)!=EOF) 
    { 
        for(i=1;i<=N;i++) 
            scanf("%I64d",&num[i]); 
        CreateTree(1,1,N); 
        while(Q--) 
        { 
   getchar(); 
            scanf("%c %d %d",&ch,&i,&j); 
            if(ch=='Q') 
   { 
                ans = 0; 
                Query(1,i,j,0); 
    printf("%I64d ",ans); 
            } 
            else 
            { 
                scanf("%I64d",&x); 
                Add(1,i,j,x); 
            } 
        } 
    }
 return 0;

原文地址:https://www.cnblogs.com/lxm940130740/p/3408669.html