#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<int, int> point;
const int MAXN = 2505, inf = 1e9, MAX = 2500;
int n, total[MAXN][MAXN];
bool imam[MAXN][MAXN];
struct Sum{
int X[MAXN][MAXN], Y[MAXN][MAXN];
Sum(){}
void init_prefix(){
REP(i, MAXN) REP(j, MAXN) X[i][j] = Y[i][j] = inf;
}
void init_suffix(){
REP(i, MAXN) REP(j, MAXN) X[i][j] = Y[i][j] = 0;
}
void update_prefix(int x, int y){
X[x][y] = min(X[x - 1][y], X[x][y - 1]);
Y[x][y] = min(Y[x - 1][y], Y[x][y - 1]);
if(imam[x][y]){
X[x][y] = min(X[x][y], x);
Y[x][y] = min(Y[x][y], y);
}
}
void update_suffix(int x, int y){
X[x][y] = max(X[x + 1][y], X[x][y + 1]);
Y[x][y] = max(Y[x + 1][y], Y[x][y + 1]);
if(imam[x][y]){
X[x][y] = max(X[x][y], x);
Y[x][y] = max(Y[x][y], y);
}
}
} prefix, suffix;
int dp_lt[MAXN][MAXN], dp_rt[MAXN][MAXN];
point in[MAXN * MAXN];
int get(int x1, int y1, int x2, int y2){
return total[x2][y2] - total[x1 - 1][y2] - total[x2][y1 - 1] + total[x1 - 1][y1 - 1];
}
int rek_lt(int x, int y){
//TRACE("OK"); TRACE(x); TRACE(y);
//TRACE(get(x, 1, MAX, y));
//TRACE(dp_lt[x][y]);
if(dp_lt[x][y] != -1) { return dp_lt[x][y]; }
if(get(x, 1, MAX, y) == 1) return dp_lt[x][y] = 1;
int mx_x = suffix.X[x][y + 1], mn_y = prefix.Y[x - 1][y];
if(mx_x == x && mn_y == y) return dp_lt[x][y] = 0;
return dp_lt[x][y] = get(x, 1, MAX, y) + rek_lt( max(mx_x, x), min(mn_y, y) );
}
int rek_rt(int x, int y){
//TRACE(x); TRACE(y); TRACE("KOK"); TRACE(get(1, y, x, MAX));
if(dp_rt[x][y] != -1) return dp_rt[x][y];
if(get(1, y, x, MAX) == 1) return dp_rt[x][y] = 1;
//TRACE(x); TRACE(y);
int mn_x = prefix.X[x][y - 1], mx_y = suffix.Y[x + 1][y];
if(mn_x == x && mx_y == y) return dp_rt[x][y] = 0;
return dp_rt[x][y] = get(1, y, x, MAX) + rek_rt( min(mn_x, x), max(mx_y, y) );
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
memset(dp_lt, -1, sizeof(dp_lt));
memset(dp_rt, -1, sizeof(dp_rt));
prefix.init_prefix();
suffix.init_suffix();
cin >> n;
REP(i, n){
int a, b; cin >> a >> b;
imam[a][b] = 1;
in[i] = point(a, b);
}
FOR(i, 1, MAX + 1) FOR(j, 1, MAX + 1){
prefix.update_prefix(i, j);
total[i][j] = total[i][j - 1] + total[i - 1][j] - total[i - 1][j - 1] + imam[i][j];
}
for(int i = MAX; i > 0; --i)
for(int j = MAX; j > 0; --j)
suffix.update_suffix(i, j);
REP(i, n){
int x = in[i].first, y = in[i].second;
cout << rek_lt(x, y) + rek_rt(x, y) + total[MAX][MAX] - 3 << "\n";
//break;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
172536 KB |
Output is correct |
2 |
Correct |
191 ms |
172664 KB |
Output is correct |
3 |
Correct |
193 ms |
172664 KB |
Output is correct |
4 |
Correct |
194 ms |
172296 KB |
Output is correct |
5 |
Correct |
194 ms |
172588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
175804 KB |
Output is correct |
2 |
Correct |
194 ms |
176036 KB |
Output is correct |
3 |
Correct |
196 ms |
176020 KB |
Output is correct |
4 |
Correct |
193 ms |
172524 KB |
Output is correct |
5 |
Correct |
192 ms |
172892 KB |
Output is correct |
6 |
Correct |
195 ms |
174816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
177860 KB |
Output is correct |
2 |
Correct |
200 ms |
178236 KB |
Output is correct |
3 |
Correct |
218 ms |
178168 KB |
Output is correct |
4 |
Correct |
196 ms |
173176 KB |
Output is correct |
5 |
Correct |
230 ms |
173296 KB |
Output is correct |
6 |
Correct |
200 ms |
178488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
178752 KB |
Output is correct |
2 |
Correct |
216 ms |
178900 KB |
Output is correct |
3 |
Correct |
219 ms |
178884 KB |
Output is correct |
4 |
Correct |
211 ms |
173916 KB |
Output is correct |
5 |
Correct |
205 ms |
174256 KB |
Output is correct |
6 |
Correct |
209 ms |
178996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
182428 KB |
Output is correct |
2 |
Correct |
390 ms |
184292 KB |
Output is correct |
3 |
Correct |
460 ms |
184352 KB |
Output is correct |
4 |
Correct |
350 ms |
180248 KB |
Output is correct |
5 |
Correct |
339 ms |
180380 KB |
Output is correct |
6 |
Correct |
350 ms |
184528 KB |
Output is correct |
7 |
Correct |
360 ms |
184664 KB |
Output is correct |