# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1047505 |
2024-08-07T13:44:35 Z |
fuad27 |
Rectangles (IOI19_rect) |
C++17 |
|
5000 ms |
941116 KB |
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC optimize("avx2")
const int N = 2510;
int n,m;
vector<pair<int,int>> gdr[N];
vector<pair<int,int>> gdc[N];
struct Side {
int r1, r2, c1, c2;
};
vector<pair<short,short>> hsides[N][N];
vector<pair<short,short>> vsides[N][N];
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].push_back({stk.back(), j});
}
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].push_back({j, stk.back()});
}
stk.push_back(j);
}
sort(gdr[i].begin(), gdr[i].end());
}
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[i].push_back({stk.back(), j});
}
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()) {
gdc[i].push_back({j, stk.back()});
}
stk.push_back(j);
}
sort(gdc[i].begin(), gdc[i].end());
gdc[i].erase(unique(gdc[i].begin(), gdc[i].end()), gdc[i].end());
}
Side seg;
for(int i = 0;i<n;i++) {
for(auto pr:gdr[i]) {
if(pr.first +1 == pr.second)continue;
if(i!=n-1 and (*lower_bound(gdr[i+1].begin(), gdr[i+1].end(), pr)) == pr)continue;
seg.r1 = pr.first;
seg.r2 = pr.second;
seg.c2 = i;
for(int j = i;j>=0;j--){
auto it = (lower_bound(gdr[j].begin(), gdr[j].end(), pr));
if(it == gdr[j].end())break;
if((*it)!=pr)break;
seg.c1=j;
}
seg.c1--;
seg.c2++;
seg.c1=max(seg.c1, 0);
seg.c2=min(seg.c2, n-1);
for(int j = seg.c1;j<=seg.c2;j++) {
hsides[j][seg.r2].push_back(pair<int,int>(seg.c1, seg.r1));
}
}
}
for(int i = 0;i<m;i++) {
for(auto pr:gdc[i]) {
if(pr.first +1 == pr.second)continue;
if(i!=m-1 and (*lower_bound(gdc[i+1].begin(), gdc[i+1].end(), pr)) == pr)continue;
seg.r1 = pr.first;
seg.r2 = pr.second;
seg.c2 = i;
for(int j = i;j>=0;j--){
auto it = (lower_bound(gdc[j].begin(), gdc[j].end(), pr));
if(it == gdc[j].end())break;
if((*it)!=pr)break;
seg.c1=j;
}
seg.c1--;
seg.c2++;
seg.c1=max(seg.c1, 0);
seg.c2=min(seg.c2, m-1);
for(int j = seg.c1;j<=seg.c2;j++) {
vsides[seg.r2][j].push_back(pair<int,int>(seg.r1, seg.c1));
}
}
}
long long ans=0;
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
int tmp=0;
sort(vsides[i][j].begin(), vsides[i][j].end());
sort(hsides[i][j].begin(), hsides[i][j].end());
int p1 = 0;
for(auto k:vsides[i][j]) {
while(p1 < (int)hsides[i][j].size() and hsides[i][j][p1].first <= k.first) {
upd(hsides[i][j][p1++].second, 1);
}
ans+=qry(k.second);
}
while(p1 < (int)hsides[i][j].size()) {
upd(hsides[i][j][p1++].second, 1);
}
for(auto k:hsides[i][j]) {
upd(k.second, -1);
}
}
}
return ans;
}
Compilation message
rect.cpp:5:28: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
5 | #pragma GCC optimize("avx2")
| ^
rect.cpp:19:25: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
19 | void upd(int at, int add) {
| ^
rect.cpp:26:15: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
26 | int qry(int at) {
| ^
rect.cpp:37:60: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
37 | long long count_rectangles(std::vector<std::vector<int> > a) {
| ^
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:135:8: warning: unused variable 'tmp' [-Wunused-variable]
135 | int tmp=0;
| ^~~
rect.cpp:99:10: warning: 'seg.Side::c1' may be used uninitialized in this function [-Wmaybe-uninitialized]
99 | seg.c1--;
| ~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296284 KB |
Output is correct |
2 |
Correct |
42 ms |
296352 KB |
Output is correct |
3 |
Correct |
48 ms |
296272 KB |
Output is correct |
4 |
Correct |
52 ms |
296444 KB |
Output is correct |
5 |
Correct |
44 ms |
296212 KB |
Output is correct |
6 |
Correct |
46 ms |
296436 KB |
Output is correct |
7 |
Correct |
45 ms |
296276 KB |
Output is correct |
8 |
Correct |
46 ms |
296272 KB |
Output is correct |
9 |
Correct |
41 ms |
296268 KB |
Output is correct |
10 |
Correct |
47 ms |
296276 KB |
Output is correct |
11 |
Correct |
42 ms |
296540 KB |
Output is correct |
12 |
Correct |
48 ms |
296272 KB |
Output is correct |
13 |
Correct |
41 ms |
296328 KB |
Output is correct |
14 |
Correct |
46 ms |
296416 KB |
Output is correct |
15 |
Correct |
39 ms |
296188 KB |
Output is correct |
16 |
Correct |
42 ms |
296272 KB |
Output is correct |
17 |
Correct |
42 ms |
296264 KB |
Output is correct |
18 |
Correct |
43 ms |
296276 KB |
Output is correct |
19 |
Correct |
51 ms |
296440 KB |
Output is correct |
20 |
Correct |
48 ms |
296272 KB |
Output is correct |
21 |
Correct |
40 ms |
296284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296284 KB |
Output is correct |
2 |
Correct |
42 ms |
296352 KB |
Output is correct |
3 |
Correct |
48 ms |
296272 KB |
Output is correct |
4 |
Correct |
52 ms |
296444 KB |
Output is correct |
5 |
Correct |
44 ms |
296212 KB |
Output is correct |
6 |
Correct |
46 ms |
296436 KB |
Output is correct |
7 |
Correct |
45 ms |
296276 KB |
Output is correct |
8 |
Correct |
46 ms |
296272 KB |
Output is correct |
9 |
Correct |
41 ms |
296268 KB |
Output is correct |
10 |
Correct |
47 ms |
296276 KB |
Output is correct |
11 |
Correct |
42 ms |
296540 KB |
Output is correct |
12 |
Correct |
48 ms |
296272 KB |
Output is correct |
13 |
Correct |
41 ms |
296328 KB |
Output is correct |
14 |
Correct |
46 ms |
296416 KB |
Output is correct |
15 |
Correct |
39 ms |
296188 KB |
Output is correct |
16 |
Correct |
42 ms |
296272 KB |
Output is correct |
17 |
Correct |
42 ms |
296264 KB |
Output is correct |
18 |
Correct |
43 ms |
296276 KB |
Output is correct |
19 |
Correct |
51 ms |
296440 KB |
Output is correct |
20 |
Correct |
48 ms |
296272 KB |
Output is correct |
21 |
Correct |
40 ms |
296284 KB |
Output is correct |
22 |
Correct |
42 ms |
296788 KB |
Output is correct |
23 |
Correct |
41 ms |
296796 KB |
Output is correct |
24 |
Correct |
44 ms |
296760 KB |
Output is correct |
25 |
Correct |
44 ms |
296952 KB |
Output is correct |
26 |
Correct |
45 ms |
297052 KB |
Output is correct |
27 |
Correct |
44 ms |
297032 KB |
Output is correct |
28 |
Correct |
44 ms |
296920 KB |
Output is correct |
29 |
Correct |
42 ms |
296532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296284 KB |
Output is correct |
2 |
Correct |
42 ms |
296352 KB |
Output is correct |
3 |
Correct |
48 ms |
296272 KB |
Output is correct |
4 |
Correct |
52 ms |
296444 KB |
Output is correct |
5 |
Correct |
44 ms |
296212 KB |
Output is correct |
6 |
Correct |
46 ms |
296436 KB |
Output is correct |
7 |
Correct |
45 ms |
296276 KB |
Output is correct |
8 |
Correct |
46 ms |
296272 KB |
Output is correct |
9 |
Correct |
41 ms |
296268 KB |
Output is correct |
10 |
Correct |
47 ms |
296276 KB |
Output is correct |
11 |
Correct |
42 ms |
296540 KB |
Output is correct |
12 |
Correct |
48 ms |
296272 KB |
Output is correct |
13 |
Correct |
41 ms |
296328 KB |
Output is correct |
14 |
Correct |
46 ms |
296416 KB |
Output is correct |
15 |
Correct |
39 ms |
296188 KB |
Output is correct |
16 |
Correct |
42 ms |
296272 KB |
Output is correct |
17 |
Correct |
42 ms |
296788 KB |
Output is correct |
18 |
Correct |
41 ms |
296796 KB |
Output is correct |
19 |
Correct |
44 ms |
296760 KB |
Output is correct |
20 |
Correct |
44 ms |
296952 KB |
Output is correct |
21 |
Correct |
45 ms |
297052 KB |
Output is correct |
22 |
Correct |
44 ms |
297032 KB |
Output is correct |
23 |
Correct |
44 ms |
296920 KB |
Output is correct |
24 |
Correct |
42 ms |
296532 KB |
Output is correct |
25 |
Correct |
42 ms |
296264 KB |
Output is correct |
26 |
Correct |
43 ms |
296276 KB |
Output is correct |
27 |
Correct |
51 ms |
296440 KB |
Output is correct |
28 |
Correct |
48 ms |
296272 KB |
Output is correct |
29 |
Correct |
40 ms |
296284 KB |
Output is correct |
30 |
Correct |
47 ms |
298580 KB |
Output is correct |
31 |
Correct |
51 ms |
298700 KB |
Output is correct |
32 |
Correct |
53 ms |
298688 KB |
Output is correct |
33 |
Correct |
70 ms |
300112 KB |
Output is correct |
34 |
Correct |
73 ms |
300624 KB |
Output is correct |
35 |
Correct |
71 ms |
300620 KB |
Output is correct |
36 |
Correct |
72 ms |
300328 KB |
Output is correct |
37 |
Correct |
75 ms |
300448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296284 KB |
Output is correct |
2 |
Correct |
42 ms |
296352 KB |
Output is correct |
3 |
Correct |
48 ms |
296272 KB |
Output is correct |
4 |
Correct |
52 ms |
296444 KB |
Output is correct |
5 |
Correct |
44 ms |
296212 KB |
Output is correct |
6 |
Correct |
46 ms |
296436 KB |
Output is correct |
7 |
Correct |
45 ms |
296276 KB |
Output is correct |
8 |
Correct |
46 ms |
296272 KB |
Output is correct |
9 |
Correct |
41 ms |
296268 KB |
Output is correct |
10 |
Correct |
47 ms |
296276 KB |
Output is correct |
11 |
Correct |
42 ms |
296540 KB |
Output is correct |
12 |
Correct |
48 ms |
296272 KB |
Output is correct |
13 |
Correct |
41 ms |
296328 KB |
Output is correct |
14 |
Correct |
46 ms |
296416 KB |
Output is correct |
15 |
Correct |
39 ms |
296188 KB |
Output is correct |
16 |
Correct |
42 ms |
296272 KB |
Output is correct |
17 |
Correct |
42 ms |
296788 KB |
Output is correct |
18 |
Correct |
41 ms |
296796 KB |
Output is correct |
19 |
Correct |
44 ms |
296760 KB |
Output is correct |
20 |
Correct |
44 ms |
296952 KB |
Output is correct |
21 |
Correct |
45 ms |
297052 KB |
Output is correct |
22 |
Correct |
44 ms |
297032 KB |
Output is correct |
23 |
Correct |
44 ms |
296920 KB |
Output is correct |
24 |
Correct |
42 ms |
296532 KB |
Output is correct |
25 |
Correct |
47 ms |
298580 KB |
Output is correct |
26 |
Correct |
51 ms |
298700 KB |
Output is correct |
27 |
Correct |
53 ms |
298688 KB |
Output is correct |
28 |
Correct |
70 ms |
300112 KB |
Output is correct |
29 |
Correct |
73 ms |
300624 KB |
Output is correct |
30 |
Correct |
71 ms |
300620 KB |
Output is correct |
31 |
Correct |
72 ms |
300328 KB |
Output is correct |
32 |
Correct |
75 ms |
300448 KB |
Output is correct |
33 |
Correct |
42 ms |
296264 KB |
Output is correct |
34 |
Correct |
43 ms |
296276 KB |
Output is correct |
35 |
Correct |
51 ms |
296440 KB |
Output is correct |
36 |
Correct |
48 ms |
296272 KB |
Output is correct |
37 |
Correct |
40 ms |
296284 KB |
Output is correct |
38 |
Correct |
169 ms |
324268 KB |
Output is correct |
39 |
Correct |
174 ms |
327456 KB |
Output is correct |
40 |
Correct |
178 ms |
327760 KB |
Output is correct |
41 |
Correct |
173 ms |
331092 KB |
Output is correct |
42 |
Correct |
162 ms |
326924 KB |
Output is correct |
43 |
Correct |
175 ms |
326796 KB |
Output is correct |
44 |
Correct |
176 ms |
326892 KB |
Output is correct |
45 |
Correct |
155 ms |
325264 KB |
Output is correct |
46 |
Correct |
238 ms |
333928 KB |
Output is correct |
47 |
Correct |
276 ms |
337748 KB |
Output is correct |
48 |
Correct |
460 ms |
350828 KB |
Output is correct |
49 |
Correct |
461 ms |
351060 KB |
Output is correct |
50 |
Correct |
248 ms |
323792 KB |
Output is correct |
51 |
Correct |
247 ms |
323780 KB |
Output is correct |
52 |
Correct |
432 ms |
346964 KB |
Output is correct |
53 |
Correct |
449 ms |
347040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
296788 KB |
Output is correct |
2 |
Correct |
44 ms |
296708 KB |
Output is correct |
3 |
Correct |
43 ms |
296540 KB |
Output is correct |
4 |
Correct |
41 ms |
296276 KB |
Output is correct |
5 |
Correct |
47 ms |
296752 KB |
Output is correct |
6 |
Correct |
58 ms |
296784 KB |
Output is correct |
7 |
Correct |
44 ms |
296860 KB |
Output is correct |
8 |
Correct |
49 ms |
296820 KB |
Output is correct |
9 |
Correct |
45 ms |
296784 KB |
Output is correct |
10 |
Correct |
49 ms |
296532 KB |
Output is correct |
11 |
Correct |
46 ms |
296528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296264 KB |
Output is correct |
2 |
Correct |
43 ms |
296276 KB |
Output is correct |
3 |
Correct |
51 ms |
296440 KB |
Output is correct |
4 |
Correct |
48 ms |
296272 KB |
Output is correct |
5 |
Correct |
40 ms |
296284 KB |
Output is correct |
6 |
Correct |
40 ms |
296272 KB |
Output is correct |
7 |
Correct |
1298 ms |
509320 KB |
Output is correct |
8 |
Correct |
2741 ms |
748312 KB |
Output is correct |
9 |
Correct |
2801 ms |
750300 KB |
Output is correct |
10 |
Correct |
2826 ms |
750608 KB |
Output is correct |
11 |
Correct |
332 ms |
408148 KB |
Output is correct |
12 |
Correct |
654 ms |
499536 KB |
Output is correct |
13 |
Correct |
867 ms |
512784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
296284 KB |
Output is correct |
2 |
Correct |
42 ms |
296352 KB |
Output is correct |
3 |
Correct |
48 ms |
296272 KB |
Output is correct |
4 |
Correct |
52 ms |
296444 KB |
Output is correct |
5 |
Correct |
44 ms |
296212 KB |
Output is correct |
6 |
Correct |
46 ms |
296436 KB |
Output is correct |
7 |
Correct |
45 ms |
296276 KB |
Output is correct |
8 |
Correct |
46 ms |
296272 KB |
Output is correct |
9 |
Correct |
41 ms |
296268 KB |
Output is correct |
10 |
Correct |
47 ms |
296276 KB |
Output is correct |
11 |
Correct |
42 ms |
296540 KB |
Output is correct |
12 |
Correct |
48 ms |
296272 KB |
Output is correct |
13 |
Correct |
41 ms |
296328 KB |
Output is correct |
14 |
Correct |
46 ms |
296416 KB |
Output is correct |
15 |
Correct |
39 ms |
296188 KB |
Output is correct |
16 |
Correct |
42 ms |
296272 KB |
Output is correct |
17 |
Correct |
42 ms |
296788 KB |
Output is correct |
18 |
Correct |
41 ms |
296796 KB |
Output is correct |
19 |
Correct |
44 ms |
296760 KB |
Output is correct |
20 |
Correct |
44 ms |
296952 KB |
Output is correct |
21 |
Correct |
45 ms |
297052 KB |
Output is correct |
22 |
Correct |
44 ms |
297032 KB |
Output is correct |
23 |
Correct |
44 ms |
296920 KB |
Output is correct |
24 |
Correct |
42 ms |
296532 KB |
Output is correct |
25 |
Correct |
47 ms |
298580 KB |
Output is correct |
26 |
Correct |
51 ms |
298700 KB |
Output is correct |
27 |
Correct |
53 ms |
298688 KB |
Output is correct |
28 |
Correct |
70 ms |
300112 KB |
Output is correct |
29 |
Correct |
73 ms |
300624 KB |
Output is correct |
30 |
Correct |
71 ms |
300620 KB |
Output is correct |
31 |
Correct |
72 ms |
300328 KB |
Output is correct |
32 |
Correct |
75 ms |
300448 KB |
Output is correct |
33 |
Correct |
169 ms |
324268 KB |
Output is correct |
34 |
Correct |
174 ms |
327456 KB |
Output is correct |
35 |
Correct |
178 ms |
327760 KB |
Output is correct |
36 |
Correct |
173 ms |
331092 KB |
Output is correct |
37 |
Correct |
162 ms |
326924 KB |
Output is correct |
38 |
Correct |
175 ms |
326796 KB |
Output is correct |
39 |
Correct |
176 ms |
326892 KB |
Output is correct |
40 |
Correct |
155 ms |
325264 KB |
Output is correct |
41 |
Correct |
238 ms |
333928 KB |
Output is correct |
42 |
Correct |
276 ms |
337748 KB |
Output is correct |
43 |
Correct |
460 ms |
350828 KB |
Output is correct |
44 |
Correct |
461 ms |
351060 KB |
Output is correct |
45 |
Correct |
248 ms |
323792 KB |
Output is correct |
46 |
Correct |
247 ms |
323780 KB |
Output is correct |
47 |
Correct |
432 ms |
346964 KB |
Output is correct |
48 |
Correct |
449 ms |
347040 KB |
Output is correct |
49 |
Correct |
44 ms |
296788 KB |
Output is correct |
50 |
Correct |
44 ms |
296708 KB |
Output is correct |
51 |
Correct |
43 ms |
296540 KB |
Output is correct |
52 |
Correct |
41 ms |
296276 KB |
Output is correct |
53 |
Correct |
47 ms |
296752 KB |
Output is correct |
54 |
Correct |
58 ms |
296784 KB |
Output is correct |
55 |
Correct |
44 ms |
296860 KB |
Output is correct |
56 |
Correct |
49 ms |
296820 KB |
Output is correct |
57 |
Correct |
45 ms |
296784 KB |
Output is correct |
58 |
Correct |
49 ms |
296532 KB |
Output is correct |
59 |
Correct |
46 ms |
296528 KB |
Output is correct |
60 |
Correct |
40 ms |
296272 KB |
Output is correct |
61 |
Correct |
1298 ms |
509320 KB |
Output is correct |
62 |
Correct |
2741 ms |
748312 KB |
Output is correct |
63 |
Correct |
2801 ms |
750300 KB |
Output is correct |
64 |
Correct |
2826 ms |
750608 KB |
Output is correct |
65 |
Correct |
332 ms |
408148 KB |
Output is correct |
66 |
Correct |
654 ms |
499536 KB |
Output is correct |
67 |
Correct |
867 ms |
512784 KB |
Output is correct |
68 |
Correct |
42 ms |
296264 KB |
Output is correct |
69 |
Correct |
43 ms |
296276 KB |
Output is correct |
70 |
Correct |
51 ms |
296440 KB |
Output is correct |
71 |
Correct |
48 ms |
296272 KB |
Output is correct |
72 |
Correct |
40 ms |
296284 KB |
Output is correct |
73 |
Correct |
2106 ms |
614084 KB |
Output is correct |
74 |
Correct |
1961 ms |
659392 KB |
Output is correct |
75 |
Correct |
2272 ms |
659232 KB |
Output is correct |
76 |
Correct |
1952 ms |
706128 KB |
Output is correct |
77 |
Correct |
2498 ms |
636144 KB |
Output is correct |
78 |
Correct |
3431 ms |
686840 KB |
Output is correct |
79 |
Correct |
3571 ms |
708656 KB |
Output is correct |
80 |
Execution timed out |
5068 ms |
941116 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |