Submission #540384

# Submission time Handle Problem Language Result Execution time Memory
540384 2022-03-20T08:49:14 Z alextodoran Nuclearia (CEOI15_nuclearia) C++17
100 / 100
990 ms 538532 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

#pragma GCC optimize ("O3,Ofast,fast-math,unroll-loops")
#pragma GCC target ("avx2,tune=native")

using namespace std;

typedef long long ll;

const int N_MAX = 2500000;

const int BUFFER_SIZE = 40000;

char buffer[BUFFER_SIZE];
int bpos = BUFFER_SIZE - 1;

char read_char ()
{
    bpos++;
    if(bpos == BUFFER_SIZE)
    {
        fread(buffer, sizeof(char), BUFFER_SIZE, stdin);
        bpos = 0;
    }
    return buffer[bpos];
}

bool isDigit[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int read_int ()
{
    int ans = 0;
    char c;
    while(isDigit[c = read_char()])
    {
        ans = ans * 10 + c - '0';
    }
    return ans;
}

ll** mat;
ll** UL;
ll** UR;
ll** DL;
ll** DR;
ll U[N_MAX + 2];
ll D[N_MAX + 2];
ll L[N_MAX + 2];
ll R[N_MAX + 2];

int N, M;

void addUL (int x, int y, int len, int val) {
    mat[1][1] += (ll) val * max(0, len - max(x, y));
    DR[x][y] += val;
    if (x - len + 1 >= 1 && y - len + 1 >= 1) {
        DR[x - len][y - len] -= val;
    } else {
        int mn = min(x, y) - 1;
        x -= mn; y -= mn;
        DR[x - 1][y - 1] -= val;
        len -= mn;
        if (y == 1) {
            D[x - 1] += val;
            if (x - len >= 1) {
                D[x - len] -= val;
            }
        } else if (x == 1) {
            R[y - 1] += val;
            if (y - len >= 1) {
                R[y - len] -= val;
            }
        }
    }
}
void addUR (int x, int y, int len, int val) {
    DL[x][y] += val;
    if (x - len + 1 >= 1 && y + len - 1 <= M) {
        DL[x - len][y + len] -= val;
    } else {
        int mn = min(x, M - y + 1) - 1;
        x -= mn; y += mn;
        DL[x - 1][y + 1] -= val;
        len -= mn;
        if (x == 1) {
            L[y + 1] += val;
            if (y + len <= M) {
                L[y + len] -= val;
            }
        }
    }
}
void addDL (int x, int y, int len, int val) {
    UR[x][y] += val;
    if (x + len - 1 <= N && y - len + 1 >= 1) {
        UR[x + len][y - len] -= val;
    } else {
        int mn = min(N - x + 1, y) - 1;
        x += mn; y -= mn;
        UR[x + 1][y - 1] -= val;
        len -= mn;
        if (y == 1) {
            U[x + 1] += val;
            if (x + len <= N) {
                U[x + len] -= val;
            }
        }
    }
}
void addDR (int x, int y, int len, int val) {
    UL[x][y] += val;
    if (x + len <= N && y + len <= M) {
        UL[x + len][y + len] -= val;
    }
}
void add (int x, int y, int len, int val) {
    int x1 = x - len + 1, y1 = y - len + 1;
    int x2 = x + len - 1, y2 = y + len - 1;
    x1 = max(1, x1); y1 = max(1, y1);
    x2 = min(N, x2); y2 = min(M, y2);
    mat[x1][y1] += val;
    mat[x1][y2 + 1] -= val;
    mat[x2 + 1][y1] -= val;
    mat[x2 + 1][y2 + 1] += val;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    N = read_int();
    M = read_int();
    mat = new ll*[N + 2];
    UL = new ll*[N + 2];
    UR = new ll*[N + 2];
    DL = new ll*[N + 2];
    DR = new ll*[N + 2];
    for (int i = 0; i <= N + 1; i++) {
        mat[i] = new ll[M + 2];
        UL[i] = new ll[M + 2];
        UR[i] = new ll[M + 2];
        DL[i] = new ll[M + 2];
        DR[i] = new ll[M + 2];
        for (int j = 0; j <= M + 1; j++) {
            mat[i][j] = 0;
            UL[i][j] = 0;
            UR[i][j] = 0;
            DL[i][j] = 0;
            DR[i][j] = 0;
        }
    }

    int K;
    K = read_int();
    while (K--) {
        int x, y, a, b;
        x = read_int();
        y = read_int();
        a = read_int();
        b = read_int();
        int len = a / b;
        addUL(x, y, len, +b);
        addUR(x, y + 1, len, -b);
        addDL(x + 1, y, len, -b);
        addDR(x + 1, y + 1, len, +b);
        add(x, y, len + 1, a % b);
    }

    for (int i = 1; i <= N; i++) {
        U[i] += U[i - 1];
        mat[i][1] += U[i];
    }
    for (int i = N; i >= 1; i--) {
        D[i] += D[i + 1];
        mat[i][1] += D[i];
    }
    for (int j = 1; j <= M; j++) {
        L[j] += L[j - 1];
        mat[1][j] += L[j];
    }
    for (int j = M; j >= 1; j--) {
        R[j] += R[j + 1];
        mat[1][j] += R[j];
    }
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            UL[i][j] += UL[i - 1][j - 1];
            UR[i][j] += UR[i - 1][j + 1];
        }
    }
    for (int i = N; i >= 1; i--) {
        for (int j = M; j >= 1; j--) {
            DL[i][j] += DL[i + 1][j - 1];
            DR[i][j] += DR[i + 1][j + 1];
        }
    }

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            mat[i][j] += UL[i][j];
            mat[i][j] += UR[i][j];
            mat[i][j] += DL[i][j];
            mat[i][j] += DR[i][j];
            mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
        }
    }
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
        }
    }

    int Q = read_int();
    while (Q--) {
        int x1, y1, x2, y2;
        x1 = read_int();
        y1 = read_int();
        x2 = read_int();
        y2 = read_int();
        ll sum = mat[x2][y2] - mat[x1 - 1][y2] - mat[x2][y1 - 1] + mat[x1 - 1][y1 - 1];
        ll area = (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
        ll answer = sum / area;
        if (sum % area >= (area + 1) / 2 && area != 1) {
            answer++;
        }
        cout << answer << "\n";
    }

    return 0;
}

Compilation message

nuclearia.cpp: In function 'int read_int()':
nuclearia.cpp:58:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   58 |     while(isDigit[c = read_char()])
      |                   ~~^~~~~~~~~~~~~
nuclearia.cpp: In function 'char read_char()':
nuclearia.cpp:30:14: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         fread(buffer, sizeof(char), BUFFER_SIZE, stdin);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 763 ms 528780 KB Output is correct
2 Correct 29 ms 4640 KB Output is correct
3 Correct 24 ms 3912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 714 ms 528716 KB Output is correct
2 Correct 28 ms 4684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 99272 KB Output is correct
2 Correct 27 ms 4400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 110316 KB Output is correct
2 Correct 30 ms 4636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 826 ms 534720 KB Output is correct
2 Correct 32 ms 5436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 217548 KB Output is correct
2 Correct 27 ms 4644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 104476 KB Output is correct
2 Correct 28 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 150984 KB Output is correct
2 Correct 28 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 990 ms 538532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 987 ms 538476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 108720 KB Output is correct
2 Correct 196 ms 108228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 108236 KB Output is correct
2 Correct 188 ms 108184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 111800 KB Output is correct
2 Correct 198 ms 109748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 108668 KB Output is correct
2 Correct 314 ms 342800 KB Output is correct