1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<bits/stdc++.h> #define ll long long #define mp make_pair using namespace std; const int mxn=1e5+5; ll n,m,sn,a[mxn],b[mxn],ta[444],sb[444],tb[444],ans,th,tl,tr; int main(){ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>m,sn=sqrt(n); for(int i=0;i<n;++i)a[i]=i+1; for(;m--;){ int tmp,l,r;cin>>tmp>>l>>r,--l,--r; if(tmp==1){ int x;cin>>x; for(int i=l;i<=r;){ th=i/sn,tl=th*sn,tr=min(tl+sn,n); if(i==tl and r>=tr-1){ if(ta[th])tb[th]+=abs(ta[th]-x),sb[th]+=abs(ta[th]-x)*(tr-tl); else for(int i2=tl;i2<tr;++i2)b[i2]+=abs(a[i2]-x),sb[th]+=abs(a[i2]-x); ta[th]=x,i=tr; }else{ if(ta[th]){ for(int i2=tl;i2<tr;++i2)a[i2]=ta[th]; ta[th]=0; } b[i]+=abs(a[i]-x),sb[th]+=abs(a[i]-x),a[i]=x,++i; } } }else{ ans=0; for(int i=l;i<=r;){ tl=i/sn*sn,tr=min(tl+sn,n),th=i/sn; if(i==tl and r>=tr-1)ans+=sb[th],i=tr; else ans+=tb[th]+b[i],++i; } cout<<ans<<endl; } } return 0; }
|