#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define mp make_pair
#define All(v) v.begin(),v.end()
#define mod 1000000007
#define write freopen("output.txt","w",stdout)
#define read freopen("test (1).txt","r",stdin)
#define FIO std::ios_base::sync_with_stdio(false);
using namespace std;
int a[100005],timer;
ll square[4*100005];
int suming[4*100005];
int lazy1[4*100005],lazy2[4*100005];
int t1[4*100005],t2[4*100005];
void init()
{
memset(square,0,sizeof(square));
memset(suming,0,sizeof(suming));
memset(lazy1,0,sizeof(lazy1));
memset(lazy2,0,sizeof(lazy2));
memset(t1,0,sizeof(t1));
memset(t2,0,sizeof(t2));
}
void shift(int node,int L,int R)
{
if(t1[node] > t2[node])
{
if(lazy1[node])
{
int val = lazy1[node];
int len = R-L+1;
square[node] = (square[node] + 2LL * val * suming[node] + len * val * val);
suming[node] += (1LL * len * val);
if(t1[2*node] > t2[2*node])
{
lazy1[2*node] += lazy1[node];
lazy1[2*node] += lazy1[node];
t1[2*node] = t1[node];
}
else
{
lazy2[2*node] += lazy1[node];
lazy2[2*node] += lazy1[node];
t2[2*node] = t1[node];
}
if(t1[2*node+1] > t2[2*node+1])
{
lazy1[2*node+1] += lazy1[node];
lazy1[2*node+1] += lazy1[node];
t1[2*node+1] = t1[node];
}
else
{
lazy2[2*node+1] += lazy1[node];
lazy2[2*node+1] += lazy1[node];
t2[2*node+1] = t1[node];
}
}
lazy1[node] = 0;
}
else
{
if(lazy2[node])
{
int val = lazy2[node];
int len = R-L+1;
square[node] = 1LL * len * val * val;
suming[node] = (1LL * len * val);
if(t1[2*node] > t2[2*node]) /// time increment
{
lazy1[2*node] += lazy2[node];
lazy1[2*node] += lazy2[node];
t1[2*node] = t2[node];
}
else
{
lazy2[2*node] += lazy2[node];
lazy2[2*node] += lazy2[node];
t2[2*node] = t2[node];
}
if(t1[2*node+1] > t2[2*node+1]) /// time increment
{
lazy1[2*node+1] += lazy2[node];
lazy1[2*node+1] += lazy2[node];
t1[2*node+1] = t2[node];
}
else
{
lazy2[2*node+1] += lazy2[node];
lazy2[2*node+1] += lazy2[node];
t2[2*node+1] = t2[node];
}
}
lazy2[node] = 0;
}
t1[node] = t2[node] = 0;
}
void update1(int node,int L,int R,int l,int r,int val) /// add val to all number [l,r]
{
shift(node,L,R);
if(L > R || L > r || R < l)
return ;
if(L >= l && R <= r)
{
lazy1[node] = val;
t1[node] = timer;
shift(node,L,R);
return ;
}
int mid = (L+R)/2;
update1(2*node,L,mid,l,r,val);
update1(2*node+1,mid+1,R,l,r,val);
square[node] = square[2*node] + square[2*node+1];
suming[node] = suming[2*node] + suming[2*node+1];
}
void update2(int node,int L,int R,int l,int r,int x) /// set x to all number [l,r]
{
shift(node,L,R);
if(L > R || L > r || R < l)
return ;
if(L >= l && R <= r)
{
lazy2[node] = x;
t2[node] = timer;
square[node] = 0;
suming[node] = 0;
shift(node,L,R);
return ;
}
int mid = (L+R)/2;
update2(2*node,L,mid,l,r,x);
update2(2*node+1,mid+1,R,l,r,x);
square[node] = square[2*node] + square[2*node+1];
suming[node] = suming[2*node] + suming[2*node+1];
}
void build(int node,int L,int R)
{
if(L == R)
{
square[node] = 1LL * a[L] * a[L];
suming[node] = a[L];
return ;
}
int mid = (L+R)/2;
build(2*node,L,mid);
build(2*node+1,mid+1,R);
square[node] = square[2*node] + square[2*node+1];
suming[node] = suming[2*node] + suming[2*node+1];
}
ll query(int node,int L,int R,int l,int r)
{
if(L > R || L > r || R < l)
return 0;
shift(node,L,R);
if(L >= l && R <= r)
return square[node];
int mid = (L + R) / 2;
ll sum1 = query(node*2, L, mid, l, r);
ll sum2 = query(node*2 + 1, mid + 1, R, l, r);
return (sum1 + sum2);
}
int main()
{
int t,num = 1;
cin >> t;
while(t--)
{
printf("Case %d:\n",num++);
init();
timer = 0;
int n,q;
cin >> n >> q;
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
build(1,0,n-1);
while(q--)
{
timer++;
int c,x,y,z;
scanf("%d",&c);
if(c == 2)
{
scanf("%d%d",&x,&y);
ll ans =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gICBpZih0MVtub2RlXSA+IHQyW25vZGVdKQogICAgewogICAgICAgIGlmKGxhenkxW25vZGVdKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHZhbCA9IGxhenkxW25vZGVdOwogICAgICAgICAgICBpbnQgbGVuID0gUi1MKzE7CiAgICAgICAgICAgIHNxdWFyZVtub2RlXSA9IChzcXVhcmVbbm9kZV0gKyAyTEwgKiB2YWwgKiBzdW1pbmdbbm9kZV0gKyBsZW4gKiB2YWwgKiB2YWwpOwogICAgICAgICAgICBzdW1pbmdbbm9kZV0gKz0gKDFMTCAqIGxlbiAqIHZhbCk7CgogICAgICAgICAgICBpZih0MVsyKm5vZGVdID4gdDJbMipub2RlXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbGF6eTFbMipub2RlXSArPSBsYXp5MVtub2RlXTsKICAgICAgICAgICAgICAgIGxhenkxWzIqbm9kZV0gKz0gbGF6eTFbbm9kZV07CiAgICAgICAgICAgICAgICB0MVsyKm5vZGVdID0gdDFbbm9kZV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsYXp5MlsyKm5vZGVdICs9IGxhenkxW25vZGVdOwogICAgICAgICAgICAgICAgbGF6eTJbMipub2RlXSArPSBsYXp5MVtub2RlXTsKICAgICAgICAgICAgICAgIHQyWzIqbm9kZV0gPSB0MVtub2RlXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZih0MVsyKm5vZGUrMV0gPiB0MlsyKm5vZGUrMV0pIAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsYXp5MVsyKm5vZGUrMV0gKz0gbGF6eTFbbm9kZV07CiAgICAgICAgICAgICAgICBsYXp5MVsyKm5vZGUrMV0gKz0gbGF6eTFbbm9kZV07CiAgICAgICAgICAgICAgICB0MVsyKm5vZGUrMV0gPSB0MVtub2RlXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxhenkyWzIqbm9kZSsxXSArPSBsYXp5MVtub2RlXTsKICAgICAgICAgICAgICAgIGxhenkyWzIqbm9kZSsxXSArPSBsYXp5MVtub2RlXTsKICAgICAgICAgICAgICAgIHQyWzIqbm9kZSsxXSA9IHQxW25vZGVdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGxhenkxW25vZGVdID0gMDsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBpZihsYXp5Mltub2RlXSkKICAgICAgICB7CiAgICAgICAgICAgIGludCB2YWwgPSBsYXp5Mltub2RlXTsKICAgICAgICAgICAgaW50IGxlbiA9IFItTCsxOwogICAgICAgICAgICBzcXVhcmVbbm9kZV0gPSAxTEwgKiBsZW4gKiB2YWwgKiB2YWw7CiAgICAgICAgICAgIHN1bWluZ1tub2RlXSA9ICgxTEwgKiBsZW4gKiB2YWwpOwogICAgICAgICAgICBpZih0MVsyKm5vZGVdID4gdDJbMipub2RlXSkgLy8vIHRpbWUgaW5jcmVtZW50CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxhenkxWzIqbm9kZV0gKz0gbGF6eTJbbm9kZV07CiAgICAgICAgICAgICAgICBsYXp5MVsyKm5vZGVdICs9IGxhenkyW25vZGVdOwogICAgICAgICAgICAgICAgdDFbMipub2RlXSA9IHQyW25vZGVdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbGF6eTJbMipub2RlXSArPSBsYXp5Mltub2RlXTsKICAgICAgICAgICAgICAgIGxhenkyWzIqbm9kZV0gKz0gbGF6eTJbbm9kZV07CiAgICAgICAgICAgICAgICB0MlsyKm5vZGVdID0gdDJbbm9kZV07CiAgICAgICAgICAgIH0KCgogICAgICAgICAgICBpZih0MVsyKm5vZGUrMV0gPiB0MlsyKm5vZGUrMV0pIC8vLyB0aW1lIGluY3JlbWVudAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsYXp5MVsyKm5vZGUrMV0gKz0gbGF6eTJbbm9kZV07CiAgICAgICAgICAgICAgICBsYXp5MVsyKm5vZGUrMV0gKz0gbGF6eTJbbm9kZV07CiAgICAgICAgICAgICAgICB0MVsyKm5vZGUrMV0gPSB0Mltub2RlXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxhenkyWzIqbm9kZSsxXSArPSBsYXp5Mltub2RlXTsKICAgICAgICAgICAgICAgIGxhenkyWzIqbm9kZSsxXSArPSBsYXp5Mltub2RlXTsKICAgICAgICAgICAgICAgIHQyWzIqbm9kZSsxXSA9IHQyW25vZGVdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGxhenkyW25vZGVdID0gMDsKICAgIH0KICAgIHQxW25vZGVdID0gdDJbbm9kZV0gPSAwOwp9CnZvaWQgdXBkYXRlMShpbnQgbm9kZSxpbnQgTCxpbnQgUixpbnQgbCxpbnQgcixpbnQgdmFsKSAvLy8gYWRkIHZhbCB0byBhbGwgbnVtYmVyIFtsLHJdCnsKICAgIHNoaWZ0KG5vZGUsTCxSKTsKICAgIGlmKEwgPiBSIHx8IEwgPiByIHx8IFIgPCBsKQogICAgICAgIHJldHVybiA7CiAgICBpZihMID49IGwgJiYgUiA8PSByKQogICAgewogICAgICAgIGxhenkxW25vZGVdID0gdmFsOwogICAgICAgIHQxW25vZGVdID0gdGltZXI7CiAgICAgICAgc2hpZnQobm9kZSxMLFIpOwogICAgICAgIHJldHVybiA7CiAgICB9CgogICAgaW50IG1pZCA9IChMK1IpLzI7CiAgICB1cGRhdGUxKDIqbm9kZSxMLG1pZCxsLHIsdmFsKTsKICAgIHVwZGF0ZTEoMipub2RlKzEsbWlkKzEsUixsLHIsdmFsKTsKCiAgICBzcXVhcmVbbm9kZV0gPSBzcXVhcmVbMipub2RlXSArIHNxdWFyZVsyKm5vZGUrMV07CiAgICBzdW1pbmdbbm9kZV0gPSBzdW1pbmdb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HNxdWFyZVsyKm5vZGUrMV07CiAgICBzdW1pbmdbbm9kZV0gPSBzdW1pbmdbMipub2RlXSArIHN1bWluZ1syKm5vZGUrMV07Cn0KbGwgcXVlcnkoaW50IG5vZGUsaW50IEwsaW50IFIsaW50IGwsaW50IHIpCnsKICAgIGlmKEwgPiBSIHx8IEwgPiByIHx8IFIgPCBsKQogICAgICAgIHJldHVybiAwOwoKICAgIHNoaWZ0KG5vZGUsTCxSKTsKCiAgICBpZihMID49IGwgJiYgUiA8PSByKQogICAgICAgIHJldHVybiBzcXVhcmVbbm9kZV07CgogICAgaW50IG1pZCA9IChMICsgUikgLyAyOwogICAgbGwgc3VtMSA9IHF1ZXJ5KG5vZGUqMiwgTCwgbWlkLCBsLCByKTsKICAgIGxsIHN1bTIgPSBxdWVyeShub2RlKjIgKyAxLCBtaWQgKyAxLCBSLCBsLCByKTsKICAgIHJldHVybiAoc3VtMSArIHN1bTIpOwp9CgppbnQgbWFpbigpCnsKICAgIGludCB0LG51bSA9IDE7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBwcmludGYoIkNhc2UgJWQ6XG4iLG51bSsrKTsKICAgICAgICBpbml0KCk7CiAgICAgICAgdGltZXIgPSAwOwogICAgICAgIGludCBuLHE7CiAgICAgICAgY2luID4+IG4gPj4gcTsKICAgICAgICBmb3IoaW50IGk9MDsgaTxuOyBpKyspCiAgICAgICAgICAgIHNjYW5mKCIlZCIsJmFbaV0pOwogICAgICAgIGJ1aWxkKDEsMCxuLTEpOwogICAgICAgIHdoaWxlKHEtLSkKICAgICAgICB7CiAgICAgICAgICAgIHRpbWVyKys7CiAgICAgICAgICAgIGludCBjLHgseSx6OwogICAgICAgICAgICBzY2FuZigiJWQiLCZjKTsKICAgICAgICAgICAgaWYoYyA9PSAyKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQlZCIsJngsJnkpOwogICAgICAgICAgICAgICAgbGwgYW5zID0gcXVlcnkoMSwwLG4tMSx4LTEseS0xKTsKICAgICAgICAgICAgICAgIHByaW50ZigiJUk2NGRcbiIsYW5zKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHNjYW5mKCIlZCVkJWQiLCZ4LCZ5LCZ6KTsKICAgICAgICAgICAgICAgIGlmKGMgPT0gMSkKICAgICAgICAgICAgICAgICAgICB1cGRhdGUxKDEsMCxuLTEseC0xLHktMSx6KTsKICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB1cGRhdGUyKDEsMCxuLTEseC0xLHktMSx6KTsKCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgoKCiAgICByZXR1cm4gMDsKfQo=