Submission #154561

# Submission time Handle Problem Language Result Execution time Memory
154561 2019-09-22T17:46:44 Z liwi Rectangles (IOI19_rect) C++14
72 / 100
5000 ms 931320 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4,popcnt,abm,mmx,tune=native")

#include <bits/stdc++.h>

//using namespace std;

#define INT_INF 0x3f3f3f3f
#define mp std::make_pair

#define MAXN 2501

typedef long long ll;
typedef std::pair<int,int> pii;

int num_rows,num_cols,arr[MAXN][MAXN],bit[MAXN],cnt[MAXN][MAXN],ans,l,r,lst[MAXN],pp;
std::vector<pii> row_dp[MAXN][MAXN],col_dp[MAXN][MAXN];
pii dq[MAXN];

inline void update(int i, int val, int curr){
	for(; i<=num_cols; i+=i&-i){
		if(lst[i] != curr) bit[i] = val, lst[i] = curr;
		else bit[i]+=val;
	}
}

inline int query(int i, int curr){
	int res = 0;
	for(; i>0; i-=i&-i)
		if(lst[i] == curr)
			res+=bit[i];
	return res;
}

inline void init_rows(){
//	memset(cnt,0,sizeof cnt);
	for(int i=2; i<num_rows; i++){
		l = 1, r = 0;
		for(int k=1; k<=num_cols; k++){
			while(r-l+1 > 0 && dq[r].first < arr[i][k]) r--;
			int lft = (r-l+1>0?dq[r].second:INT_INF);
			if(lft < k-1 && (row_dp[lft+1][k-1].empty() || row_dp[lft+1][k-1].back().first != i))
				row_dp[lft+1][k-1].emplace_back(i,i), cnt[lft+1][k-1]++; //rows[i].pb(mp(lft+1,k-1));
			while(r-l+1 > 0 && dq[r].first == arr[i][k]) r--;
			dq[++r] = mp(arr[i][k],k);
		}
		l = 1, r = 0;
		for(int k=num_cols; k>=1; k--){
			while(r-l+1 > 0 && dq[r].first < arr[i][k]) r--;
			int rgt = (r-l+1>0?dq[r].second:-INT_INF);
			if(rgt > k+1 && (row_dp[k+1][rgt-1].empty() || row_dp[k+1][rgt-1].back().first != i))
				row_dp[k+1][rgt-1].emplace_back(i,i), cnt[k+1][rgt-1]++; //rows[i].pb(mp(k+1,rgt-1));
			while(r-l+1 > 0 && dq[r].first == arr[i][k]) r--;
			dq[++r] = mp(arr[i][k],k);
		}
	}
}

inline void init_cols(){
	memset(cnt,0,sizeof cnt);
	for(int i=2; i<num_cols; i++){
		l = 1, r = 0;
		for(int k=1; k<=num_rows; k++){
			while(r-l+1 > 0 && dq[r].first < arr[k][i]) r--;
			int lft = (r-l+1>0?dq[r].second:INT_INF);
			if(lft < k-1 && (col_dp[lft+1][k-1].empty() || col_dp[lft+1][k-1].back().first != i))
				col_dp[lft+1][k-1].emplace_back(i,i), cnt[lft+1][k-1]++; //cols[i].pb(mp(lft+1,k-1));
			while(r-l+1 > 0 && dq[r].first == arr[k][i]) r--;
			dq[++r] = mp(arr[k][i],k);
		}
		l = 1, r = 0;
		for(int k=num_rows; k>=1; k--){
			while(r-l+1 > 0 && dq[r].first < arr[k][i]) r--;
			int rgt = (r-l+1>0?dq[r].second:-INT_INF);
			if(rgt > k+1 && (col_dp[k+1][rgt-1].empty() || col_dp[k+1][rgt-1].back().first != i))
				col_dp[k+1][rgt-1].emplace_back(i,i), cnt[k+1][rgt-1]++; //cols[i].pb(mp(k+1,rgt-1));
			while(r-l+1 > 0 && dq[r].first == arr[k][i]) r--;
			dq[++r] = mp(arr[k][i],k);
		}
	}
}

inline void calc_hor_dp(){
//	for(int i=2; i<num_rows; i++){
//		sort(all(rows[i])); complete_unique(rows[i]);
//		for(pii check:rows[i]){
//			h_segs[check.first][check.second].pb(i);
////			r_range[i][check.first].pb(check.second);
//		}
//		rows[i].clear();
//	}
	std::vector<std::pair<pii,int>> dep[MAXN];
	for(int l=2; l<=num_cols-1; l++){
		for(int r=l; r<=num_cols-1; r++){
			if(cnt[l][r] == 0) continue;
			#define nums row_dp[l][r]
//			std::vector<pii> &nums = row_dp[l][r];
//			sort(all(nums)); complete_unique(nums);
//			std::vector<pii> t;
			int lst = 0;
			for(int i=1; i<cnt[l][r]; i++){
				if(nums[i].first == nums[i-1].first+1) continue;
				for(int k=nums[lst].first; k<=nums[i-1].first; k++)
					dep[nums[i-1].first-k+1].emplace_back(mp(k,l),r);
//					row_dp[k][l].pb(mp(nums[i-1].first-k+1,r));
				lst = i;
			}
			for(int k=nums[lst].first; k<=nums[cnt[l][r]-1].first; k++)
				dep[nums[cnt[l][r]-1].first-k+1].emplace_back(mp(k,l),r);
//				row_dp[k][l].pb(mp(nums[cnt[l][r]-1].first-k+1,r));
			nums.clear();
		}
	}
	for(int i=1; i<=num_rows; i++){
		for(auto &check:dep[i])
			row_dp[check.first.first][check.first.second].emplace_back(i,check.second);
	}
//	for(int l=2; l<=num_cols-1; l++){
//		for(int r=l; r<=num_cols-1; r++){
//			reverse(all(row_dp[l][r]));
//			while(cnt[l][r]--) row_dp[l][r].pop_back();
//			reverse(all(row_dp[l][r]));
//		}
//	}
}

inline void calc_col_dp(){
//	for(int i=2; i<num_cols; i++){
//		sort(all(cols[i])); complete_unique(cols[i]);
//		for(pii check:cols[i]){
//			v_segs[check.first][check.second].pb(i);
////			v_range[check.first][i].pb(check.second);
//		}
//		cols[i].clear();
//	}
	for(int l=2; l<=num_rows-1; l++){
		for(int r=l; r<=num_rows-1; r++){
			if(cnt[l][r] == 0) continue;
			#define nums col_dp[l][r]
//			std::vector<pii> &nums = col_dp[l][r];
//			sort(all(nums)); complete_unique(nums);
			int lst = 0;
			for(int i=1; i<cnt[l][r]; i++){
				if(nums[i].first == nums[i-1].first+1) continue;
				for(int k=nums[lst].first; k<=nums[i-1].first; k++)
					col_dp[l][k].emplace_back(r,nums[i-1].first);
				lst = i;
			}
			for(int k=nums[lst].first; k<=nums[cnt[l][r]-1].first; k++)
				col_dp[l][k].emplace_back(r,nums[cnt[l][r]-1].first);
		}
	}
//	for(int l=2; l<=num_cols-1; l++){
//		for(int r=l; r<=num_cols-1; r++){
//			reverse(all(col_dp[l][r]));
//			while(cnt[l][r]--) col_dp[l][r].pop_back();
//			reverse(all(col_dp[l][r]));
//		}
//	}
}

ll count_rectangles(std::vector<std::vector<int>> a){
	ans = 0, num_rows = (int)a.size(), num_cols = (int)a[0].size();
	for(int i=0; i<a.size(); i++)
		for(int k=0; k<a[i].size(); k++)
			arr[i+1][k+1] = a[i][k];
	init_rows(); calc_hor_dp();
	init_cols(); calc_col_dp();
	for(int i=num_rows-1; i>=2; i--){
		for(int k=num_cols-1; k>=2; k--){
//			sort(all(row_dp[i][k]));
//			sort(all(col_dp[i][k]));
			int ptr = cnt[i][k], res = 0, cc = 0; ++pp;
			for(int v=0; v<row_dp[i][k].size(); v++){
//				pii curr = row_dp[i][k][v];
				while(ptr < col_dp[i][k].size() && col_dp[i][k][ptr].first-i+1 <= row_dp[i][k][v].first){
//					assert(col_dp[i][k][ptr].second>=k);
					update(col_dp[i][k][ptr].second-k+1,1,pp);
					ptr++, cc++;
				}
				res+=cc-query(row_dp[i][k][v].second-k,pp);
			}
//			while(--ptr >= cnt[i][k]) update(col_dp[i][k][ptr].second-k+1,-1);
			ans+=res;
		}
	}
	return ans;
}

Compilation message

rect.cpp:140:0: warning: "nums" redefined
    #define nums col_dp[l][r]
 
rect.cpp:97:0: note: this is the location of the previous definition
    #define nums row_dp[l][r]
 
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:165:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<a.size(); i++)
               ~^~~~~~~~~
rect.cpp:166:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0; k<a[i].size(); k++)
                ~^~~~~~~~~~~~
rect.cpp:175:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int v=0; v<row_dp[i][k].size(); v++){
                 ~^~~~~~~~~~~~~~~~~~~~
rect.cpp:177:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(ptr < col_dp[i][k].size() && col_dp[i][k][ptr].first-i+1 <= row_dp[i][k][v].first){
           ~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 314 ms 318712 KB Output is correct
2 Correct 311 ms 318884 KB Output is correct
3 Correct 311 ms 318936 KB Output is correct
4 Correct 333 ms 318868 KB Output is correct
5 Correct 312 ms 318940 KB Output is correct
6 Correct 308 ms 318840 KB Output is correct
7 Correct 308 ms 318840 KB Output is correct
8 Correct 315 ms 318836 KB Output is correct
9 Correct 310 ms 318904 KB Output is correct
10 Correct 309 ms 318844 KB Output is correct
11 Correct 308 ms 318844 KB Output is correct
12 Correct 309 ms 318844 KB Output is correct
13 Correct 308 ms 318712 KB Output is correct
14 Correct 310 ms 318696 KB Output is correct
15 Correct 319 ms 318644 KB Output is correct
16 Correct 312 ms 318712 KB Output is correct
17 Correct 310 ms 318684 KB Output is correct
18 Correct 309 ms 318716 KB Output is correct
19 Correct 311 ms 318744 KB Output is correct
20 Correct 310 ms 318764 KB Output is correct
21 Correct 311 ms 318724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 318712 KB Output is correct
2 Correct 311 ms 318884 KB Output is correct
3 Correct 311 ms 318936 KB Output is correct
4 Correct 333 ms 318868 KB Output is correct
5 Correct 312 ms 318940 KB Output is correct
6 Correct 308 ms 318840 KB Output is correct
7 Correct 308 ms 318840 KB Output is correct
8 Correct 315 ms 318836 KB Output is correct
9 Correct 310 ms 318904 KB Output is correct
10 Correct 309 ms 318844 KB Output is correct
11 Correct 308 ms 318844 KB Output is correct
12 Correct 309 ms 318844 KB Output is correct
13 Correct 308 ms 318712 KB Output is correct
14 Correct 310 ms 318696 KB Output is correct
15 Correct 319 ms 318644 KB Output is correct
16 Correct 312 ms 318712 KB Output is correct
17 Correct 324 ms 319548 KB Output is correct
18 Correct 316 ms 319580 KB Output is correct
19 Correct 312 ms 319580 KB Output is correct
20 Correct 310 ms 319256 KB Output is correct
21 Correct 315 ms 319320 KB Output is correct
22 Correct 313 ms 319352 KB Output is correct
23 Correct 314 ms 319484 KB Output is correct
24 Correct 310 ms 319224 KB Output is correct
25 Correct 310 ms 318684 KB Output is correct
26 Correct 309 ms 318716 KB Output is correct
27 Correct 311 ms 318744 KB Output is correct
28 Correct 310 ms 318764 KB Output is correct
29 Correct 311 ms 318724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 318712 KB Output is correct
2 Correct 311 ms 318884 KB Output is correct
3 Correct 311 ms 318936 KB Output is correct
4 Correct 333 ms 318868 KB Output is correct
5 Correct 312 ms 318940 KB Output is correct
6 Correct 308 ms 318840 KB Output is correct
7 Correct 308 ms 318840 KB Output is correct
8 Correct 315 ms 318836 KB Output is correct
9 Correct 310 ms 318904 KB Output is correct
10 Correct 309 ms 318844 KB Output is correct
11 Correct 308 ms 318844 KB Output is correct
12 Correct 309 ms 318844 KB Output is correct
13 Correct 308 ms 318712 KB Output is correct
14 Correct 310 ms 318696 KB Output is correct
15 Correct 319 ms 318644 KB Output is correct
16 Correct 312 ms 318712 KB Output is correct
17 Correct 324 ms 319548 KB Output is correct
18 Correct 316 ms 319580 KB Output is correct
19 Correct 312 ms 319580 KB Output is correct
20 Correct 310 ms 319256 KB Output is correct
21 Correct 315 ms 319320 KB Output is correct
22 Correct 313 ms 319352 KB Output is correct
23 Correct 314 ms 319484 KB Output is correct
24 Correct 310 ms 319224 KB Output is correct
25 Correct 327 ms 323320 KB Output is correct
26 Correct 325 ms 323400 KB Output is correct
27 Correct 325 ms 323448 KB Output is correct
28 Correct 325 ms 321136 KB Output is correct
29 Correct 340 ms 322432 KB Output is correct
30 Correct 336 ms 322700 KB Output is correct
31 Correct 332 ms 322504 KB Output is correct
32 Correct 329 ms 322280 KB Output is correct
33 Correct 310 ms 318684 KB Output is correct
34 Correct 309 ms 318716 KB Output is correct
35 Correct 311 ms 318744 KB Output is correct
36 Correct 310 ms 318764 KB Output is correct
37 Correct 311 ms 318724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 318712 KB Output is correct
2 Correct 311 ms 318884 KB Output is correct
3 Correct 311 ms 318936 KB Output is correct
4 Correct 333 ms 318868 KB Output is correct
5 Correct 312 ms 318940 KB Output is correct
6 Correct 308 ms 318840 KB Output is correct
7 Correct 308 ms 318840 KB Output is correct
8 Correct 315 ms 318836 KB Output is correct
9 Correct 310 ms 318904 KB Output is correct
10 Correct 309 ms 318844 KB Output is correct
11 Correct 308 ms 318844 KB Output is correct
12 Correct 309 ms 318844 KB Output is correct
13 Correct 308 ms 318712 KB Output is correct
14 Correct 310 ms 318696 KB Output is correct
15 Correct 319 ms 318644 KB Output is correct
16 Correct 312 ms 318712 KB Output is correct
17 Correct 324 ms 319548 KB Output is correct
18 Correct 316 ms 319580 KB Output is correct
19 Correct 312 ms 319580 KB Output is correct
20 Correct 310 ms 319256 KB Output is correct
21 Correct 315 ms 319320 KB Output is correct
22 Correct 313 ms 319352 KB Output is correct
23 Correct 314 ms 319484 KB Output is correct
24 Correct 310 ms 319224 KB Output is correct
25 Correct 327 ms 323320 KB Output is correct
26 Correct 325 ms 323400 KB Output is correct
27 Correct 325 ms 323448 KB Output is correct
28 Correct 325 ms 321136 KB Output is correct
29 Correct 340 ms 322432 KB Output is correct
30 Correct 336 ms 322700 KB Output is correct
31 Correct 332 ms 322504 KB Output is correct
32 Correct 329 ms 322280 KB Output is correct
33 Correct 493 ms 351692 KB Output is correct
34 Correct 469 ms 350628 KB Output is correct
35 Correct 454 ms 350756 KB Output is correct
36 Correct 405 ms 349768 KB Output is correct
37 Correct 519 ms 370684 KB Output is correct
38 Correct 520 ms 370424 KB Output is correct
39 Correct 514 ms 370944 KB Output is correct
40 Correct 498 ms 367912 KB Output is correct
41 Correct 426 ms 338620 KB Output is correct
42 Correct 477 ms 343032 KB Output is correct
43 Correct 653 ms 356672 KB Output is correct
44 Correct 653 ms 357180 KB Output is correct
45 Correct 467 ms 340084 KB Output is correct
46 Correct 468 ms 338616 KB Output is correct
47 Correct 615 ms 354656 KB Output is correct
48 Correct 649 ms 355500 KB Output is correct
49 Correct 310 ms 318684 KB Output is correct
50 Correct 309 ms 318716 KB Output is correct
51 Correct 311 ms 318744 KB Output is correct
52 Correct 310 ms 318764 KB Output is correct
53 Correct 311 ms 318724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 319096 KB Output is correct
2 Correct 318 ms 319104 KB Output is correct
3 Correct 322 ms 318792 KB Output is correct
4 Correct 312 ms 318636 KB Output is correct
5 Correct 324 ms 318988 KB Output is correct
6 Correct 321 ms 319040 KB Output is correct
7 Correct 341 ms 319056 KB Output is correct
8 Correct 347 ms 319064 KB Output is correct
9 Correct 322 ms 318968 KB Output is correct
10 Correct 321 ms 318700 KB Output is correct
11 Correct 321 ms 318888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 318744 KB Output is correct
2 Correct 1144 ms 418936 KB Output is correct
3 Correct 2344 ms 530868 KB Output is correct
4 Correct 2431 ms 531780 KB Output is correct
5 Correct 2296 ms 531764 KB Output is correct
6 Correct 458 ms 357108 KB Output is correct
7 Correct 616 ms 391420 KB Output is correct
8 Correct 633 ms 394440 KB Output is correct
9 Correct 310 ms 318684 KB Output is correct
10 Correct 309 ms 318716 KB Output is correct
11 Correct 311 ms 318744 KB Output is correct
12 Correct 310 ms 318764 KB Output is correct
13 Correct 311 ms 318724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 318712 KB Output is correct
2 Correct 311 ms 318884 KB Output is correct
3 Correct 311 ms 318936 KB Output is correct
4 Correct 333 ms 318868 KB Output is correct
5 Correct 312 ms 318940 KB Output is correct
6 Correct 308 ms 318840 KB Output is correct
7 Correct 308 ms 318840 KB Output is correct
8 Correct 315 ms 318836 KB Output is correct
9 Correct 310 ms 318904 KB Output is correct
10 Correct 309 ms 318844 KB Output is correct
11 Correct 308 ms 318844 KB Output is correct
12 Correct 309 ms 318844 KB Output is correct
13 Correct 308 ms 318712 KB Output is correct
14 Correct 310 ms 318696 KB Output is correct
15 Correct 319 ms 318644 KB Output is correct
16 Correct 312 ms 318712 KB Output is correct
17 Correct 324 ms 319548 KB Output is correct
18 Correct 316 ms 319580 KB Output is correct
19 Correct 312 ms 319580 KB Output is correct
20 Correct 310 ms 319256 KB Output is correct
21 Correct 315 ms 319320 KB Output is correct
22 Correct 313 ms 319352 KB Output is correct
23 Correct 314 ms 319484 KB Output is correct
24 Correct 310 ms 319224 KB Output is correct
25 Correct 327 ms 323320 KB Output is correct
26 Correct 325 ms 323400 KB Output is correct
27 Correct 325 ms 323448 KB Output is correct
28 Correct 325 ms 321136 KB Output is correct
29 Correct 340 ms 322432 KB Output is correct
30 Correct 336 ms 322700 KB Output is correct
31 Correct 332 ms 322504 KB Output is correct
32 Correct 329 ms 322280 KB Output is correct
33 Correct 493 ms 351692 KB Output is correct
34 Correct 469 ms 350628 KB Output is correct
35 Correct 454 ms 350756 KB Output is correct
36 Correct 405 ms 349768 KB Output is correct
37 Correct 519 ms 370684 KB Output is correct
38 Correct 520 ms 370424 KB Output is correct
39 Correct 514 ms 370944 KB Output is correct
40 Correct 498 ms 367912 KB Output is correct
41 Correct 426 ms 338620 KB Output is correct
42 Correct 477 ms 343032 KB Output is correct
43 Correct 653 ms 356672 KB Output is correct
44 Correct 653 ms 357180 KB Output is correct
45 Correct 467 ms 340084 KB Output is correct
46 Correct 468 ms 338616 KB Output is correct
47 Correct 615 ms 354656 KB Output is correct
48 Correct 649 ms 355500 KB Output is correct
49 Correct 320 ms 319096 KB Output is correct
50 Correct 318 ms 319104 KB Output is correct
51 Correct 322 ms 318792 KB Output is correct
52 Correct 312 ms 318636 KB Output is correct
53 Correct 324 ms 318988 KB Output is correct
54 Correct 321 ms 319040 KB Output is correct
55 Correct 341 ms 319056 KB Output is correct
56 Correct 347 ms 319064 KB Output is correct
57 Correct 322 ms 318968 KB Output is correct
58 Correct 321 ms 318700 KB Output is correct
59 Correct 321 ms 318888 KB Output is correct
60 Correct 310 ms 318744 KB Output is correct
61 Correct 1144 ms 418936 KB Output is correct
62 Correct 2344 ms 530868 KB Output is correct
63 Correct 2431 ms 531780 KB Output is correct
64 Correct 2296 ms 531764 KB Output is correct
65 Correct 458 ms 357108 KB Output is correct
66 Correct 616 ms 391420 KB Output is correct
67 Correct 633 ms 394440 KB Output is correct
68 Correct 3295 ms 686476 KB Output is correct
69 Correct 2601 ms 668464 KB Output is correct
70 Correct 2512 ms 668416 KB Output is correct
71 Correct 1569 ms 650276 KB Output is correct
72 Correct 3437 ms 931320 KB Output is correct
73 Correct 3322 ms 582724 KB Output is correct
74 Correct 3673 ms 612392 KB Output is correct
75 Execution timed out 5109 ms 751744 KB Time limit exceeded
76 Halted 0 ms 0 KB -