Submission details
Task:Maalaus
Sender:NicholasAhman
Submission time:2025-11-03 19:09:46 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void draw_horizontal(size_t, int)':
input/code.cpp:25:35: error: 'x' was not declared in this scope
   25 |         colors[output[i * width + x]]--;
      |                                   ^
input/code.cpp: In function 'int main()':
input/code.cpp:34:12: warning: unused variable 'len' [-Wunused-variable]
   34 |     size_t len = read(0, input, 1024*1024*32);
      |            ^~~

Code

#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int *output;
int *colors;

size_t width;
size_t height;
size_t color_count;

void draw_vertical(size_t x, int color) {
    for (size_t i = 0; i < height; i++) {
        colors[output[i * width + x]]--;
        colors[color]++;

        output[i * width + x] = color;
    }
}

void draw_horizontal(size_t y, int color) {
    for (size_t i = 0; i < width; i++) {
        colors[output[i * width + x]]--;
        colors[color]++;

        output[y * width + i] = color;
    }
}

int main(void) {
    char *input = (char *)malloc(1024*1024*32);
    size_t len = read(0, input, 1024*1024*32);

    char *ptr = input;

    size_t next_space = strchr(input, ' ') - input;
    input[next_space] = '\0';

    height = atoi(input);
    ptr += next_space + 1;

    next_space = strchr(ptr, ' ') - ptr;
    ptr[next_space] = '\0';
    width = atoi(ptr);
    ptr += next_space + 1;

    next_space = strchr(ptr, ' ') - ptr;
    ptr[next_space] = '\0';
    color_count = atoi(ptr);
    ptr += next_space + 1;

    next_space = strchr(ptr, ' ') - ptr;
    ptr[next_space] = '\0';
    size_t operations = atoi(ptr);
    ptr += next_space + 1;

    colors = (int *)malloc(color_count * sizeof(int) + sizeof(int));
    output = (int *)malloc(height * width * sizeof(int));

    for (size_t i = 0; i < operations; i++) {
        int rc = ptr[0];
        ptr += 2;

        next_space = strchr(ptr, ' ') - ptr;
        ptr[next_space] = '\0';
        size_t n = atoi(ptr);
        ptr += next_space + 1;

        next_space = strchr(ptr, '\n') - ptr;
        ptr[next_space] = '\0';
        size_t color = atoi(ptr);
        ptr += next_space + 1;

        if (rc == 'R') {
            draw_vertical(n, color);
        } else {
            draw_horizontal(n, color);
        }
    }

    for (size_t i = 0; i < color_count + 1; i++) {
        printf("%d ", colors[i]);
    }

    free(input);
    free(colors);
    free(output);

}