题解:P6404 [COCI 2014/2015 #2] BOB

· · 题解

P6404 [COCI 2014/2015 #2] BOB

这个题感觉是仓鼠窝的进化版,不知道为什么颜色反而更浅了,做此题前可以先做一下仓鼠窝,处理方式差不多。

思路

Code

#include<bits/stdc++.h>
#include<bits/extc++.h>
#define int long long
#define f(i,j,k) for(int i=j;i<=k;i++)
#define F(i,j,k) for(int i=j;i>=k;i--)
char Buf[1<<23],*P1=Buf,*P2=Buf;
#define getchar() (P1==P2&&(P2=(P1=Buf)+fread(Buf,1,1<<23,stdin),P1==P2)?EOF:*P1++)
const int maxn=1e3+10;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline double readDouble(){
    double x=0; int f=1,cnt=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
    if(ch=='.'){
        ch=getchar();
        while (ch>='0'&&ch<='9') x+=(ch-48)/std::pow(10,cnt),ch=getchar(),cnt++;
    }
    return x*f;
}
inline void write(int x){
    if(x<0) {x=~(x-1); putchar('-');}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int n=read(),m=read(),a[maxn][maxn],pos[maxn][maxn],stack[maxn],top,ans,sum[maxn];
inline void work(){
    f(i,1,n){
        top=0;
        f(j,1,m){
            a[i][j]=read();
            if(a[i][j]==a[i-1][j]) pos[i][j]=pos[i-1][j];
            else pos[i][j]=i;
            if(a[i][j]!=a[i][j-1]) stack[top=1]=j,sum[j]=i-pos[i][j]+1;
            else{
                while(top&&pos[i][stack[top]]<=pos[i][j]) top--;
                if(!top) ++top,sum[j]=(j-stack[top]+1)*(i-pos[i][j]+1),stack[++top]=j;
                else stack[++top]=j,sum[j]=sum[stack[top-1]]+(i-pos[i][j]+1)*(j-stack[top-1]);
            }
            ans+=sum[j];
        }
    }
    write(ans);
}
signed main(){work();return !!!!!("YZren");}