답안 #143921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
143921 2019-08-15T12:59:59 Z Milki 섬 항해 (CEOI13_adriatic) C++14
100 / 100
463 ms 182472 KB
#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){
  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){
  if(dp_rt[x][y] != -1) return dp_rt[x][y];
  if(get(1, y, x, MAX) == 1) return dp_rt[x][y] = 1;
  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";
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 172604 KB Output is correct
2 Correct 194 ms 172596 KB Output is correct
3 Correct 196 ms 172632 KB Output is correct
4 Correct 194 ms 172336 KB Output is correct
5 Correct 194 ms 172392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 175972 KB Output is correct
2 Correct 195 ms 176200 KB Output is correct
3 Correct 195 ms 175988 KB Output is correct
4 Correct 193 ms 172456 KB Output is correct
5 Correct 195 ms 173048 KB Output is correct
6 Correct 195 ms 174844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 177912 KB Output is correct
2 Correct 199 ms 178212 KB Output is correct
3 Correct 201 ms 178144 KB Output is correct
4 Correct 195 ms 173072 KB Output is correct
5 Correct 195 ms 173320 KB Output is correct
6 Correct 199 ms 178528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 210 ms 178816 KB Output is correct
2 Correct 217 ms 178680 KB Output is correct
3 Correct 219 ms 178788 KB Output is correct
4 Correct 209 ms 173912 KB Output is correct
5 Correct 204 ms 173992 KB Output is correct
6 Correct 234 ms 178804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 377 ms 182408 KB Output is correct
2 Correct 382 ms 181964 KB Output is correct
3 Correct 463 ms 182104 KB Output is correct
4 Correct 347 ms 178424 KB Output is correct
5 Correct 335 ms 178016 KB Output is correct
6 Correct 351 ms 182472 KB Output is correct
7 Correct 351 ms 182296 KB Output is correct