Submission #64166949


Source Code Expand

Copy
options(scipen = 100, digits = 10)
conn <- file("stdin", open = "r")
rin <- function(n = 1) readLines(conn, n = n)
split <- function(str, split = " ") unlist(strsplit(str, split = split))
get_root <- function(uf, x) {
path <- numeric()
while (uf[x] > 0) {
path[[length(path) + 1]] <- x
x <- uf[x]
}
list(root = x, path = path)
}
HW <- as.integer(split(rin()))
H <- HW[1]
W <- HW[2]
uf <- numeric(H * W)
painted <- matrix(0, H, W)
Q <- as.integer(rin())
di <- c(-1, 0, 1, 0)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
options(scipen = 100, digits = 10)
conn <- file("stdin", open = "r")
rin <- function(n = 1) readLines(conn, n = n)
split <- function(str, split = " ") unlist(strsplit(str, split = split))

get_root <- function(uf, x) {
  path <- numeric()
  while (uf[x] > 0) {
    path[[length(path) + 1]] <- x
    x <- uf[x]
  }
  list(root = x, path = path)
}

HW <- as.integer(split(rin()))
H <- HW[1]
W <- HW[2]
uf <- numeric(H * W)
painted <- matrix(0, H, W)
Q <- as.integer(rin())
di <- c(-1, 0, 1, 0)
dj <- c(0, 1, 0, -1)
ans <- vector("logical", 0)
for (. in seq_len(Q)) {
  q <- as.integer(split(rin()))
  if (q[1] == 1) {
    i <- q[2]
    j <- q[3]
    painted[i, j] <- 1
    for (k in 1:4) {
      .i <- i + di[k]
      .j <- j + dj[k]
      if ((0 < .i) && (.i <= H) && (0 < .j) && (.j <= W) && painted[.i, .j]) {
        x <- ( i - 1) * W +  j
        y <- (.i - 1) * W + .j
        rx <- get_root(uf, x)
        uf[rx$path] <- rx$root
        ry <- get_root(uf, y)
        uf[ry$path] <- ry$root
        # unite x & y
        if (rx$root == ry$root)
          next
        if (uf[rx$root] < uf[ry$root]) {
          rz <- ry
          ry <- rx
          rx <- rz
        }
        uf[rx$root] <- uf[rx$root] + uf[ry$root] - 1
        uf[ry$root] <- rx$root
      }
    }
  } else {
    i <- q[2]
    j <- q[3]
    .i <- q[4]
    .j <- q[5]
    res <- FALSE
    if (painted[i, j] && painted[.i, .j]) {
      x <- ( i - 1) * W +  j
      y <- (.i - 1) * W + .j
      rx <- get_root(uf, x)
      uf[rx$path] <- rx$root
      ry <- get_root(uf, y)
      uf[ry$path] <- ry$root
      # united
      if (rx$root == ry$root) {
        res <- TRUE
      }
    }
    ans[[length(ans) + 1]] <- res
  }
}
ans <- paste0(ifelse(ans, "Yes", "No"), collapse = "\n")
cat(ans)

Submission Info

Submission Time
Task 012 - Red Painting(★4)
User negoto_coder
Language R (GNU R 4.2.1)
Score 4
Code Size 1831 Byte
Status AC
Exec Time 1986 ms
Memory 142672 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 4 / 4
Status
AC × 3
AC × 43
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_40.txt
Case Name Status Exec Time Memory
sample_01.txt AC 164 ms 67528 KB
sample_02.txt AC 165 ms 67516 KB
sample_03.txt AC 170 ms 67572 KB
subtask_1_01.txt AC 1270 ms 75380 KB
subtask_1_02.txt AC 560 ms 82792 KB
subtask_1_03.txt AC 650 ms 88232 KB
subtask_1_04.txt AC 1012 ms 110936 KB
subtask_1_05.txt AC 954 ms 80164 KB
subtask_1_06.txt AC 444 ms 69912 KB
subtask_1_07.txt AC 1704 ms 72336 KB
subtask_1_08.txt AC 1090 ms 96600 KB
subtask_1_09.txt AC 446 ms 77696 KB
subtask_1_10.txt AC 722 ms 77172 KB
subtask_1_11.txt AC 370 ms 87784 KB
subtask_1_12.txt AC 218 ms 72028 KB
subtask_1_13.txt AC 511 ms 77104 KB
subtask_1_14.txt AC 843 ms 72612 KB
subtask_1_15.txt AC 919 ms 98200 KB
subtask_1_16.txt AC 607 ms 114800 KB
subtask_1_17.txt AC 1441 ms 91132 KB
subtask_1_18.txt AC 352 ms 68620 KB
subtask_1_19.txt AC 1197 ms 72324 KB
subtask_1_20.txt AC 1034 ms 89016 KB
subtask_1_21.txt AC 1240 ms 122676 KB
subtask_1_22.txt AC 185 ms 96284 KB
subtask_1_23.txt AC 478 ms 91172 KB
subtask_1_24.txt AC 948 ms 94112 KB
subtask_1_25.txt AC 699 ms 85880 KB
subtask_1_26.txt AC 217 ms 75392 KB
subtask_1_27.txt AC 1222 ms 75976 KB
subtask_1_28.txt AC 1266 ms 77008 KB
subtask_1_29.txt AC 1322 ms 80368 KB
subtask_1_30.txt AC 1494 ms 77564 KB
subtask_1_31.txt AC 1078 ms 81800 KB
subtask_1_32.txt AC 1675 ms 74932 KB
subtask_1_33.txt AC 1986 ms 72436 KB
subtask_1_34.txt AC 1402 ms 128940 KB
subtask_1_35.txt AC 1276 ms 139784 KB
subtask_1_36.txt AC 1502 ms 137752 KB
subtask_1_37.txt AC 1119 ms 140072 KB
subtask_1_38.txt AC 1108 ms 142672 KB
subtask_1_39.txt AC 1358 ms 138636 KB
subtask_1_40.txt AC 1514 ms 137584 KB


2025-04-04 (Fri)
07:28:21 +00:00