1 / 2
Mar 21

Is it possible to somehow see what tests exactly my code does not pass? I am pretty sure my solution is correct and when I check it on my own, the results are correct in all cases. How can I check what fails?

#include <stdio.h>
#include <stdbool.h>

int min(int a, int b) {
    return (a < b) ? a : b;
}

int max(int a, int b) {
    return (a > b) ? a : b;
}

int count_red_squares(int xld, int yld, int xpg, int ypg) {
    int red_count = 0;
    
    for (int y = yld; y < ypg; y++) {
        if (y % 2 == 1) {
            int max_x = min(y, xpg - 1);
            if (max_x >= xld) {
                red_count += max_x - xld + 1;
            }
        }
        
        int start_x = max(y + 1, xld);
        if (start_x < xpg) {
            start_x += (start_x % 2 == 0);
            
            if (start_x < xpg) {
                int count_odds = (xpg - start_x + 1) / 2;
                red_count += count_odds;
            }
        }
    }
    
    return red_count;
}


bool check_input_validity(int xld, int yld, int xpg, int ypg) {
    if (xld < 0 || yld < 0 || xpg < 0 || ypg < 0) {
        printf("xld, yld, xpg and ypg should be positive integers\n");
        return false;
    }
    if (xld > xpg || yld > ypg) {
        printf("xld and yld should be less than xpg and ypg\n");
        return false;
    }
    if (xld > 1000000 || yld > 1000000 || xpg > 1000000 || ypg > 1000000) {
        printf("xld, yld, xpg and ypg should be less than 1000000\n");
        return false;
    }
    return true;
}

int main() {
    int xld, yld, xpg, ypg;
    bool is_input_valid = false;
    
    while (!is_input_valid) {
        scanf("%d %d %d %d", &xld, &yld, &xpg, &ypg);
        is_input_valid = check_input_validity(xld, yld, xpg, ypg);
    }
    
    printf("%d", count_red_squares(xld, yld, xpg, ypg));
    
    return 0;
}

You can’t check unless the problem setter decides to share with you.

0 ≤ XLD, XPG ≤ 1000000, 0 ≤ YLD, YPG ≤ 1000000

I’ve not solved this, but it looks to me like these constraints are carefully written so that they do not say XLD ≤ XPG and YLN ≤ YBG. If XLD > XBG, or YLD > YBG your code will write an error message, which will result in wrong answer.

Suggested Topics

Want to read more? Browse other topics in Online Judge System or view latest topics.