萌新线段树20tps求调

P1253 扶苏的问题

FIRESTARS @ 2024-08-19 20:46:57

#include<bits/stdc++.h>
using namespace std;
int a[1000005],t[4000005],b[40000005],b2[40000005];
void build(int v,int tl,int tr)
{
    if(tl==tr)
    {
        t[v]=a[tl];
        return;
    }
    int tm=(tl+tr)/2;
    build(v*2,tl,tm);
    build(v*2+1,tm+1,tr);
    t[v]=max(t[v*2],t[v*2+1]);
}
int getmax(int v,int tl,int tr,int l,int r)
{
    if(tl>=l&&tr<=r)return t[v];
    int tm=(tl+tr)/2;
    int s1=0,s2=0;
    if(tm>=l)s1=getmax(v*2,tl,tm,l,r);
    if(tm<r)s2=getmax(v*2+1,tm+1,tr,l,r);
    return max(s1,s2);
}
void updata(int v,int tl,int tr,int l,int r,int x)
{
    if(l<=tl&&tr<=r)
    {
        t[v]=x;b[v]=1;
        return;
    }
    if(b[v]!=0)
    {
        t[v*2]=t[v];
        t[v*2+1]=t[v];
        b[v*2]=t[v];
        b[v*2+1]=t[v];
        b[v]=0;
    }
    int tm=(tl+tr)/2;
    if(tm>=l)updata(v*2,tl,tm,l,r,x);
    if(tm<r)updata(v*2+1,tm+1,tr,l,r,x);
    t[v]=max(t[v*2],t[v*2+1]);
    return;
}
void updata2(int v,int tl,int tr,int l,int r,int x)
{
    if(l<=tl&&tr<=r)
    {
        t[v]+=x;b2[v]+=x;
        return;
    }
    int tm=(tl+tr)/2;
    if(b2[v]!=0)
    {
        t[v*2]+=b2[v]*(tm-tl+1);
        t[v*2+1]+=b2[v]*(tr-tm);
        b2[v*2]+=b2[v];
        b2[v*2+1]+=b2[v];
        b2[v]=0;
    }
    if(tm>=l)updata2(v*2,tl,tm,l,r,x);
    if(tm<r)updata2(v*2+1,tm+1,tr,l,r,x);
    t[v]=max(t[v*2],t[v*2+1]);
    return;
}
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n,q;cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>a[i];
    build(1,1,n);
    while(q--)
    {
        int opt,l,r,x;
        cin>>opt;
        if(opt==1)
        {
            cin>>l>>r>>x;
            updata(1,1,n,l,r,x);
        }
        else if(opt==2)
        {
            cin>>l>>r>>x;
            a[l]=r;
            updata2(1,1,n,l,r,x);
        }
        else if(opt==3)
        {
            cin>>l>>r;
            cout<<getmax(1,1,n,l,r)<<'\n';
        }
    }
}

by zhizhenhuyuzhe @ 2024-08-19 20:47:14

dalao我教你


by fire_flies @ 2024-08-19 20:47:43

# gengen队,集合


by zhizhenyaohanyu @ 2024-08-19 20:47:54

看不懂


by Emil_ @ 2024-08-19 20:48:35

@FIRESTARS

不教renji


by Dejected @ 2024-08-19 20:48:39

@fire_flies 到!


by FIRESTARS @ 2024-08-19 20:48:41

@zhizhenhuyuzhe @fire_flies 我寻思着你们也不会线段树吧(

而且正版gengen跟我在一个学校集训,他今天把我骂了一通


by FIRESTARS @ 2024-08-19 20:49:08

耶帖子编号……


by fire_flies @ 2024-08-19 20:49:39

@FIRESTARS 错了


by FIRESTARS @ 2024-08-19 20:50:22

@Emil_ 我不是renji(恼

验证码wdqp


by zhizhenyaohanyu @ 2024-08-19 20:50:42

学过,不会@FIRESTARS


| 下一页