#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2,popcnt,sse4")
#pragma GCC optimize("O3,unroll-loops")
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
#define _all(T) T.begin(),T.end()
#define tiii tuple<int,int,int>
const int mxn = 2520;
int N,M;
vector<vector<int>> arr;
vector<pii> row[mxn][mxn],col[mxn][mxn];
struct BIT{
int bit[mxn];
void modify(int p,int v){
p++;
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
int getval(int s,int e = -1){
s++,e++;
if(s>e)swap(s,e);
int re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
}
};
BIT bit;
ll calc(vector<pii> &v1,vector<pii> &v2){
vector<tiii> v;
ll re = 0;
for(auto &i:v1)if(i.sc != -1)v.push_back(tiii(i.fs,0,i.sc));
for(auto &i:v2)if(i.sc != -1)v.push_back(tiii(i.sc,1,i.fs));
sort(v.begin(),v.end());
for(auto [_,tp,pos]:v){
if(tp == 0)bit.modify(pos,1);
else re += bit.getval(pos,mxn-1);
}
for(auto [_,tp,pos]:v){
bit.modify(pos,-bit.getval(pos,pos));
}
return re;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
N = a.size(),M = a[0].size();
arr = a;
{
for(int i = 0;i<N;i++){
vector<int> st;
vector<pii> rng(M,pii(-1,-1));
for(int j = 0;j<M;j++){
while(!st.empty()&&arr[i][j]>=arr[i][st.back()])st.pop_back();
if(!st.empty())rng[j].fs = st.back();
st.push_back(j);
}
st.clear();
for(int j = M-1;j>=0;j--){
while(!st.empty()&&arr[i][j]>=arr[i][st.back()])st.pop_back();
if(!st.empty())rng[j].sc = st.back();
st.push_back(j);
}
for(int j = 0;j<M;j++){
if(rng[j].fs != -1&&rng[j].sc != -1){
row[i][rng[j].fs+1].push_back(pii(rng[j].sc-1,-1));
}
}
//cerr<<"ROW: "<<i<<":";for(auto &j:rng)cerr<<j.fs<<','<<j.sc<<' ';cerr<<endl;
}
for(int i = 0;i<M;i++){
vector<int> st;
vector<pii> rng(N,pii(-1,-1));
for(int j = 0;j<N;j++){
while(!st.empty()&&arr[st.back()][i]<=arr[j][i])st.pop_back();
if(!st.empty())rng[j].fs = st.back();
st.push_back(j);
}
st.clear();
for(int j = N-1;j>=0;j--){
while(!st.empty()&&arr[st.back()][i]<=arr[j][i])st.pop_back();
if(!st.empty())rng[j].sc = st.back();
st.push_back(j);
}
for(int j = 0;j<N;j++){
if(rng[j].fs != -1&&rng[j].sc != -1){
col[rng[j].fs+1][i].push_back(pii(rng[j].sc-1,-1));
}
}
//cerr<<"COL: "<<i<<":";for(auto &j:rng)cerr<<j.fs<<','<<j.sc<<' ';cerr<<endl;
}
}
for(int i = 0;i<N;i++)for(int j = 0;j<M;j++){
sort(_all(row[i][j]));
sort(_all(col[i][j]));
row[i][j].resize(unique(_all(row[i][j]))-row[i][j].begin());
col[i][j].resize(unique(_all(col[i][j]))-col[i][j].begin());
}
{
int nxt[mxn] = {};
for(int i = 0;i<M;i++){
memset(nxt,-1,sizeof(nxt));
for(int j = 0;j<N;j++){
for(auto &[tar,rp]:row[j][i]){
if(nxt[tar]<j)nxt[tar] = j;
while(nxt[tar]+1<N){
int tmp = nxt[tar]+1;
auto it = lower_bound(_all(row[tmp][i]),pii(tar,-1));
if(it == row[tmp][i].end()||it->fs != tar)break;
nxt[tar]++;
}
rp = nxt[tar];
}
}
}
for(int i = 0;i<N;i++){
memset(nxt,-1,sizeof(nxt));
for(int j = 0;j<M;j++){
for(auto &[tar,rp]:col[i][j]){
if(nxt[tar]<j)nxt[tar] = j;
while(nxt[tar]+1<M){
int tmp = nxt[tar]+1;
auto it = lower_bound(_all(col[i][tmp]),pii(tar,-1));
if(it == col[i][tmp].end()||it->fs != tar)break;
nxt[tar]++;
}
rp = nxt[tar];
}
}
}
}
ll ans = 0;
{
for(int i = 0;i<N;i++){
for(int j = 0;j<M;j++){
//cerr<<"CALC: "<<i<<','<<j<<endl;
if(row[i][j].empty()||col[i][j].empty())continue;
/*
cerr<<"CALCING: "<<i<<','<<j<<endl;
for(auto &k:row[i][j])cerr<<k.fs<<','<<k.sc<<' ';cerr<<endl;
for(auto &k:col[i][j])cerr<<k.fs<<','<<k.sc<<' ';cerr<<endl;
*/
ans += calc(row[i][j],col[i][j]);
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
298648 KB |
Output is correct |
2 |
Correct |
120 ms |
298576 KB |
Output is correct |
3 |
Correct |
120 ms |
298576 KB |
Output is correct |
4 |
Correct |
108 ms |
298580 KB |
Output is correct |
5 |
Correct |
121 ms |
298576 KB |
Output is correct |
6 |
Correct |
116 ms |
298572 KB |
Output is correct |
7 |
Correct |
116 ms |
298572 KB |
Output is correct |
8 |
Correct |
112 ms |
298580 KB |
Output is correct |
9 |
Correct |
115 ms |
298584 KB |
Output is correct |
10 |
Correct |
112 ms |
298536 KB |
Output is correct |
11 |
Correct |
146 ms |
298580 KB |
Output is correct |
12 |
Correct |
113 ms |
298580 KB |
Output is correct |
13 |
Correct |
122 ms |
298580 KB |
Output is correct |
14 |
Correct |
113 ms |
298692 KB |
Output is correct |
15 |
Correct |
118 ms |
298580 KB |
Output is correct |
16 |
Correct |
117 ms |
298580 KB |
Output is correct |
17 |
Correct |
131 ms |
298468 KB |
Output is correct |
18 |
Correct |
119 ms |
298576 KB |
Output is correct |
19 |
Correct |
128 ms |
298580 KB |
Output is correct |
20 |
Correct |
121 ms |
298460 KB |
Output is correct |
21 |
Correct |
114 ms |
298616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
298648 KB |
Output is correct |
2 |
Correct |
120 ms |
298576 KB |
Output is correct |
3 |
Correct |
120 ms |
298576 KB |
Output is correct |
4 |
Correct |
108 ms |
298580 KB |
Output is correct |
5 |
Correct |
121 ms |
298576 KB |
Output is correct |
6 |
Correct |
116 ms |
298572 KB |
Output is correct |
7 |
Correct |
116 ms |
298572 KB |
Output is correct |
8 |
Correct |
112 ms |
298580 KB |
Output is correct |
9 |
Correct |
115 ms |
298584 KB |
Output is correct |
10 |
Correct |
112 ms |
298536 KB |
Output is correct |
11 |
Correct |
146 ms |
298580 KB |
Output is correct |
12 |
Correct |
113 ms |
298580 KB |
Output is correct |
13 |
Correct |
122 ms |
298580 KB |
Output is correct |
14 |
Correct |
113 ms |
298692 KB |
Output is correct |
15 |
Correct |
118 ms |
298580 KB |
Output is correct |
16 |
Correct |
117 ms |
298580 KB |
Output is correct |
17 |
Correct |
131 ms |
298468 KB |
Output is correct |
18 |
Correct |
119 ms |
298576 KB |
Output is correct |
19 |
Correct |
128 ms |
298580 KB |
Output is correct |
20 |
Correct |
121 ms |
298460 KB |
Output is correct |
21 |
Correct |
114 ms |
298616 KB |
Output is correct |
22 |
Correct |
125 ms |
299164 KB |
Output is correct |
23 |
Correct |
127 ms |
299092 KB |
Output is correct |
24 |
Correct |
123 ms |
299156 KB |
Output is correct |
25 |
Correct |
136 ms |
298836 KB |
Output is correct |
26 |
Correct |
123 ms |
298840 KB |
Output is correct |
27 |
Correct |
123 ms |
299004 KB |
Output is correct |
28 |
Correct |
117 ms |
298996 KB |
Output is correct |
29 |
Correct |
119 ms |
298628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
298648 KB |
Output is correct |
2 |
Correct |
120 ms |
298576 KB |
Output is correct |
3 |
Correct |
120 ms |
298576 KB |
Output is correct |
4 |
Correct |
108 ms |
298580 KB |
Output is correct |
5 |
Correct |
121 ms |
298576 KB |
Output is correct |
6 |
Correct |
116 ms |
298572 KB |
Output is correct |
7 |
Correct |
116 ms |
298572 KB |
Output is correct |
8 |
Correct |
112 ms |
298580 KB |
Output is correct |
9 |
Correct |
115 ms |
298584 KB |
Output is correct |
10 |
Correct |
112 ms |
298536 KB |
Output is correct |
11 |
Correct |
146 ms |
298580 KB |
Output is correct |
12 |
Correct |
113 ms |
298580 KB |
Output is correct |
13 |
Correct |
122 ms |
298580 KB |
Output is correct |
14 |
Correct |
113 ms |
298692 KB |
Output is correct |
15 |
Correct |
118 ms |
298580 KB |
Output is correct |
16 |
Correct |
117 ms |
298580 KB |
Output is correct |
17 |
Correct |
125 ms |
299164 KB |
Output is correct |
18 |
Correct |
127 ms |
299092 KB |
Output is correct |
19 |
Correct |
123 ms |
299156 KB |
Output is correct |
20 |
Correct |
136 ms |
298836 KB |
Output is correct |
21 |
Correct |
123 ms |
298840 KB |
Output is correct |
22 |
Correct |
123 ms |
299004 KB |
Output is correct |
23 |
Correct |
117 ms |
298996 KB |
Output is correct |
24 |
Correct |
119 ms |
298628 KB |
Output is correct |
25 |
Correct |
131 ms |
298468 KB |
Output is correct |
26 |
Correct |
119 ms |
298576 KB |
Output is correct |
27 |
Correct |
128 ms |
298580 KB |
Output is correct |
28 |
Correct |
121 ms |
298460 KB |
Output is correct |
29 |
Correct |
114 ms |
298616 KB |
Output is correct |
30 |
Correct |
130 ms |
301648 KB |
Output is correct |
31 |
Correct |
132 ms |
301648 KB |
Output is correct |
32 |
Correct |
135 ms |
301904 KB |
Output is correct |
33 |
Correct |
132 ms |
300112 KB |
Output is correct |
34 |
Correct |
136 ms |
300624 KB |
Output is correct |
35 |
Correct |
134 ms |
300884 KB |
Output is correct |
36 |
Correct |
142 ms |
300628 KB |
Output is correct |
37 |
Correct |
141 ms |
300628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
298648 KB |
Output is correct |
2 |
Correct |
120 ms |
298576 KB |
Output is correct |
3 |
Correct |
120 ms |
298576 KB |
Output is correct |
4 |
Correct |
108 ms |
298580 KB |
Output is correct |
5 |
Correct |
121 ms |
298576 KB |
Output is correct |
6 |
Correct |
116 ms |
298572 KB |
Output is correct |
7 |
Correct |
116 ms |
298572 KB |
Output is correct |
8 |
Correct |
112 ms |
298580 KB |
Output is correct |
9 |
Correct |
115 ms |
298584 KB |
Output is correct |
10 |
Correct |
112 ms |
298536 KB |
Output is correct |
11 |
Correct |
146 ms |
298580 KB |
Output is correct |
12 |
Correct |
113 ms |
298580 KB |
Output is correct |
13 |
Correct |
122 ms |
298580 KB |
Output is correct |
14 |
Correct |
113 ms |
298692 KB |
Output is correct |
15 |
Correct |
118 ms |
298580 KB |
Output is correct |
16 |
Correct |
117 ms |
298580 KB |
Output is correct |
17 |
Correct |
125 ms |
299164 KB |
Output is correct |
18 |
Correct |
127 ms |
299092 KB |
Output is correct |
19 |
Correct |
123 ms |
299156 KB |
Output is correct |
20 |
Correct |
136 ms |
298836 KB |
Output is correct |
21 |
Correct |
123 ms |
298840 KB |
Output is correct |
22 |
Correct |
123 ms |
299004 KB |
Output is correct |
23 |
Correct |
117 ms |
298996 KB |
Output is correct |
24 |
Correct |
119 ms |
298628 KB |
Output is correct |
25 |
Correct |
130 ms |
301648 KB |
Output is correct |
26 |
Correct |
132 ms |
301648 KB |
Output is correct |
27 |
Correct |
135 ms |
301904 KB |
Output is correct |
28 |
Correct |
132 ms |
300112 KB |
Output is correct |
29 |
Correct |
136 ms |
300624 KB |
Output is correct |
30 |
Correct |
134 ms |
300884 KB |
Output is correct |
31 |
Correct |
142 ms |
300628 KB |
Output is correct |
32 |
Correct |
141 ms |
300628 KB |
Output is correct |
33 |
Correct |
131 ms |
298468 KB |
Output is correct |
34 |
Correct |
119 ms |
298576 KB |
Output is correct |
35 |
Correct |
128 ms |
298580 KB |
Output is correct |
36 |
Correct |
121 ms |
298460 KB |
Output is correct |
37 |
Correct |
114 ms |
298616 KB |
Output is correct |
38 |
Correct |
181 ms |
323008 KB |
Output is correct |
39 |
Correct |
199 ms |
318196 KB |
Output is correct |
40 |
Correct |
172 ms |
318036 KB |
Output is correct |
41 |
Correct |
188 ms |
313428 KB |
Output is correct |
42 |
Correct |
305 ms |
338256 KB |
Output is correct |
43 |
Correct |
257 ms |
338156 KB |
Output is correct |
44 |
Correct |
254 ms |
338516 KB |
Output is correct |
45 |
Correct |
250 ms |
336212 KB |
Output is correct |
46 |
Correct |
208 ms |
314600 KB |
Output is correct |
47 |
Correct |
246 ms |
317496 KB |
Output is correct |
48 |
Correct |
316 ms |
324176 KB |
Output is correct |
49 |
Correct |
311 ms |
326224 KB |
Output is correct |
50 |
Correct |
235 ms |
312320 KB |
Output is correct |
51 |
Correct |
214 ms |
312400 KB |
Output is correct |
52 |
Correct |
302 ms |
324448 KB |
Output is correct |
53 |
Correct |
316 ms |
325716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
298916 KB |
Output is correct |
2 |
Correct |
142 ms |
298844 KB |
Output is correct |
3 |
Correct |
127 ms |
298892 KB |
Output is correct |
4 |
Correct |
129 ms |
298636 KB |
Output is correct |
5 |
Correct |
131 ms |
298832 KB |
Output is correct |
6 |
Correct |
123 ms |
298832 KB |
Output is correct |
7 |
Correct |
126 ms |
298836 KB |
Output is correct |
8 |
Correct |
118 ms |
298732 KB |
Output is correct |
9 |
Correct |
127 ms |
298836 KB |
Output is correct |
10 |
Correct |
119 ms |
298580 KB |
Output is correct |
11 |
Correct |
123 ms |
298840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
298468 KB |
Output is correct |
2 |
Correct |
119 ms |
298576 KB |
Output is correct |
3 |
Correct |
128 ms |
298580 KB |
Output is correct |
4 |
Correct |
121 ms |
298460 KB |
Output is correct |
5 |
Correct |
114 ms |
298616 KB |
Output is correct |
6 |
Correct |
143 ms |
298608 KB |
Output is correct |
7 |
Correct |
716 ms |
391504 KB |
Output is correct |
8 |
Correct |
1458 ms |
500204 KB |
Output is correct |
9 |
Correct |
1516 ms |
501440 KB |
Output is correct |
10 |
Correct |
1414 ms |
501212 KB |
Output is correct |
11 |
Correct |
226 ms |
341072 KB |
Output is correct |
12 |
Correct |
374 ms |
379472 KB |
Output is correct |
13 |
Correct |
382 ms |
384576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
298648 KB |
Output is correct |
2 |
Correct |
120 ms |
298576 KB |
Output is correct |
3 |
Correct |
120 ms |
298576 KB |
Output is correct |
4 |
Correct |
108 ms |
298580 KB |
Output is correct |
5 |
Correct |
121 ms |
298576 KB |
Output is correct |
6 |
Correct |
116 ms |
298572 KB |
Output is correct |
7 |
Correct |
116 ms |
298572 KB |
Output is correct |
8 |
Correct |
112 ms |
298580 KB |
Output is correct |
9 |
Correct |
115 ms |
298584 KB |
Output is correct |
10 |
Correct |
112 ms |
298536 KB |
Output is correct |
11 |
Correct |
146 ms |
298580 KB |
Output is correct |
12 |
Correct |
113 ms |
298580 KB |
Output is correct |
13 |
Correct |
122 ms |
298580 KB |
Output is correct |
14 |
Correct |
113 ms |
298692 KB |
Output is correct |
15 |
Correct |
118 ms |
298580 KB |
Output is correct |
16 |
Correct |
117 ms |
298580 KB |
Output is correct |
17 |
Correct |
125 ms |
299164 KB |
Output is correct |
18 |
Correct |
127 ms |
299092 KB |
Output is correct |
19 |
Correct |
123 ms |
299156 KB |
Output is correct |
20 |
Correct |
136 ms |
298836 KB |
Output is correct |
21 |
Correct |
123 ms |
298840 KB |
Output is correct |
22 |
Correct |
123 ms |
299004 KB |
Output is correct |
23 |
Correct |
117 ms |
298996 KB |
Output is correct |
24 |
Correct |
119 ms |
298628 KB |
Output is correct |
25 |
Correct |
130 ms |
301648 KB |
Output is correct |
26 |
Correct |
132 ms |
301648 KB |
Output is correct |
27 |
Correct |
135 ms |
301904 KB |
Output is correct |
28 |
Correct |
132 ms |
300112 KB |
Output is correct |
29 |
Correct |
136 ms |
300624 KB |
Output is correct |
30 |
Correct |
134 ms |
300884 KB |
Output is correct |
31 |
Correct |
142 ms |
300628 KB |
Output is correct |
32 |
Correct |
141 ms |
300628 KB |
Output is correct |
33 |
Correct |
181 ms |
323008 KB |
Output is correct |
34 |
Correct |
199 ms |
318196 KB |
Output is correct |
35 |
Correct |
172 ms |
318036 KB |
Output is correct |
36 |
Correct |
188 ms |
313428 KB |
Output is correct |
37 |
Correct |
305 ms |
338256 KB |
Output is correct |
38 |
Correct |
257 ms |
338156 KB |
Output is correct |
39 |
Correct |
254 ms |
338516 KB |
Output is correct |
40 |
Correct |
250 ms |
336212 KB |
Output is correct |
41 |
Correct |
208 ms |
314600 KB |
Output is correct |
42 |
Correct |
246 ms |
317496 KB |
Output is correct |
43 |
Correct |
316 ms |
324176 KB |
Output is correct |
44 |
Correct |
311 ms |
326224 KB |
Output is correct |
45 |
Correct |
235 ms |
312320 KB |
Output is correct |
46 |
Correct |
214 ms |
312400 KB |
Output is correct |
47 |
Correct |
302 ms |
324448 KB |
Output is correct |
48 |
Correct |
316 ms |
325716 KB |
Output is correct |
49 |
Correct |
130 ms |
298916 KB |
Output is correct |
50 |
Correct |
142 ms |
298844 KB |
Output is correct |
51 |
Correct |
127 ms |
298892 KB |
Output is correct |
52 |
Correct |
129 ms |
298636 KB |
Output is correct |
53 |
Correct |
131 ms |
298832 KB |
Output is correct |
54 |
Correct |
123 ms |
298832 KB |
Output is correct |
55 |
Correct |
126 ms |
298836 KB |
Output is correct |
56 |
Correct |
118 ms |
298732 KB |
Output is correct |
57 |
Correct |
127 ms |
298836 KB |
Output is correct |
58 |
Correct |
119 ms |
298580 KB |
Output is correct |
59 |
Correct |
123 ms |
298840 KB |
Output is correct |
60 |
Correct |
143 ms |
298608 KB |
Output is correct |
61 |
Correct |
716 ms |
391504 KB |
Output is correct |
62 |
Correct |
1458 ms |
500204 KB |
Output is correct |
63 |
Correct |
1516 ms |
501440 KB |
Output is correct |
64 |
Correct |
1414 ms |
501212 KB |
Output is correct |
65 |
Correct |
226 ms |
341072 KB |
Output is correct |
66 |
Correct |
374 ms |
379472 KB |
Output is correct |
67 |
Correct |
382 ms |
384576 KB |
Output is correct |
68 |
Correct |
131 ms |
298468 KB |
Output is correct |
69 |
Correct |
119 ms |
298576 KB |
Output is correct |
70 |
Correct |
128 ms |
298580 KB |
Output is correct |
71 |
Correct |
121 ms |
298460 KB |
Output is correct |
72 |
Correct |
114 ms |
298616 KB |
Output is correct |
73 |
Correct |
1274 ms |
614676 KB |
Output is correct |
74 |
Correct |
1273 ms |
547548 KB |
Output is correct |
75 |
Correct |
995 ms |
547672 KB |
Output is correct |
76 |
Correct |
1157 ms |
480720 KB |
Output is correct |
77 |
Correct |
2093 ms |
811372 KB |
Output is correct |
78 |
Correct |
1748 ms |
495956 KB |
Output is correct |
79 |
Correct |
1902 ms |
513892 KB |
Output is correct |
80 |
Correct |
2811 ms |
627540 KB |
Output is correct |
81 |
Correct |
1757 ms |
511060 KB |
Output is correct |
82 |
Correct |
2237 ms |
582188 KB |
Output is correct |
83 |
Correct |
2956 ms |
653236 KB |
Output is correct |
84 |
Correct |
1638 ms |
498960 KB |
Output is correct |
85 |
Correct |
2655 ms |
646032 KB |
Output is correct |
86 |
Correct |
2685 ms |
635984 KB |
Output is correct |
87 |
Correct |
1259 ms |
606328 KB |
Output is correct |
88 |
Correct |
2011 ms |
810860 KB |
Output is correct |
89 |
Correct |
1982 ms |
811268 KB |
Output is correct |
90 |
Correct |
2109 ms |
811380 KB |
Output is correct |