Submission #1048933

# Submission time Handle Problem Language Result Execution time Memory
1048933 2024-08-08T11:22:16 Z fuad27 Rectangles (IOI19_rect) C++17
100 / 100
2690 ms 902232 KB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int N = 2510;
int n,m;
vector<array<int, 2>> gdr[N][N];
vector<array<int, 2>> gdc[N][N];

struct Side {
	int r1, r2, c1, c2;
};


int fen[N];
void upd(int at, int add) {
	at++;
	while(at) {
		fen[at]+=add;
		at-=at&(-at);
	}
}
int qry(int at) {
	at++;
	int sum=0;
	while(at<N) {
		sum+=fen[at];
		at+=at&(-at);
	}
	return sum;
}
vector<int> stk;

long long count_rectangles(std::vector<std::vector<int> > a) {
	n = a.size();
	m = a[0].size();
	for(int i = 0;i<n;i++) {
			stk.clear();
		for(int j = 0;j<m;j++) {
			while(stk.size() and a[i][stk.back()] < a[i][j]){
				stk.pop_back();
			}
			if(stk.size()) {
				gdr[i][j].push_back({stk.back(), 1});
			}
			stk.push_back(j);
		}
		stk.clear();
		for(int j = m-1;j>=0;j--) {
			while(stk.size() and a[i][stk.back()] < a[i][j])stk.pop_back();
			if(stk.size()) {
				if(a[i][j] != a[i][stk.back()]) {
					gdr[i][stk.back()].push_back({j,1});
				}
			}
			stk.push_back(j);
		}
	}
	for(int i = 0;i<m;i++) {
			stk.clear();
		for(int j = 0;j<n;j++) {

			while(stk.size() and a[stk.back()][i] < a[j][i]){
				stk.pop_back();
			}
			if(stk.size()) {
				gdc[j][i].push_back({stk.back(),1});
			}
			stk.push_back(j);
		}
		stk.clear();
		for(int j = n-1;j>=0;j--) {
			while(stk.size() and a[stk.back()][i] < a[j][i])stk.pop_back();
			if(stk.size()) {
				if(a[j][i] != a[stk.back()][i]) {
					gdc[stk.back()][i].push_back({j, 1});
				}
			}
			stk.push_back(j);
		}
	}


	long long ans=0;
	for(int i = 0;i<n;i++) {
		for(int j = 0;j<m;j++) {
		sort(gdr[i][j].begin(), gdr[i][j].end());
		sort(gdc[i][j].begin(), gdc[i][j].end());
		gdr[i][j].erase(unique(gdr[i][j].begin(), gdr[i][j].end()), gdr[i][j].end());
		gdc[i][j].erase(unique(gdc[i][j].begin(), gdc[i][j].end()), gdc[i][j].end());
		if(i) {
				for(auto &[u, v]:gdr[i][j]) {
					auto it = lower_bound(gdr[i-1][j].begin(), gdr[i-1][j].end(), array<int,2>{u, 0});
					if(it!=gdr[i-1][j].end() and (*it)[0] == u) {
						v+=(*it)[1];
					}
				}
			}
			if(j) {
				for(auto &[u, v]:gdc[i][j]) {
					auto it = lower_bound(gdc[i][j-1].begin(), gdc[i][j-1].end(), array<int,2>{u, 0});
					if(it!=gdc[i][j-1].end() and (*it)[0] == u) {
						v+=(*it)[1];
					}
				}
			}
			if(i and j) {
				vector<pair<int,int>> A;
				vector<pair<int,int>> B;
				for(auto k:gdr[i-1][j]) {
					if(k[0]+1==j)continue;
					A.push_back(pair<int,int>(k[0], max(0, i-k[1]-1)));
				}
				for(auto k:gdc[i][j-1]) {
					if(k[0]+1 == i)continue;
					B.push_back(pair<int,int>(max(0, j-k[1]-1), k[0]));
				}
				sort(A.begin(),A.end());
				sort(B.begin(),B.end());
//				A[0] >= B[0]
//
//				B[1] >= A[1]
				int p1 = 0;
				for(auto k:A) {
					while(p1 < (int)B.size() and B[p1].first <= k.first) {
						upd(B[p1].second, 1);
						p1++;
					}
					ans+=qry(k.second);
				}
				while(p1 < B.size()) {
						upd(B[p1].second, 1);
						p1++;
				}
				for(auto k:B)upd(k.second, -1);
			}
		}
	}
	return ans;
}


Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:131:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     while(p1 < B.size()) {
      |           ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 81 ms 296300 KB Output is correct
2 Correct 80 ms 296284 KB Output is correct
3 Correct 100 ms 296380 KB Output is correct
4 Correct 83 ms 296272 KB Output is correct
5 Correct 86 ms 296180 KB Output is correct
6 Correct 91 ms 296152 KB Output is correct
7 Correct 90 ms 296272 KB Output is correct
8 Correct 90 ms 296200 KB Output is correct
9 Correct 86 ms 296272 KB Output is correct
10 Correct 87 ms 296276 KB Output is correct
11 Correct 82 ms 296272 KB Output is correct
12 Correct 83 ms 296312 KB Output is correct
13 Correct 79 ms 296216 KB Output is correct
14 Correct 85 ms 296280 KB Output is correct
15 Correct 81 ms 296272 KB Output is correct
16 Correct 82 ms 296272 KB Output is correct
17 Correct 98 ms 296276 KB Output is correct
18 Correct 93 ms 296300 KB Output is correct
19 Correct 83 ms 296276 KB Output is correct
20 Correct 82 ms 296324 KB Output is correct
21 Correct 89 ms 296276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 296300 KB Output is correct
2 Correct 80 ms 296284 KB Output is correct
3 Correct 100 ms 296380 KB Output is correct
4 Correct 83 ms 296272 KB Output is correct
5 Correct 86 ms 296180 KB Output is correct
6 Correct 91 ms 296152 KB Output is correct
7 Correct 90 ms 296272 KB Output is correct
8 Correct 90 ms 296200 KB Output is correct
9 Correct 86 ms 296272 KB Output is correct
10 Correct 87 ms 296276 KB Output is correct
11 Correct 82 ms 296272 KB Output is correct
12 Correct 83 ms 296312 KB Output is correct
13 Correct 79 ms 296216 KB Output is correct
14 Correct 85 ms 296280 KB Output is correct
15 Correct 81 ms 296272 KB Output is correct
16 Correct 82 ms 296272 KB Output is correct
17 Correct 98 ms 296276 KB Output is correct
18 Correct 93 ms 296300 KB Output is correct
19 Correct 83 ms 296276 KB Output is correct
20 Correct 82 ms 296324 KB Output is correct
21 Correct 89 ms 296276 KB Output is correct
22 Correct 82 ms 296788 KB Output is correct
23 Correct 101 ms 296832 KB Output is correct
24 Correct 84 ms 296928 KB Output is correct
25 Correct 84 ms 296536 KB Output is correct
26 Correct 84 ms 296784 KB Output is correct
27 Correct 84 ms 296788 KB Output is correct
28 Correct 83 ms 296784 KB Output is correct
29 Correct 83 ms 296532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 296300 KB Output is correct
2 Correct 80 ms 296284 KB Output is correct
3 Correct 100 ms 296380 KB Output is correct
4 Correct 83 ms 296272 KB Output is correct
5 Correct 86 ms 296180 KB Output is correct
6 Correct 91 ms 296152 KB Output is correct
7 Correct 90 ms 296272 KB Output is correct
8 Correct 90 ms 296200 KB Output is correct
9 Correct 86 ms 296272 KB Output is correct
10 Correct 87 ms 296276 KB Output is correct
11 Correct 82 ms 296272 KB Output is correct
12 Correct 83 ms 296312 KB Output is correct
13 Correct 79 ms 296216 KB Output is correct
14 Correct 85 ms 296280 KB Output is correct
15 Correct 81 ms 296272 KB Output is correct
16 Correct 82 ms 296272 KB Output is correct
17 Correct 82 ms 296788 KB Output is correct
18 Correct 101 ms 296832 KB Output is correct
19 Correct 84 ms 296928 KB Output is correct
20 Correct 84 ms 296536 KB Output is correct
21 Correct 84 ms 296784 KB Output is correct
22 Correct 84 ms 296788 KB Output is correct
23 Correct 83 ms 296784 KB Output is correct
24 Correct 83 ms 296532 KB Output is correct
25 Correct 98 ms 296276 KB Output is correct
26 Correct 93 ms 296300 KB Output is correct
27 Correct 83 ms 296276 KB Output is correct
28 Correct 82 ms 296324 KB Output is correct
29 Correct 89 ms 296276 KB Output is correct
30 Correct 92 ms 299904 KB Output is correct
31 Correct 96 ms 299928 KB Output is correct
32 Correct 90 ms 299860 KB Output is correct
33 Correct 104 ms 299088 KB Output is correct
34 Correct 107 ms 299484 KB Output is correct
35 Correct 99 ms 299344 KB Output is correct
36 Correct 96 ms 299348 KB Output is correct
37 Correct 101 ms 299324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 296300 KB Output is correct
2 Correct 80 ms 296284 KB Output is correct
3 Correct 100 ms 296380 KB Output is correct
4 Correct 83 ms 296272 KB Output is correct
5 Correct 86 ms 296180 KB Output is correct
6 Correct 91 ms 296152 KB Output is correct
7 Correct 90 ms 296272 KB Output is correct
8 Correct 90 ms 296200 KB Output is correct
9 Correct 86 ms 296272 KB Output is correct
10 Correct 87 ms 296276 KB Output is correct
11 Correct 82 ms 296272 KB Output is correct
12 Correct 83 ms 296312 KB Output is correct
13 Correct 79 ms 296216 KB Output is correct
14 Correct 85 ms 296280 KB Output is correct
15 Correct 81 ms 296272 KB Output is correct
16 Correct 82 ms 296272 KB Output is correct
17 Correct 82 ms 296788 KB Output is correct
18 Correct 101 ms 296832 KB Output is correct
19 Correct 84 ms 296928 KB Output is correct
20 Correct 84 ms 296536 KB Output is correct
21 Correct 84 ms 296784 KB Output is correct
22 Correct 84 ms 296788 KB Output is correct
23 Correct 83 ms 296784 KB Output is correct
24 Correct 83 ms 296532 KB Output is correct
25 Correct 92 ms 299904 KB Output is correct
26 Correct 96 ms 299928 KB Output is correct
27 Correct 90 ms 299860 KB Output is correct
28 Correct 104 ms 299088 KB Output is correct
29 Correct 107 ms 299484 KB Output is correct
30 Correct 99 ms 299344 KB Output is correct
31 Correct 96 ms 299348 KB Output is correct
32 Correct 101 ms 299324 KB Output is correct
33 Correct 98 ms 296276 KB Output is correct
34 Correct 93 ms 296300 KB Output is correct
35 Correct 83 ms 296276 KB Output is correct
36 Correct 82 ms 296324 KB Output is correct
37 Correct 89 ms 296276 KB Output is correct
38 Correct 175 ms 336596 KB Output is correct
39 Correct 170 ms 333696 KB Output is correct
40 Correct 158 ms 333648 KB Output is correct
41 Correct 182 ms 330696 KB Output is correct
42 Correct 221 ms 342096 KB Output is correct
43 Correct 231 ms 341840 KB Output is correct
44 Correct 190 ms 341844 KB Output is correct
45 Correct 217 ms 339404 KB Output is correct
46 Correct 166 ms 330832 KB Output is correct
47 Correct 190 ms 331600 KB Output is correct
48 Correct 255 ms 336536 KB Output is correct
49 Correct 268 ms 336464 KB Output is correct
50 Correct 172 ms 316244 KB Output is correct
51 Correct 179 ms 316240 KB Output is correct
52 Correct 262 ms 335952 KB Output is correct
53 Correct 256 ms 335952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 296788 KB Output is correct
2 Correct 84 ms 296632 KB Output is correct
3 Correct 84 ms 296684 KB Output is correct
4 Correct 86 ms 296276 KB Output is correct
5 Correct 83 ms 296784 KB Output is correct
6 Correct 86 ms 296784 KB Output is correct
7 Correct 82 ms 296784 KB Output is correct
8 Correct 83 ms 296716 KB Output is correct
9 Correct 86 ms 296788 KB Output is correct
10 Correct 83 ms 296272 KB Output is correct
11 Correct 85 ms 296484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296276 KB Output is correct
2 Correct 93 ms 296300 KB Output is correct
3 Correct 83 ms 296276 KB Output is correct
4 Correct 82 ms 296324 KB Output is correct
5 Correct 89 ms 296276 KB Output is correct
6 Correct 92 ms 296276 KB Output is correct
7 Correct 713 ms 499096 KB Output is correct
8 Correct 1481 ms 736504 KB Output is correct
9 Correct 1578 ms 739092 KB Output is correct
10 Correct 1480 ms 738980 KB Output is correct
11 Correct 418 ms 514640 KB Output is correct
12 Correct 736 ms 711256 KB Output is correct
13 Correct 767 ms 738900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 296300 KB Output is correct
2 Correct 80 ms 296284 KB Output is correct
3 Correct 100 ms 296380 KB Output is correct
4 Correct 83 ms 296272 KB Output is correct
5 Correct 86 ms 296180 KB Output is correct
6 Correct 91 ms 296152 KB Output is correct
7 Correct 90 ms 296272 KB Output is correct
8 Correct 90 ms 296200 KB Output is correct
9 Correct 86 ms 296272 KB Output is correct
10 Correct 87 ms 296276 KB Output is correct
11 Correct 82 ms 296272 KB Output is correct
12 Correct 83 ms 296312 KB Output is correct
13 Correct 79 ms 296216 KB Output is correct
14 Correct 85 ms 296280 KB Output is correct
15 Correct 81 ms 296272 KB Output is correct
16 Correct 82 ms 296272 KB Output is correct
17 Correct 82 ms 296788 KB Output is correct
18 Correct 101 ms 296832 KB Output is correct
19 Correct 84 ms 296928 KB Output is correct
20 Correct 84 ms 296536 KB Output is correct
21 Correct 84 ms 296784 KB Output is correct
22 Correct 84 ms 296788 KB Output is correct
23 Correct 83 ms 296784 KB Output is correct
24 Correct 83 ms 296532 KB Output is correct
25 Correct 92 ms 299904 KB Output is correct
26 Correct 96 ms 299928 KB Output is correct
27 Correct 90 ms 299860 KB Output is correct
28 Correct 104 ms 299088 KB Output is correct
29 Correct 107 ms 299484 KB Output is correct
30 Correct 99 ms 299344 KB Output is correct
31 Correct 96 ms 299348 KB Output is correct
32 Correct 101 ms 299324 KB Output is correct
33 Correct 175 ms 336596 KB Output is correct
34 Correct 170 ms 333696 KB Output is correct
35 Correct 158 ms 333648 KB Output is correct
36 Correct 182 ms 330696 KB Output is correct
37 Correct 221 ms 342096 KB Output is correct
38 Correct 231 ms 341840 KB Output is correct
39 Correct 190 ms 341844 KB Output is correct
40 Correct 217 ms 339404 KB Output is correct
41 Correct 166 ms 330832 KB Output is correct
42 Correct 190 ms 331600 KB Output is correct
43 Correct 255 ms 336536 KB Output is correct
44 Correct 268 ms 336464 KB Output is correct
45 Correct 172 ms 316244 KB Output is correct
46 Correct 179 ms 316240 KB Output is correct
47 Correct 262 ms 335952 KB Output is correct
48 Correct 256 ms 335952 KB Output is correct
49 Correct 82 ms 296788 KB Output is correct
50 Correct 84 ms 296632 KB Output is correct
51 Correct 84 ms 296684 KB Output is correct
52 Correct 86 ms 296276 KB Output is correct
53 Correct 83 ms 296784 KB Output is correct
54 Correct 86 ms 296784 KB Output is correct
55 Correct 82 ms 296784 KB Output is correct
56 Correct 83 ms 296716 KB Output is correct
57 Correct 86 ms 296788 KB Output is correct
58 Correct 83 ms 296272 KB Output is correct
59 Correct 85 ms 296484 KB Output is correct
60 Correct 92 ms 296276 KB Output is correct
61 Correct 713 ms 499096 KB Output is correct
62 Correct 1481 ms 736504 KB Output is correct
63 Correct 1578 ms 739092 KB Output is correct
64 Correct 1480 ms 738980 KB Output is correct
65 Correct 418 ms 514640 KB Output is correct
66 Correct 736 ms 711256 KB Output is correct
67 Correct 767 ms 738900 KB Output is correct
68 Correct 98 ms 296276 KB Output is correct
69 Correct 93 ms 296300 KB Output is correct
70 Correct 83 ms 296276 KB Output is correct
71 Correct 82 ms 296324 KB Output is correct
72 Correct 89 ms 296276 KB Output is correct
73 Correct 1191 ms 843600 KB Output is correct
74 Correct 1194 ms 813132 KB Output is correct
75 Correct 1159 ms 813048 KB Output is correct
76 Correct 1277 ms 782260 KB Output is correct
77 Correct 1864 ms 901200 KB Output is correct
78 Correct 1557 ms 618316 KB Output is correct
79 Correct 1838 ms 644116 KB Output is correct
80 Correct 2641 ms 833068 KB Output is correct
81 Correct 1703 ms 634288 KB Output is correct
82 Correct 2190 ms 746828 KB Output is correct
83 Correct 2690 ms 859304 KB Output is correct
84 Correct 1568 ms 623444 KB Output is correct
85 Correct 2667 ms 853856 KB Output is correct
86 Correct 2503 ms 838424 KB Output is correct
87 Correct 1079 ms 663488 KB Output is correct
88 Correct 1868 ms 900684 KB Output is correct
89 Correct 1866 ms 902224 KB Output is correct
90 Correct 1913 ms 902232 KB Output is correct