前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ--3468 A Simple Problem with Integers(线段树)

POJ--3468 A Simple Problem with Integers(线段树)

作者头像
ShenduCC
发布2018-04-25 17:30:02
6880
发布2018-04-25 17:30:02
举报
文章被收录于专栏:算法修养算法修养

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

线段树的简单应用。

#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;
#define MAX 100000
long long int segTree[MAX*4+5];
long long int add[MAX*4+5];
int n,q;
char a;
int x,y,z;
void PushUp(int node)
{
    segTree[node]=segTree[node<<1]+segTree[node<<1|1];
}
void PushDown(int node,int m)
{
    if(add[node]!=0)
    {
        add[node<<1]+=add[node];
        add[node<<1|1]+=add[node];
        segTree[node<<1]+=add[node]*(m-(m>>1));
        segTree[node<<1|1]+=add[node]*(m>>1);
        add[node]=0;
    }
}
void build(int node,int begin,int end)
{
    add[node]=0;
    if(begin==end)
    {
        scanf("%lld",&segTree[node]);
        return;
    }
    int m=(begin+end)>>1;
    build(node<<1,begin,m);
    build(node<<1|1,m+1,end);
    PushUp(node);
}
void Update(int node,int begin,int end,int left,int right,int num)
{
    if(left<=begin&&end<=right)
    {
        add[node]+=num;
        segTree[node]+=num*(end-begin+1);
        return;
    }
    PushDown(node,end-begin+1);
    int m=(begin+end)>>1;
    if(left<=m)
        Update(node<<1,begin,m,left,right,num);
    if(right>m)
        Update(node<<1|1,m+1,end,left,right,num);
    PushUp(node);
}
long long int Query(int node,int begin,int end,int left,int right)
{
    if(left<=begin&&end<=right)
        return segTree[node];
    PushDown(node,end-begin+1);
    int m=(begin+end)>>1;
    long long int ret=0;
    if(left<=m)
        ret+=Query(node<<1,begin,m,left,right);
    if(right>m)
        ret+=Query(node<<1|1,m+1,end,left,right);
    return ret;
}
int main()
{
    while(scanf("%d%d",&n,&q)!=EOF)
    {
    build(1,1,n);
    long long int ans;
    for(int i=0;i<q;i++)
    {
        ans=0;
        getchar();
        scanf("%c",&a);
        if(a=='Q')
        {
            scanf("%d%d",&x,&y);
            ans=Query(1,1,n,x,y);
            printf("%lld\n",ans);
        }
        else
        {
            scanf("%d%d%d",&x,&y,&z);
            Update(1,1,n,x,y,z);
        }
    }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-12-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档