Submission #1276366

#TimeUsernameProblemLanguageResultExecution timeMemory
1276366LucaLucaMLand of the Rainbow Gold (APIO17_rainbow)C++20
11 / 100
3121 ms634276 KiB
#include "rainbow.h"
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using ordered_set = tree<std::pair<int, int>, null_type, std::less<std::pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;

const int MAXN = 2e5 + 7;
using ll = long long;

struct Fenwick2D {
  ordered_set aib[MAXN + 1];

  void add(int x, int y) {
    x++;
    assert(1 <= x && x <= MAXN);
    for (int i = x; i <= MAXN; i += i & -i) {
      aib[i].insert({y, x});
    }
  }
  int query(int x, int y) {
    x++;
    assert(1 <= x && x <= MAXN);
    int ret = 0;
    for (int i = x; i > 0; i -= i & -i) {
      ret += aib[i].order_of_key({y + 1, 0});
    }
    return ret;
  }
  int query(int x1, int y1, int x2, int y2) {
    return query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1);
  }
};

namespace {
  int minX, maxX, minY, maxY;
  Fenwick2D vert, horizontalEdges, verticalEdges, boxes; 
};

void init(int R, int C, int sr, int sc, int M, char *S) {
  int x = sr, y = sc;
  minX = R + 1;
  minY = C + 1;
  maxX = 0;
  maxY = 0;

  auto add = [&](int x, int y) {
    minX = std::min(minX, x);
    maxX = std::max(maxX, x);
    minY = std::min(minY, y);
    maxY = std::max(maxY, y);
    vert.add(x, y);
    horizontalEdges.add(x, y);
    horizontalEdges.add(x, y - 1);
    verticalEdges.add(x, y);
    verticalEdges.add(x - 1, y);
    boxes.add(x, y);
    boxes.add(x, y - 1);
    boxes.add(x - 1, y);
    boxes.add(x - 1, y - 1);
  };

  add(x, y);
  for (int i = 0; i < M; i++) {
    if (S[i] == 'N') {
      x--;
    } else if (S[i] == 'S') {
      x++;
    } else if (S[i] == 'W') {
      y--;
    } else {
      y++;
    }
    add(x, y);
  }
}

int colour(int x1, int y1, int x2, int y2) {
  ll V = (ll) (x2 - x1 + 1) * (y2 - y1 + 1) - vert.query(x1, y1, x2, y2);
  ll E = 0;
  E += (ll) (x2 - x1 + 1) * (y2 - y1) - horizontalEdges.query(x1, y1, x2, y2 - 1);
  E += (ll) (x2 - x1) * (y2 - y1 + 1) - verticalEdges.query(x1, y1, x2 - 1, y2);
  ll F = (ll) (x2 - x1) * (y2 - y1) - boxes.query(x1, y1, x2 - 1, y2 - 1);
  F++;
  if (x1 < minX && maxX < x2 && y1 < minY && maxY < y2) {
    F++;
  }
  // V + F = E + C + 1 (cred)
  return V + F - E - 1;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...