# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1028652 |
2024-07-20T06:44:21 Z |
onbert |
Rectangles (IOI19_rect) |
C++17 |
|
1738 ms |
725092 KB |
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct range {
int l, 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 = 1e9;
int n, m;
vector<vector<int>> a;
vector<pair<int,int>> pos[2][maxn][maxn];
vector<range> start[2][maxn][maxn];
int 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;
}
long long count_rectangles(vector<vector<int>> A) {
a = A;
n = a.size(), m = a[0].size();
for (int i=0;i<n;i++) {
vector<pair<int,int>> cur;
for (int j=0;j<m;j++) cur.push_back({a[i][j], j});
sort(cur.rbegin(), cur.rend());
set<int> s; s.insert(-1); s.insert(INF);
vector<int> temp;
for (int j=0;j<m;j++) {
auto [val, id] = cur[j];
if (j>0 && val!=cur[j-1].first) {
for (int x:temp) s.insert(x);
temp.clear();
}
auto it = s.upper_bound(id);
int l = *prev(it), r = *it;
if (l!=-1 && r!=INF) {
l++, r--;
pos[0][l][r].push_back({i, -1});
start[0][i][l].push_back({l, r, -1});
}
temp.push_back(id);
}
}
for (int j=0;j<m;j++) {
vector<pair<int,int>> cur;
for (int i=0;i<n;i++) cur.push_back({a[i][j], i});
sort(cur.rbegin(), cur.rend());
set<int> s; s.insert(-1); s.insert(INF);
vector<int> temp;
for (int i=0;i<n;i++) {
auto [val, id] = cur[i];
if (i>0 && val!=cur[i-1].first) {
for (int x:temp) s.insert(x);
temp.clear();
}
auto it = s.upper_bound(id);
int l = *prev(it), r = *it;
if (l!=-1 && r!=INF) {
l++, r--;
pos[1][l][r].push_back({j, -1});
start[1][l][j].push_back({l, r, -1});
}
temp.push_back(id);
}
}
for (int I=0;I<=1;I++) for (int i=0;i<n;i++) for (int j=0;j<m;j++) sort(pos[I][i][j].begin(), pos[I][i][j].end());
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++) {
int sz = pos[I][l][r].size();
// if (I==1 && sz>0) cout << l << " " << r << " " << sz << endl;
for (int i=sz-1;i>=0;i--) {
if (i==sz-1 || pos[I][l][r][i+1].first!=pos[I][l][r][i].first+1) pos[I][l][r][i].second = pos[I][l][r][i].first;
else pos[I][l][r][i].second = pos[I][l][r][i+1].second;
// if (I==1) cout << pos[I][l][r][i].first << " " << pos[I][l][r][i].second << endl;
}
}
}
for (int I=0;I<=1;I++) {
for (int i=0;i<n;i++) for (int j=0;j<m;j++) {
// cout << "test " << I << " " << i << " " << j << endl;
for (auto &[l, r, mx]:start[I][i][j]) {
// cout << l << " " << r << " " << mx << endl;
int id = i;
if (I==1) id = j;
mx = (*lower_bound(pos[I][l][r].begin(), pos[I][l][r].end(), make_pair(id, 0))).second;
}
}
}
ll ans = 0;
for (int i=0;i<n;i++) for (int j=0;j<m;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;
vector<int> LOG;
for (auto [l, 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:113:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | while (pt+1 < start[1][i][j].size() && start[1][i][j][pt+1].mx >= r) {
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
589672 KB |
Output is correct |
2 |
Correct |
215 ms |
589912 KB |
Output is correct |
3 |
Correct |
221 ms |
589888 KB |
Output is correct |
4 |
Correct |
224 ms |
589908 KB |
Output is correct |
5 |
Correct |
223 ms |
589756 KB |
Output is correct |
6 |
Correct |
243 ms |
589804 KB |
Output is correct |
7 |
Correct |
247 ms |
589652 KB |
Output is correct |
8 |
Correct |
217 ms |
589648 KB |
Output is correct |
9 |
Incorrect |
227 ms |
589676 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
589672 KB |
Output is correct |
2 |
Correct |
215 ms |
589912 KB |
Output is correct |
3 |
Correct |
221 ms |
589888 KB |
Output is correct |
4 |
Correct |
224 ms |
589908 KB |
Output is correct |
5 |
Correct |
223 ms |
589756 KB |
Output is correct |
6 |
Correct |
243 ms |
589804 KB |
Output is correct |
7 |
Correct |
247 ms |
589652 KB |
Output is correct |
8 |
Correct |
217 ms |
589648 KB |
Output is correct |
9 |
Incorrect |
227 ms |
589676 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
589672 KB |
Output is correct |
2 |
Correct |
215 ms |
589912 KB |
Output is correct |
3 |
Correct |
221 ms |
589888 KB |
Output is correct |
4 |
Correct |
224 ms |
589908 KB |
Output is correct |
5 |
Correct |
223 ms |
589756 KB |
Output is correct |
6 |
Correct |
243 ms |
589804 KB |
Output is correct |
7 |
Correct |
247 ms |
589652 KB |
Output is correct |
8 |
Correct |
217 ms |
589648 KB |
Output is correct |
9 |
Incorrect |
227 ms |
589676 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
589672 KB |
Output is correct |
2 |
Correct |
215 ms |
589912 KB |
Output is correct |
3 |
Correct |
221 ms |
589888 KB |
Output is correct |
4 |
Correct |
224 ms |
589908 KB |
Output is correct |
5 |
Correct |
223 ms |
589756 KB |
Output is correct |
6 |
Correct |
243 ms |
589804 KB |
Output is correct |
7 |
Correct |
247 ms |
589652 KB |
Output is correct |
8 |
Correct |
217 ms |
589648 KB |
Output is correct |
9 |
Incorrect |
227 ms |
589676 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
590424 KB |
Output is correct |
2 |
Correct |
240 ms |
590420 KB |
Output is correct |
3 |
Correct |
262 ms |
589804 KB |
Output is correct |
4 |
Correct |
253 ms |
589656 KB |
Output is correct |
5 |
Correct |
348 ms |
590480 KB |
Output is correct |
6 |
Correct |
236 ms |
590396 KB |
Output is correct |
7 |
Correct |
265 ms |
590436 KB |
Output is correct |
8 |
Correct |
265 ms |
590488 KB |
Output is correct |
9 |
Correct |
247 ms |
590420 KB |
Output is correct |
10 |
Correct |
244 ms |
590124 KB |
Output is correct |
11 |
Correct |
236 ms |
590252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
228 ms |
589652 KB |
Output is correct |
2 |
Incorrect |
1738 ms |
725092 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
589672 KB |
Output is correct |
2 |
Correct |
215 ms |
589912 KB |
Output is correct |
3 |
Correct |
221 ms |
589888 KB |
Output is correct |
4 |
Correct |
224 ms |
589908 KB |
Output is correct |
5 |
Correct |
223 ms |
589756 KB |
Output is correct |
6 |
Correct |
243 ms |
589804 KB |
Output is correct |
7 |
Correct |
247 ms |
589652 KB |
Output is correct |
8 |
Correct |
217 ms |
589648 KB |
Output is correct |
9 |
Incorrect |
227 ms |
589676 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |