#include "rect.h"
#include <bits/stdc++.h>
#define N 2505
using namespace std;
struct BIT{
vector<short> bit;
short n;
short tot = 0;
BIT(short size){
n = size + 5;
bit.assign(n+5,0);
}
void upd(short pos,short val){
tot += val;
for(++pos;pos<n;pos += pos & -pos){
bit[pos] += val;
}
}
short get(short pos){
short ret = 0;
for(++pos;pos > 0;pos -= pos & -pos){
ret += bit[pos];
}
return ret;
}
short get(short l,short r){
return tot - get(l-1);
}
};
struct SegTree{
vector<BIT> t;
short n;
SegTree(short size){
n = size + 5;
t.assign(4*n,BIT(n));
}
void upd(short v,short tl,short tr,short l,short r,short val){
//cout << val << endl;
t[v].upd(r,val);
if(tl == tr)return;
short tm = (tl + tr)/2;
if(l <= tm){
upd(v*2,tl,tm,l,r,val);
}
else upd(v*2+1,tm+1,tr,l,r,val);
}
short get(short v,short tl,short tr,short l,short r){
if(tr <= l){
return t[v].get(r,n-1);
}
if(tl > l){
return 0;
}
short tm = (tl + tr)/2;
return get(v*2,tl,tm,l,r) + get(v*2+1,tm+1,tr,l,r);
}
void upd(short l,short r,short val){
upd(1,0,n-1,l,r,val);
}
short get(short l,short r){
return get(1,0,n-1,l,r);
}
};
short mp[N][N];
vector<short> pos[N];
vector<short> prepos[N];
vector<pair<short,short>> ranges[N][N];
vector<pair<short,short>> queries[N][N];
long long count_rectangles(vector<vector<int>> a){
auto beg = clock();
short n = a.size();
short m = a[0].size();
//cout << n << " " << m << endl;
if(n < 3 || m < 3){
return 0;
}
for(short i = m-1;i>=0;i--){
vector<short> st;
for(short j = 0;j<n;j++){
while(st.size() && a[j][i] > a[st.back()][i])
st.pop_back();
if(st.size() && st.back() != j-1 && a[st.back()][i] != a[j][i]){
pos[st.back()].push_back(j);
}
st.push_back(j);
}
st.clear();
for(short j = n-1;j>=0;j--){
while(st.size() && a[j][i] > a[st.back()][i])
st.pop_back();
if(st.size() && st.back() != j+1){
pos[j].push_back(st.back());
}
st.push_back(j);
}
for(short j = 0;j<n;j++){
for(auto u:prepos[j]){
mp[j][u] = -mp[j][u];
}
}
for(short j = 0;j<n;j++){
for(auto u:pos[j]){
mp[j][u] = 1 - mp[j][u];
if(i)
queries[i-1][mp[j][u]+1].push_back({j+1,u-1});
//cout << i << " " << mp[j][u] << " " << j << " " << u << endl;
}
}
for(short j = 0;j<n;j++){
for(auto u:prepos[j]){
if(mp[j][u] < 0)
mp[j][u] = 0;
}
swap(prepos[j],pos[j]);
pos[j].clear();
}
}
for(short i = 0;i<N;i++){
pos[i].clear();
prepos[i].clear();
for(short j = 0;j<N;j++){
mp[i][j] = 0;
}
}
for(short i = n-1;i>=0;i--){
vector<short> st;
for(short j = 0;j<m;j++){
while(st.size() && a[i][j] > a[i][st.back()])
st.pop_back();
if(st.size() && st.back() != j-1 && a[i][j] != a[i][st.back()]){
pos[st.back()].push_back(j);
}
st.push_back(j);
}
st.clear();
for(short j = m-1;j>=0;j--){
while(st.size() && a[i][j] > a[i][st.back()])
st.pop_back();
if(st.size() && st.back() != j+1){
pos[j].push_back(st.back());
}
st.push_back(j);
}
for(short j = 0;j<m;j++){
for(auto u:prepos[j]){
mp[j][u] = -mp[j][u];
}
}
for(short j = 0;j<m;j++){
for(auto u:pos[j]){
mp[j][u] = 1 - mp[j][u];
}
}
for(short j = 0;j<m;j++){
for(auto u:prepos[j]){
if(mp[j][u] < 0){
//cout << j << " " << u << " " << i + 1 << " " << i - mp[j][u] << endl;
ranges[j][u-j].push_back({i+1,i - mp[j][u]});
mp[j][u] = 0;
}
}
swap(prepos[j],pos[j]);
pos[j].clear();
}
}
for(short j = 0;j<m;j++){
for(auto u:prepos[j]){
if(mp[j][u] > 0){
//cout << j << " " << u << " " << -1 + 1 << " " << -1 + mp[j][u] << endl;
ranges[j][u-j].push_back({-1+1,-1 + mp[j][u]});
mp[j][u] = 0;
}
}
}
assert((double)(clock() - beg)/CLOCKS_PER_SEC < 1.0);
SegTree tree(n);
long long ans = 0;
for(short i = 0;i<m;i++){
for(short j = 0;j<=m+1;j++){
for(auto u:ranges[i][j]){
//cout << "add" << u.first << " " << u.second << endl;
tree.upd(u.first,u.second,1);
}
for(auto u:queries[i][j]){
//cout << "..." << i << " " << i+j << " " << u.first << " " << u.second << endl;
ans += tree.get(u.first,u.second);
//cout << ans << endl;
}
}
for(short j = 0;j<=m+1;j++){
for(auto u:ranges[i][j]){
//cout << "del" << u.first << " " << u.second << endl;
tree.upd(u.first,u.second,-1);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
307400 KB |
Output is correct |
2 |
Correct |
136 ms |
307308 KB |
Output is correct |
3 |
Correct |
142 ms |
307532 KB |
Output is correct |
4 |
Correct |
141 ms |
307500 KB |
Output is correct |
5 |
Correct |
146 ms |
307300 KB |
Output is correct |
6 |
Correct |
138 ms |
307416 KB |
Output is correct |
7 |
Correct |
146 ms |
307408 KB |
Output is correct |
8 |
Correct |
140 ms |
307336 KB |
Output is correct |
9 |
Correct |
145 ms |
307376 KB |
Output is correct |
10 |
Correct |
144 ms |
307316 KB |
Output is correct |
11 |
Correct |
150 ms |
307384 KB |
Output is correct |
12 |
Correct |
137 ms |
307328 KB |
Output is correct |
13 |
Correct |
132 ms |
295116 KB |
Output is correct |
14 |
Correct |
131 ms |
295100 KB |
Output is correct |
15 |
Correct |
155 ms |
307356 KB |
Output is correct |
16 |
Correct |
150 ms |
307332 KB |
Output is correct |
17 |
Correct |
135 ms |
295092 KB |
Output is correct |
18 |
Correct |
137 ms |
295120 KB |
Output is correct |
19 |
Correct |
140 ms |
307404 KB |
Output is correct |
20 |
Correct |
137 ms |
307328 KB |
Output is correct |
21 |
Correct |
131 ms |
295080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
307400 KB |
Output is correct |
2 |
Correct |
136 ms |
307308 KB |
Output is correct |
3 |
Correct |
142 ms |
307532 KB |
Output is correct |
4 |
Correct |
141 ms |
307500 KB |
Output is correct |
5 |
Correct |
146 ms |
307300 KB |
Output is correct |
6 |
Correct |
138 ms |
307416 KB |
Output is correct |
7 |
Correct |
146 ms |
307408 KB |
Output is correct |
8 |
Correct |
140 ms |
307336 KB |
Output is correct |
9 |
Correct |
145 ms |
307376 KB |
Output is correct |
10 |
Correct |
144 ms |
307316 KB |
Output is correct |
11 |
Correct |
150 ms |
307384 KB |
Output is correct |
12 |
Correct |
137 ms |
307328 KB |
Output is correct |
13 |
Correct |
132 ms |
295116 KB |
Output is correct |
14 |
Correct |
131 ms |
295100 KB |
Output is correct |
15 |
Correct |
155 ms |
307356 KB |
Output is correct |
16 |
Correct |
150 ms |
307332 KB |
Output is correct |
17 |
Correct |
135 ms |
295092 KB |
Output is correct |
18 |
Correct |
137 ms |
295120 KB |
Output is correct |
19 |
Correct |
140 ms |
307404 KB |
Output is correct |
20 |
Correct |
137 ms |
307328 KB |
Output is correct |
21 |
Correct |
131 ms |
295080 KB |
Output is correct |
22 |
Correct |
154 ms |
307600 KB |
Output is correct |
23 |
Correct |
138 ms |
307492 KB |
Output is correct |
24 |
Correct |
139 ms |
307536 KB |
Output is correct |
25 |
Correct |
147 ms |
307448 KB |
Output is correct |
26 |
Correct |
157 ms |
307584 KB |
Output is correct |
27 |
Correct |
138 ms |
307568 KB |
Output is correct |
28 |
Correct |
138 ms |
307496 KB |
Output is correct |
29 |
Correct |
139 ms |
307468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
307400 KB |
Output is correct |
2 |
Correct |
136 ms |
307308 KB |
Output is correct |
3 |
Correct |
142 ms |
307532 KB |
Output is correct |
4 |
Correct |
141 ms |
307500 KB |
Output is correct |
5 |
Correct |
146 ms |
307300 KB |
Output is correct |
6 |
Correct |
138 ms |
307416 KB |
Output is correct |
7 |
Correct |
146 ms |
307408 KB |
Output is correct |
8 |
Correct |
140 ms |
307336 KB |
Output is correct |
9 |
Correct |
145 ms |
307376 KB |
Output is correct |
10 |
Correct |
144 ms |
307316 KB |
Output is correct |
11 |
Correct |
150 ms |
307384 KB |
Output is correct |
12 |
Correct |
137 ms |
307328 KB |
Output is correct |
13 |
Correct |
132 ms |
295116 KB |
Output is correct |
14 |
Correct |
131 ms |
295100 KB |
Output is correct |
15 |
Correct |
155 ms |
307356 KB |
Output is correct |
16 |
Correct |
150 ms |
307332 KB |
Output is correct |
17 |
Correct |
154 ms |
307600 KB |
Output is correct |
18 |
Correct |
138 ms |
307492 KB |
Output is correct |
19 |
Correct |
139 ms |
307536 KB |
Output is correct |
20 |
Correct |
147 ms |
307448 KB |
Output is correct |
21 |
Correct |
157 ms |
307584 KB |
Output is correct |
22 |
Correct |
138 ms |
307568 KB |
Output is correct |
23 |
Correct |
138 ms |
307496 KB |
Output is correct |
24 |
Correct |
139 ms |
307468 KB |
Output is correct |
25 |
Correct |
135 ms |
295092 KB |
Output is correct |
26 |
Correct |
137 ms |
295120 KB |
Output is correct |
27 |
Correct |
140 ms |
307404 KB |
Output is correct |
28 |
Correct |
137 ms |
307328 KB |
Output is correct |
29 |
Correct |
131 ms |
295080 KB |
Output is correct |
30 |
Correct |
155 ms |
308264 KB |
Output is correct |
31 |
Correct |
156 ms |
308196 KB |
Output is correct |
32 |
Correct |
149 ms |
308300 KB |
Output is correct |
33 |
Correct |
151 ms |
308308 KB |
Output is correct |
34 |
Correct |
159 ms |
308644 KB |
Output is correct |
35 |
Correct |
167 ms |
308648 KB |
Output is correct |
36 |
Correct |
153 ms |
308604 KB |
Output is correct |
37 |
Correct |
155 ms |
308424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
307400 KB |
Output is correct |
2 |
Correct |
136 ms |
307308 KB |
Output is correct |
3 |
Correct |
142 ms |
307532 KB |
Output is correct |
4 |
Correct |
141 ms |
307500 KB |
Output is correct |
5 |
Correct |
146 ms |
307300 KB |
Output is correct |
6 |
Correct |
138 ms |
307416 KB |
Output is correct |
7 |
Correct |
146 ms |
307408 KB |
Output is correct |
8 |
Correct |
140 ms |
307336 KB |
Output is correct |
9 |
Correct |
145 ms |
307376 KB |
Output is correct |
10 |
Correct |
144 ms |
307316 KB |
Output is correct |
11 |
Correct |
150 ms |
307384 KB |
Output is correct |
12 |
Correct |
137 ms |
307328 KB |
Output is correct |
13 |
Correct |
132 ms |
295116 KB |
Output is correct |
14 |
Correct |
131 ms |
295100 KB |
Output is correct |
15 |
Correct |
155 ms |
307356 KB |
Output is correct |
16 |
Correct |
150 ms |
307332 KB |
Output is correct |
17 |
Correct |
154 ms |
307600 KB |
Output is correct |
18 |
Correct |
138 ms |
307492 KB |
Output is correct |
19 |
Correct |
139 ms |
307536 KB |
Output is correct |
20 |
Correct |
147 ms |
307448 KB |
Output is correct |
21 |
Correct |
157 ms |
307584 KB |
Output is correct |
22 |
Correct |
138 ms |
307568 KB |
Output is correct |
23 |
Correct |
138 ms |
307496 KB |
Output is correct |
24 |
Correct |
139 ms |
307468 KB |
Output is correct |
25 |
Correct |
155 ms |
308264 KB |
Output is correct |
26 |
Correct |
156 ms |
308196 KB |
Output is correct |
27 |
Correct |
149 ms |
308300 KB |
Output is correct |
28 |
Correct |
151 ms |
308308 KB |
Output is correct |
29 |
Correct |
159 ms |
308644 KB |
Output is correct |
30 |
Correct |
167 ms |
308648 KB |
Output is correct |
31 |
Correct |
153 ms |
308604 KB |
Output is correct |
32 |
Correct |
155 ms |
308424 KB |
Output is correct |
33 |
Correct |
135 ms |
295092 KB |
Output is correct |
34 |
Correct |
137 ms |
295120 KB |
Output is correct |
35 |
Correct |
140 ms |
307404 KB |
Output is correct |
36 |
Correct |
137 ms |
307328 KB |
Output is correct |
37 |
Correct |
131 ms |
295080 KB |
Output is correct |
38 |
Correct |
292 ms |
324400 KB |
Output is correct |
39 |
Correct |
254 ms |
325176 KB |
Output is correct |
40 |
Correct |
299 ms |
325196 KB |
Output is correct |
41 |
Correct |
247 ms |
325068 KB |
Output is correct |
42 |
Correct |
231 ms |
318216 KB |
Output is correct |
43 |
Correct |
256 ms |
318180 KB |
Output is correct |
44 |
Correct |
232 ms |
318160 KB |
Output is correct |
45 |
Correct |
218 ms |
317476 KB |
Output is correct |
46 |
Correct |
251 ms |
317024 KB |
Output is correct |
47 |
Correct |
272 ms |
317344 KB |
Output is correct |
48 |
Correct |
445 ms |
321732 KB |
Output is correct |
49 |
Correct |
436 ms |
321824 KB |
Output is correct |
50 |
Correct |
318 ms |
316736 KB |
Output is correct |
51 |
Correct |
273 ms |
313904 KB |
Output is correct |
52 |
Correct |
395 ms |
319920 KB |
Output is correct |
53 |
Correct |
389 ms |
319976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
307768 KB |
Output is correct |
2 |
Correct |
169 ms |
307652 KB |
Output is correct |
3 |
Correct |
171 ms |
307436 KB |
Output is correct |
4 |
Correct |
137 ms |
295048 KB |
Output is correct |
5 |
Correct |
177 ms |
307772 KB |
Output is correct |
6 |
Correct |
180 ms |
307776 KB |
Output is correct |
7 |
Correct |
179 ms |
307788 KB |
Output is correct |
8 |
Correct |
187 ms |
307636 KB |
Output is correct |
9 |
Correct |
174 ms |
307660 KB |
Output is correct |
10 |
Correct |
131 ms |
295164 KB |
Output is correct |
11 |
Correct |
138 ms |
295168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
295092 KB |
Output is correct |
2 |
Correct |
137 ms |
295120 KB |
Output is correct |
3 |
Correct |
140 ms |
307404 KB |
Output is correct |
4 |
Correct |
137 ms |
307328 KB |
Output is correct |
5 |
Correct |
131 ms |
295080 KB |
Output is correct |
6 |
Correct |
137 ms |
307392 KB |
Output is correct |
7 |
Correct |
965 ms |
352656 KB |
Output is correct |
8 |
Correct |
2090 ms |
428440 KB |
Output is correct |
9 |
Correct |
2371 ms |
429608 KB |
Output is correct |
10 |
Correct |
2273 ms |
429704 KB |
Output is correct |
11 |
Correct |
306 ms |
344460 KB |
Output is correct |
12 |
Correct |
479 ms |
403840 KB |
Output is correct |
13 |
Correct |
501 ms |
406744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
307400 KB |
Output is correct |
2 |
Correct |
136 ms |
307308 KB |
Output is correct |
3 |
Correct |
142 ms |
307532 KB |
Output is correct |
4 |
Correct |
141 ms |
307500 KB |
Output is correct |
5 |
Correct |
146 ms |
307300 KB |
Output is correct |
6 |
Correct |
138 ms |
307416 KB |
Output is correct |
7 |
Correct |
146 ms |
307408 KB |
Output is correct |
8 |
Correct |
140 ms |
307336 KB |
Output is correct |
9 |
Correct |
145 ms |
307376 KB |
Output is correct |
10 |
Correct |
144 ms |
307316 KB |
Output is correct |
11 |
Correct |
150 ms |
307384 KB |
Output is correct |
12 |
Correct |
137 ms |
307328 KB |
Output is correct |
13 |
Correct |
132 ms |
295116 KB |
Output is correct |
14 |
Correct |
131 ms |
295100 KB |
Output is correct |
15 |
Correct |
155 ms |
307356 KB |
Output is correct |
16 |
Correct |
150 ms |
307332 KB |
Output is correct |
17 |
Correct |
154 ms |
307600 KB |
Output is correct |
18 |
Correct |
138 ms |
307492 KB |
Output is correct |
19 |
Correct |
139 ms |
307536 KB |
Output is correct |
20 |
Correct |
147 ms |
307448 KB |
Output is correct |
21 |
Correct |
157 ms |
307584 KB |
Output is correct |
22 |
Correct |
138 ms |
307568 KB |
Output is correct |
23 |
Correct |
138 ms |
307496 KB |
Output is correct |
24 |
Correct |
139 ms |
307468 KB |
Output is correct |
25 |
Correct |
155 ms |
308264 KB |
Output is correct |
26 |
Correct |
156 ms |
308196 KB |
Output is correct |
27 |
Correct |
149 ms |
308300 KB |
Output is correct |
28 |
Correct |
151 ms |
308308 KB |
Output is correct |
29 |
Correct |
159 ms |
308644 KB |
Output is correct |
30 |
Correct |
167 ms |
308648 KB |
Output is correct |
31 |
Correct |
153 ms |
308604 KB |
Output is correct |
32 |
Correct |
155 ms |
308424 KB |
Output is correct |
33 |
Correct |
292 ms |
324400 KB |
Output is correct |
34 |
Correct |
254 ms |
325176 KB |
Output is correct |
35 |
Correct |
299 ms |
325196 KB |
Output is correct |
36 |
Correct |
247 ms |
325068 KB |
Output is correct |
37 |
Correct |
231 ms |
318216 KB |
Output is correct |
38 |
Correct |
256 ms |
318180 KB |
Output is correct |
39 |
Correct |
232 ms |
318160 KB |
Output is correct |
40 |
Correct |
218 ms |
317476 KB |
Output is correct |
41 |
Correct |
251 ms |
317024 KB |
Output is correct |
42 |
Correct |
272 ms |
317344 KB |
Output is correct |
43 |
Correct |
445 ms |
321732 KB |
Output is correct |
44 |
Correct |
436 ms |
321824 KB |
Output is correct |
45 |
Correct |
318 ms |
316736 KB |
Output is correct |
46 |
Correct |
273 ms |
313904 KB |
Output is correct |
47 |
Correct |
395 ms |
319920 KB |
Output is correct |
48 |
Correct |
389 ms |
319976 KB |
Output is correct |
49 |
Correct |
181 ms |
307768 KB |
Output is correct |
50 |
Correct |
169 ms |
307652 KB |
Output is correct |
51 |
Correct |
171 ms |
307436 KB |
Output is correct |
52 |
Correct |
137 ms |
295048 KB |
Output is correct |
53 |
Correct |
177 ms |
307772 KB |
Output is correct |
54 |
Correct |
180 ms |
307776 KB |
Output is correct |
55 |
Correct |
179 ms |
307788 KB |
Output is correct |
56 |
Correct |
187 ms |
307636 KB |
Output is correct |
57 |
Correct |
174 ms |
307660 KB |
Output is correct |
58 |
Correct |
131 ms |
295164 KB |
Output is correct |
59 |
Correct |
138 ms |
295168 KB |
Output is correct |
60 |
Correct |
137 ms |
307392 KB |
Output is correct |
61 |
Correct |
965 ms |
352656 KB |
Output is correct |
62 |
Correct |
2090 ms |
428440 KB |
Output is correct |
63 |
Correct |
2371 ms |
429608 KB |
Output is correct |
64 |
Correct |
2273 ms |
429704 KB |
Output is correct |
65 |
Correct |
306 ms |
344460 KB |
Output is correct |
66 |
Correct |
479 ms |
403840 KB |
Output is correct |
67 |
Correct |
501 ms |
406744 KB |
Output is correct |
68 |
Correct |
135 ms |
295092 KB |
Output is correct |
69 |
Correct |
137 ms |
295120 KB |
Output is correct |
70 |
Correct |
140 ms |
307404 KB |
Output is correct |
71 |
Correct |
137 ms |
307328 KB |
Output is correct |
72 |
Correct |
131 ms |
295080 KB |
Output is correct |
73 |
Correct |
2832 ms |
521220 KB |
Output is correct |
74 |
Correct |
1953 ms |
530168 KB |
Output is correct |
75 |
Correct |
3045 ms |
530236 KB |
Output is correct |
76 |
Correct |
1828 ms |
529992 KB |
Output is correct |
77 |
Correct |
1828 ms |
441084 KB |
Output is correct |
78 |
Correct |
2843 ms |
400540 KB |
Output is correct |
79 |
Correct |
3356 ms |
435084 KB |
Output is correct |
80 |
Runtime error |
2361 ms |
847044 KB |
Execution killed with signal 6 |
81 |
Halted |
0 ms |
0 KB |
- |