Submission #555289

# Submission time Handle Problem Language Result Execution time Memory
555289 2022-04-30T11:17:22 Z new_acc Strah (COCI18_strah) C++14
110 / 110
217 ms 47508 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[2003][2003],n,m,fau[N],rep[N],pierw[N],dp[N];
vi g[N];
int Find(int a){
    if(fau[a]==a) return a;
    return fau[a]=Find(fau[a]);
}
void Union(int a,int b){
    fau[Find(a)]=Find(fau[b]);
}
int wzo(int dl){
    return dp[dl];
}
ll single(int i){
    for(int j=1;j<=m;j++){
        if(!t[i][j]) pierw[j]=-1;
        else{
            if(!t[i-1][j]){
                int curr=i;
                while(t[curr][j]) curr++;
                pierw[j]=curr-1;
            }
        }
        if(pierw[j]!=-1) g[pierw[j]].push_back(j);
    }
    ll curr=0,res=0;
    for(int j=n;j>=i;j--){
        for(auto u:g[j]){
            fau[u]=u;
            int dl=1;
            if(fau[u-1]) dl+=rep[Find(u-1)],curr-=wzo(rep[Find(u-1)]),Union(u,u-1);
            if(fau[u+1]) dl+=rep[Find(u+1)],curr-=wzo(rep[Find(u+1)]),Union(u,u+1);
            rep[Find(u)]=dl;
            curr+=(ll)wzo(dl);
        }
        res+=curr*(ll)(j-i+1);
    }
    for(int j=i;j<=n;j++) g[j].clear();
    for(int j=1;j<=m;j++) fau[j]=0,rep[j]=0;
    return res;
}
void solve(){
    cin>>n>>m;
    for(int i=1;i<=m;i++) dp[i]=dp[i-1]+((i*(i+1))>>1);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char a;
            cin>>a;
            t[i][j]=(a=='.');
        }
    }
    ll res=0;
    for(int i=1;i<=n;i++) res+=(ll)single(i);
    cout<<res<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    int tt=1;
    while(tt--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23876 KB Output is correct
2 Correct 13 ms 23880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 25416 KB Output is correct
2 Correct 16 ms 25436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 25380 KB Output is correct
2 Correct 16 ms 25440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 25368 KB Output is correct
2 Correct 17 ms 25512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 31176 KB Output is correct
2 Correct 117 ms 36480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 36888 KB Output is correct
2 Correct 179 ms 40136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 30644 KB Output is correct
2 Correct 129 ms 37244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 32392 KB Output is correct
2 Correct 152 ms 38596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 39604 KB Output is correct
2 Correct 187 ms 47508 KB Output is correct