#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAXN=1e5+5;
const LL INF=2e18;
LL a[MAXN];
struct Node{
int left,right;
LL mi,ma;
LL sum;
LL add;
int set;
}tree[MAXN*4];
void PushUp(int rt){
int lson=rt<<1,rson=rt<<1|1;
tree[rt].mi=min(tree[lson].mi,tree[rson].mi);
tree[rt].ma=max(tree[lson].ma,tree[rson].ma);
tree[rt].sum=tree[lson].sum+tree[rson].sum;
}
void PushDown(int rt){
if(tree[rt].left<tree[rt].right){
int lson=rt<<1,rson=rt<<1|1;
if(tree[rt].set!=-1){
tree[lson].set=tree[rt].set;tree[rson].set=tree[rt].set;
tree[lson].add=tree[rson].add=0;
tree[lson].mi=tree[rson].mi=tree[rt].set;
tree[lson].ma=tree[rson].ma=tree[rt].set;
tree[lson].sum=(LL)tree[rt].set*(tree[lson].right-tree[lson].left+1);
tree[rson].sum=(LL)tree[rt].set*(tree[rson].right-tree[rson].left+1);
tree[rt].set=-1;
}
if(tree[rt].add){
tree[lson].add+=tree[rt].add;tree[rson].add+=tree[rt].add;
tree[lson].mi+=tree[rt].add;tree[rson].mi+=tree[rt].add;
tree[lson].ma+=tree[rt].add;tree[rson].ma+=tree[rt].add;
tree[lson].sum+=(LL)tree[rt].add*(tree[lson].right-tree[lson].left+1);
tree[rson].sum+=(LL)tree[rt].add*(tree[rson].right-tree[rson].left+1);
tree[rt].add=0;
}
}
}
void Build(int rt,int L,int R){
tree[rt].left=L;tree[rt].right=R;tree[rt].add=0;tree[rt].set=-1;
if(L==R) {tree[rt].sum=tree[rt].mi=tree[rt].ma=a[L]; return;}
int mid=(tree[rt].left+tree[rt].right)>>1;
Build(rt<<1,L,mid);
Build(rt<<1|1,mid+1,R);
PushUp(rt);
}
LL QuerySum(int rt,int L,int R){
if(R<tree[rt].left||L>tree[rt].right) return 0;
if(L<=tree[rt].left&&tree[rt].right<=R) return tree[rt].sum;
PushDown(rt);
int mid=(tree[rt].left+tree[rt].right)>>1;
LL res=0;
if(L<=mid) res+=QuerySum(rt<<1,L,R);
if(R>mid) res+=QuerySum(rt<<1|1,L,R);
PushUp(rt);
return res;
}
LL QueryMin(int rt,int L,int R){
if(L<=tree[rt].left&&tree[rt].right<=R) return tree[rt].mi;
PushDown(rt);
int mid=(tree[rt].left+tree[rt].right)>>1;
LL res=INF;
if(L<=mid) res=min(res,QueryMin(rt<<1,L,R));
if(R>mid) res=min(res,QueryMin(rt<<1|1,L,R));
PushUp(rt);
return res;
}
LL QueryMax(int rt,int L,int R){
if(L<=tree[rt].left&&tree[rt].right<=R) return tree[rt].ma;
PushDown(rt);
int mid=(tree[rt].left+tree[rt].right)>>1;
LL res=-INF;
if(L<=mid) res=max(res,QueryMax(rt<<1,L,R));
if(R>mid) res=max(res,QueryMax(rt<<1|1,L,R));
PushUp(rt);
return res;
}
void UpdateAdd(int rt,int L,int R,int x){
if(L<=tree[rt].left&&tree[rt].right<=R){
tree[rt].add+=x;
tree[rt].sum+=(LL)x*(tree[rt].right-tree[rt].left+1);
tree[rt].mi+=x;tree[rt].ma+=x;
return;
}
PushDown(rt);
int mid=(tree[rt].left+tree[rt].right)>>1;
if(L<=mid) UpdateAdd(rt<<1,L,R,x);
if(R>mid) UpdateAdd(rt<<1|1,L,R,x);
PushUp(rt);
}
void Display(int rt){
cout<<"-------------"<<endl;
cout<<"id: "<<rt<<endl;
cout<<"["<<tree[rt].left<<","<<tree[rt].right<<"]"<<endl;
cout<<"mi: "<<tree[rt].mi<<endl;
cout<<"ma: "<<tree[rt].ma<<endl;
cout<<"sum: "<<tree[rt].sum<<endl;
cout<<"add: "<<tree[rt].add<<endl;
cout<<"set: "<<tree[rt].set<<endl;
}
void bfs(int rt){
queue<int> q;
while(!q.empty()) q.pop();
q.push(rt);
while(!q.empty()){
int fst=q.front();
q.pop();
Display(fst);
if(tree[fst].right>tree[fst].left){
q.push(fst<<1);
q.push(fst<<1|1);
}
}
}
void UpdateSet(int rt,int L,int R,int x){
if(L<=tree[rt].left&&tree[rt].right<=R){
tree[rt].set=x;
tree[rt].sum=(LL)x*(tree[rt].right-tree[rt].left+1);
tree[rt].mi=x;tree[rt].ma=x;
tree[rt].add=0;
return;
}
PushDown(rt);
int mid=(tree[rt].left+tree[rt].right)>>1;
if(L<=mid) UpdateSet(rt<<1,L,R,x);
if(R>mid){
UpdateSet(rt<<1|1,L,R,x);
}
PushUp(rt);
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
Build(1,1,n);
int op,x,y,z;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&op,&x,&y);
if(op==1){
printf("Case #%d:\n",i);
printf("%lld\n",QuerySum(1,x,y));
}else if(op==2){
printf("Case #%d:\n",i);
printf("%lld\n",QueryMax(1,x,y));
}else if(op==3){
printf("Case #%d:\n",i);
printf("%lld\n",QueryMin(1,x,y));
}else if(op==4){
scanf("%d",&z);
UpdateAdd(1,x,y,z);
}else{
scanf("%d",&z);
UpdateSet(1,x,y,z);
}
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) ((x)&(-(x)))
using namespace std;
typedef long long LL;
const int MAXN=2e5+5;
int val[MAXN];
struct node
{
int tot;
int left,right;
int mark;
}tree[MAXN*4];
int build(int rt,int left,int right)
{
tree[rt].mark=0;
tree[rt].left=left;
tree[rt].right=right;
if(left==right) return tree[rt].tot=val[left];
int mid=(left+right)/2;
int a=build(lson,left,mid);
int b=build(rson,mid+1,right);
return tree[rt].tot=a+b;
}
void update_mark(int rt)
{
if(tree[rt].mark)
{
tree[rt].tot=tree[rt].mark*(tree[rt].right-tree[rt].left+1);
if(tree[rt].left!=tree[rt].right)
tree[lson].mark=tree[rson].mark=tree[rt].mark;
tree[rt].mark=0;
}
}
int calc(int rt,int left,int right)
{
update_mark(rt);
if(tree[rt].left>right||tree[rt].right<left) return 0;
if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot;
int a=calc(lson,left,right);
int b=calc(rson,left,right);
return a+b;
}
int update(int rt,int left,int right,int val)
{
update_mark(rt);
if(tree[rt].left>right||tree[rt].right<left) return tree[rt].tot;
if(tree[rt].left>=left&&tree[rt].right<=right)
{
tree[rt].mark=val;
return tree[rt].tot=val*(tree[rt].right-tree[rt].left+1);
}
int a=update(lson,left,right,val);
int b=update(rson,left,right,val);
return tree[rt].tot=a+b;
}
int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)
{
val[i]=1;
}
build(1,1,n);
int x,y,z;
for(int i=1;i<=q;++i)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,calc(1,1,n));
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) ((x)&(-(x)))
using namespace std;
typedef long long LL;
const int MAXN=1e5+5;
int val[MAXN];
struct node
{
LL tot,add;
int left,right;
}tree[MAXN*4];
void pushup(int rt)
{
tree[rt].tot=tree[lson].tot+tree[rson].tot;
}
void pushdown(int rt,int m)
{
if(tree[rt].add)
{
tree[lson].add+=tree[rt].add;
tree[rson].add+=tree[rt].add;
tree[lson].tot+=(m-(m>>1))*tree[rt].add;
tree[rson].tot+=(m>>1)*tree[rt].add;
tree[rt].add=0;
}
}
void build(int rt,int left,int right)
{
tree[rt].add=0;
tree[rt].left=left;
tree[rt].right=right;
if(left==right) {tree[rt].tot=val[left];return;}
int mid=(left+right)>>1;
build(lson,left,mid);
build(rson,mid+1,right);
pushup(rt);
}
LL query(int rt,int left,int right)
{
if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot;
pushdown(rt,tree[rt].right-tree[rt].left+1);
int mid=(tree[rt].left+tree[rt].right)>>1;
LL res=0;
if(left<=mid) res+=query(lson,left,right);
if(right>mid) res+=query(rson,left,right);
return res;
}
void update(int rt,int left,int right,int val)
{
if(left<=tree[rt].left&&tree[rt].right<=right)
{
tree[rt].add+=val;
tree[rt].tot+=(LL)val*(tree[rt].right-tree[rt].left+1);
return;
}
pushdown(rt,tree[rt].right-tree[rt].left+1);
int mid=(tree[rt].left+tree[rt].right)>>1;
if(left<=mid) update(lson,left,right,val);
if(right>mid) update(rson,left,right,val);
pushup(rt);
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=1;i<=n;++i)
{
scanf("%d",&val[i]);
}
build(1,1,n);
char op;
int x,y,z;
for(int i=1;i<=q;++i)
{
getchar();
scanf("%c",&op);
if(op=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,x,y));
}else
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
}
}
return 0;
}