线段树

本文介绍了一种使用线段树进行区间更新和查询的方法,包括构建树、更新区间值及查询区间和或奇数个数等操作。适用于解决涉及大量区间修改与查询的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
LL x[10010],sum;
struct Node
{
    LL l,r,sum,flag,odd,even;
} node[100010];
void build_tree(LL l,LL r,LL n)//建树
{
    node[n].flag=0,node[n].l=l,node[n].r=r;
    if(l==r)
    {
        node[n].sum=x[l];
        if(x[l]&1ll)
            node[n].odd=1ll,node[n].even=0;
        else
            node[n].odd=0,node[n].even=1ll;
        return ;
    }
    LL mid=(l+r)>>1ll,L=n<<1ll,R=(n<<1ll)|1ll;
    build_tree(l,mid,L);
    build_tree(mid+1ll,r,R);
    node[n].sum=node[L].sum+node[R].sum;
    node[n].odd=node[L].odd+node[R].odd;
    node[n].even=node[L].even+node[R].even;
}
void pushdown(LL n)//下放
{
    if(node[n].flag!=0)
    {
        LL L=n<<1ll,R=(n<<1ll)|1ll;
        if(node[n].flag&1ll)
        {
            swap(node[L].odd,node[L].even);
            swap(node[R].odd,node[R].even);
        }
        node[L].flag+=node[n].flag,node[R].flag+=node[n].flag;//标记下放子节点
        node[L].sum+=(node[L].r-node[L].l+1ll)*node[n].flag;//更新子节点的值
        node[R].sum+=(node[R].r-node[R].l+1ll)*node[n].flag;//同上
        node[n].flag=0;//清空标记
    }
}
void update(LL l,LL r,LL add,LL n)//更新
{
    if(node[n].l==l&&node[n].r==r)
    {
        node[n].flag+=add;
        node[n].sum+=(node[n].r-node[n].l+1ll)*add;//更新当前节点值
        if(add&1ll) swap(node[n].odd,node[n].even);
        return ;
    }
    pushdown(n);
    LL mid=(node[n].l+node[n].r)>>1ll,L=n<<1ll,R=(n<<1ll)|1ll;
    if(r<=mid)
        update(l,r,add,L);
    else if(l>mid)
        update(l,r,add,R);
    else
        update(l,mid,add,L),update(mid+1ll,r,add,R);
    node[n].sum=node[L].sum+node[R].sum;
    node[n].odd=node[L].odd+node[R].odd;
    node[n].even=node[L].even+node[R].even;
}
void Search(LL l,LL r,LL n,char s)//查询
{
    if(node[n].l==l&&node[n].r==r)
    {
        if(s=='S') sum+=node[n].sum;
        else sum+=node[n].odd;
        return ;
    }
    pushdown(n);
    LL mid=(node[n].l+node[n].r)>>1ll,L=(n<<1ll),R=(n<<1ll)|1ll;
    if(r<=mid)
        Search(l,r,L,s);
    else if(l>mid)
        Search(l,r,R,s);
    else
        Search(l,mid,L,s),Search(mid+1ll,r,R,s);
}
int main()
{
    //freopen("Input.txt","r",stdin);
    //freopen("Output.txt","w",stdout);
    LL n,m;
    while(~scanf("%lld%lld",&n,&m))
    {
        memset(x,0,sizeof(x));
        memset(node,0,sizeof(node));
        for(int i=1; i<=n; i++)
            scanf("%lld",&x[i]);
        build_tree(1ll,n,1ll);
        while(m--)
        {
            char s[5];
            scanf("%s",s);
            if(s[0]=='A')
            {
                LL a,b,c;
                scanf("%lld%lld%lld",&a,&b,&c);
                update(a,b,c,1ll);
            }
            else
            {
                sum=0;
                LL a,b;
                scanf("%lld%lld",&a,&b);
                Search(a,b,1ll,s[0]);
                printf("%lld\n",sum);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值