题目背景好评,约战好评。
赋值为等差数列可以用三元组 表示区间,于是可以颜色段均摊。
全局逆序对已经明示你拆贡献了,那我们就拆个贡献吧。
加入/删除一个颜色段 会使答案增加/减少:
以下我们认为 ,也就是颜色段的值域区间。
由于公差为 ,所以颜色段内部没有贡献。
对于所有的在 的并且大小在 或在 的并且大小在 的数会与该颜色段中每个数产生贡献,故还要乘上 。
还有一种数存在贡献,就是不在区间 内并且大小在 以内的数。 设该数为 。
若其在 ,则其贡献为 中比他小的数,所以是 。
若其在 ,则其贡献为 中比他大的数,所以是 。
不难发现,我们需要维护区间赋值为等差数列,询问一个区间中大小在某值域区间中的数的个数与和。
考虑序列分块,但是修改不好维护,所以应该没法套值域分块,那我们就对值域在每个块上开两个树状数组,分别维护两种信息。
然后散块修改可以暴力重构。整块打标记,用 表示块被赋值为从 开始的等差数列。
询问时对于没有标记的直接树状数组。
有标记的直接计算:
设一个有标记块内值域出现情况为 ,询问值域区间为 。
则有贡献区间 ,其中 。
若 ,则大小在 的数有 个,这些数的和为 。
若 ,两者均为 。
这样的复杂度是 ,比标程差得多,但是常数小,成功跑到最优解。
最后给一份没卡常的代码。
#include<bits/stdc++.h>
#define IT set<node>::iterator
#define rd read()
#define gc pa == pb && (pb = (pa = buf) + fread(buf, 1, 100000, stdin), pa == pb) ? EOF : *pa++
#define max(x,y) (x>y?x:y)
using namespace std;
static char buf[100000], * pa(buf), * pb(buf);
inline int read()
{
unsigned int x=0,s=gc;
while(!isdigit(s))s=gc;
while(isdigit(s))x=(x<<1)+(x<<3)+(s^48),s=gc;
return x;
}
const int N=30005,S=3005,V=30001;
int n,m,block,tot,a[N],bl[N],L[S],R[S],tag[N];
struct BIT
{
int c[V+5];
inline void add(int x,int y){if(!x)return;while(x<=V)c[x]+=y,x+=(x&-x);}
inline int ask(int x){int s=0;while(x)s+=c[x],x^=(x&-x);return s;}
}B[S],M[S],G;
struct node
{
int l,r,v;
node(int L,int R=0,int V=0):l(L),r(R),v(V){}
inline bool operator <(const node &o)const{return l<o.l;}
};
struct ODT
{
set<node> s;
int L,R,V;
inline IT split(int x)
{
IT it=s.lower_bound(node(x));
if(it!=s.end()&&it->l==x)return it;
--it,L=it->l,R=it->r,V=it->v;
s.erase(it),s.insert(node(L,x-1,V));
return s.insert(node(x,R,V+x-L)).first;
}
}T;
inline void pushdown(int id)
{
if(tag[id])
{
int cnt=tag[id];
for(int i=L[id];i<=R[id];i++)B[id].add(a[i],-1),M[id].add(a[i],-a[i]);
for(int i=L[id];i<=R[id];cnt++,i++)a[i]=cnt,B[id].add(a[i],1),M[id].add(a[i],a[i]);
tag[id]=0;
}
}
inline void add(int l,int r,int v)
{
if(bl[l]==bl[r])
{
pushdown(bl[l]);
int cnt=v,id=bl[l];
for(int i=l;i<=r;i++)B[id].add(a[i],-1),M[id].add(a[i],-a[i]);
for(int i=l;i<=r;cnt++,i++)a[i]=cnt,B[id].add(a[i],1),M[id].add(a[i],a[i]);
return;
}
add(l,R[bl[l]],v),add(L[bl[r]],r,v+L[bl[r]]-l);int cnt=v+L[bl[l]+1]-l;
for(int i=bl[l]+1;i<bl[r];cnt+=R[i]-L[i]+1,i++)tag[i]=cnt;
}
inline int askcnt(int l,int r,int lv,int rv)
{
if(l>r||lv>rv)return 0;
int cnt=0;
if(bl[l]==bl[r])
{
pushdown(bl[l]);
for(int i=l;i<=r;i++)cnt+=(lv<=a[i]&&a[i]<=rv);
return cnt;
}
cnt=askcnt(l,R[bl[l]],lv,rv)+askcnt(L[bl[r]],r,lv,rv);
for(int i=bl[l]+1;i<bl[r];i++)
if(tag[i])
{
int LL=max(lv,tag[i]),RR=min(rv,tag[i]+R[i]-L[i]);
if(LL<=RR)cnt+=RR-LL+1;
}
else cnt+=B[i].ask(rv)-B[i].ask(lv-1);
return cnt;
}
inline int asksum(int l,int r,int lv,int rv)
{
if(l>r||lv>rv)return 0;
int cnt=0;
if(bl[l]==bl[r])
{
pushdown(bl[l]);
for(int i=l;i<=r;i++)cnt+=((lv<=a[i]&&a[i]<=rv)*a[i]);
return cnt;
}
cnt=asksum(l,R[bl[l]],lv,rv)+asksum(L[bl[r]],r,lv,rv);
for(int i=bl[l]+1;i<bl[r];i++)
if(tag[i])
{
int LL=max(lv,tag[i]),RR=min(rv,tag[i]+R[i]-L[i]);
if(LL<=RR)cnt+=((RR+1ll)*RR-(LL-1ll)*LL)/2ll;
}
else cnt+=M[i].ask(rv)-M[i].ask(lv-1);
return cnt;
}
int res=0;
inline void assign(int l,int r,int v)
{
IT it2=T.split(r+1),it=T.split(l);
for(IT i=it;i!=it2;i++)
{
int lq=i->l,rq=i->r,lv=i->v,rv=i->v+rq-lq;
res-=(askcnt(1,l-1,rv+1,V)+askcnt(rq+1,n,1,lv-1))*(rq-lq+1);
res-=asksum(1,l-1,lv,rv)-askcnt(1,l-1,lv,rv)*lv;
res-=askcnt(rq+1,n,lv,rv)*rv-asksum(rq+1,n,lv,rv);
}
T.s.erase(it,it2),T.s.insert(node(l,r,v));
int lv=v,rv=v+r-l;add(l,r,v);
res+=(askcnt(1,l-1,rv+1,V)+askcnt(r+1,n,1,lv-1))*(r-l+1);
res+=asksum(1,l-1,lv,rv)-askcnt(1,l-1,lv,rv)*lv;
res+=askcnt(r+1,n,lv,rv)*rv-asksum(r+1,n,lv,rv);
}
signed main()
{
n=rd,m=rd;block=sqrt(n),tot=(n-1)/block+1;
for(int i=1;i<=n;bl[i]=(i-1)/block+1,i++)a[i]=rd;
for(int i=n;i>=1;i--)res+=G.ask(a[i]-1),G.add(a[i],1);
for(int i=1;i<=tot;i++)L[i]=R[i-1]+1,R[i]=i*block;R[tot]=n;
for(int i=1;i<=n;i++)B[bl[i]].add(a[i],1),M[bl[i]].add(a[i],a[i]),T.s.insert(node(i,i,a[i]));
for(int i=1,op,l,r;i<=m;i++)
{
op=rd;
if(op==1)l=rd,r=rd,assign(l,r,rd);
else cout<<res<<'\n';
}
return 0;
}