CSES - Datatähti 2025 alku - Results
Submission details
Task:Niitty
Sender:maweiyin24562
Submission time:2024-11-03 12:19:14 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'bool check(int, int, int, int)':
input/code.cpp:19:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   19 |         for(int i=1;i<=C;i+=3)
      |         ^~~
input/code.cpp:22:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   22 |                 if(sum(x1,y1,x2,y2,i+1)==0&&i+1<=C)
      |                 ^~
input/code.cpp:22:36: error: 'i' was not declared in this scope
   22 |                 if(sum(x1,y1,x2,y2,i+1)==0&&i+1<=C)
      |                                    ^
input/code.cpp:7:41: note: in definition of macro 'sum'
    7 | #define sum(x1,y1,x2,y2,id) pre[x2][y2][id]-pre[x1-1][y2][id]-pre[x2][y1-1][id]+pre[x1-1][y1-1][id]
      |                                         ^~
input/code.cpp:24:36: error: 'i' was not declared in this scope
   24 |                 if(sum(x1,y1,x2,y2,i+2)==0&&i+2<=C)
      |                                    ^
input/code.cpp:7:41: not...

Code

#include <bits/stdc++.h>

using namespace std;

#define IC(c) (int)(c-'A')

#define sum(x1,y1,x2,y2,id) pre[x2][y2][id]-pre[x1-1][y2][id]-pre[x2][y1-1][id]+pre[x1-1][y1-1][id]
const int N=509;
const int F=26;
int n,C;

int pre[N][N][F+1];
//pre[i][j][c] : prefix sum
//compact[j][c] for every array[top,btm]
//the compacted prefix sum
int idx[F+1];//char-int list

bool check(int x1,int y1,int x2,int y2){
	for(int i=1;i<=C;i+=3)
		if(sum(x1,y1,x2,y2,i)==0)
			return false;
		if(sum(x1,y1,x2,y2,i+1)==0&&i+1<=C)
			return false;
		if(sum(x1,y1,x2,y2,i+2)==0&&i+2<=C)
			return false;
	return true;
//	return true&&sum(x1,y1,x2,y2,1)&&sum(x1,y1,x2,y2,2)&&sum(x1,y1,x2,y2,3)&&sum(x1,y1,x2,y2,4)&&sum(x1,y1,x2,y2,5)
//	&&sum(x1,y1,x2,y2,6)&&sum(x1,y1,x2,y2,7)&&sum(x1,y1,x2,y2,8)&&sum(x1,y1,x2,y2,9)&&sum(x1,y1,x2,y2,10)
//	&&sum(x1,y1,x2,y2,11)&&sum(x1,y1,x2,y2,12)&&sum(x1,y1,x2,y2,13)&&sum(x1,y1,x2,y2,14)&&sum(x1,y1,x2,y2,15)
//	&&sum(x1,y1,x2,y2,16)&&sum(x1,y1,x2,y2,17)&&sum(x1,y1,x2,y2,18)&&sum(x1,y1,x2,y2,19)&&sum(x1,y1,x2,y2,20)
//	&&sum(x1,y1,x2,y2,21)&&sum(x1,y1,x2,y2,22)&&sum(x1,y1,x2,y2,23)&&sum(x1,y1,x2,y2,24)&&sum(x1,y1,x2,y2,25)
//	&&sum(x1,y1,x2,y2,26);
}
int ans;
//sum up answer

int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			char c;
			cin>>c;
			int id=IC(c);
			if(!idx[id]){
				C++;
				idx[id]=C;
			}
			for(int k=1;k<=C;k++){
				if(k==idx[id])
					pre[i][j][k]=pre[i-1][j][k]+pre[i][j-1][k]-pre[i-1][j-1][k]+1;
				else
					pre[i][j][k]=pre[i-1][j][k]+pre[i][j-1][k]-pre[i-1][j-1][k];
			}
		}
	//input ends here
//	for(int i=1;i<=n;i++)
//		for(int j=1;j<=n;j++)
//			for(int k=C+1;k<=F;k++)
//				pre[i][j][k]=i*j;
	//for every [top,btm] in [1,n]
	for(int top=1;top<=n;top++){
		for(int btm=top;btm<=n;btm++){//O(n^2)
			if(!check(top,1,btm,n)){
				if(btm==n){
					cout<<ans<<endl;
					return 0;
				}
				else continue;
			}
			int l=1,r=1;
			//scan the array
			bool tag=false;
			while(r<=n){//O(n)
				while(check(top,l+1,btm,r)&&l<r){
					l++;
				}
				if(tag)ans+=l;
				else if(check(top,l,btm,r)){tag=true,ans+=l;}
				r++;
			}
		}
	}

 printf("%d\n",ans);
	return 0;
}