D - AtCoder Wallpaper(求图形面积)

思路:求f(c, d) + f(a, b) - f(a, d) - f(c, b);

代码:

int f(int x, int y){
    if(y % 2 == 0){
        y = y / 2;
        int ans = y * (x / 4) * 8;
        x %= 4;
        if(x == 1){
            ans += y * 3;
        }else if(x == 2){
            ans += y * 6;
        }else if(x == 3){
            ans += y * 7;
        }
        return ans;
    }else{
        y /= 2;
        int ans = y * (x / 4) * 8 + 2 * (x / 4) * 2;
        x %= 4;
        if(x == 1){
            ans += y * 3 + 2;
        }else if(x == 2){
            ans += y * 6 + 3;
        }else if(x == 3){
            ans += y * 7 + 3;
        }
        return ans;
    }
}


void solve(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if(a < 0){
        int cnt = (-a + 3) / 4;
        a += cnt * 4;
        c += cnt * 4; 
    }
    if(b < 0){
        int cnt = (-b + 3) / 4;
        b += cnt * 4;
        d += cnt * 4;
    }
    cout << f(c, d) + f(a, b) - f(a, d) - f(c, b);
}

 

posted on 2024-05-22 16:43  临江柔  阅读(11)  评论(0编辑  收藏  举报