# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1028961 |
2024-07-20T10:47:22 Z |
onbert |
Rectangles (IOI19_rect) |
C++17 |
|
5000 ms |
1002884 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;
}
ss seg[10005];
void build(int id, int l, int r) {
if (l==r) {
seg[id] = l;
return;
}
int mid = (l+r)/2;
build(id*2, l, mid); build(id*2+1, mid+1, r);
seg[id] = max(seg[id*2], seg[id*2+1]);
}
void update1(int id, int l, int r, int target) {
if (r<target || target<l) return;
if (l==r) {
seg[id] = -1;
return;
}
int mid = (l+r)>>1;
update1(id*2, l, mid, target); update1(id*2+1, mid+1, r, target);
seg[id] = max(seg[id*2], seg[id*2+1]);
}
ss qry1(int id, int l, int r, int findl, int findr) {
if (r<findl || findr<l) return -1;
if (findl<=l && r<=findr) return seg[id];
int mid = (l+r)>>1;
ss lhs = qry1(id*2, l, mid, findl, findr), rhs = qry1(id*2+1, mid+1, r, findl, findr);
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;
vector<pair<int,int>> link(m);
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.begin(), cur.end());
build(1, 0, m-1);
for (int j=0;j<m;j++) link[j] = {j-1, j+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) {
ss l = qry1(1, 0, m-1, 0, x), r = INF;
if (l>=0) r = link[l].second;
if (l>=0 && r<m) {
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.clear();
}
update1(1, 0, m-1, id);
ss l = link[id].first, r = link[id].second;
if (l>=0) link[l].second = r;
if (r<m) link[r].first = l;
temp.push_back(id);
}
}
link.resize(n);
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.begin(), cur.end());
build(1, 0, n-1);
for (int i=0;i<n;i++) link[i] = {i-1, i+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) {
ss l = qry1(1, 0, n-1, 0, x), r = INF;
if (l>=0) r = link[l].second;
if (l>=0 && r<n) {
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.clear();
}
update1(1, 0, n-1, id);
ss l = link[id].first, r = link[id].second;
if (l>=0) link[l].second = r;
if (r<n) link[r].first = l;
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:164:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
432 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 |
436 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
432 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 |
436 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
1472 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
6 ms |
1116 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
748 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 |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
432 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 |
1472 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
436 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
19 ms |
6708 KB |
Output is correct |
31 |
Correct |
18 ms |
6596 KB |
Output is correct |
32 |
Correct |
19 ms |
6748 KB |
Output is correct |
33 |
Correct |
17 ms |
4544 KB |
Output is correct |
34 |
Correct |
25 ms |
5464 KB |
Output is correct |
35 |
Correct |
26 ms |
5572 KB |
Output is correct |
36 |
Correct |
27 ms |
5716 KB |
Output is correct |
37 |
Correct |
23 ms |
5464 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 |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
432 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 |
1472 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
19 ms |
6708 KB |
Output is correct |
26 |
Correct |
18 ms |
6596 KB |
Output is correct |
27 |
Correct |
19 ms |
6748 KB |
Output is correct |
28 |
Correct |
17 ms |
4544 KB |
Output is correct |
29 |
Correct |
25 ms |
5464 KB |
Output is correct |
30 |
Correct |
26 ms |
5572 KB |
Output is correct |
31 |
Correct |
27 ms |
5716 KB |
Output is correct |
32 |
Correct |
23 ms |
5464 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
436 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
248 ms |
72532 KB |
Output is correct |
39 |
Correct |
234 ms |
66644 KB |
Output is correct |
40 |
Correct |
216 ms |
66356 KB |
Output is correct |
41 |
Correct |
188 ms |
60240 KB |
Output is correct |
42 |
Correct |
281 ms |
78360 KB |
Output is correct |
43 |
Correct |
273 ms |
78392 KB |
Output is correct |
44 |
Correct |
302 ms |
78676 KB |
Output is correct |
45 |
Correct |
258 ms |
73812 KB |
Output is correct |
46 |
Correct |
219 ms |
48972 KB |
Output is correct |
47 |
Correct |
254 ms |
52564 KB |
Output is correct |
48 |
Correct |
400 ms |
62676 KB |
Output is correct |
49 |
Correct |
424 ms |
64852 KB |
Output is correct |
50 |
Correct |
186 ms |
34132 KB |
Output is correct |
51 |
Correct |
172 ms |
34128 KB |
Output is correct |
52 |
Correct |
369 ms |
61524 KB |
Output is correct |
53 |
Correct |
373 ms |
62476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
74836 KB |
Output is correct |
2 |
Correct |
27 ms |
54104 KB |
Output is correct |
3 |
Correct |
36 ms |
74328 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
37 ms |
74676 KB |
Output is correct |
6 |
Correct |
41 ms |
74588 KB |
Output is correct |
7 |
Correct |
40 ms |
74588 KB |
Output is correct |
8 |
Correct |
39 ms |
74580 KB |
Output is correct |
9 |
Correct |
43 ms |
74724 KB |
Output is correct |
10 |
Correct |
35 ms |
74076 KB |
Output is correct |
11 |
Correct |
39 ms |
74476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
1381 ms |
299060 KB |
Output is correct |
8 |
Correct |
3192 ms |
617808 KB |
Output is correct |
9 |
Correct |
3137 ms |
620620 KB |
Output is correct |
10 |
Correct |
3426 ms |
620884 KB |
Output is correct |
11 |
Correct |
862 ms |
267112 KB |
Output is correct |
12 |
Correct |
1723 ms |
471560 KB |
Output is correct |
13 |
Correct |
1756 ms |
502572 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 |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
436 KB |
Output is correct |
15 |
Correct |
0 ms |
432 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 |
1472 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
19 ms |
6708 KB |
Output is correct |
26 |
Correct |
18 ms |
6596 KB |
Output is correct |
27 |
Correct |
19 ms |
6748 KB |
Output is correct |
28 |
Correct |
17 ms |
4544 KB |
Output is correct |
29 |
Correct |
25 ms |
5464 KB |
Output is correct |
30 |
Correct |
26 ms |
5572 KB |
Output is correct |
31 |
Correct |
27 ms |
5716 KB |
Output is correct |
32 |
Correct |
23 ms |
5464 KB |
Output is correct |
33 |
Correct |
248 ms |
72532 KB |
Output is correct |
34 |
Correct |
234 ms |
66644 KB |
Output is correct |
35 |
Correct |
216 ms |
66356 KB |
Output is correct |
36 |
Correct |
188 ms |
60240 KB |
Output is correct |
37 |
Correct |
281 ms |
78360 KB |
Output is correct |
38 |
Correct |
273 ms |
78392 KB |
Output is correct |
39 |
Correct |
302 ms |
78676 KB |
Output is correct |
40 |
Correct |
258 ms |
73812 KB |
Output is correct |
41 |
Correct |
219 ms |
48972 KB |
Output is correct |
42 |
Correct |
254 ms |
52564 KB |
Output is correct |
43 |
Correct |
400 ms |
62676 KB |
Output is correct |
44 |
Correct |
424 ms |
64852 KB |
Output is correct |
45 |
Correct |
186 ms |
34132 KB |
Output is correct |
46 |
Correct |
172 ms |
34128 KB |
Output is correct |
47 |
Correct |
369 ms |
61524 KB |
Output is correct |
48 |
Correct |
373 ms |
62476 KB |
Output is correct |
49 |
Correct |
39 ms |
74836 KB |
Output is correct |
50 |
Correct |
27 ms |
54104 KB |
Output is correct |
51 |
Correct |
36 ms |
74328 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
37 ms |
74676 KB |
Output is correct |
54 |
Correct |
41 ms |
74588 KB |
Output is correct |
55 |
Correct |
40 ms |
74588 KB |
Output is correct |
56 |
Correct |
39 ms |
74580 KB |
Output is correct |
57 |
Correct |
43 ms |
74724 KB |
Output is correct |
58 |
Correct |
35 ms |
74076 KB |
Output is correct |
59 |
Correct |
39 ms |
74476 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1381 ms |
299060 KB |
Output is correct |
62 |
Correct |
3192 ms |
617808 KB |
Output is correct |
63 |
Correct |
3137 ms |
620620 KB |
Output is correct |
64 |
Correct |
3426 ms |
620884 KB |
Output is correct |
65 |
Correct |
862 ms |
267112 KB |
Output is correct |
66 |
Correct |
1723 ms |
471560 KB |
Output is correct |
67 |
Correct |
1756 ms |
502572 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
436 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
4053 ms |
927012 KB |
Output is correct |
74 |
Correct |
3717 ms |
846128 KB |
Output is correct |
75 |
Correct |
3533 ms |
845868 KB |
Output is correct |
76 |
Correct |
2953 ms |
765872 KB |
Output is correct |
77 |
Execution timed out |
5004 ms |
1002884 KB |
Time limit exceeded |
78 |
Halted |
0 ms |
0 KB |
- |