# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1029291 |
2024-07-20T15:28:16 Z |
onbert |
Rectangles (IOI19_rect) |
C++17 |
|
4019 ms |
1003440 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, lg = 12;
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<int,int> st[maxn][lg];
int qry1(int l, int r) {
int lg = log2(r-l+1);
pair<int,int> lhs = st[l][lg], rhs = st[r-(1<<lg)+1][lg];
if (rhs.first > lhs.first) return rhs.second;
else return lhs.second;
}
int nn, II, ii;
void dfs(int l, int r) {
if (l>r) return;
int mid = qry1(l, r);
// if (II==0 && ii==1) cout << l << " " << r << " " << mid << endl;
if (l>=1 && r<=nn-2 && st[mid][0].first < st[l-1][0].first && st[mid][0].first < st[r+1][0].first) {
// if (II==1) cout << ii << " " << l << " " << r << endl;
pos[II][l][r-l].push_back({(ss)ii, -1});
if (II==0) start[II][ii][l].push_back({(ss)r, -1});
else start[II][l][ii].push_back({(ss)r, -1});
}
dfs(l, mid-1); dfs(mid+1, r);
}
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);
nn = m, II = 0;
for (int i=0;i<n;i++) {
ii = i;
for (int j=m-1;j>=0;j--) {
st[j][0] = {a[i][j], j};
for (int k=1;k<lg;k++) {
if (j+(1<<k)-1 < m) {
st[j][k] = st[j][k-1];
pair<int,int> nxt = st[j + (1 << (k-1))][k-1];
if (nxt.first > st[j][k].first) st[j][k] = nxt;
} else break;
}
}
dfs(0, m-1);
}
nn = n, II = 1;
for (int j=0;j<m;j++) {
ii = j;
for (int i=n-1;i>=0;i--) {
st[i][0] = {a[i][j], i};
for (int k=1;k<lg;k++) {
if (i+(1<<k)-1 < n) {
st[i][k] = st[i][k-1];
pair<int,int> nxt = st[i + (1 << (k-1))][k-1];
if (nxt.first > st[i][k].first) st[i][k] = nxt;
} else break;
}
}
dfs(0, n-1);
}
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:124:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
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 |
344 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 |
440 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 |
0 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
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 |
344 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 |
440 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 |
0 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 |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1476 KB |
Output is correct |
25 |
Correct |
2 ms |
1152 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1272 KB |
Output is correct |
28 |
Correct |
3 ms |
1252 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
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 |
344 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 |
440 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1476 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1272 KB |
Output is correct |
23 |
Correct |
3 ms |
1252 KB |
Output is correct |
24 |
Correct |
1 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 |
0 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 |
13 ms |
6596 KB |
Output is correct |
31 |
Correct |
14 ms |
6744 KB |
Output is correct |
32 |
Correct |
13 ms |
6604 KB |
Output is correct |
33 |
Correct |
10 ms |
4700 KB |
Output is correct |
34 |
Correct |
16 ms |
5496 KB |
Output is correct |
35 |
Correct |
18 ms |
5720 KB |
Output is correct |
36 |
Correct |
21 ms |
5600 KB |
Output is correct |
37 |
Correct |
16 ms |
5464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
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 |
344 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 |
440 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1476 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1272 KB |
Output is correct |
23 |
Correct |
3 ms |
1252 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
13 ms |
6596 KB |
Output is correct |
26 |
Correct |
14 ms |
6744 KB |
Output is correct |
27 |
Correct |
13 ms |
6604 KB |
Output is correct |
28 |
Correct |
10 ms |
4700 KB |
Output is correct |
29 |
Correct |
16 ms |
5496 KB |
Output is correct |
30 |
Correct |
18 ms |
5720 KB |
Output is correct |
31 |
Correct |
21 ms |
5600 KB |
Output is correct |
32 |
Correct |
16 ms |
5464 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 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 |
154 ms |
72532 KB |
Output is correct |
39 |
Correct |
162 ms |
66492 KB |
Output is correct |
40 |
Correct |
130 ms |
66372 KB |
Output is correct |
41 |
Correct |
118 ms |
60244 KB |
Output is correct |
42 |
Correct |
173 ms |
78416 KB |
Output is correct |
43 |
Correct |
171 ms |
78420 KB |
Output is correct |
44 |
Correct |
177 ms |
78672 KB |
Output is correct |
45 |
Correct |
163 ms |
73888 KB |
Output is correct |
46 |
Correct |
118 ms |
48980 KB |
Output is correct |
47 |
Correct |
141 ms |
52528 KB |
Output is correct |
48 |
Correct |
239 ms |
62800 KB |
Output is correct |
49 |
Correct |
283 ms |
64952 KB |
Output is correct |
50 |
Correct |
117 ms |
34276 KB |
Output is correct |
51 |
Correct |
125 ms |
34132 KB |
Output is correct |
52 |
Correct |
229 ms |
61400 KB |
Output is correct |
53 |
Correct |
218 ms |
62544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
75048 KB |
Output is correct |
2 |
Correct |
25 ms |
54364 KB |
Output is correct |
3 |
Correct |
35 ms |
74396 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
37 ms |
74932 KB |
Output is correct |
6 |
Correct |
38 ms |
74868 KB |
Output is correct |
7 |
Correct |
37 ms |
74776 KB |
Output is correct |
8 |
Correct |
38 ms |
74844 KB |
Output is correct |
9 |
Correct |
40 ms |
74832 KB |
Output is correct |
10 |
Correct |
34 ms |
74328 KB |
Output is correct |
11 |
Correct |
36 ms |
74576 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 |
0 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 |
344 KB |
Output is correct |
7 |
Correct |
730 ms |
299436 KB |
Output is correct |
8 |
Correct |
1747 ms |
617940 KB |
Output is correct |
9 |
Correct |
1715 ms |
620664 KB |
Output is correct |
10 |
Correct |
1682 ms |
621120 KB |
Output is correct |
11 |
Correct |
416 ms |
267132 KB |
Output is correct |
12 |
Correct |
731 ms |
471832 KB |
Output is correct |
13 |
Correct |
838 ms |
502708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
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 |
344 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 |
440 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1476 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1272 KB |
Output is correct |
23 |
Correct |
3 ms |
1252 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
13 ms |
6596 KB |
Output is correct |
26 |
Correct |
14 ms |
6744 KB |
Output is correct |
27 |
Correct |
13 ms |
6604 KB |
Output is correct |
28 |
Correct |
10 ms |
4700 KB |
Output is correct |
29 |
Correct |
16 ms |
5496 KB |
Output is correct |
30 |
Correct |
18 ms |
5720 KB |
Output is correct |
31 |
Correct |
21 ms |
5600 KB |
Output is correct |
32 |
Correct |
16 ms |
5464 KB |
Output is correct |
33 |
Correct |
154 ms |
72532 KB |
Output is correct |
34 |
Correct |
162 ms |
66492 KB |
Output is correct |
35 |
Correct |
130 ms |
66372 KB |
Output is correct |
36 |
Correct |
118 ms |
60244 KB |
Output is correct |
37 |
Correct |
173 ms |
78416 KB |
Output is correct |
38 |
Correct |
171 ms |
78420 KB |
Output is correct |
39 |
Correct |
177 ms |
78672 KB |
Output is correct |
40 |
Correct |
163 ms |
73888 KB |
Output is correct |
41 |
Correct |
118 ms |
48980 KB |
Output is correct |
42 |
Correct |
141 ms |
52528 KB |
Output is correct |
43 |
Correct |
239 ms |
62800 KB |
Output is correct |
44 |
Correct |
283 ms |
64952 KB |
Output is correct |
45 |
Correct |
117 ms |
34276 KB |
Output is correct |
46 |
Correct |
125 ms |
34132 KB |
Output is correct |
47 |
Correct |
229 ms |
61400 KB |
Output is correct |
48 |
Correct |
218 ms |
62544 KB |
Output is correct |
49 |
Correct |
39 ms |
75048 KB |
Output is correct |
50 |
Correct |
25 ms |
54364 KB |
Output is correct |
51 |
Correct |
35 ms |
74396 KB |
Output is correct |
52 |
Correct |
0 ms |
344 KB |
Output is correct |
53 |
Correct |
37 ms |
74932 KB |
Output is correct |
54 |
Correct |
38 ms |
74868 KB |
Output is correct |
55 |
Correct |
37 ms |
74776 KB |
Output is correct |
56 |
Correct |
38 ms |
74844 KB |
Output is correct |
57 |
Correct |
40 ms |
74832 KB |
Output is correct |
58 |
Correct |
34 ms |
74328 KB |
Output is correct |
59 |
Correct |
36 ms |
74576 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
730 ms |
299436 KB |
Output is correct |
62 |
Correct |
1747 ms |
617940 KB |
Output is correct |
63 |
Correct |
1715 ms |
620664 KB |
Output is correct |
64 |
Correct |
1682 ms |
621120 KB |
Output is correct |
65 |
Correct |
416 ms |
267132 KB |
Output is correct |
66 |
Correct |
731 ms |
471832 KB |
Output is correct |
67 |
Correct |
838 ms |
502708 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 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 |
2567 ms |
928136 KB |
Output is correct |
74 |
Correct |
2427 ms |
847236 KB |
Output is correct |
75 |
Correct |
1826 ms |
847236 KB |
Output is correct |
76 |
Correct |
1641 ms |
766332 KB |
Output is correct |
77 |
Correct |
2667 ms |
1003140 KB |
Output is correct |
78 |
Correct |
2317 ms |
486396 KB |
Output is correct |
79 |
Correct |
2423 ms |
521988 KB |
Output is correct |
80 |
Correct |
3878 ms |
787968 KB |
Output is correct |
81 |
Correct |
2548 ms |
503548 KB |
Output is correct |
82 |
Correct |
3401 ms |
659636 KB |
Output is correct |
83 |
Correct |
4019 ms |
816612 KB |
Output is correct |
84 |
Correct |
2075 ms |
479176 KB |
Output is correct |
85 |
Correct |
3520 ms |
793732 KB |
Output is correct |
86 |
Correct |
3671 ms |
770932 KB |
Output is correct |
87 |
Correct |
1524 ms |
611688 KB |
Output is correct |
88 |
Correct |
3023 ms |
1002072 KB |
Output is correct |
89 |
Correct |
2910 ms |
1003392 KB |
Output is correct |
90 |
Correct |
3044 ms |
1003440 KB |
Output is correct |