codeforces 444C 分块标记
标记当前区间是否完全相等
#include<bits/stdc++.h> #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) #define ll long long using namespace std; const int maxn=1e5+10,maxm=sqrt(1e5+1)+10; int n,m; ll a[maxn],b[maxn]; class sqblock{public: int id[maxn],sz,cnt; struct block{int l,r;ll sum,taga,tagb;}node[maxm]; void pushdown(int now){ block &nd=node[now]; if(nd.taga) rep(i,nd.l,nd.r) a[i]=nd.taga; nd.taga=0; } void init(int n){ sz=sqrt(n+0.5); rep(i,1,n) id[i]=(i-1)/sz+1;cnt=id[n]; rep(i,1,cnt) node[i].l=(i-1)*sz+1; rep(i,1,cnt-1) node[i].r=i*sz; node[cnt].r=n; } void update(int s,int t,ll x){ int posl=id[s],posr=id[t]; pushdown(posl);pushdown(posr); if(posl==posr){ rep(i,s,t) { node[posl].sum+=abs(a[i]-x); b[i]+=abs(a[i]-x); a[i]=x; } return ; } rep(i,s,node[posl].r){ b[i]+=abs(a[i]-x); node[posl].sum+=abs(a[i]-x); a[i]=x; } rep(i,node[posr].l,t){ b[i]+=abs(a[i]-x); node[posr].sum+=abs(a[i]-x); a[i]=x; } rep(i,posl+1,posr-1){ if(node[i].taga){ node[i].tagb+=abs(x-node[i].taga); node[i].sum+=1ll*abs(x-node[i].taga)*(node[i].r-node[i].l+1); node[i].taga=x; }else { rep(j,node[i].l,node[i].r){ b[j]+=abs(a[j]-x); node[i].sum+=abs(a[j]-x); a[j]=x; } node[i].taga=x; } } } ll query(int s,int t){ int posl=id[s],posr=id[t];ll ans=0; if(posl==posr) { rep(i,s,t) ans+=b[i]+node[posl].tagb; return ans; } rep(i,s,node[posl].r) ans+=b[i]+node[posl].tagb; rep(i,node[posr].l,t) ans+=b[i]+node[posr].tagb; rep(i,posl+1,posr-1) ans+=node[i].sum; return ans; } }square; int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n>>m; rep(i,1,n) a[i]=i; square.init(n); int a,b,c; while(m--){ cin>>a; if(a==1){ cin>>a>>b>>c; square.update(a,b,c); }else { cin>>a>>b; cout<<square.query(a,b)<<'\n'; } } }