POJ 3468 A Simple Problem with Integers(线段树Lazy_Tag详解)

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

Source


题目链接:http://poj.org/problem?id=3468

这道题是一个练习线段树区间更新很好的一个裸题;

直接一个Lazy_Tag模板就解决了

PushUp(由下往上更新修改后的数据),Build(初始化线段树),Update(修改叶子结点的数据),Query(查询区间数据)

涉及到的位运算:n<<1 == n*2;    n<<2 == n*4;     n>>1==n/2;         n<<1|1 == n*2+1;

AC代码如下:


#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
LL sum[maxn<<2];
LL cnt[maxn<<2];  ///延迟标记
int N,Q;

void build(int l,int r,int rt)///建树+结点lazy初始化
{
  cnt[rt]=0;///lazy初始化
  if(l==r)
  {
    scanf("%lld",&sum[rt]);  ///输入时注意lld,没注意到WA了一次
    return ;
  }

  int m=(l+r)>>1;///中间
  build(l,m,rt<<1);///左子树
  build(m+1,r,rt<<1|1);///右子树
  sum[rt]=sum[rt<<1]+sum[rt<<1|1];///线段树功能:区间求和
}

void Down(int rt,int len)///查询lazy_Tag
{
  if(cnt[rt])///存在标记,则向下传递标记
  {///并更新左右子树的值
    cnt[rt<<1]+=cnt[rt];///左子树
    cnt[rt<<1|1]+=cnt[rt];///右子树
    sum[rt<<1]+=cnt[rt]*(len-(len>>1));///更新左子树区间和
    sum[rt<<1|1]+=cnt[rt]*(len>>1);///更新右子树区间和
    cnt[rt]=0;///取消标记
  }
}

LL Query(int a,int b,int l,int r,int rt)///查询区间和
{
  if(a<=l && b>=r)
  {
    return sum[rt];
  }

  Down(rt,r-l+1);///查询是否需要更新值
  ///更新该结点的左右子树
  int m=(l+r)>>1;
  LL ans=0;
  if(a<=m)
    ans+=Query(a,b,l,m,rt<<1);
  if(b>m)
    ans+=Query(a,b,m+1,r,rt<<1|1);
   ///区间求和,所以ans+=Query(a,b,m+1,r,rt<<1|1)
   ///如果是求区间max则ans=max(ans,Query(a,b,m+1,r,rt<<1|1));
  sum[rt]=sum[rt<<1]+sum[rt<<1|1];
  
  return ans;
}


void Update(int a,int b,int c,int l,int r,int rt)
{///修改叶子结点的数据
  if(a<=l && b>=r)
  {
    cnt[rt]+=c;///标记存在延迟修改量为C
    sum[rt]+=(LL)(r-l+1)*c;///单点更新
    ///涉及*/运算时记得加强制转换
    return ;
  }
  Down(rt,r-l+1);///更新该结点的左右子树的值
  int m=(r+l)>>1;
  if(a<=m)///更新左子树
    Update(a,b,c,l,m,rt<<1);
  if(b>m)///更新右子树
    Update(a,b,c,m+1,r,rt<<1|1);

  sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

int main()
{
  while(scanf("%d%d",&N,&Q)==2)
  {
    build(1,N,1);

    for(int i=0; i<Q; i++)
    {
      char ch;
      int a,b,c;
      getchar();
      scanf("%c",&ch);
      if(ch=='Q')
      {
        scanf("%d%d",&a,&b);
        LL ans=Query(a,b,1,N,1);///查询区间数据
        printf("%lld\n",ans);
      }
      else
      {
        scanf("%d%d%d",&a,&b,&c);
        Update(a,b,c,1,N,1);///更新区间数据
      }
    }

  }
  return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41668093/article/details/79981605