【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst

题目大意:

2个操作

A.区间a b 增加 c

B 查询a b;


注意事项:1.记住要清除标记

                2.查询时要下放标记,但没必要向上更新


线段:自带的,不用建模

区间和性质:sum;


/*
WA 1次 以为不要LONG LONG
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int maxn=100000+5;
using namespace std;
int N,Q;
LL tree[maxn*4];
LL col[maxn*4];
void PushUp(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(int l,int r,int rt)
{
    col[rt]=0;
    if(l==r){scanf("%lld",&tree[rt]);return 0;}
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void Pushdown(int rt,int k)          //k是长度
{
    if(col[rt])
        {
            col[rt<<1]+=col[rt];
            col[rt<<1|1]+=col[rt];
            tree[rt<<1]+=(k-(k>>1))*col[rt];
            tree[rt<<1|1]+=(k>>1)*col[rt];
            col[rt]=0;
        }
}
int update(int L,int R,int c,int l,int r,int rt) {
       if (L<=l&&r<= R) {
              col[rt]+=c;               //不同题目处理方式不同 ,
              tree[rt]+=(LL)c*(r-l+1);
              return 0;
       }
       Pushdown(rt,r-l+1);             //Lazy 下放
       int m=(l+r)>>1;                  //下面与以往一样
       if (L<=m) update(L,R,c,lson);
       if (R>m) update(L,R,c,rson);
       PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    LL temp=0;
    if(L<=l&&r<=R){return tree[rt];}
    Pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) temp+=query(L,R,lson);
    if(R>m)  temp+=query(L,R,rson);
    return temp;
}
void solve()
{
    char temp;
    int a,b,c;
    getchar();
    for(int i=1;i<=Q;i++)
    {
        scanf("%c",&temp);
        if(temp=='Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(a,b,1,N,1));
        }
        else if(temp=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,N,1);
        }
        getchar();
    }
}
void init()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
}
int main()
{
  //  init();
    while(cin>>N>>Q)
    {
        build(1,N,1);
        solve();
    }
    return 0;
}

全long long 形式

/*
WA 1次 以为不要LONG LONG
WA 2次 全LONG LONG为妙
WA 3次 m 忘记改成long long 了
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int maxn=100000+5;
using namespace std;
int N,Q;
LL tree[maxn*4];
LL col[maxn*4];
void PushUp(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(LL l,LL r,int rt)
{
    col[rt]=0;
    if(l==r){scanf("%lld",&tree[rt]);return 0;}
    LL m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void Pushdown(int rt,LL k)          //k是长度
{
    if(col[rt])
        {
            col[rt<<1]+=col[rt];
            col[rt<<1|1]+=col[rt];
            tree[rt<<1]+=(k-(k>>1))*col[rt];
            tree[rt<<1|1]+=(k>>1)*col[rt];
            col[rt]=0;
        }
}
int update(int L,int R,LL c,LL l,LL r,int rt) {
       if (L<=l&&r<= R) {
              col[rt]+=c;               //不同题目处理方式不同 ,
              tree[rt]+=c*(r-l+1);
              return 0;
       }
       Pushdown(rt,r-l+1);             //Lazy 下放
       LL m=(l+r)>>1;                  //下面与以往一样
       if (L<=m) update(L,R,c,lson);
       if (R>m) update(L,R,c,rson);
       PushUp(rt);
}
LL query(int L,int R,LL l,LL r,int rt)
{
    LL temp=0;
    if(L<=l&&r<=R){return tree[rt];}
    Pushdown(rt,r-l+1);
    LL m=(l+r)>>1;
    if(L<=m) temp+=query(L,R,lson);
    if(R>m)  temp+=query(L,R,rson);
    return temp;
}
void solve()
{
    char temp;
    LL a,b;
    LL c;
    getchar();
    for(int i=1;i<=Q;i++)
    {
        scanf("%c",&temp);
        if(temp=='Q')
        {
            scanf("%lld%lld",&a,&b);
            printf("%d\n",query(a,b,1,N,1));
        }
        else if(temp=='C')
        {
            scanf("%lld%lld%lld",&a,&b,&c);
            update(a,b,c,1,N,1);
        }
        getchar();
    }
}
void init()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
}
int main()
{
  //  init();
    while(cin>>N>>Q)
    {
        build(1,N,1);
        solve();
    }
    return 0;
}



转载于:https://www.cnblogs.com/zy691357966/p/5480379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值