1. 程式人生 > 其它 >NOIP 模擬 $38\; \rm a$

NOIP 模擬 $38\; \rm a$

題解

題解 \(by\;zj\varphi\)

壓行。

列舉兩行,將中間的行壓成一行,然後直接字首和加二分。

注意邊界細節問題。

Code
#include<bits/stdc++.h>
#define Re register
#define ri Re signed
#define p(i) ++i
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            Re bool f=0;x=0;Re char ch=getchar();
            while(!isdigit(ch)) f|=ch=='-',ch=getchar();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x, T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x, T y) {return x>y?y:x;}
    typedef long long ll;
    static const int N=5e4+7;
    int mat[33][N],sum[33][N],pre[N],n,m,l,r;
    char s[N];
    ll ans;
    inline int main() {
        //FI=freopen("nanfeng.in","r",stdin);
        //FO=freopen("nanfeng.out","w",stdout);
        cin >> n >> m;
        for (ri i(1);i<=n;p(i)) {
            scanf("%s",s+1);
            for (ri j(1);j<=m;p(j)) mat[i][j]=s[j]=='1',sum[i][j]=sum[i-1][j]+mat[i][j];
        }
        cin >> l >> r;
        for (ri i(1);i<=n;p(i)) 
            for (ri j(1);j<=i;p(j)) 
                for (ri k(1);k<=m;p(k)) {
                    pre[k]=pre[k-1]+sum[i][k]-sum[j-1][k];
                    if (pre[k]<l) continue;
                    int le=pre[k]-l;
                    int nm=std::upper_bound(pre,pre+k,le)-pre;
                    --nm;
                    ans+=nm+1;
                    if (pre[k]<=r) continue;
                    int re=pre[k]-r;
                    nm=std::lower_bound(pre,pre+k,re)-pre;
                    ans-=nm;
                }
        printf("%lld\n",ans);
        return 0;
    }
}
int main() {return nanfeng::main();}