# 【LibreOJ 6280】 数列分块入门 4 （分块）

code:

//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#define ll long long
#define f(A,B,C) for(int A=B;A<=C;A++)
#define minn(a,b) (a>b?b:a) //一定要记得加括号
using namespace std;

ll rd() {
ll x=0,fla=1; char c=' ';
while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
while(c<='9'&&c>='0') x=x*10+c-'0',c=getchar();
return x*fla;
}

const int MAX=100005;
const int INF=0x3f3f3f3f;
int n,blo;
ll v[MAX],bl[MAX],tag[MAX],sum[MAX];

void add(int a,int b,int c) {
f(i,a,minn(bl[a]*blo,b)) v[i]+=c,sum[bl[a]]+=c;
if(bl[a]!=bl[b])
f(i,(bl[b]-1)*blo+1,b) v[i]+=c,sum[bl[b]]+=c;
f(i,bl[a]+1,bl[b]-1) tag[i]+=c;
}

ll query(int a,int b) {
ll ans=0;
f(i,a,minn(bl[a]*blo,b)) ans+=v[i]+tag[bl[a]];
if(bl[a]!=bl[b])
f(i,(bl[b]-1)*blo+1,b) ans+=v[i]+tag[bl[b]];
f(i,bl[a]+1,bl[b]-1) ans+=sum[i]+blo*tag[i]; // 注意tag一定要乘上blo
return ans;
}

int main() {
n=rd(); blo=sqrt(n);
f(i,1,n) v[i]=rd();
f(i,1,n) {
bl[i]=(i-1)/blo+1;
sum[bl[i]]+=v[i];
}
f(i,1,n) {
int opt=rd(),a=rd(),b=rd(),c=rd();
if(opt) printf("%lld\n",(ll)query(a,b)%(c+1));