【北大夏令营笔记-线段树】POJ3468-A Simple Problem with Integers

简介:
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 57993 Accepted: 17658
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.
Source

POJ Monthly--2007.11.25, Yang Yi






AC代码:

#include<iostream>
using namespace std;
struct CNode
{
   int L,R;
   CNode *pLeft,*pRight;
   long long nSum;//原来的和 
   long long lnc;//增量c的累加 
};
CNode Tree[200010];//两倍叶子节点数目就足够 
int nCount=0;
int Mid(CNode *pRoot)
{
    return (pRoot->L+pRoot->R)/2;
}
void BuildTree(CNode *pRoot,int L,int R)
{
     pRoot->L=L;
     pRoot->R=R;
     pRoot->nSum=0;
     pRoot->lnc=0;
     if(L==R)
     return;
     nCount++;
     pRoot->pLeft=Tree+nCount;
     nCount++;
     pRoot->pRight=Tree+nCount;
     BuildTree(pRoot->pLeft,L,(L+R)/2);
     BuildTree(pRoot->pRight,(L+R)/2+1,R);
}
void Insert(CNode *pRoot,int i,int v)
{
     if(pRoot->L==i&&pRoot->R==i)
     {
        pRoot->nSum=v;
        return;
     }
     
     pRoot->nSum+=v;
     if(i<=Mid(pRoot))
          Insert(pRoot->pLeft,i,v);
     else
          Insert(pRoot->pRight,i,v);
} 
//添加的规律:找到终节点前计算(或更新)nSum,仅在终结点(完全属于所加区间的节点)加c
//即是,更新nSum的没有更新C(但nSum是更新后的值),更新C的没有更新nSum; 
void Add(CNode *pRoot,int a,int b,long long c) 
{
     if(pRoot->L==a&&pRoot->R==b)
     {
         pRoot->lnc+=c;
         return;
     }
     pRoot->nSum+=c*(b-a+1);
     if(b<=(pRoot->L+pRoot->R)/2) 
        Add(pRoot->pLeft,a,b,c);
     else if(a>=(pRoot->L+pRoot->R)/2+1)
        Add(pRoot->pRight,a,b,c);
     else
     {
         Add(pRoot->pLeft,a,(pRoot->L+pRoot->R)/2,c);
         Add(pRoot->pRight,(pRoot->L+pRoot->R)/2+1,b,c);
     }
}
long long QuerynSum(CNode *pRoot,int a,int b)
{
     if(pRoot->L==a&&pRoot->R==b)
     { 
        //终节点仅计算和,不对c进行下一级更新 
        return pRoot->nSum+(pRoot->R-pRoot->L+1)*pRoot->lnc;
     }
     //算好非终节点的nSum之后,更新后面的增量c,将本节点的增量清0 
     pRoot->nSum+=(pRoot->R-pRoot->L+1)*pRoot->lnc;
     Add(pRoot->pLeft,pRoot->L,Mid(pRoot),pRoot->lnc);
     Add(pRoot->pRight,Mid(pRoot)+1,pRoot->R,pRoot->lnc); 
     pRoot->lnc=0;
     
     if(b<=Mid(pRoot))
         return QuerynSum(pRoot->pLeft,a,b);
     else if(a>=Mid(pRoot)+1)
         return QuerynSum(pRoot->pRight,a,b); 
     else{
          return QuerynSum(pRoot->pLeft,a,Mid(pRoot))+
          QuerynSum(pRoot->pRight,Mid(pRoot)+1,b);
     }
}
int main()
{
    int n,q,a,b,c;
    char cmd[10];
    scanf("%d %d",&n,&q);
    int i,j,k;
    nCount=0;
    BuildTree(Tree,1,n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a);
        Insert(Tree,i,a);
    } 
    for(i=0;i<q;i++)
    {
        scanf("%s",cmd);
        if(cmd[0]=='C'){
           scanf("%d%d%d",&a,&b,&c);
           Add(Tree,a,b,c);
        }
        else
        {
            scanf("%d%d",&a,&b);
            printf("%I64d\n",QuerynSum(Tree,a,b));
        }
    }
    return 0;
}

相关文章
|
C++ 网络架构
【PAT甲级 - C++题解】1013 Battle Over Cities
【PAT甲级 - C++题解】1013 Battle Over Cities
52 1
|
12月前
|
知识图谱
ACM刷题之路(二十三) HDU 1114 完全背包 Piggy-Bank
ACM刷题之路(二十三) HDU 1114 完全背包 Piggy-Bank
|
12月前
|
Java
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
ACM刷题之路(二十四)HDU 2844 多重背包转换 Coins
|
12月前
|
程序员
ACM刷题之路(一)第K个排列问题 Ignatius and the Princess II
ACM刷题之路(一)第K个排列问题 Ignatius and the Princess II
|
12月前
ACM刷题之路(二十一)大素数筛选 2019暑期集训 POJ 2689 Prime Distance
ACM刷题之路(二十一)大素数筛选 2019暑期集训 POJ 2689 Prime Distance
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
55 0
CodeForces 1195C Basketball Exercise (线性DP)
CodeForces 1195C Basketball Exercise (线性DP)
93 0
2019牛客暑期多校2-Partition problem深搜
题意: 将2*n个人分成两部分,每部分都有n个人 而且每个人只能属于一个组,问按照给出的算式得到的竞争力最大值是多少
83 0
2019牛客暑期多校2-Partition problem深搜
|
开发者
牛客第六场-Combination of Physics and Maths
题意:选出一个子矩阵,使得所求的压强最大,压强是指这个子矩阵中每个元素之和 / 这个子矩阵最下面一行的元素之和
46 0
牛客第六场-Combination of Physics and Maths
HDU-1002,A + B Problem II(Java大数)
HDU-1002,A + B Problem II(Java大数)