How F?
  • 板块学术版
  • 楼主New_Void
  • 当前回复10
  • 已保存回复10
  • 发布时间2025/6/14 21:47
  • 上次更新2025/6/15 17:14:22
查看原帖
How F?
1048576
New_Void楼主2025/6/14 21:47

复杂度似乎是对的,但是TLE了

#include <bits/stdc++.h>
using namespace std;
#define int long long
int count0(int*a,int n){
    unordered_map<int,int>m;
    m.reserve(n+1);
    m[0]=1;
    int c=0;
    int s=0;
    for(int i=0;i<n;i++){
        s+=a[i];
        auto it=m.find(s);
        if(it!=m.end()){
            c+=it->second;
        }
        m[s]++;
    }
    return c;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--){
        int h,w;
        cin>>h>>w;
        vector<string> g(h);
        for(int i=0;i<h;i++){
            cin>>g[i];
        }
        bool tran=false;
        if(h>w){
            tran=true;
            vector<string> ng(w,string(h,' '));
            for(int i=0;i<h;i++)
                for(int j=0;j<w;j++)
                    ng[j][i]=g[i][j];
            swap(h,w);
            g=move(ng);
        }
        vector<vector<int>> mat(h,vector<int>(w));
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                mat[i][j]=(g[i][j]=='#')?1:-1;
        int ans=0;
        vector<int>colSum(w,0);
        for(int u=0;u<h;u++){
            fill(colSum.begin(),colSum.end(),0);
            for(int d=u;d<h;d++){
                for(int j=0;j<w;j++){
                    colSum[j]+=mat[d][j];
                }
                ans+=count0(colSum.data(),w);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

2025/6/14 21:47
加载中...