题解:AT_arc201_e [ARC201E] Total Area of Bounding Boxes
考虑拆下贡献,对每块
对某块矩形,记其左上、右下、右上、左下的点的个数分别为
容易发现某个集合包含他,等价于
所以简单容斥一下可以写出式子
展开一下得到
注意到前五项和最后一项都与
考虑
于是就做完了,总复杂度
代码,非常好写。
#include<bits/stdc++.h>
#define ll long long
#define p 998244353ll
#define N 200009
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
ll tr[N<<2],tg[N<<2];
void up(int rt){
tr[rt]=(tr[ls]+tr[rs])%p;
}
void push(int rt,ll x){
tr[rt]=tr[rt]*x%p;
tg[rt]=tg[rt]*x%p;
}
void down(int rt){
if(tg[rt]^1){
push(ls,tg[rt]);
push(rs,tg[rt]);
tg[rt]=1;
}
}
void build(int rt,int l,int r){
tg[rt]=1;
if(l==r){tr[rt]=1;}
else{
int mid=l+r>>1;
build(ls,l,mid);
build(rs,mid+1,r);
up(rt);
}
}
void upd(int rt,int l,int r,int ul,int ur){
if(ul>r||ur<l)return;
else if(ul<=l&&ur>=r)push(rt,2);
else{
int mid=l+r>>1;down(rt);
upd(ls,l,mid,ul,ur);upd(rs,mid+1,r,ul,ur);
up(rt);
}
}
int n,a[N];
ll ans;
void calc(){
build(1,1,n-1);
for(int i=1;i<n;i++){
upd(1,1,n-1,a[i],n-1);
ans+=tr[1];
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;for(int i=1;i<=n;i++)cin>>a[i];
ll tmp=1;
for(int i=1;i<n;i++){
tmp=tmp*2%p;
ans-=tmp;
}ans=ans%p*4ll*(n-1)%p;
ans+=2ll*(n-1)*(n-1)%p*tmp%p;
ans-=1ll*(n-1)*(n-1)%p;
calc();reverse(a+1,a+n+1);calc();
for(int i=1;i<=n;i++)a[i]=n+1-a[i];
calc();reverse(a+1,a+n+1);calc();
cout<<(ans%p+p)%p<<"\n";return 0;
}