NC213129. BoundingBox
描述
输入描述
The input consists of several test cases terminated by end-of-file. For each test case:
The first line contains two integers n and m -- the size of the matrix.
The second line contains integers , , , , where is the value in the i-th row and the j-th column.
*
*
* It is guaranteed that all cells which contain the same value are 4-side connected.
* It is guaranteed that the sum of in all test cases does not exceed .
输出描述
For each test case, output an integer denoting the value of , where denotes the exclusive-or (XOR) operator.
示例1
输入:
4 2 4 8 4 4 4 2 2 2 2 7 12 12 12 13 8 9 14 12 12 7 4 10 11 5 3 5 13 13 3 3 14 2 2 1 1 11 2 2 1 5 7
输出:
20 93 56
C++(clang++11) 解法, 执行用时: 3205ms, 内存消耗: 148968K, 提交时间: 2020-10-31 16:39:03
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1001000; ll ret,ans; int a[N],le[N],vis[N],cnt,ri[N],up[N],dw[N],n,m; inline int id(int x,int y){return (x-1)*m+y;} vector<int>g[N]; bool in_test(int x,int y){return le[y]<=le[x]&&ri[x]<=ri[y]&&up[y]<=up[x]&&dw[x]<=dw[y];} bool li_test(int x,int y){return ri[x]<le[y]||ri[y]<le[x]||dw[x]<up[y]||dw[y]<up[x];} void add(int x,int y){ y=a[y];if(x==y)return; if(in_test(x,y)||in_test(y,x)||li_test(x,y))return; g[x].push_back(y);g[y].push_back(x); } void chk(int x,int y){ if(x==y||vis[y]==cnt)return; vis[y]=cnt; ret+=y; } int main(){ ios::sync_with_stdio(0);cin.tie(0); for(;cin>>n>>m;){ for(int i=1;i<=n*m;++i) le[i]=n+1,ri[i]=0,up[i]=m+1,dw[i]=0,g[i].clear(); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){ int x;cin>>x;a[id(i,j)]=x; le[x]=min(le[x],i);ri[x]=max(ri[x],i); up[x]=min(up[x],j);dw[x]=max(dw[x],j); } ans=0; for(int i=1;i<=n*m;++i) if(le[i]<=ri[i]&&up[i]<=dw[i]){ int L=ri[i]-le[i]+1,R=dw[i]-up[i]+1; for(int j=max(1,le[i]-L);j<=min(n,ri[i]+L);++j) add(i,id(j,up[i])), add(i,id(j,dw[i])); for(int j=max(1,up[i]-R);j<=min(m,dw[i]+R);++j) add(i,id(le[i],j)), add(i,id(ri[i],j)); } for(int i=1;i<=n*m;++i)if(le[i]<=ri[i]&&up[i]<=dw[i]){ ++cnt;ret=0; for(int j:g[i])chk(i,j); ans+=ret^i; } cout<<ans<<'\n'; } return 0; }