佬?邦邦,线段树0分

P3372 【模板】线段树 1

jntm233 @ 2024-04-23 16:46:35


#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define no cout<<"NO\n";
#define yes cout<<"YES\n";
#define A cout<<"Alice\n";
#define B cout<<"Bob\n";
#define cll(x) cout<<(x)<<endl;
#define ccl(x) cout<<(x)<<" ";
#define range(a) (a).begin(),(a).end()
#define fall(a) for(auto i:(a))cout<<i<<" ";cout<<"\n";
#define homo 1145141919810
#define vec vector
#define que queue
#define deq deque
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define _max *max_element
#define _min *min_element
#define man int main()
#define whatcanisay ll t;cin>>t;while(t--)solve();
#define manbaout return 0;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define lep(i,a,b) for(int i=(a);i>=(b);i--)
using ll=long long;
using ld=long double;
using plst=pair<ll,string>;
using pll=pair<ll,ll>;
ll n,m;
//ll fa[114514];
//ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll find(ll x){return x==fa[x]?x:fa[x]=find(fa[x]);}
//ll dp[1919810];
const int N=1000001;
const int inf=1e9;
const ll mod=998244353;
ll vis[214514];
ll a[N],s[N];
ll tree[N<<2],tag[N<<2];//tag代表这个地方的懒标记
inline void push_up(ll id){
    tree[id]=tree[id<<1]+tree[id<<1|1];
}
inline void modi(ll id,ll l,ll r,ll k){
    tree[id]+=k*(r-l+1);
    tag[id]+=k;
}
inline void build(ll id,ll l,ll r){
    tag[id]=0;
    if(l==r){
        tree[id]=a[l];
        return;
    }
//  else{
    ll mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    push_up(id);
//  }
}
inline void push_down(ll id,ll l,ll r){//向下传递懒标记
    ll mid=(l+r)>>1;
    modi(id<<1,l,mid,tag[id]);
    modi(id<<1|1,mid+1,r,tag[id]);
    tag[id]=0;
}
inline void update(ll id,ll ql,ll qr,ll l,ll r,ll k){
    //区间修改
    if(ql<=l&&r<=qr){
        //当前区间已经在查询的范围内
        tree[id]+=k*(r-l+1);
        tag[id]+=k;
        return;
    }
    ll mid=(l+r)>>1;
    push_down(id,l,r);
    if(qr<=mid)update(id<<1,ql,qr,l,mid,k);
    if(ql>mid)update(id<<1|1,ql,qr,mid+1,r,k);//修改的全部在右

    push_up(id);
}
//ll ans=0;
ll query(ll id,ll ql,ll qr,ll l,ll r){
    ll ans=0;
    if(ql<=l&&r<=qr)return tree[id];
    ll mid=(l+r)>>1;
    push_down(id,l,r);
    if(qr<=mid)ans+=query(id<<1,ql,qr,l,mid);
    if(ql>mid)ans+=query(id<<1|1,ql,qr,mid+1,r);//修改的全部在右

    return ans;
}
//void solve(){
//  
//}

man{ 

    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cin>>n>>m;
    rep(i,1,n)cin>>a[i];
    build(1,1,n);
    while(m--){
        ll op,l,r,k;cin>>op>>l>>r;
        if(op==1){
            cin>>k;
            update(1,l,r,1,n,k);//修改范围l,r;在1,n内找
        }else{
            //          rep(i,1,n)cout<<a[i]<<" ";cout<<"\n";
            cll(query(1,l,r,1,n))
        }
    }
}```

by L_zaa_L @ 2024-04-23 16:51:36

@jntm233 update和query里面应该是 if(ql<=mid)if(qr>mid)


by jntm233 @ 2024-04-23 17:04:47

@zaa xiexienishihaoren


|