procon

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub mugen1337/procon

:heavy_check_mark: test/yuki4374.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/4374"

#include "../template.hpp"

#include "../SegmentTree/LazySegmentTree.hpp"

using tp=tuple<ll,ll,ll>;

tp f(tp a,tp b){
    return tp(get<0>(a)+get<0>(b),get<1>(a)+get<1>(b),get<2>(a)+get<2>(b));
}

tp g(tp a,ll b){
    ll x2,x,cnt;
    tie(x2,x,cnt)=a;
    return tp(x2+b*b*cnt+2*x*b,x+cnt*b,cnt);
}

ll h(ll a,ll b){
    return a+b;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n;cin>>n;
    LazySegmentTree<tp,ll> seg(n,f,g,h,tp(0,0,0),0);
    rep(i,n){
        ll a;cin>>a;
        seg.set(i,tp(a*a,a,1));
    }
    seg.build();
    int q;cin>>q;
    while(q--){
        int t;cin>>t;
        if(t==1){
            int l,r;ll x;cin>>l>>r>>x;l--;
            seg.update(l,r,x);
        }else{
            int l,r;cin>>l>>r;l--;
            cout<<(get<0>(seg.query(l,r)))<<endl;
        }
    }

    return 0;
}
#line 1 "test/yuki4374.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/4374"

#line 1 "template.hpp"
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

#line 4 "test/yuki4374.test.cpp"

#line 1 "SegmentTree/LazySegmentTree.hpp"
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
    using F=function<Monoid(Monoid,Monoid)>;
    using G=function<Monoid(Monoid,OperatorMonoid)>;
    using H=function<OperatorMonoid(OperatorMonoid,OperatorMonoid)>;
 
    private:
 
    int sz,height;
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    // propagate lazy value -> data (node k)
    inline void propagate(int k){
        if(lazy[k]!=OM0){
            if(k<sz){
                lazy[2*k+0]=h(lazy[2*k+0],lazy[k]);
                lazy[2*k+1]=h(lazy[2*k+1],lazy[k]);
            }
            data[k]=g(data[k],lazy[k]);
            lazy[k]=OM0;
        }
    }
 
    void update(int a,int b,const OperatorMonoid &x,int k,int l,int r){
        propagate(k);
        if(b<=l or r<=a) return ;
        if(a<=l and r<=b){
            lazy[k]=h(lazy[k],x);
            propagate(k);
        }else{
            update(a,b,x,2*k,l,(l+r)/2);
            update(a,b,x,2*k+1,(l+r)/2,r);
            data[k]=f(data[2*k],data[2*k+1]);
        }
    }
 
    Monoid query(int a,int b,int k,int l,int r){
        if(b<=l or r<=a) return M1;
 
        propagate(k);
        if(a<=l and r<=b) return data[k];
 
        Monoid L=query(a,b,2*k+0,l,(l+r)/2);
        Monoid R=query(a,b,2*k+1,(l+r)/2,r);
        return f(L,R);
    }
 
    public:
 
    const F f;
    const G g;
    const H h;
    const Monoid M1;
    const OperatorMonoid OM0;
 
    LazySegmentTree(int n,const F f,const G g,const H h,const Monoid &M1,const OperatorMonoid OM0)
    : f(f),g(g),h(h),M1(M1),OM0(OM0) {
        sz=1;height=0;
        while(sz<n) sz<<=1,height++;
        data.assign(2*sz,M1);lazy.assign(2*sz,OM0);
    }
 
    void set(int k,const Monoid &x){
        data[k+sz]=x;
    }
    void build(){
        for(int k=sz-1;k>0;k--) data[k]=f(data[2*k+0],data[2*k+1]);
    }
    void update(int a,int b,const OperatorMonoid &x){
        update(a,b,x,1,0,sz);
    }
    Monoid query(int a,int b){
        return query(a,b,1,0,sz);
    }
    Monoid operator[](const int &k){
        return query(k,k+1);
    }
}; 

// // range set range min
// using M=ll;
// using OM=ll;
// const M M1=LINF;
// const OM OM0=-LINF;
// M segf(M a,M b){
//     return (a<b?a:b);
// }
// M segg(M a,OM b){
//     return (b==OM0?a:b);
// }
// OM segh(OM a,OM b){
//     return (b==OM0?a:b);
// }
 
// // range set range max
// using M=ll;
// using OM=ll;
// const M M1=-LINF;
// const OM OM0=-LINF;
// M segf(M a,M b){
//     return (a>b?a:b);
// }
// M segg(M a,OM b){
//     return (b==OM0?a:b);
// }
// OM segh(OM a,OM b){
//     return (b==OM0?a:b);
// }
 
// // range add range min
// using M=ll;
// using OM=ll;
// const M M1=LINF;
// const OM OM0=0;
// M segf(M a,M b){
//     return (a<b?a:b);
// }
// M segg(M a,OM b){
//     return a+b;
// }
// OM segh(OM a,OM b){
//     return a+b;
// }
 
// // range add range max
// using M=ll;
// using OM=ll;
// const M M1=-LINF;
// const OM OM0=0;
// M segf(M a,M b){
//     return (a>b?a:b);
// }
// M segg(M a,OM b){
//     return a+b;
// }
// OM segh(OM a,OM b){
//     return a+b;
// }
 
// // range set range sum (sum, count)
// using M=pair<ll,ll>;
// using OM=ll;
// const M M1=M(0,0);
// const OM OM0=-LINF;
// M segf(M a,M b){
//     return M(a.first+b.first,a.second+b.second);
// }
// M segg(M a,OM b){
//     return M(a.second*b,a.second);
// }
// OM segh(OM a,OM b){
//     return (b==OM0?a:b);
// }

// // range add range sum (sum, count)
// using M=pair<ll,ll>;
// using OM=ll;
// const M M1=M(0,0);
// const OM OM0=0;
// M segf(M a,M b){
//     return M(a.first+b.first,a.second+b.second);
// }
// M segg(M a,OM b){
//     return M(a.first+a.second*b,a.second);
// }
// OM segh(OM a,OM b){
//     return a+b;
// }
#line 6 "test/yuki4374.test.cpp"

using tp=tuple<ll,ll,ll>;

tp f(tp a,tp b){
    return tp(get<0>(a)+get<0>(b),get<1>(a)+get<1>(b),get<2>(a)+get<2>(b));
}

tp g(tp a,ll b){
    ll x2,x,cnt;
    tie(x2,x,cnt)=a;
    return tp(x2+b*b*cnt+2*x*b,x+cnt*b,cnt);
}

ll h(ll a,ll b){
    return a+b;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n;cin>>n;
    LazySegmentTree<tp,ll> seg(n,f,g,h,tp(0,0,0),0);
    rep(i,n){
        ll a;cin>>a;
        seg.set(i,tp(a*a,a,1));
    }
    seg.build();
    int q;cin>>q;
    while(q--){
        int t;cin>>t;
        if(t==1){
            int l,r;ll x;cin>>l>>r>>x;l--;
            seg.update(l,r,x);
        }else{
            int l,r;cin>>l>>r;l--;
            cout<<(get<0>(seg.query(l,r)))<<endl;
        }
    }

    return 0;
}
Back to top page