#6029. 「雅礼集训 2017 Day1」市场
用线段树维护数列,区间上维护最大最小值,区间和还有标记,修改时,区间加直接做,而区间除时,递归到线段树上某一区间,如果这一操作等价于区间加(也就是最大和最小值变化量相等),那么就直接打加法标记,不用递归。
#include<bits/stdc++.h>using namespace std;
using ll=long long;
template <class T=int> T rd()
{T res=0;T fg=1;char ch=getchar();while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();return res*fg;
}
const int N=200010;
const ll INF=0x3f3f3f3f3f3f3f3fll;
struct node
{int l,r;ll v,mn,mx;ll tag;
}tree[N<<2];
int a[N],n,m;
void pushup(int u)
{tree[u].mn=min(tree[u<<1].mn,tree[u<<1|1].mn);tree[u].mx=max(tree[u<<1].mx,tree[u<<1|1].mx);tree[u].v=tree[u<<1].v+tree[u<<1|1].v;
}
void put(int u,int v)
{tree[u].v+=1ll*(tree[u].r-tree[u].l+1)*v;tree[u].mn+=v,tree[u].mx+=v;tree[u].tag+=v;
}
void pushdown(int u)
{if(!tree[u].tag) return;put(u<<1,tree[u].tag);put(u<<1|1,tree[u].tag);tree[u].tag=0;
}
ll Div(ll a,ll d)
{if(a%d==0) return a/d-a;else if(a>=0) return a/d-a;else return a/d-a-1;
}
void build(int u,int l,int r)
{tree[u]={l,r,0,INF,-INF,0};if(l==r) return tree[u].v=tree[u].mn=tree[u].mx=a[l],void();int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);
}
void modify1(int u,int l,int r,int v)
{if(l<=tree[u].l&&tree[u].r<=r) return put(u,v),void();pushdown(u);int mid=tree[u].l+tree[u].r>>1;if(l<=mid) modify1(u<<1,l,r,v);if(r>mid) modify1(u<<1|1,l,r,v);pushup(u);
}
void modify2(int u,int l,int r,int d)
{if(l<=tree[u].l&&tree[u].r<=r) if(Div(tree[u].mn,d)==Div(tree[u].mx,d)) return put(u,Div(tree[u].mx,d)),void();pushdown(u);int mid=tree[u].l+tree[u].r>>1;if(l<=mid) modify2(u<<1,l,r,d);if(r>mid) modify2(u<<1|1,l,r,d);pushup(u);
}
ll qmin(int u,int l,int r)
{if(l<=tree[u].l&&tree[u].r<=r) return tree[u].mn;pushdown(u);int mid=tree[u].l+tree[u].r>>1;ll v=INF;if(l<=mid) v=min(v,qmin(u<<1,l,r));if(r>mid) v=min(v,qmin(u<<1|1,l,r));return v;
}
ll qsum(int u,int l,int r)
{if(l<=tree[u].l&&tree[u].r<=r) return tree[u].v;pushdown(u);int mid=tree[u].l+tree[u].r>>1;ll v=0;if(l<=mid) v+=qsum(u<<1,l,r);if(r>mid) v+=qsum(u<<1|1,l,r);return v;
}
int main()
{n=rd(),m=rd();for(int i=1;i<=n;i++) a[i]=rd();build(1,1,n);while(m--){int op=rd(),l=rd()+1,r=rd()+1,c;if(op==1) {c=rd();modify1(1,l,r,c);}else if(op==2){c=rd();modify2(1,l,r,c);}else if(op==3) printf("%lld\n",qmin(1,l,r));else printf("%lld\n",qsum(1,l,r));}return 0;
}