Problem:
Copy and Sum
There are two sequences, A and B, of length $N$. Denote the $i$-th elements of A and B by $a_i$ and $b_i$, respectively. $i$ is an integer between $1$ and $N$. Perform the following operations on these sequences of numbers.
copy($x$, $y$, $z$) : replace $a_x, a_{x+1}, ..., a_{x+z-1}$ with $b_y, b_{y+1}, ..., b_{y+z-1}$ respectively.
sum(p, q) : output the value of $a_p + a_{p+1} + ... + a_q$.
Given a sequence of numbers A and B and some operations, write a program that outputs the result of the sum operation.
Input
The input is given in the following form.
$N$ $M$
$a_1$ $a_2$ ... $a_N$
$b_1$ $b_2$ ... $b_N$
$op_1$
$op_2$
:
$op_M$
In the first line, the length of the sequence $N$ ($1 \leq N \leq 200,000$) and the number of operations $M$ ($1 \leq M \leq 200,000$) are given. In the next line, the $i$-th element $a_i$ ($0 \leq a_i \leq 1,000,000,000$) of sequence A is given as integers. In the next line, the $i$-th element $b_i$ ($0 \leq b_i \leq 1,000,000,000$) of sequence B is given as integers. In the following $M$ lines, the information $op_i$ indicating the operation is given. Each $op_i$ is given in one of the following forms.
0 $x$ $y$ $z$
or
1 $p$ $q$
If the first character is 0, it indicates that the operation copy($x$, $y$, $z$) is to be performed. Note that $1 \leq x,y \leq N, 1 \leq z,$ max$(x,y)+z-1 \leq N$, where max($x$,$y$) is the larger of $x$ and $y$. If the first letter is 1, it indicates that the operation sum($p$, $q$) is to be performed. Note that $1 \leq p \leq q \leq N$. The operation sum is given to the input one or more times.
Output
For each sum operation given, output the result of the sum in a line.
Sample Input and Output
Sample Input 1
5 5
1 3 5 7 9
2 4 6 8 10
1 1 3
1 4 5
0 4 1 2
1 1 3
1 4 5
Sample Output 1
9
16
9
6
Sample Input 2
6 5
1 2 3 4 5 6
2 4 8 16 32 64
1 1 6
0 1 4 3
1 1 6
0 3 1 3
1 1 6
Sample Output 2
21
127
68

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id>
struct LazySegTree {
	int N, height, size;
	vector<S> data;
	vector<F> lazy;
	
	LazySegTree(const vector<S> &v) : N(v.size()) {
		size = 1, height = 0;
		while (size < N) size <<= 1, ++height;
		data = vector<S>(2*size, e());
		lazy = vector<F>(2*size, id());
		for (int i = 0; i < N; ++i) data[size + i] = v[i];
		for (int i = size - 1; i >= 1; --i) update(i);
	}
	
	void update(int i) {
		data[i] = op(data[2*i], data[2*i + 1]);
	}
	
	void all_apply(int i, F f) {
		data[i] = mapping(f, data[i]);
		if (i < size) lazy[i] = composition(f, lazy[i]);
	}
	
	void push(int i) {
		all_apply(2*i, lazy[i]);
		all_apply(2*i + 1, lazy[i]);
		lazy[i] = id();
	}
	
	void set(int p, S x) {
		p += size;
		for (int i = height; i >= 1; --i) push(p >> i);
		data[p] = x;
		for (int i = 1; i <= height; ++i) update(p >> i);
	}
	
	S get(int p) {
		p += size;
		for (int i = height; i >= 1; --i) push(p >> i);
		return data[p];
	}
	
	S prod(int l, int r) {
		if (l == r) return e();
		l += size, r += size;
		for (int i = height; i >= 1; --i) {
			if (((l >> i) << i) != l) push(l >> i);
			if (((r >> i) << i) != r) push((r - 1) >> i);
		}
		S vl = e(), vr = e();
		for (int a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1) vl = op(vl, data[a++]);
			if (b & 1) vr = op(data[--b], vr);
		}
		return op(vl, vr);
	}
	
	void apply(int l, int r, F f) {
		if (l == r) return;
		l += size, r += size;
		for (int i = height; i >= 1; --i) {
			if (((l >> i) << i) != l) push(l >> i);
			if (((r >> i) << i) != r) push((r - 1) >> i);
		}
		for (int a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1) all_apply(a++, f);
			if (b & 1) all_apply(--b, f);
		}
		for (int i = 1; i <= height; ++i) {
			if (((l >> i) << i) != l) update(l >> i);
			if (((r >> i) << i) != r) update((r - 1) >> i);
		}
	}
};

int N, M;
vector<ll> A(200050), B(200050);

struct S {
	ll val;
	int size, pos;
};
struct F {
	int start, bias;
};

S op(S a, S b) { return {a.val + b.val, a.size + b.size, min(a.pos, b.pos)}; }
S e() { return {0, 0, (int)1e9}; }
S mapping(F f, S x) {
	if (f.start != -1) {
		int l = x.pos - f.start + f.bias;
		int r = l + x.size - 1;
		if (l == 0) x.val = B[r];
		else x.val = B[r] - B[l - 1];
	}
	return x;
}
F composition(F f, F g) { return (f.start == -1 ? g : f); }
F id() { return {-1, -1}; }

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	cin >> N >> M;
	rep(i, 0, N) cin >> A[i];
	rep(i, 0, N) cin >> B[i];
	
	rep(i, 1, N) B[i] += B[i - 1];
	
	vector<S> v(N);
	rep(i, 0, N) v[i] = {A[i], 1, i};
	LazySegTree<S, op, e, F, mapping, composition, id> seg(v);
	
	rep(query, 0, M) {
		int ord;
		cin >> ord;
		
		if (ord == 0) {
			int x, y, z;
			cin >> x >> y >> z;
			--x, --y;
			seg.apply(x, x + z, {x, y});
		} else {
			int p, q;
			cin >> p >> q;
			--p, --q;
			cout << seg.prod(p, q + 1).val << '\n';
		}
	}
}
accuracy: 1
time_ms: 160
memory_kb: 21540
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define ld long double
#define rep(i,N) for(int i=0;i<(int)N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back

class seg{
    public:
    int N;
    vector<ll>data;
    vector<pair<int,int>>lazy;
    vector<ll>sum;
    void init(int n,vector<ll>A,vector<ll>B){
        N = 1;
        while(N<n)N*=2;
        data.assign(N*2,0);
        lazy.assign(N*2,{-1,-1});
        sum={0};
        rep(i,N)sum.pb(sum[i]+B[i]);
        rep(i,N)data[N+i-1]=A[i];
        for(int i = N-2;i>=0;i--)data[i]=data[i*2+1]+data[i*2+2];
    }
    void eval(int k){
        if(lazy[k].fi==-1)return;
        data[k]=sum[lazy[k].se]-sum[lazy[k].fi];
        if(lazy[k].se-lazy[k].fi>1){
            lazy[k*2+1]={lazy[k].fi,(lazy[k].se+lazy[k].fi)/2};
            lazy[k*2+2]={(lazy[k].se+lazy[k].fi)/2,lazy[k].se};
        }
        lazy[k]={-1,-1};
    }
    ll Sum(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0)r=N;
        eval(k);
        if(b<=l||r<=a)return 0;
        if(a<=l&&r<=b)return data[k];
        return Sum(a,b,k*2+1,l,(l+r)/2)+Sum(a,b,k*2+2,(l+r)/2,r);
    }
    void update(int a,int b,int x,int y,int k=0,int l=0,int r=-1){
        if(r<0)r=N;
        eval(k);
        if(b<=l||r<=a)return;
        if(a<=l&&r<=b){
            lazy[k]={x+(l-a),x+(r-a)};
            eval(k);
        }
        else {
            update(a,b,x,y,k*2+1,l,(l+r)/2);
            update(a,b,x,y,k*2+2,(l+r)/2,r);
            data[k]=data[k*2+1]+data[k*2+2];
        }
    }
};

int main(){
    int N,M;cin>>N>>M;
    vector<ll>A(N),B(N);
    rep(i,N)cin>>A[i];
    rep(i,N)cin>>B[i];
    seg seg;
    seg.init(N,A,B);
    rep(m,M){
        int t;cin>>t;
        if(t==1){
            int p,q;cin>>p>>q;p--;
            cout<<seg.Sum(p,q)<<endl;
        }
        else {
            int x,y,z;cin>>x>>y>>z;
            x--;y--;
            seg.update(x,x+z,y,y+z);
        }
    }
}
accuracy: 1
time_ms: 350
memory_kb: 23324
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int N,M;
ll a[200009],b[200009];
struct S{
    ll value;
    int start,end;
};
using F=ll;
const F ID=8e18;
S e(){//Sの単位元
    S res={0,-1,-1};
    return res;
}
F id(){//Fの単位元
    return ID;
}
S op(S a,S b){//Sの結合
    if(a.start==-1) return b;
    if(b.start==-1) return a;
    S res;
    res.value=a.value+b.value;
    res.start=a.start;
    res.end=b.end;
    return res;
}
S mapping(F f,S x){//Sに遅延伝播をかける
    if(f==ID) return x;
    if(x.start==-1) return x;
    int s=x.start+f,t=x.end+f;
    S res;
    res.value=b[t]-b[s-1];
    res.start=x.start;
    res.end=x.end;
    return res;
}
F composition(F f,F g){//lazy同士の合成(gにfの操作をする)
    if(f==ID) return g;
    return f;
}
class lazysegtree{
    public:
    S dat[800009];
    F lazy[800009];
    ll siz;
    lazysegtree(ll N){
        siz=1;
        while(siz<N) siz*=2;
        for(ll i=1;i<siz*2;i++){
            dat[i]=e();
            lazy[i]=id();
        }
    }

    S prod(int l,int r,int a,int b,int u){
        if(b<=l || r<=a)return e();
        if(l<=a && b<=r){
            return dat[u];
        }
        int m=(a+b)/2;
        S L=mapping(lazy[u],prod(l,r,a,m,u*2));
        S R=mapping(lazy[u],prod(l,r,m,b,u*2+1));
        return op(L,R);
    }
    S prod(int l,int r){
        return prod(l,r,1,siz+1,1);
    }
    S all_prod(){
        return prod(1,siz+1,1,siz+1,1);
    }
    S get(int p){
        return prod(p,p+1,1,siz+1,1);
    }
    void apply(int l,int r,int a,int b,int u,F f){
        if(b<=l || r<=a) return;
        if(l<=a && b<=r){
            lazy[u]=composition(f,lazy[u]);
            dat[u]=mapping(f,dat[u]);
            return;
        }
        lazy[u*2]=composition(lazy[u],lazy[u*2]);
        lazy[u*2+1]=composition(lazy[u],lazy[u*2+1]);
        dat[u*2]=mapping(lazy[u],dat[u*2]);
        dat[u*2+1]=mapping(lazy[u],dat[u*2+1]);
        lazy[u]=id();
        int m=(a+b)/2;
        apply(l,r,a,m,u*2,f);
        apply(l,r,m,b,u*2+1,f);
        S L=mapping(lazy[u],dat[u*2]);
        S R=mapping(lazy[u],dat[u*2+1]);
        dat[u]=op(L,R);
        return;
    }

    void apply(int p,F f){
        apply(p,p+1,1,siz+1,1,f);
    }
    void apply(int l,int r,F f){
        apply(l,r,1,siz+1,1,f);
    }
    void set(int p,S x){
        p=p+siz-1;
        dat[p]=x;
        while(p>=2){
            p/=2;
            dat[p]=op(dat[p*2],dat[p*2+1]);
        }
        return;
    }
};
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    cin>>N>>M;
    for(int i=1;i<=N;i++) cin>>a[i];
    for(int i=1;i<=N;i++) cin>>b[i];
    b[0]=0;
    for(int i=1;i<=N;i++) b[i]+=b[i-1];
    lazysegtree seg(N);
    for(int i=1;i<=N;i++){
        S d={a[i],i,i};
        seg.set(i,d);
    }
    for(int z=1;z<=M;z++){
        int query;
        cin>>query;
        if(query==0){
            int x,y,z;
            cin>>x>>y>>z;
            ll cnt=y-x;
            seg.apply(x,x+z,cnt);
        }
        if(query==1){
            int p,q;
            cin>>p>>q;
            cout<<seg.prod(p,q+1).value<<"\n";
        }
    }
}
accuracy: 1
time_ms: 160
memory_kb: 22900
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ld=long double;
#define fi first
#define se second
#define i128 __int128_t
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define si(a) (long)(a.size())
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}
const int INF=1e9+100;
const long INFL=3e18+100;
struct _ {
   _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;


using U = uint64_t;
template<class S, S (*op)(S, S), S (*e)(), class F, S (*mpp)(F, S), F (*cmpo)(F, F), F (*id)()> struct lazy_segtree {
    lazy_segtree() : lazy_segtree(0) {}
    explicit lazy_segtree(int n) : lazy_segtree(vector<S>(n, e())) {}
    explicit lazy_segtree(const vector<S>& v) : n(si(v)) {
        s = __bit_ceil(U(n));
        log = __countr_zero(U(s));
        d = vector<S>(2 * s, e());
        lz = vector<F>(s, id());
        rep(i, n) d[s + i] = v[i];
        for(int i=s; --i;) update(i);
    }
    void set(int p, S x) {
        p += s;
        PUSH(p);
        d[p] = x;
        for(int i=1;i<=log;i++) update(p >> i);
    }
    S get(int p) {
        p += s;
        PUSH(p);
        return d[p];
    }
    S prod(int l, int r) {
        if(l == r) return e();
        l += s, r += s;
        for(int i=log+1;--i;) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        S sml = e(), smr = e();
        while(l < r) {
            if(l & 1) sml = op(sml, d[l++]);
            if(r & 1) smr = op(d[--r], smr);
            l >>= 1, r >>= 1;
        }
        return op(sml, smr);
    }
    S all_prod() {
        return d[1];
    }
    void apply(int p, F f) {
        // assert(0 <= p && p < n);
        p += s;
        PUSH(p);
        d[p] = mpp(f, d[p]);
        for(int i=1;i<=log;i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        // assert(0 <= l && l <= r && r <= _n);
        if(l == r) return;
        l += s, r += s;

        for(int i=log+1;--i;) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        int ml = l, mr = r;
        while(l < r) {
            if(l & 1) all_apply(l++, f);
            if(r & 1) all_apply(--r, f);
            l >>= 1, r >>= 1;
        }
        l = ml, r = mr;
        for(int i=1;i<=log;i++) {
            if(((l >> i) << i) != l) update(l >> i);
            if(((r >> i) << i) != r) update((r - 1) >> i);
        }
    }
    template<class G> int max_right(int l, G g) {
        assert(g(e()));
        if(l == n) return n;
        l += s;
        PUSH(l);
        S sm = e();
        do {
            while(~l & 1) l >>= 1;
            if(!g(op(sm, d[l]))) {
                while(l < s) {
                    push(l);
                    l <<= 1;
                    if(g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - s;
            }
            sm = op(sm, d[l]);
            l++;
        } while((l & -l) != l);
        return n;
    }
    template<class G> int min_left(int r, G g) {
        assert(g(e()));
        if(r == 0) return 0;
        r += s;
        PUSH(r - 1);
        S sm = e();
        do {
            r--;
            while(r > 1 && r & 1) r >>= 1;
            if(!g(op(d[r], sm))) {
                while(r < s) {
                    push(r);
                    r = (2 * r + 1);
                    if(g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - s;
            }
            sm = op(d[r], sm);
        } while((r & -r) != r);
        return 0;
    }
    S operator[](int k) {
        return get(k);
    }
    int len() {
        return n;
    }

private:
    int n, s, log;
    vector<S> d;
    vector<F> lz;
    void update(int k) {
        d[k] = op(d[2 * k], d[2 * k + 1]);
    }
    void all_apply(int k, F f) {
        d[k] = mpp(f, d[k]);
        if(k < s) lz[k] = cmpo(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
    void PUSH(int k) {
        for(int i=log+1;--i;) push(k >> i);
    }
};

long N,M,A[202020],B[202020];


struct S{
    long x;
    int l,r;
};
S e(){
    return {0,(int)1e9,-1};
}
S op(S l, S r){
    S s=e();
    s.l=min(l.l,r.l);
    s.r=max(l.r,r.r);
    s.x=l.x+r.x;
    return s;
}
struct F{
    int x,y,z;
};
S mapping(F f, S s){
    if(f.x==-1) return s;
    int b=f.y+s.l-f.x;
    s.x=B[b+(s.r-s.l)]-B[b];
    return s;
}
F composition(F f, F g){
    return (f.x==-1?g:f);
}
F id(){
    return {-1,-1,-1};
}

int main(){
    cin>>N>>M;
    rep(i,N)cin>>A[i];
    rep(i,N)cin>>B[i+1],B[i+1]+=B[i];
    lazy_segtree<S,op,e,F,mapping,composition,id> seg(N);
    rep(i,N) seg.set(i, {A[i], i, i+1});
    while(M--){
        int o;
        cin>>o;
        if(o==0){
            int x,y,z;
            cin>>x>>y>>z;
            x--;y--;
            seg.apply(x,x+z,{x,y,z});
        }
        else{
            int p,q;
            cin>>p>>q;
            p--;
            S s=seg.prod(p,q);
            cout<<s.x<<"\n";
        }
    }

}
accuracy: 1
time_ms: 140
memory_kb: 20892
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
constexpr int bs = 300;
using ll = long long;
struct solver{
    vector<int> a, b;
    solver(vector<int> a_, vector<int> b_){
        a = a_;
        b = b_;
    }
    void q1(int x, int y, int z){
        return;
        for(int i = 0; i < z; i++){
            a[x + i] = b[y + i];
        }
    }
    void q2(int x, int y, ll ans){
        return;
        ll ret = 0;
        for(int i = x; i < y; i++) ret += a[i];
        if(ret != ans){
            cerr << x << " " << y << " " << ans << " " << ret << '\n';
        }
    }
};
int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n + bs), b(n + bs);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<ll> asub(n / bs + 1);
    vector<int> aind(n / bs + 1, -1);
    for(int i = 0; i < n; i++) asub[i / bs] += a[i];
    vector<ll> bsum(n + 1);
    for(int i = 0; i < n; i++) bsum[i + 1] = bsum[i] + b[i];
    solver s(a, b);
    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            if(x % bs != 0){
                asub[x / bs] = 0;
            }
            if((x + z) % bs != 0){
                asub[(x + z) / bs] = 0;
            }
            for(int i = x; i < x + z;){
                if(i % bs == 0 && i + bs <= x + z){
                    aind[i / bs] = y + (i - x);
                    asub[i / bs] = bsum[y + i - x + bs] - bsum[y + i - x];
                    i += bs;
                }else{
                    a[i] = b[y + i - x];
                    asub[i / bs] += a[i];
                    i++;
                }
            }
            int u2 = -1;
            if(x % bs != 0){
                int u1 = aind[x / bs];
                u2 = u1;
                aind[x / bs] = -1;
                for(int i = x / bs * bs; i < x; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - x / bs * bs];
                    }
                    asub[i / bs] += a[i];
                }
            }
            if((x + z) % bs != 0){
                int u1 = aind[(x + z) / bs];
                if((x + z) / bs == x / bs) u1 = max(u1, u2);
                aind[(x + z) / bs] = -1;
                //cerr << u1 << endl;
                for(int i = x + z; i < ((x + z) / bs + 1) * bs; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i % bs];
                    }
                    //cerr << a[i] << " ";
                    asub[i / bs] += a[i];
                }
                //cerr << endl;
            }
            s.q1(x, y, z);
        }else{
            int x, y;
            cin >> x >> y;
            x--;
            ll ans = 0;
            for(int i = x; i < y;){
                if(i % bs == 0 && i + bs <= y){
                    ans += asub[i / bs];
                    i += bs;
                }else{
                    if(aind[i / bs] != -1) a[i] = b[aind[i / bs] + i % bs];
                    ans += a[i];
                    i++;
                }
            }
            cout << ans << '\n';
            s.q2(x, y, ans);
        }
    }
}
accuracy: 1
time_ms: 640
memory_kb: 9416
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
constexpr int bs = 499;
using ll = long long;
struct solver{
    vector<int> a, b;
    solver(vector<int> a_, vector<int> b_){
        a = a_;
        b = b_;
    }
    void q1(int x, int y, int z){
        return;
        for(int i = 0; i < z; i++){
            a[x + i] = b[y + i];
        }
    }
    void q2(int x, int y, ll ans){
        return;
        ll ret = 0;
        for(int i = x; i < y; i++) ret += a[i];
        if(ret != ans){
            cerr << x << " " << y << " " << ans << " " << ret << '\n';
        }
    }
};
int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n + bs), b(n + bs);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<ll> asub(n / bs + 1);
    vector<int> aind(n / bs + 1, -1);
    for(int i = 0; i < n; i++) asub[i / bs] += a[i];
    vector<ll> bsum(n + 1);
    for(int i = 0; i < n; i++) bsum[i + 1] = bsum[i] + b[i];
    solver s(a, b);
    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            if(x % bs != 0){
                asub[x / bs] = 0;
            }
            if((x + z) % bs != 0){
                asub[(x + z) / bs] = 0;
            }
            for(int i = x; i < x + z;){
                if(i % bs == 0 && i + bs <= x + z){
                    aind[i / bs] = y + (i - x);
                    asub[i / bs] = bsum[y + i - x + bs] - bsum[y + i - x];
                    i += bs;
                }else{
                    a[i] = b[y + i - x];
                    asub[i / bs] += a[i];
                    i++;
                }
            }
            int u2 = -1;
            if(x % bs != 0){
                int u1 = aind[x / bs];
                u2 = u1;
                aind[x / bs] = -1;
                for(int i = x / bs * bs; i < x; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - x / bs * bs];
                    }
                    asub[i / bs] += a[i];
                }
            }
            if((x + z) % bs != 0){
                int u1 = aind[(x + z) / bs];
                //cerr << u1 << endl;
                if((x + z) / bs == x / bs) u1 = u2;
                aind[(x + z) / bs] = -1;
                //cerr << u1 << endl;
                for(int i = x + z; i < ((x + z) / bs + 1) * bs; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i % bs];
                    }
                    //cerr << a[i] << " ";
                    asub[i / bs] += a[i];
                }
                //cerr << endl;
            }
            s.q1(x, y, z);
        }else{
            int x, y;
            cin >> x >> y;
            x--;
            ll ans = 0;
            for(int i = x; i < y;){
                if(i % bs == 0 && i + bs <= y){
                    ans += asub[i / bs];
                    i += bs;
                }else{
                    if(aind[i / bs] != -1) a[i] = b[aind[i / bs] + i % bs];
                    ans += a[i];
                    i++;
                }
            }
            cout << ans << '\n';
            s.q2(x, y, ans);
        }
    }
}
accuracy: 0.7692307692307693
time_ms: 510
memory_kb: 9236
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
constexpr int bs = 500;
using ll = long long;
struct solver{
    vector<int> a, b;
    solver(vector<int> a_, vector<int> b_){
        a = a_;
        b = b_;
    }
    void q1(int x, int y, int z){
        return;
        for(int i = 0; i < z; i++){
            a[x + i] = b[y + i];
        }
    }
    void q2(int x, int y, ll ans){
        return;
        ll ret = 0;
        for(int i = x; i < y; i++) ret += a[i];
        if(ret != ans){
            cerr << x << " " << y << " " << ans << " " << ret << '\n';
        }
    }
};
int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n + bs), b(n + bs);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<ll> asub(n / bs + 1);
    vector<int> aind(n / bs + 1, -1);
    for(int i = 0; i < n; i++) asub[i / bs] += a[i];
    vector<ll> bsum(n + 1);
    for(int i = 0; i < n; i++) bsum[i + 1] = bsum[i] + b[i];
    solver s(a, b);
    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            if(x % bs != 0){
                asub[x / bs] = 0;
            }
            if((x + z) % bs != 0){
                asub[(x + z) / bs] = 0;
            }
            for(int i = x; i < x + z;){
                if(i % bs == 0 && i + bs <= x + z){
                    aind[i / bs] = y + (i - x);
                    asub[i / bs] = bsum[y + i - x + bs] - bsum[y + i - x];
                    i += bs;
                }else{
                    a[i] = b[y + i - x];
                    asub[i / bs] += a[i];
                    i++;
                }
            }
            if(x % bs != 0){
                int u1 = aind[x / bs];
                aind[x / bs] = -1;
                for(int i = x / bs * bs; i < x; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - x / bs * bs];
                    }
                    asub[i / bs] += a[i];
                }
            }
            if((x + z) % bs != 0){
                int u1 = aind[(x + z) / bs];
                aind[(x + z) / bs] = -1;
                for(int i = x + z; i < ((x + z) / bs + 1) * bs; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - (x + z)];
                    }
                    asub[i / bs] += a[i];
                }
            }
            s.q1(x, y, z);
        }else{
            int x, y;
            cin >> x >> y;
            x--;
            ll ans = 0;
            for(int i = x; i < y;){
                if(i % bs == 0 && i + bs <= y){
                    ans += asub[i / bs];
                    i += bs;
                }else{
                    if(aind[i / bs] != -1) a[i] = b[aind[i / bs] + i % bs];
                    ans += a[i];
                    i++;
                }
            }
            cout << ans << '\n';
            s.q2(x, y, ans);
        }
    }
}
accuracy: 0.7692307692307693
time_ms: 530
memory_kb: 9308
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
constexpr int bs = 700;
using ll = long long;
struct solver{
    vector<int> a, b;
    solver(vector<int> a_, vector<int> b_){
        a = a_;
        b = b_;
    }
    void q1(int x, int y, int z){
        return;
        for(int i = 0; i < z; i++){
            a[x + i] = b[y + i];
        }
    }
    void q2(int x, int y, ll ans){
        return;
        ll ret = 0;
        for(int i = x; i < y; i++) ret += a[i];
        if(ret != ans){
            cerr << x << " " << y << " " << ans << " " << ret << '\n';
        }
    }
};
int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n + bs), b(n + bs);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<ll> asub(n / bs + 1);
    vector<int> aind(n / bs + 1, -1);
    for(int i = 0; i < n; i++) asub[i / bs] += a[i];
    vector<ll> bsum(n + 1);
    for(int i = 0; i < n; i++) bsum[i + 1] = bsum[i] + b[i];
    solver s(a, b);
    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            if(x % bs != 0){
                asub[x / bs] = 0;
            }
            if((x + z) % bs != 0){
                asub[(x + z) / bs] = 0;
            }
            for(int i = x; i < x + z;){
                if(i % bs == 0 && i + bs <= x + z){
                    aind[i / bs] = y - (i - x);
                    asub[i / bs] = bsum[y + i - x + bs] - bsum[y + i - x];
                    i += bs;
                }else{
                    a[i] = b[y + i - x];
                    asub[i / bs] += a[i];
                    i++;
                }
            }
            if(x % bs != 0){
                int u1 = aind[x / bs];
                aind[x / bs] = -1;
                for(int i = x / bs * bs; i < x; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - x / bs * bs];
                    }
                    asub[i / bs] += a[i];
                }
            }
            if((x + z) % bs != 0){
                int u1 = aind[(x + z) / bs];
                aind[(x + z) / bs] = -1;
                for(int i = x + z; i < ((x + z) / bs + 1) * bs; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - (x + z)];
                    }
                    asub[i / bs] += a[i];
                }
            }
            s.q1(x, y, z);
        }else{
            int x, y;
            cin >> x >> y;
            x--;
            ll ans = 0;
            for(int i = x; i < y;){
                if(i % bs == 0 && i + bs <= y){
                    ans += asub[i / bs];
                    i += bs;
                }else{
                    if(aind[i / bs] != -1) a[i] = b[aind[i / bs] + i % bs];
                    ans += a[i];
                    i++;
                }
            }
            cout << ans << '\n';
            s.q2(x, y, ans);
        }
    }
}
accuracy: 0.7692307692307693
time_ms: 10
memory_kb: 3504
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
constexpr int bs = 700;
using ll = long long;
struct solver{
    vector<int> a, b;
    solver(vector<int> a_, vector<int> b_){
        a = a_;
        b = b_;
    }
    void q1(int x, int y, int z){
        return;
        for(int i = 0; i < z; i++){
            a[x + i] = b[y + i];
        }
    }
    void q2(int x, int y, ll ans){
        return;
        ll ret = 0;
        for(int i = x; i < y; i++) ret += a[i];
        if(ret != ans){
            cerr << x << " " << y << " " << ans << " " << ret << '\n';
        }
    }
};
int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n + bs), b(n + bs);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<ll> asub(n / bs + 1);
    vector<int> aind(n / bs + 1, -1);
    for(int i = 0; i < n; i++) asub[i / bs] += a[i];
    vector<ll> bsum(n + 1);
    for(int i = 0; i < n; i++) bsum[i + 1] = bsum[i] + b[i];
    solver s(a, b);
    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            if(x % bs != 0){
                asub[x / bs] = 0;
            }
            if((x + z) % bs != 0){
                asub[(x + z) / bs] = 0;
            }
            for(int i = x; i < x + z;){
                if(i % bs == 0 && i + bs <= x + z){
                    aind[i / bs] = y;
                    asub[i / bs] = bsum[y + i - x + bs] - bsum[y + i - x];
                    i += bs;
                }else{
                    a[i] = b[y + i - x];
                    asub[i / bs] += a[i];
                    i++;
                }
            }
            if(x % bs != 0){
                int u1 = aind[x / bs];
                aind[x / bs] = -1;
                for(int i = x / bs * bs; i < x; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - x / bs * bs];
                    }
                    asub[i / bs] += a[i];
                }
            }
            if((x + z) % bs != 0){
                int u1 = aind[(x + z) / bs];
                aind[(x + z) / bs] = -1;
                for(int i = x + z; i < ((x + z) / bs + 1) * bs; i++){
                    if(u1 != -1){
                        a[i] = b[u1 + i - (x + z)];
                    }
                    asub[i / bs] += a[i];
                }
            }
            s.q1(x, y, z);
        }else{
            int x, y;
            cin >> x >> y;
            x--;
            ll ans = 0;
            for(int i = x; i < y;){
                if(i % bs == 0 && i + bs <= y){
                    ans += asub[i / bs];
                    i += bs;
                }else{
                    if(aind[i / bs] != -1) a[i] = b[aind[i / bs] + i % bs];
                    ans += a[i];
                    i++;
                }
            }
            cout << ans << '\n';
            s.q2(x, y, ans);
        }
    }
}
accuracy: 0.7692307692307693
time_ms: 630
memory_kb: 9276
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif

template <class S, class T>
struct Intervals {
    static constexpr S INF = numeric_limits<S>::max() / 2;
    static constexpr S L = -INF, R = INF;
    T none_val;
    int total_num;  // none_val でない区間の個数
    S total_len;    // none_val でない区間の長さの合計
    map<S, T> data;

    // コンストラクタ：全体 [L, R) を none_val に初期化
    Intervals(T nv = -1) : none_val(nv), total_num(0), total_len(0) {
        data.emplace(L, none_val);
        data.emplace(R, none_val);
    }

    // 区間 [l, r) の値を v に設定
    void set(S l, S r, T v) {
        assert(L <= l && r < R);
        if (l >= r) return;
        auto it_l = split(l), it_r = split(r);
        // [l, r) に含まれる既存の区間をすべて削除
        for (auto it = it_l; it != it_r;) {
            if (it->second != none_val) {
                S seg_l = it->first, seg_r = next(it)->first;
                total_len -= (seg_r - seg_l);
                total_num--;
            }
            it = data.erase(it);
        }
        // 新たに [l, r) を挿入
        auto it = data.emplace(l, v).first;
        if (v != none_val) {
            total_len += (r - l);
            total_num++;
        }
        // 左側との結合
        if (it != data.begin()) {
            auto it_prev = prev(it);
            if (it_prev->second == v) {
                data.erase(it);
                total_num--;
                l = it_prev->first;
                it = it_prev;
            }
        }
        // 右側との結合
        auto it_next = next(it);
        if (it_next != data.end() && it_next->second == v) {
            data.erase(it_next);
            total_num--;
        }
    }

    // 点 i が含まれる [i, i + 1) の値を返す
    T get_value(S i) {
        assert(L <= i && i < R);
        auto it = prev(data.upper_bound(i));
        return it->second;
    }

    // 点 i が含まれる区間 [l, r) とその値 v を返す
    tuple<S, S, T> get(S i) {
        assert(L <= i && i < R);
        auto it_r = data.upper_bound(i), it_l = prev(it_r);
        return make_tuple(it_l->first, it_r->first, it_l->second);
    }

    // 区間 [l, r) 内の区間を（境界ごとに分割した） run-length 圧縮結果として返す
    vector<tuple<S, S, T>> get(S l, S r) {
        assert(L <= l && r < R);
        vector<tuple<S, S, T>> res;
        if (l >= r) return res;
        auto it_r = data.upper_bound(l), it_l = prev(it_r);
        while (it_l->first < r) {
            res.emplace_back(max(l, it_l->first), min(it_r->first, r), it_l->second);
            it_l = it_r++;
        }
        return res;
    }

   private:
    // 点 pos において区間を分割し，すでに境界があればその位置のイテレータを返す
    // そうでなければ，その区間 [l, r) を [l, pos) と [pos, r) に分割し，後者の先頭を返す
    typename map<S, T>::iterator split(S pos) {
        auto it = data.lower_bound(pos);
        if (it != data.end() && it->first == pos) return it;
        T v = prev(it)->second;
        auto res = data.emplace(pos, v).first;
        if (v != none_val) total_num++;
        return res;
    }
};

template <class T>
struct BinaryIndexedTree {
    int N;
    vector<T> data;

    BinaryIndexedTree() = default;
    BinaryIndexedTree(int size) : N(size), data(size + 1, 0) {}
    BinaryIndexedTree(const vector<T>& v) : N(ssize(v)), data(N + 1) {
        for (int i = 1; i <= N; i++) {
            data[i] += v[i - 1];
            int j = i + (i & -i);
            if (j <= N) data[j] += data[i];
        }
    }
    // i 番目に x を加算
    void add(int i, T x) {
        assert(0 <= i && i < N);
        for (++i; i <= N; i += i & -i) data[i] += x;
    }
    // [l, r) に x を加算 (imos 法)
    void imos(int l, int r, T x) { add(l, x), add(r, -x); }
    // [0, i) の和 (imos 法で区間加算して i 番目の値を取るときは sum(i + 1))
    T sum(int i) const {
        if (i < 0) return T{};
        T res{};
        for (; i > 0; i -= i & -i) res += data[i];
        return res;
    }
    // [l, r) の和
    T sum(int l, int r) const { return sum(r) - sum(l); }
    // i 番目の値
    T operator[](int i) const { return sum(i + 1) - sum(i); }
    // [0, i) の和が w 以上となる最小の i
    int lower_bound(T w) const {
        if (w <= 0) return 0;
        int i = 0;
        for (int k = 1 << __lg(N); k > 0; k >>= 1) {
            if (i + k <= N && data[i + k] < w) {
                w -= data[i + k];
                i += k;
            }
        }
        return i + 1;
    }
};

constexpr int INF = (1 << 30) - 1;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N, M;
    cin >> N >> M;
    vector<ll> a(N), b(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];

    BinaryIndexedTree<ll> bit1(a), bit2(b);
    Intervals<int, int> I(INF);
    while (M--) {
        int t;
        cin >> t;
        if (t == 0) {
            int x, y, z;
            cin >> x >> y >> z, x--, y--;
            // [x, x + z) を b の [y, y + z) に置き換える
            I.set(x, x + z, y - x);
        }
        if (t == 1) {
            int p, q;
            cin >> p >> q, p--;
            ll ans = 0;
            for (auto [l, r, v] : I.get(p, q)) {
                if (v == INF) {
                    ans += bit1.sum(l, r);
                } else {
                    ans += bit2.sum(l + v, r + v);
                }
            }
            cout << ans << '\n';
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 9600
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif

template <class T, T (*op)(T, T), T (*e)(), class F, T (*mapp)(F, T), F (*comp)(F, F), F (*id)()>
class LazySegmentTree {
    int n, len, height;
    vector<T> data;
    vector<F> lazy;
    constexpr void update(int k) noexcept { data[k] = op(data[k << 1 | 0], data[k << 1 | 1]); }
    constexpr void push(int k) noexcept {
        if (lazy[k] == id()) return;
        all_apply(k << 1 | 0, lazy[k]);
        // 書き換え
        all_apply(k << 1 | 1, lazy[k] + data[k << 1].second);
        lazy[k] = id();
    }
    constexpr void all_apply(int k, F f) noexcept {
        data[k] = mapp(f, data[k]);
        if (k < len) lazy[k] = comp(f, lazy[k]);
    }

   public:
    LazySegmentTree() = default;
    LazySegmentTree(int size) : LazySegmentTree(vector<T>(size, e())) {}
    LazySegmentTree(const vector<T> &v) : n(int(v.size())) {
        len = 1, height = 0;
        while (len < n) len <<= 1, height++;
        data.assign(len << 1, e());
        lazy.assign(len << 1, id());
        for (int i = 0; i < n; i++) data[i + len] = v[i];
        for (int i = len - 1; i > 0; i--) update(i);
    }
    void set(int i, T x) {
        i += len;
        for (int j = height; j > 0; j--) push(i >> j);
        data[i] = x;
        while (i >>= 1) update(i);
        // for (int j = 1; j <= height; j++) update(i >> j);
    }
    T get(int i) {
        i += len;
        for (int j = height; j > 0; j--) push(i >> j);
        return data[i];
    }
    T operator[](int i) { return get(i); }
    T prod(int l, int r) {
        if (l >= r) return e();
        l += len, r += len;
        const int l_ctz = __builtin_ctz(l), r_ctz = __builtin_ctz(r);
        for (int i = height; i > l_ctz; i--) push(l >> i);
        for (int i = height; i > r_ctz; i--) push((r - 1) >> i);
        T resl = e(), resr = e();
        while (l < r) {
            if (l & 1) resl = op(resl, data[l++]);
            if (r & 1) resr = op(data[--r], resr);
            l >>= 1, r >>= 1;
        }
        return op(resl, resr);
    }
    T all_prod() const { return data[1]; }
    void update(int l, int r, F f) {
        if (l >= r) return;
        l += len, r += len;
        const int l_ctz = __builtin_ctz(l), r_ctz = __builtin_ctz(r);
        for (int i = height; i > l_ctz; i--) push(l >> i);
        for (int i = height; i > r_ctz; i--) push((r - 1) >> i);
        {
            const int l2 = l, r2 = r;
            // lf, rf を追加し、書き換え
            F lf = f, rf = f + r - l;
            while (l < r) {
                if (l & 1) all_apply(l++, lf), lf += data[l].second;
                if (r & 1) rf -= data[r - 1].second, all_apply(--r, rf);
                l >>= 1, r >>= 1;
            }
            // l = l2, r = r2;
            l = l2 >> l_ctz, r = (r2 - 1) >> r_ctz;
        }
        while (l >>= 1) update(l);
        while (r >>= 1) update(r);
        // for (int i = l_ctz + 1; i <= height; i++) update(l >> i);
        // for (int i = r_ctz + 1; i <= height; i++) update((r - 1) >> i);
    }
    // check(A[l] * ... * A[r - 1]) = true となる最大の r
    template <class C>
    int max_right(int l, const C &check) {
        assert(0 <= l && l <= n && check(e()));
        if (l == n) return n;
        l += len;
        for (int i = height; i > 0; i--) push(l >> i);
        T sum = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!check(op(sum, data[l]))) {
                while (l < len) {
                    push(l);
                    l = l << 1 | 0;
                    if (check(op(sum, data[l]))) {
                        sum = op(sum, data[l++]);
                    }
                }
                return l - len;
            }
            sum = op(sum, data[l++]);
        } while ((l & -l) != l);
        return n;
    }

    // check(A[l] * ... * A[r - 1]) = true となる最小の l
    template <class C>
    int min_left(int r, const C &check) {
        assert(0 <= r && r <= n && check(e()));
        if (r == 0) return 0;
        r += len;
        for (int i = height; i > 0; i--) push((r - 1) >> i);
        T sum = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!check(op(data[r], sum))) {
                while (r < len) {
                    push(r);
                    r = r << 1 | 1;
                    if (check(op(data[r], sum))) {
                        sum = op(data[r--], sum);
                    }
                }
                return r + 1 - len;
            }
            sum = op(data[r], sum);
        } while ((r & -r) != r);
        return 0;
    }
};

// B の累積和
constexpr int N2 = 200001;
array<ll, N2> acc;

// 範囲 copy, 区間和取得
using S = pair<ll, int>;
S op(S a, S b) { return {a.first + b.first, a.second + b.second}; }
S e() { return {0, 0}; }
using F = int;  // B の copy の index
F id() { return N2; }
S mapp(F f, S x) {
    if (f >= id()) return x;
    return {acc[f + x.second] - acc[f], x.second};
}
F comp(F f, F g) { return f >= id() ? g : f; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N, M;
    cin >> N >> M;
    vector<ll> a(N), b(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];

    for (int i = 0; i < N; i++) acc[i + 1] = acc[i] + b[i];
    for (int i = N; i < N2; i++) acc[i + 1] = acc[i];

    vector<S> init(N);
    for (int i = 0; i < N; i++) init[i] = {a[i], 1};
    LazySegmentTree<S, op, e, F, mapp, comp, id> seg(init);

    while (M--) {
        int t;
        cin >> t;
        if (t == 0) {
            int x, y, z;
            cin >> x >> y >> z, x--, y--;
            // [x, x + z) を b の [y, y + z) に置き換える
            seg.update(x, x + z, y);
        }
        if (t == 1) {
            int p, q;
            cin >> p >> q, p--;
            cout << seg.prod(p, q).first << '\n';
        }
        for (int i = 0; i < N; i++) {
            debug(i, seg[i].first);
        }
    }
}
accuracy: 1
time_ms: 90
memory_kb: 21472
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif

template <class S, class T>
struct Intervals {
    static constexpr S INF = numeric_limits<S>::max() / 2;
    static constexpr S L = -INF, R = INF;
    T none_val;
    int total_num;  // none_val でない区間の個数
    S total_len;    // none_val でない区間の長さの合計
    map<S, T> data;

    // コンストラクタ：全体 [L, R) を none_val に初期化
    Intervals(T nv = -1) : none_val(nv), total_num(0), total_len(0) {
        data.emplace(L, none_val);
        data.emplace(R, none_val);
    }

    // 区間 [l, r) の値を v に設定
    void set(S l, S r, T v) {
        assert(L <= l && r < R);
        if (l >= r) return;
        auto it_l = split(l), it_r = split(r);
        // [l, r) に含まれる既存の区間をすべて削除
        for (auto it = it_l; it != it_r;) {
            if (it->second != none_val) {
                S seg_l = it->first, seg_r = next(it)->first;
                total_len -= (seg_r - seg_l);
                total_num--;
            }
            it = data.erase(it);
        }
        // 新たに [l, r) を挿入
        auto it = data.emplace(l, v).first;
        if (v != none_val) {
            total_len += (r - l);
            total_num++;
        }
        // 左側との結合
        if (it != data.begin()) {
            auto it_prev = prev(it);
            if (it_prev->second == v) {
                data.erase(it);
                total_num--;
                l = it_prev->first;
                it = it_prev;
            }
        }
        // 右側との結合
        auto it_next = next(it);
        if (it_next != data.end() && it_next->second == v) {
            data.erase(it_next);
            total_num--;
        }
    }

    // 点 i が含まれる [i, i + 1) の値を返す
    T get_value(S i) {
        assert(L <= i && i < R);
        auto it = prev(data.upper_bound(i));
        return it->second;
    }

    // 点 i が含まれる区間 [l, r) とその値 v を返す
    tuple<S, S, T> get(S i) {
        assert(L <= i && i < R);
        auto it_r = data.upper_bound(i), it_l = prev(it_r);
        return make_tuple(it_l->first, it_r->first, it_l->second);
    }

    // 区間 [l, r) 内の区間を（境界ごとに分割した） run-length 圧縮結果として返す
    vector<tuple<S, S, T>> get(S l, S r) {
        assert(L <= l && r < R);
        vector<tuple<S, S, T>> res;
        if (l >= r) return res;
        auto it_r = data.upper_bound(l), it_l = prev(it_r);
        while (it_l->first < r) {
            res.emplace_back(max(l, it_l->first), min(it_r->first, r), it_l->second);
            it_l = it_r++;
        }
        return res;
    }

   private:
    // 点 pos において区間を分割し，すでに境界があればその位置のイテレータを返す
    // そうでなければ，その区間 [l, r) を [l, pos) と [pos, r) に分割し，後者の先頭を返す
    typename map<S, T>::iterator split(S pos) {
        auto it = data.lower_bound(pos);
        if (it != data.end() && it->first == pos) return it;
        T v = prev(it)->second;
        auto res = data.emplace(pos, v).first;
        if (v != none_val) total_num++;
        return res;
    }
};

template <class T>
struct BinaryIndexedTree {
    int N;
    vector<T> data;

    BinaryIndexedTree() = default;
    BinaryIndexedTree(int size) : N(size), data(size + 1, 0) {}
    BinaryIndexedTree(const vector<T>& v) : N(ssize(v)), data(N + 1) {
        for (int i = 1; i <= N; i++) {
            data[i] += v[i - 1];
            int j = i + (i & -i);
            if (j <= N) data[j] += data[i];
        }
    }
    // i 番目に x を加算
    void add(int i, T x) {
        assert(0 <= i && i < N);
        for (++i; i <= N; i += i & -i) data[i] += x;
    }
    // [l, r) に x を加算 (imos 法)
    void imos(int l, int r, T x) { add(l, x), add(r, -x); }
    // [0, i) の和 (imos 法で区間加算して i 番目の値を取るときは sum(i + 1))
    T sum(int i) const {
        if (i < 0) return T{};
        T res{};
        for (; i > 0; i -= i & -i) res += data[i];
        return res;
    }
    // [l, r) の和
    T sum(int l, int r) const { return sum(r) - sum(l); }
    // i 番目の値
    T operator[](int i) const { return sum(i + 1) - sum(i); }
    // [0, i) の和が w 以上となる最小の i
    int lower_bound(T w) const {
        if (w <= 0) return 0;
        int i = 0;
        for (int k = 1 << __lg(N); k > 0; k >>= 1) {
            if (i + k <= N && data[i + k] < w) {
                w -= data[i + k];
                i += k;
            }
        }
        return i + 1;
    }
};

constexpr int INF = (1 << 30) - 1;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N, M;
    cin >> N >> M;
    vector<ll> a(N), b(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];

    BinaryIndexedTree<ll> bit1(a), bit2(b);
    Intervals<int, int> I(INF);
    while (M--) {
        int t;
        cin >> t;
        if (t == 0) {
            int x, y, z;
            cin >> x >> y >> z, x--, y--;
            // [x, x + z) を b の [y, y + z) に置き換える
            I.set(x, x + z, y - x);
        }
        if (t == 1) {
            int p, q;
            cin >> p >> q, p--;
            ll ans = 0;
            for (auto [l, r, v] : I.get(p, q)) {
                if (v == INF) {
                    ans += bit1.sum(l, r);
                } else {
                    ans += bit2.sum(l + v, r + v);
                }
            }
            cout << ans << '\n';
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 9600
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep2(i, n) for (ll i = 0 ; i < (n) ; i++)
#define rep3(i, a, b) for (ll i = a ; i < (b) ; i++)
#define overload(a, b, c, d, ...) d
#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(a) begin(a), end(a)
#define sz(a) ssize(a)

template <class S, auto op, auto e, class F, auto mapping, auto composition, auto id> struct LazySegmentTree {
    int n, logn;
    vector<S> dat;
    vector<pair<int, int>> lr;
    vector<F> lazy;
    LazySegmentTree(int n) : LazySegmentTree(vector<S>(n, e())) {}
    LazySegmentTree(const vector<S>& init) {
        n = 1, logn = 0;
        while (n < sz(init)) {
            n <<= 1;
            logn++;
        }
        dat.resize(n * 2, e());
        lazy.resize(n * 2, id());
        lr.resize(n * 2);
        for (int i = 0 ; i < n ; i++) lr[n + i] = {i, i + 1};
        copy(all(init), dat.begin() + n);
        for (int i = n - 1 ; i >= 1 ; i--) {
            update(i);
            lr[i].first = lr[i * 2].first;
            lr[i].second = lr[i * 2 + 1].second;
        }
    }
    void update(int i) { dat[i] = op(dat[i*2], dat[i*2 + 1]); }
    void propagate(int i, F f) {
        dat[i] = mapping(f, dat[i]);
        lazy[i] = composition(f, lazy[i]);
    }
    void push(int i) {
        if (lazy[i] != id()) {
            propagate(i * 2, lazy[i]);
            propagate(i * 2 + 1, lazy[i] + dat[i].cnt / 2);
            lazy[i] = id();
        }
    }
    S prod(int l, int r) {
        l += n, r += n;
        for (int i = logn ; i >= 1 ; i--) {
            push(l >> i);
            push((r - 1) >> i);
        }
        S left = e(), right = e();
        while (l < r) {
            if (l & 1) left = op(left, dat[l++]);
            if (r & 1) right = op(dat[--r], right);
            l >>= 1, r >>= 1;
        }
        return op(left, right);
    }
    void apply(int l, int r, F f) {
        auto ps = [&](int x) {
            int lsb = __builtin_ctz(x);
            for (int i = logn ; i > lsb ; i--) push(x >> i);
        };
        auto upd = [&](int x) {
            int lsb = __builtin_ctz(x);
            x >>= lsb + 1;
            while (x) {
                update(x);
                x >>= 1;
            }
        };
        l += n, r += n;
        ps(l), ps(r);
        int l2 = l, r2 = r;
        while (l < r) {
            if (l & 1) {
                propagate(l, f + lr[l].first - lr[l2].first);
                l++;
            }
            if (r & 1) {
                --r;
                propagate(r, f + lr[r].first - lr[l2].first);
            }
            l >>= 1, r >>= 1;
        }
        upd(l2), upd(r2);
    }
};
int N, M, A[2*200020], B[2*200020];
long long sumB[2*200020 + 1];
struct S {
    long long s = 0; 
    int cnt = 0;
    S() = default;
    S(int val) : s{val}, cnt{1} {}
};
S op(S l, S r) {
    l.s += r.s;
    l.cnt += r.cnt;
    return l;
}
S e() { return S(); }
using F = int;
F composition(F newf, F oldf) {
    if (newf == -1) return oldf;
    else return newf;
}
F id() { return -1; }
S mapping(F f, S s) {
    if (f == -1) return s;
    s.s = sumB[f + s.cnt] - sumB[f];
    return s;
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    cin >> N >> M;
    for (int i = 0 ; i < N ; i++) cin >> A[i];
    for (int i = 0 ; i < N ; i++) {
        cin >> B[i];
        sumB[i + 1] = sumB[i] + B[i];
    }
    for (int i = N + 1 ; i <= 2 * N + 10 ; i++) sumB[i] = sumB[i - 1];
    LazySegmentTree<S, op, e, F, mapping, composition, id> seg([&]() {
                vector<S> init(N);
                for (int i = 0 ; i < N ; i++) init[i] = S(A[i]);
                return init;
            }());
    while (M--) {
        int t;
        cin >> t;
        if (t == 0) {
            int x, y, z;
            cin >> x >> y >> z;
            x--;
            y--;
            assert(0 <= y and y + z <= N);
            seg.apply(x, x + z, y);
        }
        else if (t == 1) {
            int p, q;
            cin >> p >> q;
            p--;
            cout << seg.prod(p, q).s << '\n';
        }
        else assert(false);
    }
}
accuracy: 1
time_ms: 100
memory_kb: 26964
final_rank: 11

code_snippet: #include <iostream>
#include <string.h>
#include <vector>
#include <set>
using namespace std;

struct E{
	long long int num,no;
	bool commit;
};
struct E2{
	int l,r,x,y,z;
};
const int maxS=600000;
int R=262143;
int no0=0;
vector<E2> copyData;
vector<long long int> areaSum;
E segA[maxS];

long long int fSegASet(int no,int p1,int l2,int r2,long long int num){
	if(r2<p1 || p1<l2)return segA[no].num;
	if(l2==r2){
		segA[no].num=num;
		return segA[no].num;
	}
	int m=(l2+r2)/2;
	long long int res=fSegASet(no*2+1,p1,l2,m,num);
	res+=fSegASet(no*2+2,p1,m+1,r2,num);
	segA[no].num=res;
	return res;
}

long long int calc(int no,int no2,int no3,int l,int r,int l2,int r2,bool commit){
	//cout<<"<"<<commit<<" "<<no<<",no2="<<no2<<",no3="<<no3<<",l="<<l<<",r="<<r<<","<<l2<<","<<r2<<",>";
	
	if(commit==true){
		//cout<<1<<" "<<segA[no].num<<endl;
		
		return segA[no].num;
	}else{
		E2 e2=copyData[no2];
		if(e2.x<=l2 && r2<=e2.x+e2.z){
			int l3=e2.y+(l2-e2.x);
			int r3=l3+(r2-l2)+1;
			long long int t=areaSum[r3]-areaSum[l3];
			return t;
		}
		e2=copyData[no3];
		//cout<<3<<e2.x<<" "<<e2.z<<endl;
		return areaSum[e2.x+e2.z]-areaSum[e2.x];
	}
}


long long int fSegACalcAns(int no,int l,int r,int l2,int r2,int no2,bool commit){
	commit=commit && segA[no].commit;
	no2=(no2<segA[no].no)?segA[no].no:no2;
	segA[no].commit=commit;
	long long int res=calc(no,no2,no2,l,r,l2,r2,commit);
	if(r2<l || r<l2){
		return 0;
	}

	if(l<=l2 && r2<=r){
		return res;
	}else{
		int m=(l2+r2)/2;
		res=fSegACalcAns(no*2+1,l,r,l2,m,no2,commit);
		res+=fSegACalcAns(no*2+2,l,r,m+1,r2,no2,commit);
		return res;
	}
}

long long int fSegAUpData(int no,int l,int r,int l2,int r2,int no2,int no3,bool commit){
	commit=commit && segA[no].commit;
	no3=(no3<segA[no].no)?segA[no].no:no3;
	segA[no].commit=commit;
	long long int res=calc(no,no3,no3,l,r,l2,r2,commit);
	
	if(r2<l || r<l2){
		return res;
	}
	res=calc(no,no2,no2,l,r,l2,r2,false);
	if(l2==r2){
		segA[no].no=no2;
		segA[no].num=res;
		return res;
	}
	if(l<=l2 && r2<=r){
		segA[no].no=no2;
		segA[no].num=res;
		segA[no].commit=true;
		segA[no*2+1].commit=false;
		segA[no*2+2].commit=false;
		return res;
	}else{
		int m=(l2+r2)/2;
		res=fSegAUpData(no*2+1,l,r,l2,m,no2,no3,commit);
		res+=fSegAUpData(no*2+2,l,r,m+1,r2,no2,no3,commit);
		segA[no].num=res;
		segA[no].commit=true;
		return res;
	}
}

int main() {
	for(int i=0;i<maxS;i++){
		segA[i].num=0;
		segA[i].no=-1;
		segA[i].commit=true;
	}
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		long long int num;
		cin>>num;
		fSegASet(0,i,0,R,num);
	}
	areaSum.push_back(0);
	long long int sum=0;
	for(int i=0;i<n;i++){
		long long int num;
		cin>>num;
		sum+=num;
		areaSum.push_back(sum);
	}
	for(int i=0;i<maxS;i++){
		areaSum.push_back(sum);
	}
	
	for(int i=0;i<m;i++){
		int c1,l,r,x,y,z;
		cin>>c1;
		if(c1==0){
			cin>>x>>y>>z;
			E2 e2;
			e2.l=y-1;
			e2.r=y-2+z;
			e2.x=x-1;
			e2.y=y-1;
			e2.z=z;
			l=x-1;
			r=x-2+z;
			copyData.push_back(e2);
			fSegAUpData(0,l,r,0,R,no0,-1,true);
			no0++;
		}else{
			cin>>l>>r;
			l--;
			r--;
			long long int t=fSegACalcAns(0,l,r,0,R,-1,true);
			cout<<t<<endl;
		}
		//for(int i=0;i<maxS;i++){
			//cout<<"("<<i<<","<<segA[i].num<<")";
		//}
	//cout<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 29980
final_rank: 15

code_snippet: #include <iostream>
#include <string.h>
#include <vector>
#include <set>
using namespace std;

struct E{
	long long int num,no;
	bool commit;
};
struct E2{
	int l,r,x,y,z;
};
const int maxS=600000;
int R=262143;
int no0=0;
vector<E2> copyData;
vector<long long int> areaSum;
E segA[maxS];

long long int fSegASet(int no,int p1,int l2,int r2,long long int num){
	if(r2<p1 || p1<l2)return segA[no].num;
	if(l2==r2){
		segA[no].num=num;
		return segA[no].num;
	}
	int m=(l2+r2)/2;
	long long int res=fSegASet(no*2+1,p1,l2,m,num);
	res+=fSegASet(no*2+2,p1,m+1,r2,num);
	segA[no].num=res;
	return res;
}

long long int calc(int no,int no2,int no3,int l,int r,int l2,int r2,bool commit){
	//cout<<"<"<<commit<<" "<<no<<",no2="<<no2<<",no3="<<no3<<",l="<<l<<",r="<<r<<","<<l2<<","<<r2<<",>";
	E2 e2=copyData[no2];
	if(commit==true){
		//cout<<1<<" "<<segA[no].num<<endl;
		
		return segA[no].num;
	}else{
		if(e2.x<=l2 && r2<=e2.x+e2.z){
			int l3=e2.y+(l2-e2.x);
			int r3=l3+(r2-l2)+1;
			long long int t=areaSum[r3]-areaSum[l3];
			return t;
		}
		e2=copyData[no3];
		//cout<<3<<e2.x<<" "<<e2.z<<endl;
		return areaSum[e2.x+e2.z]-areaSum[e2.x];
	}
}


long long int fSegACalcAns(int no,int l,int r,int l2,int r2,int no2,bool commit){
	commit=commit && segA[no].commit;
	no2=(no2<segA[no].no)?segA[no].no:no2;
	segA[no].commit=commit;
	long long int res=calc(no,no2,no2,l,r,l2,r2,commit);
	if(r2<l || r<l2){
		return 0;
	}

	if(l<=l2 && r2<=r){
		return res;
	}else{
		int m=(l2+r2)/2;
		res=fSegACalcAns(no*2+1,l,r,l2,m,no2,commit);
		res+=fSegACalcAns(no*2+2,l,r,m+1,r2,no2,commit);
		return res;
	}
}

long long int fSegAUpData(int no,int l,int r,int l2,int r2,int no2,int no3,bool commit){
	commit=commit && segA[no].commit;
	no3=(no3<segA[no].no)?segA[no].no:no3;
	segA[no].commit=commit;
	long long int res=calc(no,no3,no3,l,r,l2,r2,commit);
	
	if(r2<l || r<l2){
		return res;
	}
	res=calc(no,no2,no2,l,r,l2,r2,false);
	if(l2==r2){
		segA[no].no=no2;
		segA[no].num=res;
		return res;
	}
	if(l<=l2 && r2<=r){
		segA[no].no=no2;
		segA[no].num=res;
		segA[no].commit=true;
		segA[no*2+1].commit=false;
		segA[no*2+2].commit=false;
		return res;
	}else{
		int m=(l2+r2)/2;
		res=fSegAUpData(no*2+1,l,r,l2,m,no2,no3,commit);
		res+=fSegAUpData(no*2+2,l,r,m+1,r2,no2,no3,commit);
		segA[no].num=res;
		segA[no].commit=true;
		return res;
	}
}

int main() {
	for(int i=0;i<maxS;i++){
		segA[i].num=0;
		segA[i].no=-1;
		segA[i].commit=true;
	}
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		long long int num;
		cin>>num;
		fSegASet(0,i,0,R,num);
	}
	areaSum.push_back(0);
	long long int sum=0;
	for(int i=0;i<n;i++){
		long long int num;
		cin>>num;
		sum+=num;
		areaSum.push_back(sum);
	}
	for(int i=0;i<maxS;i++){
		areaSum.push_back(sum);
	}
	
	for(int i=0;i<m;i++){
		int c1,l,r,x,y,z;
		cin>>c1;
		if(c1==0){
			cin>>x>>y>>z;
			E2 e2;
			e2.l=y-1;
			e2.r=y-2+z;
			e2.x=x-1;
			e2.y=y-1;
			e2.z=z;
			l=x-1;
			r=x-2+z;
			copyData.push_back(e2);
			fSegAUpData(0,l,r,0,R,no0,-1,true);
			no0++;
		}else{
			cin>>l>>r;
			l--;
			r--;
			long long int t=fSegACalcAns(0,l,r,0,R,-1,true);
			cout<<t<<endl;
		}
		//for(int i=0;i<maxS;i++){
			//cout<<"("<<i<<","<<segA[i].num<<")";
		//}
	//cout<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 29956
final_rank: 14

code_snippet: // AOJ #437
// Copy and Sum 2025.1.20

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// ----------------------------------------
// グローバルに必要な配列や構造体を宣言
// ----------------------------------------

// 最大要素数 N に対して4倍程度のサイズでセグメント木を構築
static const int MAXN = 200000;  // 問題の N の最大値
vector<ll> A, B, prefixB; // A, Bおよび B の prefix sum
vector<ll> seg;          // セグメント木(区間和)本体
vector<ll> lazy;         // 遅延伝搬: オフセット c = (y - x)
vector<bool> lazyFlag;          // 遅延伝搬が有効かどうか

int N, M; // 配列長, 操作数

// ----------------------------------------
// B の区間和を O(1) で返す関数
// B_{l} + ... + B_{r} = prefixB[r] - prefixB[l - 1] (1-indexed)
// ----------------------------------------
ll getSumB(int l, int r) {
    // l, r は 1-indexed で、かつ 1 <= l <= r <= N 保証
    return prefixB[r] - prefixB[l - 1];
}

// ----------------------------------------
// ノード [start, end] に対して「オフセット c で完全に上書き」したときの
// 区間和を計算する
// すなわち A_i = B_{i + c} (i in [start, end]) になった場合の合計
// ----------------------------------------
ll calcRangeSum(int start, int end, ll c) {
    // 区間 [start, end] は 1-indexed
    // A_i = B_{i + c} なので合計は sum of B in [start + c, end + c]
    int lB = start + c; 
    int rB = end + c;
    // 範囲外に行かないかは呼び出し側で保証 (y+z-1 <= N 等)
    return getSumB(lB, rB);
}

// ----------------------------------------
// セグメント木を構築する (最初は配列 A の値で構築)
// node: 節点番号 (1から始めることが多い)
// start, end: node が担当する区間 [start, end]
// ----------------------------------------
void build(int node, int start, int end) {
    lazyFlag[node] = false; // 初期状態では遅延なし
    if (start == end) {
        // 葉ノード
        seg[node] = A[start]; 
        return;
    }
    int mid = (start + end) / 2;
    build(node*2, start, mid);
    build(node*2+1, mid+1, end);
    seg[node] = seg[node*2] + seg[node*2+1];
}

// ----------------------------------------
// 遅延値 lazy[node] を子ノードに伝搬させる
// node が担当する区間は [start, end]
// ----------------------------------------
void pushDown(int node, int start, int end) {
    if (!lazyFlag[node]) return; // 遅延が無効なら何もしない

    // 子ノードの区間
    int mid = (start + end) / 2;
    int leftNode = node * 2;
    int rightNode = node * 2 + 1;

    // 子ノードを「オフセット lazy[node]」で完全上書き状態にする
    // 左の子
    lazyFlag[leftNode] = true;
    lazy[leftNode] = lazy[node];
    seg[leftNode] = calcRangeSum(start, mid, lazy[node]);

    // 右の子
    lazyFlag[rightNode] = true;
    lazy[rightNode] = lazy[node];
    seg[rightNode] = calcRangeSum(mid+1, end, lazy[node]);

    // 自分の遅延情報をクリア
    lazyFlag[node] = false;
}

// ----------------------------------------
// セグメント木の区間上書き (copy相当)
// 「区間 [l, r] をオフセット c (= y - x) で B 参照にする」
// node が担当する区間 [start, end]
// ----------------------------------------
void update(int node, int start, int end, int l, int r, ll c) {
    if (r < start || end < l) {
        // 範囲がかぶらない
        return;
    }
    if (l <= start && end <= r) {
        // [start, end] が完全に [l, r] に含まれる → 一括上書き
        lazyFlag[node] = true;
        lazy[node] = c;
        seg[node] = calcRangeSum(start, end, c);
        return;
    }
    // 部分的にかぶる場合は子に伝搬する
    pushDown(node, start, end);
    int mid = (start + end) / 2;
    update(node*2, start, mid, l, r, c);
    update(node*2+1, mid+1, end, l, r, c);
    seg[node] = seg[node*2] + seg[node*2+1];
}

// ----------------------------------------
// セグメント木の区間和取得 (sum相当)
// [l, r] の和を返す
// node が担当する区間 [start, end]
// ----------------------------------------
ll query(int node, int start, int end, int l, int r) {
    if (r < start || end < l) {
        // 範囲がかぶらない
        return 0;
    }
    if (l <= start && end <= r) {
        // 完全に含まれる
        return seg[node];
    }
    // 部分的にかぶるときは pushDown して子ノードを見る
    pushDown(node, start, end);
    int mid = (start + end) / 2;
    ll sumLeft = query(node*2, start, mid, l, r);
    ll sumRight = query(node*2+1, mid+1, end, l, r);
    return sumLeft + sumRight;
}

// ----------------------------------------
// メイン処理
// ----------------------------------------
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M;
    A.resize(N+1);
    B.resize(N+1);
    prefixB.resize(N+1, 0LL);
    seg.resize(4*(N+1), 0LL);
    lazy.resize(4*(N+1), 0LL);
    lazyFlag.resize(4*(N+1), false);

    // A, B を 1-indexed で受け取る
    for (int i = 1; i <= N; i++) cin >> A[i];
    for (int i = 1; i <= N; i++) cin >> B[i];

    // B の prefix sum を作る
    // prefixB[i] = B[1] + B[2] + ... + B[i]
    for (int i = 1; i <= N; i++){
        prefixB[i] = prefixB[i-1] + B[i];
    }

    // セグメント木を A の値で構築
    build(1, 1, N);

    // M 個の操作を処理
    while (M--) {
        int t; 
        cin >> t;
        if (t == 0) {
            // copy(x, y, z) → "0 x y z"
            ll x, y, z;
            cin >> x >> y >> z;
            // A[x..x+z-1] = B[y..y+z-1]
            // offset c = y - x を区間 [x, x+z-1] に一括上書き
            ll c = y - x; 
            update(1, 1, N, x, x + z - 1, c);

        } else {
            // sum(p, q) → "1 p q"
            ll p, q;
            cin >> p >> q;
            // 出力
            ll ans = query(1, 1, N, p, q);
            cout << ans << "\n";
        }
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 20532
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

using i64 = long long;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repi(i, a, b) for (int i = a - 1; i >= b; i--)

struct LazySeg {
    using S = pair<i64, int>;
    S op(S l, S r) {
        l.first += r.first;
        l.second += r.second;
        return l;
    }
    S e() {
        return S{0, 0};
    }
    using F = int;
    S mapping(F f, S x) {
        if (f < 0) return x;
        x.first = preb[f + x.second] - preb[f];
        return x;
    }
    F composition(F l, F r) {
        if (l < 0) return r;
        return l;
    }
    F id() { return -1e9; }

    int _n, size, log;
    vector<S> d;
    vector<i64> preb;
    vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k] + d[2 * k].second);
        lz[k] = id();
    }

    LazySeg() = default;

    LazySeg(const vector<i64> &v, const vector<i64> &preb_) : _n(v.size()), log(0), preb(preb_) {
        while ((1 << log) < _n) ++log;
        size = 1 << log;
        d = vector<S>(2 * size, e());
        lz = vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = {v[i], 1};
        for (int i = size - 1; i >= 1; i--) update(i);
    }

    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= _n);
        if (l == r) return e();

        // rep (i, 1, 2 * size) {
        //     cout << i << " " << d[i].first << " " << d[i].second << endl;
        // }

        l += size;
        r += size;
        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            // cout << l << " " << r << endl;
            if (l & 1) {
                // cout << l << endl;
                sml = op(sml, d[l++]);
            }
            if (r & 1) {
                // cout << r << endl;
                smr = op(d[--r], smr);
            }
            l >>= 1;
            r >>= 1;
        }
        // cout << d[4].first << " " << d[4].second << endl;
        // cout << d[10].first << " " << d[8].second << endl;

        return op(sml, smr);
    }

    void apply(int l, int r, F f) {
        assert(0 <= l and l <= r and r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            F lf = f, rf = f + r - l;
            while (l < r) {
                // cout << lf << " " << rf << endl;
                if (l & 1) {
                    all_apply(l, lf);
                    // cout << lf << endl;
                    lf += d[l].second;
                    l++;
                }
                if (r & 1) {
                    all_apply(r - 1, rf - d[r - 1].second);
                    // cout << rf - d[r - 1].second << endl;
                    rf -= d[r - 1].second;
                    r--;
                }
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }

        // rep (i, 1, 2 * size) {
        //     cout << i << " " << d[i].first << " " << d[i].second << endl;
        // }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<i64> a(n);
    vector<i64> preb(n + 1);
    rep (i, 0, n) { cin >> a[i]; }
    rep (i, 0, n) { cin >> preb[i + 1]; }
    rep (i, 0, n) preb[i + 1] += preb[i];

    LazySeg seg(a, preb);

    rep (_, 0, m) {
        int t;
        cin >> t;
        if (t == 0) {
            int x, y, z;
            cin >> x >> y >> z;
            --x, --y;
            seg.apply(x, x + z, y);
        }
        if (t == 1) {
            int p, q;
            cin >> p >> q;
            --p;
            cout << seg.prod(p, q).first << "\n";
            // cout << seg.prod(p, q).first << endl;
            // cout << seg.prod(p, q).second << endl;
        }
        // rep (i, 0, n) cout << seg.prod(i, i + 1).first << " ";
        // cout << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 17120
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

template<typename T,T e>
struct segtree {
	vector<long long> v;
	long long n;
	T f(T a,T b){
		return a + b;
	}
	segtree(long long n) : n(n) {
		v.resize(n * 2ll);
		for(long long i = 0ll;i < n * 2ll;i++){
			v[i] = e;
		}
	}
	segtree(vector<T> x) : n(x.size()) {
		v.resize(n * 2ll);
		for(long long i = 0ll;i < n;i++){
			v[i + n] = x[i];
		}
		for(long long i = n - 1ll;i >= 0ll;i--){
			v[i] = f(v[i * 2ll],v[i * 2ll + 1ll]);
		}
	}
	T operator[](long long a){
		return v[a + n];
	}
	void set(long long a,long long b){
		a += n;
		v[a] = b;
		while(a >>= 1ll){
			v[a] = f(v[a * 2ll],v[a * 2ll + 1ll]);
		}
	}
	T prod(long long l,long long r){
		T res = e;
		for(l += n,r += n;l < r;l >>= 1ll,r >>= 1ll){
			if(l & 1ll){
				res = f(res,v[l++]);
			}
			if(r & 1ll){
				res = f(res,v[--r]);
			}
		}
		return res;
	}
};
int main()
{
	long long n,Q;
	cin >> n >> Q;
	vector<vector<long long>> a(2ll,vector<long long>(n));
	long long m = 1ll;
	while(m * m < n) m++;
	vector<long long> cou(m,0ll),u(m,-1ll);
	for(long long j = 0ll;j < 2ll;j++){
		for(long long i = 0ll;i < n;i++){
			cin >> a[j][i];
		}
	}
	for(long long i = 0ll;i < n;i++){
		cou[i / m] += a[0ll][i];
	}
	segtree<long long,0ll> seg(a[1ll]);
	for(long long cnt = 0ll;cnt < Q;cnt++){
		long long T;
		cin >> T;
		if(T == 0ll){
			long long x,y,z;
			cin >> x >> y >> z;
			x--;y--;
			long long l = x,r = x + z - 1ll;
			for(long long i = 0ll;i < m;i++){
				long long left = i * m,right = left + m - 1ll;
				if(right < l || r < left) continue;
				if(l <= left && right <= r){
					long long it = left - l;
					cou[i] = seg.prod(y + it,y + it + m);
					u[i] = y + it;
				}else{
					cou[i] = 0ll;
					long long it = left - l;
					for(long long j = left;j <= min(right,n - 1ll);j++){
						if(u[i] != -1ll){
							a[0ll][j] = a[1ll][u[i] + (j - left)];
						}
						if(l <= j && j <= r){
							a[0ll][j] = a[1ll][y + it + (j - left)];
						}
						cou[i] += a[0ll][j];
					}
					u[i] = -1ll;
				}
			}
		}else{
			long long l,r;
			cin >> l >> r;
			l--;r--;
			long long ans = 0ll;
			for(long long i = 0ll;i < m;i++){
				long long left = i * m,right = left + m - 1ll;
				if(right < l || r < left) continue;
				if(l <= left && right <= r){
					ans += cou[i];
				}else{
					cou[i] = 0ll;
					for(long long j = left;j <= min(right,n - 1ll);j++){
						if(u[i] != -1ll){
							a[0ll][j] = a[1ll][u[i] + (j - left)];
						}
						cou[i] += a[0ll][j];
						if(l <= j && j <= r){
							ans += a[0ll][j];
						}
					}
					u[i] = -1ll;
				}
			}
			cout << ans << endl;
		}
	}
}
accuracy: 1
time_ms: 900
memory_kb: 11020
final_rank: 13

code_snippet: #include <bits/stdc++.h>

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<long long> a(n), b(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    for (int i = 0; i < n; i++) std::cin >> b[i];
    const int D = 700;
    const int b_size = (n + D - 1) / D;
    const std::pair<int, int> init = {-1, -1};
    std::vector<long long> totals(b_size);
    std::vector<std::pair<int, int>> querys(b_size, init);
    std::vector<long long> sum(n + 1);
    for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + b[i];

    for (int i = 0; i < b_size; i++) {
        for (int j = i * D, r = std::min(n, (i + 1) * D); j < r; j++) {
            totals[i] += a[j];
        }
    }
    // for (int i = 0; i < b_size; i++) {
    //     std::cerr << totals[i] << ' ';
    // }
    // std::cerr << std::endl;

    auto push = [&](int pos) {
        if (querys[pos] != init) {
            auto [l, r] = querys[pos];
            assert(r - l == D);
            int offset = D * pos;
            for (int i = 0; i < D; i++) {
                a[i + offset] = b[l + i];
            }
            totals[pos] = 0;
            for (int i = 0; i < D; i++) {
                totals[pos] += a[i + offset];
            }
            querys[pos] = init;
        }
    };

    auto update = [&](int pos) {
        assert(querys[pos] == init);
        totals[pos] = 0;
        int offset = pos * D;
        for (int i = 0; i < D; i++) {
            totals[pos] += a[i + offset];
        }
    };

    while (m--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int la, lb, len;
            std::cin >> la >> lb >> len;
            la--;
            lb--;
            int ra = la + len;
            int rb = lb + len;
            // left bucket
            if (la % D != 0) {
                int pos = la / D;
                push(pos);
                for (; la < ra && la % D != 0; la++, lb++) {
                    a[la] = b[lb];
                }
                update(pos);
            }
            // right bucket
            if (ra % D != 0) {
                int pos = ra / D;
                push(pos);
                for (; la < ra && ra % D != 0; ) {
                    ra--;
                    rb--;
                    a[ra] = b[rb];
                }
                update(pos);
            }
            assert(la == ra || (la % D == 0 && ra % D == 0));
            assert(ra - la == rb - lb);
            for (; la < ra; la += D, lb += D) {
                querys[la / D] = {lb, lb + D};
                totals[la / D] = sum[lb + D] - sum[lb];
            }
        } else {
            int l, r;
            std::cin >> l >> r;
            l--;
            long long res = 0;
            // left bucket
            if (l % D != 0) {
                int pos = l / D;
                push(pos);
                for (; l < r && l % D != 0; l++) {
                    res += a[l];
                }
            }
            if (r % D != 0) {
                int pos = r / D;
                push(pos);
                for (; l < r && r % D != 0; ) {
                    r--;
                    res += a[r];
                }
            }
            assert(l == r || (l % D == 0 && r % D == 0));
            for (; l < r; l += D) {
                res += totals[l / D];
            }
            std::cout << res << '\n';
        }
    }
}
accuracy: 1
time_ms: 640
memory_kb: 7824
final_rank: 8

code_snippet: #include <bits/stdc++.h>

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<long long> a(n), b(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    for (int i = 0; i < n; i++) std::cin >> b[i];
    const int D = 700;
    const int b_size = (n + D - 1) / D;
    const std::pair<int, int> init = {-1, -1};
    std::vector<long long> totals(b_size);
    std::vector<std::pair<int, int>> querys(b_size, init);
    std::vector<long long> sum(n + 1);
    for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + b[i];

    for (int i = 0; i < b_size; i++) {
        for (int j = i * D, r = std::min(n, (i + 1) * D); j < r; j++) {
            totals[i] += a[j];
        }
    }
    // for (int i = 0; i < b_size; i++) {
    //     std::cerr << totals[i] << ' ';
    // }
    // std::cerr << std::endl;

    auto push = [&](int pos) {
        if (querys[pos] != init) {
            auto [l, r] = querys[pos];
            assert(r - l == D);
            int offset = D * pos;
            for (int i = 0; i < D; i++) {
                a[i + offset] = b[l];
            }
            totals[pos] = 0;
            for (int i = 0; i < D; i++) {
                totals[pos] += a[i + offset];
            }
            querys[pos] = init;
        }
    };

    auto update = [&](int pos) {
        assert(querys[pos] == init);
        totals[pos] = 0;
        int offset = pos * D;
        for (int i = 0; i < D; i++) {
            totals[pos] += a[i + offset];
        }
    };

    while (m--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int la, lb, len;
            std::cin >> la >> lb >> len;
            la--;
            lb--;
            int ra = la + len;
            int rb = lb + len;
            // left bucket
            if (la % D != 0) {
                int pos = la / D;
                push(pos);
                for (; la < ra && la % D != 0; la++, lb++) {
                    a[la] = b[lb];
                }
                update(pos);
            }
            // right bucket
            if (ra % D != 0) {
                int pos = ra / D;
                push(pos);
                for (; la < ra && ra % D != 0; ) {
                    ra--;
                    rb--;
                    a[ra] = b[rb];
                }
                update(pos);
            }
            assert(la == ra || (la % D == 0 && ra % D == 0));
            assert(ra - la == rb - lb);
            for (; la < ra; la += D, lb += D) {
                querys[la / D] = {lb, lb + D};
                totals[la / D] = sum[lb + D] - sum[lb];
            }
        } else {
            int l, r;
            std::cin >> l >> r;
            l--;
            long long res = 0;
            // left bucket
            if (l % D != 0) {
                int pos = l / D;
                push(pos);
                for (; l < r && l % D != 0; l++) {
                    res += a[l];
                }
            }
            if (r % D != 0) {
                int pos = r / D;
                push(pos);
                for (; l < r && r % D != 0; ) {
                    r--;
                    res += a[r];
                }
            }
            assert(l == r || (l % D == 0 && r % D == 0));
            for (; l < r; l += D) {
                res += totals[l / D];
            }
            std::cout << res << '\n';
        }
    }
}
accuracy: 0.7692307692307693
time_ms: 590
memory_kb: 7780
final_rank: 19