poj 3468 A Simple Problem with I…

来源:互联网 发布:编程语言薪资排行 编辑:程序博客网 时间:2024/05/17 02:17

Description

You have N integers, A1,A2, ... , AN. You need to dealwith two kinds of operations. One type of operation is to add somegiven number to each number in a given interval. The other is toask 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 ofA1, A2, ... ,AN. -1000000000 ≤ Ai ≤1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each ofAa, Aa+1, ... ,Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum ofAa, Aa+1, ... ,Ab.

Output

You need to answer all Q commands in order. One answer ina line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
 __int64 a,b,sum,add;//ADD是记录其增加值的
}tree[300000];
__int64  A[100001];
void maketree(__int64 a,__int64 b,__int64 k)//首先建树用递归思想建sum树,先考虑根节点然后是左右孩子节点
{
 tree[k].a = a;//初始化结构体值
 tree[k].b = b;
 tree[k].add=0;
 if(a==b)
 {
  tree[k].sum = A[a];//建叶子节点时将其赋值 返回
  return;
 }
 __int64 mid = (a+b)/2;
 maketree(a,mid,k*2);
 maketree(mid+1,b,(k*2)+1);
 tree[k].sum = tree[k*2].sum + tree[(k*2)+1].sum;//当前结点sum值是其左右孩子结点sum值之和

void update(__int64 a,__int64 b,__int64 val,__int64 k)
{
 
 if(tree[k].a ==a&&b==tree[k].b)
 {
  tree[k].add += val;//将其增值存入add,返回。
  return ;
 }
 tree[k].sum=tree[k].sum+val*(b-a+1);//修改当前结点的sum值,因为当前结点只增加了[a,b]区间内数的个数的val倍值,所以用tree[k].sum=tree[k].sum+val*(b-a+1);
 __int64 mid=(tree[k].a+tree[k].b)/2;
 if(mid>=b)
  update(a,b,val,k*2);
 else if(mid<a)
  update(a,b,val,(k*2)+1);
  else
    {
     update(a,mid,val,k*2);
     update(mid+1,b,val,k*2+1);
    }
}
__int64 query(__int64 a,__int64 b,__int64 k)
{//找到结点区间是查询区间,直接返回其sum+(b-a+1)*tree[k].add
 if(a==tree[k].a&&b==tree[k].b)return tree[k].sum+(b-a+1)*tree[k].add;
  //如果不匹配修改当前结点的sum值,继续搜索。
 tree[k].sum+=(tree[k].b- tree[k].a+1)*tree[k].add;
 __int64 mid=(tree[k].a+tree[k].b)/2;
//这时才需更新左右子树的sum和add值知道找到匹配 [a,b]区间位置
 update(tree[k].a,mid,tree[k].add,2*k);
 update(mid+1,tree[k].b,tree[k].add,k*2+1);
 tree[k].add=0;//这时将当前结点add增值归0 因为已往下更新左右子树和当前结点sum值
 if(mid>=b)
     return query(a,b,k*2);//查询分三种情况:1.[a,b]在左子树 2.[a,b]在右子树3.[a,b]跨左右子树。
 else if(mid<a)
        return query(a,b,(k*2)+1);
  else
    {
         return query(a,mid,k*2)+query(mid+1,b,k*2+1);
    }
}
int main()
{
 __int64 i,n,m,a,b,val;
 while(scanf("%I64d%I64d",&n,&m)!=EOF)
 {
  for(i=1;i<=n;i++)
   scanf("%I64d",&A[i]);
  getchar();
  maketree(1,n,1);
  //  for(i=0;i<2*n;i++)
  //  printf("%d ",tree[i].sum);
  for(i=0;i<m;i++)
  {
   char ch;
   scanf("%c",&ch);
   if(ch=='C')
   {
    scanf("%I64d%I64d%I64d",&a,&b,&val);
    getchar();
    update(a,b,val,1);
   }
   if(ch=='Q')
   {
    scanf("%I64d%I64d",&a,&b);
    getchar();
    printf("%I64d\n",query(a,b,1));
   }
  }
 }
 
 return 0;
 
}
关键思路在于 查询函数中。加入了更新 只有当须查询的区间不为当前值时 需要更新 
当前区间的sum值及其 之下的 左右两个区间。 因为接下来的查找时要 进入更小的区间
即 当前区间的左右区间中。则需要把在此区间所加入的值分别放入其左右子树区间,
确保加入的值被一直记录。
这样从大到小的更新可以 节省程序的时间效率。若从小的区间一次更新到最后需要遍历所有的区间
而 用这个方法只需要找到 满足条件的区间即可。
原创粉丝点击