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

Compiler report

input/code.cpp: In function 'bool check(int, int, int, int)':
input/code.cpp:26:25: warning: the address of 'pre' will never be NULL [-Waddress]
   26 |         for(;i<=C;i++)T=&&sum(x1,y1,x2,y2,i);
      |                         ^
input/code.cpp:7:32: error: expected ';' before '[' token
    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:26:27: note: in expansion of macro 'sum'
   26 |         for(;i<=C;i++)T=&&sum(x1,y1,x2,y2,i);
      |                           ^~~
input/code.cpp:7:29: error: label 'pre' used but not defined
    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:26:27: note: in expansion of macro 'sum'
   26 |         for(;i<=C;i++)T=&&sum(x1,y1,x2,y2,i);
      |                           ^~~

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){
	bool T=1;
	int i;
	for(i=1;i<=C-2;i+=3){
		T=T&&sum(x1,y1,x2,y2,i);
		T=T&&sum(x1,y1,x2,y2,i+1);
		T=T&&sum(x1,y1,x2,y2,i+2);
	}
	for(;i<=C;i++)T=&&sum(x1,y1,x2,y2,i);
	return T;
//	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;
}