线段树

线段树 :我们先复习一下与或非运算
与或非运算 位操作:

先看一下几个大大大佬写的博客(易懂):
https://www.cnblogs.com/xenny/p/9801703.html
https://www.cnblogs.com/jason2003/p/9676729.html
以为神犇写的博客 包揽各种各样经典的线段树的例题:https://blog.csdn.net/whereisherofrom/article/details/78969718

基本要求:

(一)建树

(二)pushdown(下放)

(三)pushup(回溯)

(四) 区间查询

(五)区间修改以及单点修改

一定要自己手动推一遍递归过程 仔细体会
不要一心着急看完

线段树的建立与区间的查询

区间和的查询:
在这里插入图片描述
区间最值的查询:在这里插入图片描述

sample input   区间和
6 
1 8 6 4 3 5
4
1 4
5 2
3 5
2 4
samp output
19
0
13
18
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f

using namespace std;
const int maxn=1e6;

struct node
{
    int lazy,sum;
} tree[maxn<<2];

int a[maxn];
void update(int nod)
{
    tree[nod].sum=tree[nod<<1].sum+tree[nod<<1|1].sum;
}
void build(int nod,int l,int r)
{
    tree[nod].lazy=0;
    if(l==r)
    {
        tree[nod].sum=a[l];
        return ;
    }
    else
    {
        int m=(l+r)>>1;
        build(nod<<1,l,m);
        build(nod<<1|1,m+1,r);
        update(nod);
    }
}
int query(int L,int R,int l,int r,int nod)
{
    if(L<=l&&r<=R)
        return tree[nod].sum;
    else if(L>r||R<l)
        return 0;
    else
    {
        int m=(l+r)>>1,sum=0;
        if(L<=m)
            sum+=query(L,R,l,m,nod<<1);
        if(R>m)
            sum+=query(L,R,m+1,r,nod<<1|1);
        return sum;
    }
}
int main()
{
    int i,q,L,R;
    int n;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&L,&R);
        int x=query(L,R,1,n,1);
        printf("%d\n",x);
    }
    return 0;
}

sample input//区间最小最大值的查询
6 3
1
7
3
4
2
5
1 5
4 6
2 2
sample output
6
3
0
#include<set>
#include<map>
#include<string.h>
#include<string>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<math.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f

using namespace std;
const int maxn=5e4+10;

struct node
{
    int lazy,mi,ma;
} tree[maxn<<2];

int a[maxn];
int n,q,L,R;
void update(int nod)
{
    tree[nod].ma=max(tree[nod<<1].ma,tree[nod<<1|1].ma);
    tree[nod].mi=min(tree[nod<<1].mi,tree[nod<<1|1].mi);
}
void build(int nod,int l,int r)
{
    tree[nod].lazy=0;
    if(l==r)
    {
        tree[nod].ma=a[l];
        tree[nod].mi=a[l];
        printf("tree[%d].mi=%d tree[%d].ma=%d\n",nod,tree[nod].mi,nod,tree[nod].ma);
        return ;
    }
    else
    {
        int m=(l+r)>>1;
        build(nod<<1,l,m);
        build(nod<<1|1,m+1,r);
        update(nod);
    }
    printf("tree[%d].mi=%d tree[%d].ma=%d\n",nod,tree[nod].mi,nod,tree[nod].ma);
}
int query1(int L,int R,int l,int r,int nod)
{
    if(L==l&&r==R)
        return tree[nod].ma;
    else
    {
        int m=(l+r)>>1;
        if(m<L&&R<=r)
            return query1(L,R,m+1,r,nod<<1|1);
        else if(l<=L&&R<=m)
            return query1(L,R,l,m,nod<<1);
        else
            return max(query1(L,m,l,m,nod<<1),query1(m+1,R,m+1,r,nod<<1|1));
    }
}
int query2(int L,int R,int l,int r,int nod)
{
    if(L==l&&r==R)
        return tree[nod].mi;
    else
    {
        int m=(l+r)>>1;
        if(m<L&&R<=r)
            return query2(L,R,m+1,r,nod<<1|1);
        else if(l<=L&&R<=m)
            return query2(L,R,l,m,nod<<1);
        else
            return min(query2(L,m,l,m,nod<<1),query2(m+1,R,m+1,r,nod<<1|1));
    }
}
int main()
{
    int i;
    scanf("%d%d",&n,&q);
    for(i=1; i<=n; i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    while(q--)
    {
        scanf("%d%d",&L,&R);
        int x=query1(L,R,1,n,1)-query2(L,R,1,n,1);
        printf("%d\n",x);
    }
    return 0;
}

Sample Input   //加法乘法混合
7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7

Sample Output
2
35
8

Hint
【样例说明】
初始时数列为(1,2,3,4,5,6,7)。
经过第1次操作后,数列为(1,10,15,20,25,6,7)。
对第2次操作,和为10+15+20=45,模43的结果是2。
经过第3次操作后,数列为(1,10,24,29,34,15,16}
对第4次操作,和为1+10+24=35,模43的结果是35。
对第5次操作,和为29+34+15+16=94,模43的结果是8。
#include<bits/stdc++.h>
#define LL long long

using namespace std;
const int maxn=2e5+20;

struct node
{
    LL add,modi;
    int l,r;
    LL v;
} tree[5*maxn];
int n,p;
int L,R;
LL a[maxn],num;
void pushup(int nod)
{
    tree[nod].v=(tree[nod<<1].v+tree[nod<<1|1].v)%p;
}
void build(int nod,int ll,int rr)
{
    tree[nod].l=ll;
    tree[nod].r=rr;
    tree[nod].add=0;
    tree[nod].modi=1;
    if(ll==rr)
    {
        tree[nod].v=a[ll];
        return ;
    }
    else
    {
        int m=(ll+rr)>>1;
        build(nod<<1,ll,m);
        build(nod<<1|1,m+1,rr);
        pushup(nod);
    }
}
void pushdown(int nod) //乘法优先
//1.(a+b)*c       2.a*c+b    两者区别就在对C的乘积  这样我们可以知道先乘再加
{//当父节点有懒标记的存在时 对于孩子的懒标记 我们应该对乘法懒标记优先
//再乘法当中 孩子的加法懒标记也应该做记录
    if(tree[nod].modi!=1)
    {
        tree[nod<<1].modi=(tree[nod<<1].modi*tree[nod].modi)%p;
        tree[nod<<1|1].modi=(tree[nod<<1|1].modi*tree[nod].modi)%p;
        tree[nod<<1].add=(tree[nod<<1].add*tree[nod].modi)%p;
        tree[nod<<1|1].add=(tree[nod<<1|1].add*tree[nod].modi)%p;
        tree[nod<<1].v=(tree[nod<<1].v*tree[nod].modi)%p;
        tree[nod<<1|1].v=(tree[nod<<1|1].v*tree[nod].modi)%p;
        tree[nod].modi=1;
    }
    if(tree[nod].add)
    {
        tree[nod<<1].add=(tree[nod<<1].add+tree[nod].add)%p;
        tree[nod<<1|1].add=(tree[nod<<1|1].add+tree[nod].add)%p;
        tree[nod<<1].v=(tree[nod<<1].v+((tree[nod<<1].r-tree[nod<<1].l+1)*tree[nod].add))%p;
        tree[nod<<1|1].v=(tree[nod<<1|1].v+((tree[nod<<1|1].r-tree[nod<<1|1].l+1)*tree[nod].add))%p;
        tree[nod].add=0;
    }
}
void chengfa(int nod)
{
    if(L<=tree[nod].l&&tree[nod].r<=R)
    {
        tree[nod].v=(tree[nod].v*num)%p;
        tree[nod].modi=(tree[nod].modi*num)%p;
        tree[nod].add=(tree[nod].add*num)%p;
        return ;
    }
    pushdown(nod);
    int m=(tree[nod].l+tree[nod].r)>>1;
    if(L<=m)
        chengfa(nod<<1);
    if(R>m)
        chengfa(nod<<1|1);
    pushup(nod);
}
void jiafa(int nod)
{
    if(L<=tree[nod].l&&tree[nod].r<=R)
    {
        tree[nod].v=(tree[nod].v+num*(tree[nod].r-tree[nod].l+1))%p;
        tree[nod].add=(tree[nod].add+num)%p;
        return ;
    }
    pushdown(nod);
    int m=(tree[nod].l+tree[nod].r)>>1;
    if(L<=m)
        jiafa(nod<<1);
    if(R>m)
        jiafa(nod<<1|1);
    pushup(nod);
}
LL query(int nod)
{
    if(L<=tree[nod].l&&tree[nod].r<=R)
        return tree[nod].v;
    pushdown(nod);
    int m=(tree[nod].l+tree[nod].r)>>1;
    LL ans=0;
    if(L<=m)
        ans=(ans+query(nod<<1))%p;
    if(R>m)
        ans=(ans+query(nod<<1|1))%p;
    return ans;
}
int main()
{
    int i;
    while(scanf("%d%d",&n,&p)!=EOF)
    {
        for(i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        build(1,1,n);
        int ask;
        scanf("%d",&ask);
        while(ask--)
        {
            int x;
            scanf("%d%d%d",&x,&L,&R);
            if(x==1)
            {
                scanf("%lld",&num);
                chengfa(1);
            }
            else if(x==2)
            {
                scanf("%lld",&num);
                jiafa(1);
            }
            else
                printf("%lld\n",query(1)%p);
        }
    }
    return 0;
}

线段树区间改变以及查询

其实就是给我们需要改变的区间做出标记 当我们找到被打上懒惰记号的标记时我们就一直访问它的孩子并打上相应的懒惰标记 在查询的时候 我们只需要下放懒标记的区间进行求和就可以了

#include<set>
#include<map>
#include<string.h>
#include<string>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<math.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f

using namespace std;
const int maxn=1e5+10;

struct node
{
    ll lazy,sum;
} tree[maxn<<2];

ll a[maxn];
void update(int nod)
{
    tree[nod].sum=tree[nod<<1].sum+tree[nod<<1|1].sum;
}
void build(int nod,int l,int r)
{
    tree[nod].lazy=0;
    if(l==r)
    {
        tree[nod].sum=a[l];
        return ;
    }
    int m=(l+r)>>1;
    build(nod<<1,l,m);
    build(nod<<1|1,m+1,r);
    update(nod);
}
void pushdown(int l,int r,int nod)
{
    if(tree[nod].lazy==0)
        return ;
    else
    {
        int m=(l+r)>>1;
        tree[nod<<1].sum+=tree[nod].lazy*(m-l+1);//因其父亲节点被打上了懒惰标记 去更新左孩子区间的值
        tree[nod<<1|1].sum+=tree[nod].lazy*(r-m);//更新右孩子区间的值
        tree[nod<<1].lazy+=tree[nod].lazy;//分别对其左右孩子打上懒惰标记
        tree[nod<<1|1].lazy+=tree[nod].lazy;
        tree[nod].lazy=0;//其父亲节点的左右孩子已经更新过了 将自身懒惰标记取零 等待下一次的区间点更新和改变
    }
}
void change(int L,int R,int l,int r,int nod,ll num)
{
    if(L<=l&&r<=R)
    {
        tree[nod].sum+=num*(r-l+1);//区间内的每个值都改变 那么区间的和肯定改变为num*区间长度
        tree[nod].lazy+=num;
        return ;
    }
    pushdown(l,r,nod);//找到了更新区间并对其所在的孩子进行标记和更新
    int m=(l+r)>>1;
    if(R>m)//访问右孩子
        change(L,R,m+1,r,nod<<1|1,num);
    if(L<=m)//访问左孩子
        change(L,R,l,m,nod<<1,num);
    update(nod);//重新更新父节点上的和
}
ll query(int L,int R,int l,int r,int nod)
{
    if(L<=l&&r<=R)
        return tree[nod].sum;
    pushdown(l,r,nod);
    int m=(l+r)>>1;
    ll ans=0;
    if(R>m)
        ans+=query(L,R,m+1,r,nod<<1|1);
    if(L<=m)
        ans+=query(L,R,l,m,nod<<1);
    return ans;
}
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    build(1,1,n);
    //for(i=1;i<=13;i++)
    //printf("tree[%d].sum=%lld\n",i,tree[i].sum);
    while(q--)
    {
        char ss;
        int L,R;
        if(L>R)
            swap(R,L);
        ll num;
        cin>>ss;//刚开始的时候一直因为字符的输入导致结果查询次数有问题 换成cin读入就行了
        if(ss=='Q')
        {
            scanf("%d %d",&L,&R);
            printf("%lld\n",query(L,R,1,n,1));
        }
        else if(ss=='C')
        {
            scanf("%d %d %lld",&L,&R,&num);
            change(L,R,1,n,1,num);
        }
    }
    return 0;
}

C - I Hate It:https://vjudge.net/contest/313151#problem/C

#include<set>
#include<map>
#include<string.h>
#include<string>
#include<vector>
#include<iostream>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f

using namespace std;
const int maxn=2e5+10;

struct node
{
    int l,r,mmax;
} tree[3*maxn];
int a[maxn];
int n,q,L,R;
void update(int nod)
{
    tree[nod].mmax=max(tree[nod<<1].mmax,tree[nod<<1|1].mmax);
}
void build(int nod,int ll,int rr)
{
    tree[nod].l=ll;
    tree[nod].r=rr;
    if(ll==rr)
    {
        tree[nod].mmax=a[ll];
        return ;
    }
    else
    {
        int m=(ll+rr)>>1;
        build(nod<<1|1,m+1,rr);
        build(nod<<1,ll,m);
        update(nod);
    }
}
void change(int nod,int x,int num)
{
    if(tree[nod].l==x&&tree[nod].r==x)
    {
        tree[nod].mmax=num;
        return ;
    }
    else
    {
        int m=(tree[nod].l+tree[nod].r)>>1;
        if(x<=m)
            change(nod<<1,x,num);
        if(m<x)
            change(nod<<1|1,x,num);
        update(nod);
    }
}
int query(int nod)
{
    if(L<=tree[nod].l&&tree[nod].r<=R)
        return tree[nod].mmax;
    else
    {
        int m=(tree[nod].l+tree[nod].r)>>1;
        if(R<=m)
            return query(nod<<1);
        else if(m<L)
            return query(nod<<1|1);
        else
            return max(query(nod<<1),query(nod<<1|1));
    }
}
int main()
{
    int i;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        for(i=1; i<=n; i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        while(q--)
        {
            char ask[2];
            scanf("%s%d%d",ask,&L,&R);
            if(ask[0]=='Q')
                printf("%d\n",query(1));
            if(ask[0]=='U')
                change(1,L,R);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值