#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#pragma GCC target optimize("O3")
const int MAXN = 2505;
const int inf=1000000500ll;
const long long oo =1000000000000000500ll;
const int MOD = (int)1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int> pi;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int n,m;
long long ans;
struct fenw {
int fw[MAXN];
void update(int x, int nval){
for(;x<MAXN;x+=x&-x)fw[x]+=nval;
}
int point(int x){
int res=0;
for(;x;x-=x&-x)res+=fw[x];
return res;
}
}fw;
vector<int> vxi[MAXN][MAXN];
vector<int> vyi[MAXN][MAXN];
vector<pi> H[MAXN][MAXN];
vector<pi> V[MAXN][MAXN];
struct hash_pair {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(pair<uint64_t,uint64_t> x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
}
};
long long count_rectangles(std::vector<std::vector<int> > A) {
n=A.size();
m=A[0].size();
for(int i=1;i<=n;i++){
vector<pi> E;
for(int j=1;j<=m;j++){
E.push_back({A[i-1][j-1],j});
}
sort(E.begin(),E.end(),greater<pi>());
set<int> s;
vector<int> lazy;
int pre=-1;
unordered_set<pi,hash_pair> st;
for(auto x:E){
if(x.first!=pre){
for(auto y:lazy){
s.insert(y);
}
lazy.clear();
}
//debug(x.first,x.second);
auto it=s.lower_bound(x.second);
if(it == s.begin() || it==s.end());
else {
int L=*prev(it);
int R=*it;
st.insert({L+1,R-1});
}
lazy.push_back(x.second);
pre=x.first;
}
for(auto x:st){
int L=x.first;
int R=x.second;
vyi[L][R].push_back(i);
}
}
for(int i=1;i<=m;i++){
vector<pi> E;
for(int j=1;j<=n;j++){
E.push_back({A[j-1][i-1],j});
}
sort(E.begin(),E.end(),greater<pi>());
set<int> s;
vector<int> lazy;
int pre=-1;
unordered_set<pi,hash_pair> st;
for(auto x:E){
if(x.first!=pre){
for(auto y:lazy){
s.insert(y);
}
lazy.clear();
}
//debug(x.first,x.second);
auto it=s.lower_bound(x.second);
if(it == s.begin() || it==s.end());
else {
int L=*prev(it);
int R=*it;
//vxi[L][R].push_back(i);
st.insert({L+1,R-1});
}
lazy.push_back(x.second);
pre=x.first;
}
for(auto x:st){
int L=x.first;
int R=x.second;
vxi[L][R].push_back(i);
}
}
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
int st=0;
for(int k=0;k<(int)vxi[i][j].size();k++){
//debug(i,j,k,vxi[i][j][k]);
int col=vxi[i][j][k];
V[j][col].push_back({i,vxi[i][j][st]});
if(k==((int)vxi[i][j].size()-1) || vxi[i][j][k]+1!=vxi[i][j][k+1]){
st=k+1;
} else{
}
}
}
}
for(int i=1;i<=m;i++){
for(int j=i;j<=m;j++){
int st=0;
for(int k=0;k<(int)vyi[i][j].size();k++){
int row=vyi[i][j][k];
H[row][j].push_back({vyi[i][j][st],i});
if(k==((int)vyi[i][j].size()-1) || vyi[i][j][k]+1!=vyi[i][j][k+1]){
st=k+1;
} else{
}
}
}
}
for(int i=2;i<=n-1;i++){
for(int j=2;j<=m-1;j++){
// try this guy as the bottom right corner
sort(H[i][j].begin(),H[i][j].end(),[](pi x, pi y) { return x.second>y.second; });
sort(V[i][j].begin(),V[i][j].end(),[](pi x, pi y) { return x.second>y.second; });
int idx=0;
for(int k=0;k<(int)V[i][j].size();k++){
while(idx<(int)H[i][j].size() && H[i][j][idx].second >= V[i][j][k].second) {
fw.update(H[i][j][idx].first,1);
++idx;
}
ans+=fw.point(V[i][j][k].first);
}
for(int k=0;k<idx;k++){
fw.update(H[i][j][k].first,-1);
}
}
}
return ans;
}
Compilation message
rect.cpp:8:20: warning: '#pragma GCC option' is not a string [-Wpragmas]
8 | #pragma GCC target optimize("O3")
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
589652 KB |
Output is correct |
2 |
Correct |
126 ms |
589908 KB |
Output is correct |
3 |
Correct |
129 ms |
589868 KB |
Output is correct |
4 |
Correct |
126 ms |
589800 KB |
Output is correct |
5 |
Correct |
130 ms |
589944 KB |
Output is correct |
6 |
Correct |
128 ms |
589760 KB |
Output is correct |
7 |
Correct |
125 ms |
589760 KB |
Output is correct |
8 |
Correct |
129 ms |
589648 KB |
Output is correct |
9 |
Correct |
132 ms |
589748 KB |
Output is correct |
10 |
Correct |
127 ms |
589908 KB |
Output is correct |
11 |
Correct |
125 ms |
589736 KB |
Output is correct |
12 |
Correct |
124 ms |
589904 KB |
Output is correct |
13 |
Correct |
127 ms |
589648 KB |
Output is correct |
14 |
Correct |
130 ms |
589652 KB |
Output is correct |
15 |
Correct |
128 ms |
589684 KB |
Output is correct |
16 |
Correct |
130 ms |
589656 KB |
Output is correct |
17 |
Correct |
126 ms |
589648 KB |
Output is correct |
18 |
Correct |
125 ms |
589648 KB |
Output is correct |
19 |
Correct |
127 ms |
589700 KB |
Output is correct |
20 |
Correct |
128 ms |
589672 KB |
Output is correct |
21 |
Correct |
130 ms |
589760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
589652 KB |
Output is correct |
2 |
Correct |
126 ms |
589908 KB |
Output is correct |
3 |
Correct |
129 ms |
589868 KB |
Output is correct |
4 |
Correct |
126 ms |
589800 KB |
Output is correct |
5 |
Correct |
130 ms |
589944 KB |
Output is correct |
6 |
Correct |
128 ms |
589760 KB |
Output is correct |
7 |
Correct |
125 ms |
589760 KB |
Output is correct |
8 |
Correct |
129 ms |
589648 KB |
Output is correct |
9 |
Correct |
132 ms |
589748 KB |
Output is correct |
10 |
Correct |
127 ms |
589908 KB |
Output is correct |
11 |
Correct |
125 ms |
589736 KB |
Output is correct |
12 |
Correct |
124 ms |
589904 KB |
Output is correct |
13 |
Correct |
127 ms |
589648 KB |
Output is correct |
14 |
Correct |
130 ms |
589652 KB |
Output is correct |
15 |
Correct |
128 ms |
589684 KB |
Output is correct |
16 |
Correct |
130 ms |
589656 KB |
Output is correct |
17 |
Correct |
126 ms |
589648 KB |
Output is correct |
18 |
Correct |
125 ms |
589648 KB |
Output is correct |
19 |
Correct |
127 ms |
589700 KB |
Output is correct |
20 |
Correct |
128 ms |
589672 KB |
Output is correct |
21 |
Correct |
130 ms |
589760 KB |
Output is correct |
22 |
Correct |
127 ms |
590176 KB |
Output is correct |
23 |
Correct |
132 ms |
590176 KB |
Output is correct |
24 |
Correct |
128 ms |
590164 KB |
Output is correct |
25 |
Correct |
128 ms |
589908 KB |
Output is correct |
26 |
Correct |
137 ms |
590160 KB |
Output is correct |
27 |
Correct |
133 ms |
590248 KB |
Output is correct |
28 |
Correct |
128 ms |
590160 KB |
Output is correct |
29 |
Correct |
126 ms |
589908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
589652 KB |
Output is correct |
2 |
Correct |
126 ms |
589908 KB |
Output is correct |
3 |
Correct |
129 ms |
589868 KB |
Output is correct |
4 |
Correct |
126 ms |
589800 KB |
Output is correct |
5 |
Correct |
130 ms |
589944 KB |
Output is correct |
6 |
Correct |
128 ms |
589760 KB |
Output is correct |
7 |
Correct |
125 ms |
589760 KB |
Output is correct |
8 |
Correct |
129 ms |
589648 KB |
Output is correct |
9 |
Correct |
132 ms |
589748 KB |
Output is correct |
10 |
Correct |
127 ms |
589908 KB |
Output is correct |
11 |
Correct |
125 ms |
589736 KB |
Output is correct |
12 |
Correct |
124 ms |
589904 KB |
Output is correct |
13 |
Correct |
127 ms |
589648 KB |
Output is correct |
14 |
Correct |
130 ms |
589652 KB |
Output is correct |
15 |
Correct |
128 ms |
589684 KB |
Output is correct |
16 |
Correct |
130 ms |
589656 KB |
Output is correct |
17 |
Correct |
127 ms |
590176 KB |
Output is correct |
18 |
Correct |
132 ms |
590176 KB |
Output is correct |
19 |
Correct |
128 ms |
590164 KB |
Output is correct |
20 |
Correct |
128 ms |
589908 KB |
Output is correct |
21 |
Correct |
137 ms |
590160 KB |
Output is correct |
22 |
Correct |
133 ms |
590248 KB |
Output is correct |
23 |
Correct |
128 ms |
590160 KB |
Output is correct |
24 |
Correct |
126 ms |
589908 KB |
Output is correct |
25 |
Correct |
126 ms |
589648 KB |
Output is correct |
26 |
Correct |
125 ms |
589648 KB |
Output is correct |
27 |
Correct |
127 ms |
589700 KB |
Output is correct |
28 |
Correct |
128 ms |
589672 KB |
Output is correct |
29 |
Correct |
130 ms |
589760 KB |
Output is correct |
30 |
Correct |
145 ms |
591440 KB |
Output is correct |
31 |
Correct |
146 ms |
591660 KB |
Output is correct |
32 |
Correct |
148 ms |
591620 KB |
Output is correct |
33 |
Correct |
143 ms |
591184 KB |
Output is correct |
34 |
Correct |
160 ms |
592268 KB |
Output is correct |
35 |
Correct |
163 ms |
592464 KB |
Output is correct |
36 |
Correct |
155 ms |
592212 KB |
Output is correct |
37 |
Correct |
169 ms |
592208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
589652 KB |
Output is correct |
2 |
Correct |
126 ms |
589908 KB |
Output is correct |
3 |
Correct |
129 ms |
589868 KB |
Output is correct |
4 |
Correct |
126 ms |
589800 KB |
Output is correct |
5 |
Correct |
130 ms |
589944 KB |
Output is correct |
6 |
Correct |
128 ms |
589760 KB |
Output is correct |
7 |
Correct |
125 ms |
589760 KB |
Output is correct |
8 |
Correct |
129 ms |
589648 KB |
Output is correct |
9 |
Correct |
132 ms |
589748 KB |
Output is correct |
10 |
Correct |
127 ms |
589908 KB |
Output is correct |
11 |
Correct |
125 ms |
589736 KB |
Output is correct |
12 |
Correct |
124 ms |
589904 KB |
Output is correct |
13 |
Correct |
127 ms |
589648 KB |
Output is correct |
14 |
Correct |
130 ms |
589652 KB |
Output is correct |
15 |
Correct |
128 ms |
589684 KB |
Output is correct |
16 |
Correct |
130 ms |
589656 KB |
Output is correct |
17 |
Correct |
127 ms |
590176 KB |
Output is correct |
18 |
Correct |
132 ms |
590176 KB |
Output is correct |
19 |
Correct |
128 ms |
590164 KB |
Output is correct |
20 |
Correct |
128 ms |
589908 KB |
Output is correct |
21 |
Correct |
137 ms |
590160 KB |
Output is correct |
22 |
Correct |
133 ms |
590248 KB |
Output is correct |
23 |
Correct |
128 ms |
590160 KB |
Output is correct |
24 |
Correct |
126 ms |
589908 KB |
Output is correct |
25 |
Correct |
145 ms |
591440 KB |
Output is correct |
26 |
Correct |
146 ms |
591660 KB |
Output is correct |
27 |
Correct |
148 ms |
591620 KB |
Output is correct |
28 |
Correct |
143 ms |
591184 KB |
Output is correct |
29 |
Correct |
160 ms |
592268 KB |
Output is correct |
30 |
Correct |
163 ms |
592464 KB |
Output is correct |
31 |
Correct |
155 ms |
592212 KB |
Output is correct |
32 |
Correct |
169 ms |
592208 KB |
Output is correct |
33 |
Correct |
126 ms |
589648 KB |
Output is correct |
34 |
Correct |
125 ms |
589648 KB |
Output is correct |
35 |
Correct |
127 ms |
589700 KB |
Output is correct |
36 |
Correct |
128 ms |
589672 KB |
Output is correct |
37 |
Correct |
130 ms |
589760 KB |
Output is correct |
38 |
Correct |
418 ms |
617804 KB |
Output is correct |
39 |
Correct |
421 ms |
622704 KB |
Output is correct |
40 |
Correct |
447 ms |
622672 KB |
Output is correct |
41 |
Correct |
415 ms |
627460 KB |
Output is correct |
42 |
Correct |
428 ms |
613924 KB |
Output is correct |
43 |
Correct |
433 ms |
613968 KB |
Output is correct |
44 |
Correct |
430 ms |
614168 KB |
Output is correct |
45 |
Correct |
398 ms |
613204 KB |
Output is correct |
46 |
Correct |
319 ms |
603936 KB |
Output is correct |
47 |
Correct |
368 ms |
607312 KB |
Output is correct |
48 |
Correct |
603 ms |
619700 KB |
Output is correct |
49 |
Correct |
571 ms |
622140 KB |
Output is correct |
50 |
Correct |
334 ms |
606036 KB |
Output is correct |
51 |
Correct |
354 ms |
605904 KB |
Output is correct |
52 |
Correct |
534 ms |
618388 KB |
Output is correct |
53 |
Correct |
563 ms |
619344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
590136 KB |
Output is correct |
2 |
Correct |
131 ms |
590160 KB |
Output is correct |
3 |
Correct |
132 ms |
589908 KB |
Output is correct |
4 |
Correct |
125 ms |
589828 KB |
Output is correct |
5 |
Correct |
148 ms |
590688 KB |
Output is correct |
6 |
Correct |
138 ms |
590420 KB |
Output is correct |
7 |
Correct |
139 ms |
590416 KB |
Output is correct |
8 |
Correct |
136 ms |
590164 KB |
Output is correct |
9 |
Correct |
135 ms |
590164 KB |
Output is correct |
10 |
Correct |
133 ms |
590088 KB |
Output is correct |
11 |
Correct |
136 ms |
590160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
589648 KB |
Output is correct |
2 |
Correct |
125 ms |
589648 KB |
Output is correct |
3 |
Correct |
127 ms |
589700 KB |
Output is correct |
4 |
Correct |
128 ms |
589672 KB |
Output is correct |
5 |
Correct |
130 ms |
589760 KB |
Output is correct |
6 |
Correct |
124 ms |
589652 KB |
Output is correct |
7 |
Correct |
1258 ms |
672440 KB |
Output is correct |
8 |
Correct |
2731 ms |
768604 KB |
Output is correct |
9 |
Correct |
2679 ms |
769756 KB |
Output is correct |
10 |
Correct |
2618 ms |
769836 KB |
Output is correct |
11 |
Correct |
327 ms |
620116 KB |
Output is correct |
12 |
Correct |
479 ms |
647688 KB |
Output is correct |
13 |
Correct |
487 ms |
651092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
589652 KB |
Output is correct |
2 |
Correct |
126 ms |
589908 KB |
Output is correct |
3 |
Correct |
129 ms |
589868 KB |
Output is correct |
4 |
Correct |
126 ms |
589800 KB |
Output is correct |
5 |
Correct |
130 ms |
589944 KB |
Output is correct |
6 |
Correct |
128 ms |
589760 KB |
Output is correct |
7 |
Correct |
125 ms |
589760 KB |
Output is correct |
8 |
Correct |
129 ms |
589648 KB |
Output is correct |
9 |
Correct |
132 ms |
589748 KB |
Output is correct |
10 |
Correct |
127 ms |
589908 KB |
Output is correct |
11 |
Correct |
125 ms |
589736 KB |
Output is correct |
12 |
Correct |
124 ms |
589904 KB |
Output is correct |
13 |
Correct |
127 ms |
589648 KB |
Output is correct |
14 |
Correct |
130 ms |
589652 KB |
Output is correct |
15 |
Correct |
128 ms |
589684 KB |
Output is correct |
16 |
Correct |
130 ms |
589656 KB |
Output is correct |
17 |
Correct |
127 ms |
590176 KB |
Output is correct |
18 |
Correct |
132 ms |
590176 KB |
Output is correct |
19 |
Correct |
128 ms |
590164 KB |
Output is correct |
20 |
Correct |
128 ms |
589908 KB |
Output is correct |
21 |
Correct |
137 ms |
590160 KB |
Output is correct |
22 |
Correct |
133 ms |
590248 KB |
Output is correct |
23 |
Correct |
128 ms |
590160 KB |
Output is correct |
24 |
Correct |
126 ms |
589908 KB |
Output is correct |
25 |
Correct |
145 ms |
591440 KB |
Output is correct |
26 |
Correct |
146 ms |
591660 KB |
Output is correct |
27 |
Correct |
148 ms |
591620 KB |
Output is correct |
28 |
Correct |
143 ms |
591184 KB |
Output is correct |
29 |
Correct |
160 ms |
592268 KB |
Output is correct |
30 |
Correct |
163 ms |
592464 KB |
Output is correct |
31 |
Correct |
155 ms |
592212 KB |
Output is correct |
32 |
Correct |
169 ms |
592208 KB |
Output is correct |
33 |
Correct |
418 ms |
617804 KB |
Output is correct |
34 |
Correct |
421 ms |
622704 KB |
Output is correct |
35 |
Correct |
447 ms |
622672 KB |
Output is correct |
36 |
Correct |
415 ms |
627460 KB |
Output is correct |
37 |
Correct |
428 ms |
613924 KB |
Output is correct |
38 |
Correct |
433 ms |
613968 KB |
Output is correct |
39 |
Correct |
430 ms |
614168 KB |
Output is correct |
40 |
Correct |
398 ms |
613204 KB |
Output is correct |
41 |
Correct |
319 ms |
603936 KB |
Output is correct |
42 |
Correct |
368 ms |
607312 KB |
Output is correct |
43 |
Correct |
603 ms |
619700 KB |
Output is correct |
44 |
Correct |
571 ms |
622140 KB |
Output is correct |
45 |
Correct |
334 ms |
606036 KB |
Output is correct |
46 |
Correct |
354 ms |
605904 KB |
Output is correct |
47 |
Correct |
534 ms |
618388 KB |
Output is correct |
48 |
Correct |
563 ms |
619344 KB |
Output is correct |
49 |
Correct |
132 ms |
590136 KB |
Output is correct |
50 |
Correct |
131 ms |
590160 KB |
Output is correct |
51 |
Correct |
132 ms |
589908 KB |
Output is correct |
52 |
Correct |
125 ms |
589828 KB |
Output is correct |
53 |
Correct |
148 ms |
590688 KB |
Output is correct |
54 |
Correct |
138 ms |
590420 KB |
Output is correct |
55 |
Correct |
139 ms |
590416 KB |
Output is correct |
56 |
Correct |
136 ms |
590164 KB |
Output is correct |
57 |
Correct |
135 ms |
590164 KB |
Output is correct |
58 |
Correct |
133 ms |
590088 KB |
Output is correct |
59 |
Correct |
136 ms |
590160 KB |
Output is correct |
60 |
Correct |
124 ms |
589652 KB |
Output is correct |
61 |
Correct |
1258 ms |
672440 KB |
Output is correct |
62 |
Correct |
2731 ms |
768604 KB |
Output is correct |
63 |
Correct |
2679 ms |
769756 KB |
Output is correct |
64 |
Correct |
2618 ms |
769836 KB |
Output is correct |
65 |
Correct |
327 ms |
620116 KB |
Output is correct |
66 |
Correct |
479 ms |
647688 KB |
Output is correct |
67 |
Correct |
487 ms |
651092 KB |
Output is correct |
68 |
Correct |
126 ms |
589648 KB |
Output is correct |
69 |
Correct |
125 ms |
589648 KB |
Output is correct |
70 |
Correct |
127 ms |
589700 KB |
Output is correct |
71 |
Correct |
128 ms |
589672 KB |
Output is correct |
72 |
Correct |
130 ms |
589760 KB |
Output is correct |
73 |
Correct |
4359 ms |
948304 KB |
Output is correct |
74 |
Correct |
4270 ms |
1012260 KB |
Output is correct |
75 |
Correct |
4304 ms |
1012280 KB |
Output is correct |
76 |
Correct |
3923 ms |
1048576 KB |
Output is correct |
77 |
Correct |
4455 ms |
899320 KB |
Output is correct |
78 |
Correct |
4607 ms |
817020 KB |
Output is correct |
79 |
Correct |
4708 ms |
841488 KB |
Output is correct |
80 |
Execution timed out |
5065 ms |
721340 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |