题解:AT_arc201_e [ARC201E] Total Area of Bounding Boxes

· · 题解

考虑拆下贡献,对每块 1\times 1 的面积,计算有多少集合包含他。

对某块矩形,记其左上、右下、右上、左下的点的个数分别为 A,B,C,D。

容易发现某个集合包含他,等价于 A,B 每个集合选择至少一个或 C,D 每个集合选择至少一个。

所以简单容斥一下可以写出式子 (2^A-1)(2^B-1)2^C2^D+2^A2^B(2^C-1)(2^D-1)-(2^A-1)(2^B-1)(2^C-1)(2^D-1)。

展开一下得到 2^{A+B+C+D}-2^{A+C}-2^{A+D}-2^{B+C}-2^{B+D}+2^A+2^B+2^C+2^D-1。

注意到前五项和最后一项都与 Y 无关可以简单推式子解决。

考虑 2^A 的和怎么算。首先考虑 A 怎么算,注意到这是一个二维偏序,只需要从左往右扫描线,然后线段树支持区间加全局求和就好了。那么 2^A 只需要把区间加改成区间 \times2 即可。2^B,2^C,2^D 同理。

于是就做完了,总复杂度 O(n\log n)。

代码,非常好写。

#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;
}