线段树两题复习(单点更新,区间更新)

1.单点更新,求区间最大值

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long ll;
const int maxn=200010;
#define  lson i<<1
#define  rson  (i<<1)|1

struct ST
{
    int l,r;
    int maxx;
}st[maxn<<2];

void pushup(int i)
{
    st[i].maxx=max(st[lson].maxx,st[rson].maxx);
}

void build(int i,int l,int r)
{
    st[i].l=l;
    st[i].r=r;
    if(st[i].l==st[i].r)
    {
        rd(st[i].maxx);
        return;
    }
    int mid=(st[i].l+st[i].r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    pushup(i);
}

void update(int i,int p,int val)
{
    if(st[i].l==st[i].r)
    {
        st[i].maxx=val;
        return;
    }
    int mid=(st[i].l+st[i].r)>>1;
    if(p<=mid)
        update(lson,p,val);
    else
        update(rson,p,val);
    pushup(i);
}

int query(int i,int l,int r)
{
    if(st[i].l==l&&st[i].r==r)
    {
        return st[i].maxx;
    }
    int mid=(st[i].l+st[i].r)>>1;
    if(l>mid)
        return query(rson,l,r);
    else if(r<=mid)
        return query(lson,l,r);
    else
        return max(query(lson,l,mid),query(rson,mid+1,r));
}

int n,m;
char cm[2];
int l,r,p,val;

int main()
{
    while(rd2(n,m)!=EOF)
    {
        build(1,1,n);
        while(m--)
        {
            scanf("%s",cm);
            if(cm[0]=='U')
            {
                rd2(p,val);
                update(1,p,val);
            }
            else
            {
                rd2(l,r);
                printf("%d\n",query(1,l,r));
            }
        }
    }
    return 0;
}

2.区间更新,求区间和

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long ll;
const int maxn=100010;
#define  lson i<<1
#define  rson  (i<<1)|1

struct ST
{
    int l,r;
    ll sum;
    ll lazy;
}st[maxn<<2];

void pushup(int i)
{
    st[i].sum=st[lson].sum+st[rson].sum;
}

void pushdown(int i,int len)
{
    if(st[i].lazy)
    {
        st[lson].lazy+=st[i].lazy;
        st[rson].lazy+=st[i].lazy;
        st[lson].sum+=(ll)(len-(len>>1))*st[i].lazy;
        st[rson].sum+=(ll)(len>>1)*st[i].lazy;
        st[i].lazy=0;
    }
}

void build(int i,int l,int r)
{
    st[i].l=l;
    st[i].r=r;
    st[i].lazy=0;
    if(st[i].l==st[i].r)
    {
        scanf("%I64d",&st[i].sum);
        return;
    }
    int mid=(st[i].l+st[i].r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    pushup(i);
}

void update(int i,int l,int r,ll val)
{
    if(st[i].l==l&&st[i].r==r)
    {
        st[i].sum+=(ll)(r-l+1)*val;
        st[i].lazy+=val;
        return ;
    }
    int mid=(st[i].l+st[i].r)>>1;
    pushdown(i,st[i].r-st[i].l+1);
    if(r<=mid)
        update(lson,l,r,val);
    else if(l>mid)
        update(rson,l,r,val);
    else
    {
        update(lson,l,mid,val);
        update(rson,mid+1,r,val);
    }
    pushup(i);
}

ll query(int i,int l,int r)
{
    if(st[i].l==l&&st[i].r==r)
    {
        return st[i].sum;
    }
    pushdown(i,st[i].r-st[i].l+1);
    int mid=(st[i].l+st[i].r)>>1;
    if(r<=mid)
        return query(lson,l,r);
    else if(l>mid)
        return query(rson,l,r);
    else
        return query(i<<1,l,mid)+query(rson,mid+1,r);
}

int n,q;
char cm[2];
int l,r,p;
ll val;

int main()
{
  while(rd2(n,q)!=EOF)
  {
      build(1,1,n);
      while(q--)
      {
          scanf("%s",cm);
          if(cm[0]=='Q')
          {
              rd2(l,r);
              printf("%I64d\n",query(1,l,r));
          }
          else
          {
              rd2(l,r);
              scanf("%I64d\n",&val);
              update(1,l,r,val);
          }
      }
  }
  return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值