이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct plant {
    int x, y, a, b;
};
void printans(int total, int area) {
    int gcd = __gcd(total, area);
    total /= gcd;
    area /= gcd;
    if(area == 2) {
        cout << total / 2 + 1 << "\n";
        return;
    }
    cout << total / area + ((total % area <= area / 2) ? 0 : 1) << "\n";
}
int w, h;
int n;
int q;
vector<vector<int> > hori;
vector<vector<int> > vert;
vector<vector<int> > suff;
vector<vector<int> > rad;
vector<vector<int> > pref;
vector<plant> plants;
void solve() {
    cin >> w >> h;
    hori.assign(1 + w + 1, vector<int> (1 + h + 1, 0));
    vert.assign(1 + w + 1, vector<int> (1 + h + 1, 0));
    suff.assign(1 + w + 1, vector<int> (1 + h + 1, 0));
    rad.assign(1 + w + 1, vector<int> (1 + h + 1, 0));
    pref.assign(1 + w + 1, vector<int> (1 + h + 1, 0));
    cin >> n;
    plants.resize(n);
    for(int i = 0; i < n; i++) {
        cin >> plants[i].x >> plants[i].y >> plants[i].a >> plants[i].b;
    }
    for(plant p : plants) {
        int x = p.x, y = p.y, a = p.a, b = p.b;
        int x1 = x - a / b, x2 = x + a / b, y1 = y - a / b, y2 = y + a / b;
        suff[x2][y2] += a % b;
        suff[max(x1 - 1, 0ll)][y2] -= a % b;
        suff[x2][max(y1 - 1, 0ll)] -= a % b;
        suff[max(x1 - 1, 0ll)][max(y1 - 1, 0ll)] += a % b;
        a -= a % b;
        x2--;
        y2--;
        vert[x2][y2] += b;
        vert[max(x1 - 1, 0ll)][max(y1 - 1, 0ll)] -= b;
        hori[x2][y1] -= b;
        hori[max(x1 - 1, 0ll)][y2 + 1] += b;
    }
    for(int i = w; i >= 1; i--) {
        for(int j = 1; j <= h; j++) {
            vert[i][j] += vert[i + 1][j + 1];
            hori[i][j] += hori[i + 1][j - 1];
            suff[i][j] += hori[i][j] + vert[i][j];
        }
    }
    for(int i = w; i >= 1; i--) {
        for(int j = h; j >= 1; j--) {
            rad[i][j] = suff[i][j] + rad[i + 1][j] + rad[i][j + 1] - rad[i + 1][j + 1];
        }
    }
    for(int i = 1; i <= w; i++) {
        for(int j = 1; j <= h; j++) {
            pref[i][j] = rad[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
        }
    }
    /*for(int i = 1; i <= w; i++) {
        for(int j = 1; j <= h; j++) {
            cout << hori[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "----------\n";
    for(int i = 1; i <= w; i++) {
        for(int j = 1; j <= h; j++) {
            cout << vert[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "----------\n";
    for(int i = 1; i <= w; i++) {
        for(int j = 1; j <= h; j++) {
            cout << suff[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "----------\n";
    for(int i = 1; i <= w; i++) {
        for(int j = 1; j <= h; j++) {
            cout << rad[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "----------\n";*/
    cin >> q;
    for(int i = 0; i < q; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        int total = pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
        int area = (y2 - y1 + 1) * (x2 - x1 + 1);
        printans(total, area);
    }
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |