487D - Conveyor Belts - CodeForces Solution


data structures *2700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int maxLig = (1e5) + 5;
const int maxCol = 12;
const int maxReq = (1e5) + 5;
const int sqr = 400;
int nbLig, nbCol, nbReq;
char grille[maxLig][maxCol];
int proc[maxLig][maxCol];
int tps = 0;
pair<int, int> go[maxLig][maxCol];
void comp(int lig, int col, int safeLig) {
  if (proc[lig][col] == tps)
    return;
  proc[lig][col] = tps;
  int nextLig = lig, nextCol = col;
  if (grille[lig][col] == '<')
    --nextCol;
  else if (grille[lig][col] == '>')
    ++nextCol;
  else if (grille[lig][col] == '^')
    --nextLig;
  if (nextLig == safeLig || grille[nextLig][nextCol] == '~') {
    go[lig][col] = {nextLig, nextCol};
  } else if (grille[lig][col] != '^' && grille[lig][nextCol] != '^' &&
             grille[lig][col] != grille[lig][nextCol]) {
    go[lig][col] = {0, 0};
  } else {
    comp(nextLig, nextCol, safeLig);
    go[lig][col] = go[nextLig][nextCol];
  }
}
pair<int, int> getFin(int lig, int col) {
  while (grille[lig][col] != '~') {
    pii e = go[lig][col];
    lig = e.first;
    col = e.second;
  }
  return {lig, col};
}
int blocRef[maxLig];
bool compBloc(int bloc) {
  int deb = bloc * sqr, fin = min(nbLig, (bloc + 1) * sqr);
  if (deb > nbLig)
    return false;
  for (int lig = deb + 1; lig <= fin; ++lig) {
    blocRef[lig] = bloc;
    for (int col = 1; col <= nbCol; ++col) {
      comp(lig, col, deb);
    }
  }
  return true;
}
int main() {
  cin >> nbLig >> nbCol >> nbReq;
  fill_n(&grille[0][0], maxLig * maxCol, '~');
  for (int lig = 1; lig <= nbLig; ++lig) {
    for (int col = 1; col <= nbCol; ++col) {
      cin >> grille[lig][col];
    }
  }
  ++tps;
  for (int bloc = 0;; ++bloc) {
    if (!compBloc(bloc))
      break;
  }
  for (int req = 0; req < nbReq; ++req) {
    char mode;
    cin >> mode;
    int lig, col;
    cin >> lig >> col;
    if (mode == 'A') {
      pii res = getFin(lig, col);
      if (res == pii{0, 0})
        res = {-1, -1};
      cout << res.first << " " << res.second << "\n";
    } else {
      ++tps;
      char nv;
      cin >> nv;
      grille[lig][col] = nv;
      int bloc = blocRef[lig];
      compBloc(bloc);
      if (bloc)
        compBloc(bloc - 1);
    }
  }
}

// ohKHyHNWJqQOsdLaVjCXqrgrtgeeGlVjqeBRIUtHqjBCZyHUwkylApbHpAgdsjWoTerbmBfsNtJTPoEwvZvayATadUCQiGgZosxFBSpFLvMChaPlxAupVlTdnKmwanhN


Comments

Submit
0 Comments
More Questions

450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General