# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1028914 |
2024-07-20T10:15:37 Z |
onbert |
Rectangles (IOI19_rect) |
C++17 |
|
5000 ms |
615120 KB |
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define ss short
#define ll long long
struct range {
ss r, mx;
};
bool cmp0(range &a, range &b) {return a.r > b.r;}
bool cmp1(range &a, range &b) {return a.mx > b.mx;}
const int maxn = 2505, INF = 2505;
int n, m;
vector<vector<vector<pair<ss,ss>>>> pos[2];
vector<vector<vector<range>>> start[2];
ss fen[maxn];
void update(int id, int val) {
while (id<maxn) {
fen[id] += val;
id += (id & -id);
}
}
int qry(int id) {
int val = 0;
while (id >= 1) {
val += fen[id];
id -= (id & -id);
}
return val;
}
pair<ss,ss> seg[10005];
void build(int id, int l, int r) {
seg[id] = {INF, -1};
if (l==r) return;
int mid = (l+r)/2;
build(id*2, l, mid); build(id*2+1, mid+1, r);
}
void update1(int id, int l, int r, int target, int val) {
if (r<target || target<l) return;
if (l==r) {
seg[id] = {val, val};
return;
}
int mid = (l+r)/2;
update1(id*2, l, mid, target, val); update1(id*2+1, mid+1, r, target, val);
seg[id] = {min(seg[id*2].first, seg[id*2+1].first), max(seg[id*2].second, seg[id*2+1].second)};
}
ss qry1(int id, int l, int r, int findl, int findr, int t) {
if (r<findl || findr<l) {
if (t==0) return INF;
else return -1;
}
if (findl<=l && r<=findr) {
if (t==0) return seg[id].first;
else return seg[id].second;
}
int mid = (l+r)/2;
int lhs = qry1(id*2, l, mid, findl, findr, t), rhs = qry1(id*2+1, mid+1, r, findl, findr, t);
if (t==0) return min(lhs, rhs);
else return max(lhs, rhs);
}
long long count_rectangles(vector<vector<int32_t>> a) {
n = a.size(), m = a[0].size();
pos[0].resize(m);
for (int i=0;i<m;i++) pos[0][i].resize(m - i);
pos[1].resize(n);
for (int i=0;i<n;i++) pos[1][i].resize(n - i);
start[0].resize(n); start[1].resize(n);
for (int i=0;i<n;i++) start[0][i].resize(m), start[1][i].resize(m);
vector<pair<int,int>> cur;
vector<ss> temp;
for (int i=0;i<n;i++) {
cur.clear();
for (int j=0;j<m;j++) cur.push_back({a[i][j], j});
sort(cur.rbegin(), cur.rend());
build(1, 0, m-1);
temp.clear();
for (int j=0;j<m;j++) {
auto [val, id] = cur[j];
if (j>0 && val!=cur[j-1].first) {
for (int x:temp) update1(1, 0, m-1, x, x);
temp.clear();
}
ss l = qry1(1, 0, m-1, 0, id, 1), r = qry1(1, 0, m-1, id, m-1, 0);
if (l!=-1 && r!=INF) {
l++, r--;
if (pos[0][l][r-l].size()==0 || pos[0][l][r-l].back().first!=i) {
pos[0][l][r-l].push_back({i, -1});
start[0][i][l].push_back({r, -1});
}
}
temp.push_back(id);
}
}
for (int j=0;j<m;j++) {
cur.clear();
for (int i=0;i<n;i++) cur.push_back({a[i][j], i});
sort(cur.rbegin(), cur.rend());
build(1, 0, n-1);
temp.clear();
for (int i=0;i<n;i++) {
auto [val, id] = cur[i];
if (i>0 && val!=cur[i-1].first) {
for (int x:temp) update1(1, 0, n-1, x, x);
temp.clear();
}
ss l = qry1(1, 0, n-1, 0, id, 1), r = qry1(1, 0, n-1, id, n-1, 0);
if (l!=-1 && r!=INF) {
l++, r--;
if (pos[1][l][r-l].size()==0 || pos[1][l][r-l].back().first!=j) {
pos[1][l][r-l].push_back({j, -1});
start[1][l][j].push_back({r, -1});
}
}
temp.push_back(id);
}
}
for (int I=0;I<=1;I++) {
int nn = m;
if (I==1) nn = n;
for (int l=0;l<nn;l++) for (int r=l;r<nn;r++) {
sort(pos[I][l][r-l].begin(), pos[I][l][r-l].end());
int sz = pos[I][l][r-l].size();
for (int i=sz-1;i>=0;i--) {
if (i==sz-1 || pos[I][l][r-l][i+1].first!=pos[I][l][r-l][i].first+1) pos[I][l][r-l][i].second = pos[I][l][r-l][i].first;
else pos[I][l][r-l][i].second = pos[I][l][r-l][i+1].second;
}
}
}
for (int I=0;I<=1;I++) {
for (int i=0;i<n;i++) for (int j=0;j<m;j++) {
for (auto &[r, mx]:start[I][i][j]) {
ss x = j, y = i;
if (I==1) x = i, y = j;
mx = (*lower_bound(pos[I][x][r-x].begin(), pos[I][x][r-x].end(), make_pair(y, (ss)-1))).second;
}
}
}
ll ans = 0;
vector<int> LOG;
for (int i=1;i<n-1;i++) for (int j=1;j<m-1;j++) {
sort(start[0][i][j].begin(), start[0][i][j].end(), cmp0);
sort(start[1][i][j].begin(), start[1][i][j].end(), cmp1);
int pt = -1;
LOG.clear();
for (auto [r, mx]:start[0][i][j]) {
while (pt+1 < start[1][i][j].size() && start[1][i][j][pt+1].mx >= r) {
pt++;
update(start[1][i][j][pt].r, 1);
LOG.push_back(start[1][i][j][pt].r);
}
ans += qry(mx);
}
for (int x:LOG) update(x, -1);
}
return ans;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:152:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | while (pt+1 < start[1][i][j].size() && start[1][i][j][pt+1].mx >= r) {
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1492 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
4 ms |
1116 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
5 ms |
1116 KB |
Output is correct |
28 |
Correct |
6 ms |
1220 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
5 ms |
1116 KB |
Output is correct |
22 |
Correct |
5 ms |
1116 KB |
Output is correct |
23 |
Correct |
6 ms |
1220 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
21 ms |
6492 KB |
Output is correct |
31 |
Correct |
23 ms |
6484 KB |
Output is correct |
32 |
Correct |
24 ms |
6740 KB |
Output is correct |
33 |
Correct |
23 ms |
4700 KB |
Output is correct |
34 |
Correct |
32 ms |
5572 KB |
Output is correct |
35 |
Correct |
31 ms |
5724 KB |
Output is correct |
36 |
Correct |
32 ms |
5464 KB |
Output is correct |
37 |
Correct |
38 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
5 ms |
1116 KB |
Output is correct |
22 |
Correct |
5 ms |
1116 KB |
Output is correct |
23 |
Correct |
6 ms |
1220 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
21 ms |
6492 KB |
Output is correct |
26 |
Correct |
23 ms |
6484 KB |
Output is correct |
27 |
Correct |
24 ms |
6740 KB |
Output is correct |
28 |
Correct |
23 ms |
4700 KB |
Output is correct |
29 |
Correct |
32 ms |
5572 KB |
Output is correct |
30 |
Correct |
31 ms |
5724 KB |
Output is correct |
31 |
Correct |
32 ms |
5464 KB |
Output is correct |
32 |
Correct |
38 ms |
5460 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
330 ms |
72528 KB |
Output is correct |
39 |
Correct |
357 ms |
66416 KB |
Output is correct |
40 |
Correct |
312 ms |
66388 KB |
Output is correct |
41 |
Correct |
306 ms |
60212 KB |
Output is correct |
42 |
Correct |
332 ms |
78420 KB |
Output is correct |
43 |
Correct |
331 ms |
78164 KB |
Output is correct |
44 |
Correct |
341 ms |
78604 KB |
Output is correct |
45 |
Correct |
326 ms |
73816 KB |
Output is correct |
46 |
Correct |
294 ms |
49108 KB |
Output is correct |
47 |
Correct |
338 ms |
52564 KB |
Output is correct |
48 |
Correct |
522 ms |
62800 KB |
Output is correct |
49 |
Correct |
556 ms |
64916 KB |
Output is correct |
50 |
Correct |
296 ms |
34148 KB |
Output is correct |
51 |
Correct |
252 ms |
34256 KB |
Output is correct |
52 |
Correct |
512 ms |
61524 KB |
Output is correct |
53 |
Correct |
545 ms |
62432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
74848 KB |
Output is correct |
2 |
Correct |
34 ms |
54132 KB |
Output is correct |
3 |
Correct |
38 ms |
74444 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
43 ms |
74784 KB |
Output is correct |
6 |
Correct |
44 ms |
74740 KB |
Output is correct |
7 |
Correct |
42 ms |
74576 KB |
Output is correct |
8 |
Correct |
44 ms |
74712 KB |
Output is correct |
9 |
Correct |
43 ms |
74588 KB |
Output is correct |
10 |
Correct |
46 ms |
74024 KB |
Output is correct |
11 |
Correct |
53 ms |
74300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2181 ms |
298792 KB |
Output is correct |
8 |
Execution timed out |
5074 ms |
615120 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
584 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
5 ms |
1116 KB |
Output is correct |
22 |
Correct |
5 ms |
1116 KB |
Output is correct |
23 |
Correct |
6 ms |
1220 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
21 ms |
6492 KB |
Output is correct |
26 |
Correct |
23 ms |
6484 KB |
Output is correct |
27 |
Correct |
24 ms |
6740 KB |
Output is correct |
28 |
Correct |
23 ms |
4700 KB |
Output is correct |
29 |
Correct |
32 ms |
5572 KB |
Output is correct |
30 |
Correct |
31 ms |
5724 KB |
Output is correct |
31 |
Correct |
32 ms |
5464 KB |
Output is correct |
32 |
Correct |
38 ms |
5460 KB |
Output is correct |
33 |
Correct |
330 ms |
72528 KB |
Output is correct |
34 |
Correct |
357 ms |
66416 KB |
Output is correct |
35 |
Correct |
312 ms |
66388 KB |
Output is correct |
36 |
Correct |
306 ms |
60212 KB |
Output is correct |
37 |
Correct |
332 ms |
78420 KB |
Output is correct |
38 |
Correct |
331 ms |
78164 KB |
Output is correct |
39 |
Correct |
341 ms |
78604 KB |
Output is correct |
40 |
Correct |
326 ms |
73816 KB |
Output is correct |
41 |
Correct |
294 ms |
49108 KB |
Output is correct |
42 |
Correct |
338 ms |
52564 KB |
Output is correct |
43 |
Correct |
522 ms |
62800 KB |
Output is correct |
44 |
Correct |
556 ms |
64916 KB |
Output is correct |
45 |
Correct |
296 ms |
34148 KB |
Output is correct |
46 |
Correct |
252 ms |
34256 KB |
Output is correct |
47 |
Correct |
512 ms |
61524 KB |
Output is correct |
48 |
Correct |
545 ms |
62432 KB |
Output is correct |
49 |
Correct |
53 ms |
74848 KB |
Output is correct |
50 |
Correct |
34 ms |
54132 KB |
Output is correct |
51 |
Correct |
38 ms |
74444 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
43 ms |
74784 KB |
Output is correct |
54 |
Correct |
44 ms |
74740 KB |
Output is correct |
55 |
Correct |
42 ms |
74576 KB |
Output is correct |
56 |
Correct |
44 ms |
74712 KB |
Output is correct |
57 |
Correct |
43 ms |
74588 KB |
Output is correct |
58 |
Correct |
46 ms |
74024 KB |
Output is correct |
59 |
Correct |
53 ms |
74300 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
2181 ms |
298792 KB |
Output is correct |
62 |
Execution timed out |
5074 ms |
615120 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |