답안 #234153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
234153 2020-05-23T11:45:42 Z rama_pang 무지개나라 (APIO17_rainbow) C++14
50 / 100
3000 ms 184396 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

class SegmentTree {
 private:
  struct Node {
    int val = 0;
    Node *lc = nullptr;
    Node *rc = nullptr;
  };

  int sz;
  Node *root = nullptr;

  void Pull(Node* &n) {
    n->val = 0;
    if (n->lc != nullptr) n->val += n->lc->val;
    if (n->rc != nullptr) n->val += n->rc->val;
  }

  void Update(Node* &n, int tl, int tr, int p, int x) {
    if (n == nullptr) n = new Node();
    if (tl == tr) return void(n->val = x);
    int mid = (tl + tr) / 2;
    if (p <= mid) {
      Update(n->lc, tl, mid, p, x);
    } else {
      Update(n->rc, mid + 1, tr, p, x);
    }
    return Pull(n);
  }

  int Query(Node* &n, int tl, int tr, int l, int r) {
    if (n == nullptr) return 0;
    if (r < tl || tr < l || tl > tr || l > r) return 0;
    if (l <= tl && tr <= r) return n->val;
    int mid = (tl + tr) / 2;
    return Query(n->lc, tl, mid, l, r) + 
           Query(n->rc, mid + 1, tr, l, r);
  }

 public:
  SegmentTree() {}
  SegmentTree(int sz) : sz(sz) {}

  void Update(int p, int x) {
    return Update(root, 0, sz, p, x);
  }
  int Query(int l, int r) {
    return Query(root, 0, sz, l, r);
  }
};

class RangeTree {
 private:
  struct Node {
    SegmentTree seg;
    Node *lc = nullptr;
    Node *rc = nullptr;
    Node(int sz) : seg(sz) {}
  };

  int sx, sy;
  Node *root = nullptr;

  void Pull(Node* &n, int pos) {
    int val = 0;
    if (n->lc != nullptr) val += n->lc->seg.Query(pos, pos);
    if (n->rc != nullptr) val += n->rc->seg.Query(pos, pos);
    n->seg.Update(pos, val);
  }

  void Update(Node* &n, int tl, int tr, int p, int q, int x) {
    if (n == nullptr) n = new Node(sy);
    if (tl == tr) return void(n->seg.Update(q, x));
    if (tl == tr) return;
    int mid = (tl + tr) / 2;
    if (p <= mid) {
      Update(n->lc, tl, mid, p, q, x);
    } else {
      Update(n->rc, mid + 1, tr, p, q, x);
    }
    return Pull(n, q);
  }

  int Query(Node* &n, int tl, int tr, int l, int r, int d, int u) {
    if (n == nullptr) return 0;
    if (r < tl || tr < l || tl > tr || l > r) return 0;
    if (l <= tl && tr <= r) return n->seg.Query(d, u);
    int mid = (tl + tr) / 2;
    return Query(n->lc, tl, mid, l, r, d, u) + 
           Query(n->rc, mid + 1, tr, l, r, d, u);
  }

 public:
  RangeTree() {}
  RangeTree(int sx, int sy) : sx(sx), sy(sy) {}

  void Update(int p, int q, int x) {
    return Update(root, 0, sx, p, q, x);
  }
  int Query(int l, int r, int d, int u) {
    return Query(root, 0, sx, l, r, d, u);
  }
};



// V - E + F = 2
// Vertices = 4 suares points aroud river cells
// Edges = borders around river cells
// Faces = land components + number of river cells

RangeTree river_cells; // keeps V - E + F, scaled by 2
int minr, maxr, minc, maxc;

void init(int R, int C, int sr, int sc, int M, char *S) {
  river_cells = RangeTree(2 * R + 5, 2 * C + 5);

  auto Update = [&](int x, int y) { // cell (x, y) is a river cell
    // Vertices
    river_cells.Update(2 * x - 1, 2 * y - 1, 1);
    river_cells.Update(2 * x - 1, 2 * y + 1, 1);
    river_cells.Update(2 * x + 1, 2 * y - 1, 1);
    river_cells.Update(2 * x + 1, 2 * y + 1, 1);

    // Edges
    river_cells.Update(2 * x - 1, 2 * y, -1);
    river_cells.Update(2 * x + 1, 2 * y, -1);
    river_cells.Update(2 * x, 2 * y - 1, -1);
    river_cells.Update(2 * x, 2 * y + 1, -1);
    
    // Faces
    river_cells.Update(2 * x, 2 * y, 1);
  };

  minr = maxr = sr;
  minc = maxc = sc;

  Update(sr, sc);
  for (int i = 0; i < M; i++) {
    if (S[i] == 'N') sr--;
    if (S[i] == 'S') sr++;
    if (S[i] == 'W') sc--;
    if (S[i] == 'E') sc++;
    Update(sr, sc);
    minr = min(minr, sr);
    minc = min(minc, sc);
    maxr = max(maxr, sr);
    maxc = max(maxc, sc);
  }
  minr--;
  maxr++;
  minc--;
  maxc++;
}

int colour(int ar, int ac, int br, int bc) {
  ar = max(ar, minr);
  ac = max(ac, minc);
  br = min(br, maxr);
  bc = min(bc, maxc);

  return 1 + (ar == minr && ac == minc && br == maxr && bc == maxc) 
         - river_cells.Query(2 * ar, 2 * br, 2 * ac, 2 * bc);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 20 ms 1152 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 10 ms 640 KB Output is correct
5 Correct 27 ms 1272 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 15 ms 768 KB Output is correct
12 Correct 19 ms 896 KB Output is correct
13 Correct 20 ms 1536 KB Output is correct
14 Correct 40 ms 1660 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 868 ms 65328 KB Output is correct
4 Correct 1476 ms 109000 KB Output is correct
5 Correct 1482 ms 109416 KB Output is correct
6 Correct 1368 ms 78912 KB Output is correct
7 Correct 1449 ms 78844 KB Output is correct
8 Correct 1078 ms 1272 KB Output is correct
9 Correct 1475 ms 108756 KB Output is correct
10 Correct 1482 ms 109076 KB Output is correct
11 Correct 1391 ms 78968 KB Output is correct
12 Correct 1251 ms 101624 KB Output is correct
13 Correct 1383 ms 108884 KB Output is correct
14 Correct 1390 ms 109304 KB Output is correct
15 Correct 1334 ms 79000 KB Output is correct
16 Correct 994 ms 74984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB Output is correct
2 Execution timed out 3089 ms 184396 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 20 ms 1152 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 10 ms 640 KB Output is correct
5 Correct 27 ms 1272 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 15 ms 768 KB Output is correct
12 Correct 19 ms 896 KB Output is correct
13 Correct 20 ms 1536 KB Output is correct
14 Correct 40 ms 1660 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 2725 ms 71056 KB Output is correct
19 Correct 171 ms 7188 KB Output is correct
20 Correct 118 ms 4332 KB Output is correct
21 Correct 135 ms 5112 KB Output is correct
22 Correct 162 ms 5880 KB Output is correct
23 Correct 159 ms 7032 KB Output is correct
24 Correct 154 ms 4600 KB Output is correct
25 Correct 174 ms 5368 KB Output is correct
26 Correct 189 ms 6136 KB Output is correct
27 Correct 2379 ms 40580 KB Output is correct
28 Correct 2150 ms 21924 KB Output is correct
29 Correct 2317 ms 35612 KB Output is correct
30 Correct 2611 ms 92468 KB Output is correct
31 Correct 7 ms 512 KB Output is correct
32 Correct 2590 ms 37200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 20 ms 1152 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 10 ms 640 KB Output is correct
5 Correct 27 ms 1272 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 15 ms 768 KB Output is correct
12 Correct 19 ms 896 KB Output is correct
13 Correct 20 ms 1536 KB Output is correct
14 Correct 40 ms 1660 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 2725 ms 71056 KB Output is correct
19 Correct 171 ms 7188 KB Output is correct
20 Correct 118 ms 4332 KB Output is correct
21 Correct 135 ms 5112 KB Output is correct
22 Correct 162 ms 5880 KB Output is correct
23 Correct 159 ms 7032 KB Output is correct
24 Correct 154 ms 4600 KB Output is correct
25 Correct 174 ms 5368 KB Output is correct
26 Correct 189 ms 6136 KB Output is correct
27 Correct 2379 ms 40580 KB Output is correct
28 Correct 2150 ms 21924 KB Output is correct
29 Correct 2317 ms 35612 KB Output is correct
30 Correct 2611 ms 92468 KB Output is correct
31 Correct 7 ms 512 KB Output is correct
32 Correct 2590 ms 37200 KB Output is correct
33 Execution timed out 3089 ms 184396 KB Time limit exceeded
34 Halted 0 ms 0 KB -