結果

問題 No.755 Zero-Sum Rectangle
ユーザー aa
提出日時 2019-11-06 03:46:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 5,408 bytes
コンパイル時間 1,745 ms
コンパイル使用メモリ 171,608 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-13 02:34:04
合計ジャッジ時間 4,295 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 226 ms
4,352 KB
testcase_02 AC 120 ms
4,348 KB
testcase_03 AC 108 ms
4,376 KB
testcase_04 AC 102 ms
4,348 KB
testcase_05 AC 98 ms
4,348 KB
testcase_06 AC 95 ms
4,352 KB
testcase_07 AC 94 ms
4,348 KB
testcase_08 AC 95 ms
4,352 KB
testcase_09 AC 97 ms
4,352 KB
testcase_10 AC 97 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
evil_1 AC 121 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include "bits/stdc++.h"

using namespace std;

//------------------------------- Libraries --------------------------------//

template <int p>
struct Modint
{
    int value;

    Modint() : value(0) {}
    Modint(long x) : value(x >= 0 ? x % p : (p + x % p) % p) {}

    inline Modint &operator+=(const Modint &b)
    {
        if ((this->value += b.value) >= p)
            this->value -= p;
        return (*this);
    }
    inline Modint &operator-=(const Modint &b)
    {
        if ((this->value += p - b.value) >= p)
            this->value -= p;
        return (*this);
    }
    inline Modint &operator*=(const Modint &b)
    {
        this->value = (int)((1LL * this->value * b.value) % p);
        return (*this);
    }
    inline Modint &operator/=(const Modint &b)
    {
        (*this) *= b.inverse();
        return (*this);
    }

    Modint operator+(const Modint &b) const { return Modint(*this) += b; }
    Modint operator-(const Modint &b) const { return Modint(*this) -= b; }
    Modint operator*(const Modint &b) const { return Modint(*this) *= b; }
    Modint operator/(const Modint &b) const { return Modint(*this) /= b; }

    inline Modint &operator++(int) { return (*this) += 1; }
    inline Modint &operator--(int) { return (*this) -= 1; }

    inline bool operator==(const Modint &b) const
    {
        return this->value == b.value;
    }
    inline bool operator!=(const Modint &b) const
    {
        return this->value != b.value;
    }
    inline bool operator<(const Modint &b) const
    {
        return this->value < b.value;
    }
    inline bool operator<=(const Modint &b) const
    {
        return this->value <= b.value;
    }
    inline bool operator>(const Modint &b) const
    {
        return this->value > b.value;
    }
    inline bool operator>=(const Modint &b) const
    {
        return this->value >= b.value;
    }

    // requires that "this->value and p are co-prime"
    // a_i * v + a_(i+1) * p = r_i
    // r_i = r_(i+1) * q_(i+1) * r_(i+2)
    // q == 1 (i > 1)
    // reference: https://atcoder.jp/contests/agc026/submissions/2845729
    // (line:93)
    inline Modint inverse() const
    {
        assert(this->value != 0);
        int r0 = p, r1 = this->value, a0 = 0, a1 = 1;
        while (r1)
        {
            int q = r0 / r1;
            r0 -= q * r1;
            swap(r0, r1);
            a0 -= q * a1;
            swap(a0, a1);
        }
        return Modint(a0);
    }

    friend istream &operator>>(istream &is, Modint<p> &a)
    {
        long t;
        is >> t;
        a = Modint<p>(t);
        return is;
    }
    friend ostream &operator<<(ostream &os, const Modint<p> &a)
    {
        return os << a.value;
    }
};

/*
verified @ https://atcoder.jp/contests/abc034/submissions/4316971
*/

const int MOD = 1e9 + 7;

using Int = Modint<MOD>;

//------------------------------- Type Names -------------------------------//

using i64 = int_fast64_t;

using seika = string;
//akari : 1D, yukari : 2D, maki : 3D vector
template <class kizuna>
using akari = vector<kizuna>;
template <class yuzuki>
using yukari = akari<akari<yuzuki>>;
template <class tsurumaki>
using maki = akari<yukari<tsurumaki>>;
//akane : ascending order, aoi : decending order
template <class kotonoha>
using akane = priority_queue<kotonoha, akari<kotonoha>, greater<kotonoha>>;
template <class kotonoha>
using aoi = priority_queue<kotonoha>;

//------------------------------- Dubug Functions ---------------------------//
inline void print()
{
    cout << endl;
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest)
{
    cout << first << ' ';
    print(rest...);
}
//------------------------------- Solver ------------------------------------//

void solve()
{
    int n, m;
    cin >> n >> m;
    yukari<i64> grid(m + 1, akari<i64>(m + 1));
    for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> grid[i + 1][j + 1];
        }
    for (int i = 0; i <= m; i++)
        for (int j = 0; j < m; j++)
        {
            grid[i][j + 1] += grid[i][j];
        }
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= m; j++)
        {
            grid[i + 1][j] += grid[i][j];
        }
    yukari<i64> imos(m + 1, akari<i64>(m + 1));
    for (int lx = 0; lx < m; lx++)
        for (int ly = 0; ly < m; ly++)
            for (int rx = lx; rx < m; rx++)
                for (int ry = ly; ry < m; ry++)
                {
                    i64 sum = 0;
                    sum += grid[ly][lx];
                    sum -= grid[ry + 1][lx];
                    sum -= grid[ly][rx + 1];
                    sum += grid[ry + 1][rx + 1];
                    if (sum == 0)
                    {
                        imos[lx][ly]++;
                        imos[lx][ry + 1]--;
                        imos[rx + 1][ly]--;
                        imos[rx + 1][ry + 1]++;
                    }
                }
    for (int i = 0; i <= m; i++)
        for (int j = 0; j < m; j++)
        {
            imos[i][j + 1] += imos[i][j];
        }
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= m; j++)
        {
            imos[i + 1][j] += imos[i][j];
        }
    while (n--)
    {
        int x, y;
        cin >> x >> y;
        x--, y--;
        cout << imos[y][x] << endl;
    }
}

int main()
{
    solve();
    return 0;
}
0