A display board has n cells in a row, and each cell shows one integer. At first every cell shows the integer v. Cells are numbered 0, 1, …, n - 1 from the front. They are also numbered -1, -2, …, -n from the back. -1 is the last cell and -n is the first cell. So an index i names a cell when -n ≤ i ≤ n - 1, and a negative i names the same cell as index i + n. Any other index (n or more, or less than -n) names no cell. Process q commands in the given order, printing one line per command. - get i: if index i names a cell, print that cell's number. Otherwise print out. - set i x: if index i names a cell, print the number the cell showed before the change, then change the cell's number to x. Otherwise change nothing and print out. - fill a b s x: first turn a and b each into a bound by the following rule. Bound rule: if the number is negative, add n. If the result is negative, use 0; if it is greater than n, use n. If it is between 0 and n inclusive, keep it. Let A be the bound from a and B the bound from b. Change every cell whose index is one of A, A + s, A + 2s, … and less than B to x. If A ≥ B, no cell changes. fill prints how many cells it changed to x. Cells that were already x count too. The first line gives n, q and v separated by single spaces. Each of the next q lines gives one command. Values within a command are separated by single spaces. After the q lines of command output, print on the last line the board's numbers after all commands, from cell 0 in order, separated by single spaces. Write numbers as plain digits without commas, with a leading - for negatives.
Input
1 ≤ n ≤ 1,000, 1 ≤ q ≤ 2,000, -1,000,000,000 ≤ v, x ≤ 1,000,000,000, -2,000 ≤ i, a, b ≤ 2,000, 1 ≤ s ≤ 2,000. All are integers, and numbers in the input are plain digits without commas. Each command is one of get, set and fill
Output
q + 1 lines
Example 1 · stdin
5 9 0
set -1 7
get 4
get -6
set 5 3
fill 1 -1 2 9
get -2
fill -8 2 1 4
fill 3 -1 1 5
get 0
stdout
0
7
out
out
2
9
2
1
4
4 4 0 5 7
Example 2 · stdin
3 2 1
get 0
fill 0 9 1 -5
stdout
1
3
-5 -5 -5
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The index rule is the one Python lists use. Only Python board[i] and JS/TS board.at(i) (which gives undefined when there is no cell) count negative indexes from the back. Java and C# throw on a negative index, Craykon stops with a runtime error ("list only has indexes 0–4"), and C arrays and C++ vector's [] don't check the index at all, which is undefined behaviour. C#'s board[^1] is a different syntax (^) and does not accept a plain negative index. So, except for the Python solution and the JS/TS get, the solutions turn a negative i into i + n before reading. Building a board filled with v differs too: Python [v] * n, JS/TS new Array(n).fill(v), Java new int[n] then Arrays.fill(board, v), C# new int[n] then Array.Fill(board, v) (both start at 0), C++ std::vector<int> board(n, v), Craykon NewList(n, v); C sets the cells one by one in a loop. Trap one: in C, memset(board, v, sizeof board) writes the low 8 bits of v into every byte, not every int. With v = 1 each cell becomes 16843009 (0x01010101), so the first line of example 2 is 16843009 instead of 1 (only 0 and -1 come out right by luck). Trap two: JS/TS board[-1] is undefined without any error, and board[-1] = 7 creates a property named "-1" instead of changing a cell, so join never shows it. In example 1, set -1 7 prints undefined and the next get 4 prints 0 instead of 7. Trap three: Python too raises IndexError for an index below -n. Checking only i < n stops at get -6 in example 1; check -n <= i < n first. The bound rule of fill is exactly the rule Python slices board[a:b:s] and JS/TS fill(x, a, b) use. The Python solution counts the cells with len(board[a:b:s]) and fills them with board[a:b:s] = [x] * count. JS fill has no step, so the JS/TS solutions compute the bounds themselves and fill in a loop. Trap four: if a bound that is still negative after adding n is not clamped to 0, Python range(-3, 2) also paints the back cells through -3, -2, -1. fill -8 2 1 4 in example 1 prints 5 instead of 2. Trap five: range-fill functions take different arguments. Java Arrays.fill(board, from, to, x) and C++ std::fill(board.begin() + from, board.begin() + to, x) fill up to but not including the end index. The fourth argument of C# Array.Fill(board, x, start, count) is a count, not an end index, so calling Array.Fill(board, 5, 3, 4) for fill 3 -1 1 5 in example 1 runs past the array and throws ArgumentOutOfRangeException. Java Arrays.fill throws IllegalArgumentException when from > to, so check A < B first. A Python extended slice (s ≥ 2) accepts only a list of exactly the same length, so assigning [x] * (b - a) raises ValueError at fill 1 -1 2 9 in example 1 (assigning a list of another length to a slice with s = 1 silently changes the list's length).
Pythonmain.py · 29 lines
import syslines = sys.stdin.read().split("\n")n, q, v = map(int, lines[0].split())board = [v] * n # n칸을 모두 v로 채워 만든다out = []for line in lines[1:q + 1]: p = line.split() if p[0] == "get": i = int(p[1]) # Python은 음수 번호를 뒤에서부터 세지만, -n보다 작으면 IndexError라 먼저 묻는다 out.append(str(board[i]) if -n <= i < n else "out") elif p[0] == "set": i, x = int(p[1]), int(p[2]) if -n <= i < n: out.append(str(board[i])) board[i] = x else: out.append("out") else: a, b, s, x = map(int, p[1:]) # 조각 board[a:b:s]의 경계 규칙이 문제의 규칙과 같다(음수면 n을 더하고 0..n으로 가둔다) count = len(board[a:b:s]) board[a:b:s] = [x] * count # 건너뛰는 조각에는 길이가 똑같은 목록만 넣을 수 있다 out.append(str(count))out.append(" ".join(map(str, board)))print("\n".join(out))
JavaScriptmain.js · 35 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [n, q, v] = lines[0].trim().split(" ").map(Number);const board = new Array(n).fill(v); // n칸을 모두 v로 채워 만든다const out = [];// 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다const edge = (e) => Math.min(Math.max(e < 0 ? e + n : e, 0), n);for (const line of lines.slice(1, q + 1)) { const p = line.trim().split(" ").map((t, k) => (k === 0 ? t : Number(t))); if (p[0] === "get") { const value = board.at(p[1]); // at은 음수를 뒤에서 세고, 칸이 없으면 undefined out.push(value === undefined ? "out" : String(value)); } else if (p[0] === "set") { const [, i, x] = p; if (i >= -n && i < n) { const k = i < 0 ? i + n : i; // board[-1] = x는 칸이 아니라 "-1"이라는 속성을 만든다 out.push(String(board[k])); board[k] = x; } else { out.push("out"); } } else { const [, a, b, s, x] = p; const end = edge(b); let count = 0; for (let k = edge(a); k < end; k += s) { board[k] = x; count++; } out.push(String(count)); }}out.push(board.join(" "));console.log(out.join("\n"));
TypeScriptmain.ts · 37 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [n, q, v] = lines[0].trim().split(" ").map(Number);const board: number[] = new Array<number>(n).fill(v); // n칸을 모두 v로 채워 만든다const out: string[] = [];// 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다const edge = (e: number): number => Math.min(Math.max(e < 0 ? e + n : e, 0), n);for (const line of lines.slice(1, q + 1)) { const [cmd, ...rest] = line.trim().split(" "); const nums: number[] = rest.map(Number); if (cmd === "get") { const value: number | undefined = board.at(nums[0]); // 칸이 없으면 undefined out.push(value === undefined ? "out" : String(value)); } else if (cmd === "set") { const [i, x] = nums; if (i >= -n && i < n) { const k = i < 0 ? i + n : i; // board[-1]은 타입 검사를 지나지만 칸이 아니다 out.push(String(board[k])); board[k] = x; } else { out.push("out"); } } else { const [a, b, s, x] = nums; const end = edge(b); let count = 0; for (let k = edge(a); k < end; k += s) { board[k] = x; count++; } out.push(String(count)); }}out.push(board.join(" "));console.log(out.join("\n"));
JavaMain.java · 61 lines
import java.io.*;import java.util.*;public class Main { static int n; // 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다 static int edge(int e) { if (e < 0) e += n; return Math.min(Math.max(e, 0), n); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().trim().split(" "); n = Integer.parseInt(first[0]); int q = Integer.parseInt(first[1]); int v = Integer.parseInt(first[2]); int[] board = new int[n]; // 처음엔 모두 0이라 v로 다시 채운다 Arrays.fill(board, v); StringBuilder out = new StringBuilder(); for (int t = 0; t < q; t++) { String[] p = br.readLine().trim().split(" "); if (p[0].equals("get")) { int i = Integer.parseInt(p[1]); // 자바 배열은 음수 번호가 없어서 n을 더해 바꾼다 out.append(i >= -n && i < n ? String.valueOf(board[i < 0 ? i + n : i]) : "out").append('\n'); } else if (p[0].equals("set")) { int i = Integer.parseInt(p[1]), x = Integer.parseInt(p[2]); if (i >= -n && i < n) { int k = i < 0 ? i + n : i; out.append(board[k]).append('\n'); board[k] = x; } else { out.append("out\n"); } } else { int a = edge(Integer.parseInt(p[1])), b = edge(Integer.parseInt(p[2])); int s = Integer.parseInt(p[3]), x = Integer.parseInt(p[4]); int count = 0; if (s == 1) { if (a < b) { Arrays.fill(board, a, b, x); // fromIndex 포함, toIndex 제외 count = b - a; } } else { for (int k = a; k < b; k += s) { board[k] = x; count++; } } out.append(count).append('\n'); } } StringJoiner line = new StringJoiner(" "); for (int x : board) line.add(String.valueOf(x)); out.append(line).append('\n'); System.out.print(out); }}
C#Main.cs · 70 lines
using System;using System.Text;class Program{ static int n; // 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다 static int Edge(int e) => Math.Clamp(e < 0 ? e + n : e, 0, n); static void Main() { string[] first = Console.ReadLine()!.Trim().Split(' '); n = int.Parse(first[0]); int q = int.Parse(first[1]), v = int.Parse(first[2]); int[] board = new int[n]; Array.Fill(board, v); // n칸을 모두 v로 채운다 var sb = new StringBuilder(); for (int t = 0; t < q; t++) { string[] p = Console.ReadLine()!.Trim().Split(' '); if (p[0] == "get") { int i = int.Parse(p[1]); // board[^1]처럼 ^로 뒤에서 셀 수 있지만, 음수 번호는 n을 더해 바꾼다 sb.Append(i >= -n && i < n ? board[i < 0 ? i + n : i].ToString() : "out").Append('\n'); } else if (p[0] == "set") { int i = int.Parse(p[1]), x = int.Parse(p[2]); if (i >= -n && i < n) { int k = i < 0 ? i + n : i; sb.Append(board[k]).Append('\n'); board[k] = x; } else { sb.Append("out\n"); } } else { int a = Edge(int.Parse(p[1])), b = Edge(int.Parse(p[2])); int s = int.Parse(p[3]), x = int.Parse(p[4]); int count = 0; if (s == 1) { if (a < b) { Array.Fill(board, x, a, b - a); // 셋째 인자는 시작, 넷째는 '개수' count = b - a; } } else { for (int k = a; k < b; k += s) { board[k] = x; count++; } } sb.Append(count).Append('\n'); } } sb.Append(string.Join(" ", board)).Append('\n'); Console.Write(sb); }}
C++main.cpp · 54 lines
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { std::ios::sync_with_stdio(false); int n, q, v; std::cin >> n >> q >> v; std::vector<int> board(n, v); // n칸을 모두 v로 채워 만든다 // 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다 auto edge = [n](int e) { return std::clamp(e < 0 ? e + n : e, 0, n); }; for (int t = 0; t < q; t++) { std::string cmd; std::cin >> cmd; if (cmd == "get") { int i; std::cin >> i; // vector의 []는 번호를 검사하지 않으니 범위를 먼저 묻는다 if (i >= -n && i < n) std::cout << board[i < 0 ? i + n : i] << '\n'; else std::cout << "out\n"; } else if (cmd == "set") { int i, x; std::cin >> i >> x; if (i >= -n && i < n) { int k = i < 0 ? i + n : i; std::cout << board[k] << '\n'; board[k] = x; } else { std::cout << "out\n"; } } else { int a, b, s, x; std::cin >> a >> b >> s >> x; int from = edge(a), end = edge(b), count = 0; if (s == 1) { if (from < end) { std::fill(board.begin() + from, board.begin() + end, x); count = end - from; } } else { for (int k = from; k < end; k += s) { board[k] = x; count++; } } std::cout << count << '\n'; } } for (int k = 0; k < n; k++) std::cout << (k ? " " : "") << board[k]; std::cout << '\n';}
Cmain.c · 56 lines
#include <stdio.h>#include <string.h>#define MAXN 1000static int board[MAXN];static int n;/* 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다 */static int edge(int e) { if (e < 0) e += n; if (e < 0) return 0; if (e > n) return n; return e;}int main(void) { int q, v; if (scanf("%d %d %d", &n, &q, &v) != 3) return 0; /* memset은 바이트마다 같은 값을 써서 0이 아닌 v로는 못 채운다: 하나씩 넣는다 */ for (int k = 0; k < n; k++) board[k] = v; for (int t = 0; t < q; t++) { char cmd[8]; scanf("%7s", cmd); if (strcmp(cmd, "get") == 0) { int i; scanf("%d", &i); /* C 배열의 음수 번호는 배열 밖이다: 범위를 먼저 묻고 n을 더한다 */ if (i >= -n && i < n) printf("%d\n", board[i < 0 ? i + n : i]); else puts("out"); } else if (strcmp(cmd, "set") == 0) { int i, x; scanf("%d %d", &i, &x); if (i >= -n && i < n) { int k = i < 0 ? i + n : i; printf("%d\n", board[k]); board[k] = x; } else { puts("out"); } } else { int a, b, s, x; scanf("%d %d %d %d", &a, &b, &s, &x); int end = edge(b), count = 0; for (int k = edge(a); k < end; k += s) { board[k] = x; count++; } printf("%d\n", count); } } for (int k = 0; k < n; k++) printf(k ? " %d" : "%d", board[k]); putchar('\n'); return 0;}
Craykonmain.cray · 70 lines
first = Console.ReadLine().Trim().Split();n = first[0].ToNumber();q = first[1].ToNumber();v = first[2].ToNumber();board = NewList(n, v); // n칸을 모두 v로 채워 만든다// 경계: 음수면 n을 더하고, 그래도 0..n 밖이면 가까운 끝으로 가둔다경계(e){ if (e < 0) { e = e + n; } return Mathf.Min(Mathf.Max(e, 0), n);}for (t = 0; t < q; t++){ p = Console.ReadLine().Trim().Split(); cmd = p[0]; if (cmd == "get") { i = p[1].ToNumber(); // 크레이콘 목록은 음수 번호를 읽으면 실행 오류라 n을 더해 바꾼다 if (i >= -n && i < n) { if (i < 0) { i = i + n; } Console.WriteLine(board[i]); } else { Console.WriteLine("out"); } } else if (cmd == "set") { i = p[1].ToNumber(); x = p[2].ToNumber(); if (i >= -n && i < n) { if (i < 0) { i = i + n; } Console.WriteLine(board[i]); board[i] = x; } else { Console.WriteLine("out"); } } else { end = 경계(p[2].ToNumber()); s = p[3].ToNumber(); x = p[4].ToNumber(); count = 0; for (k = 경계(p[1].ToNumber()); k < end; k = k + s) { board[k] = x; count++; } Console.WriteLine(count); }}Console.WriteLine(board.Join(" "));