QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182696#6673. Be Careful 2ucup-team004WA 1ms3480kbC++207.8kb2023-09-18 13:49:322023-09-18 13:49:33

Judging History

你现在查看的是最新测评结果

  • [2023-09-18 13:49:33]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3480kb
  • [2023-09-18 13:49:32]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}
template<i64 P>
struct MLong {
    i64 x;
    constexpr MLong() : x{} {}
    constexpr MLong(i64 x) : x{norm(x % getMod())} {}
    
    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    explicit constexpr operator i64() const {
        return x;
    }
    constexpr MLong operator-() const {
        MLong res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MLong inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MLong &operator*=(MLong rhs) & {
        x = mul(x, rhs.x, getMod());
        return *this;
    }
    constexpr MLong &operator+=(MLong rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MLong &operator-=(MLong rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MLong &operator/=(MLong rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MLong operator*(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MLong operator+(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MLong operator-(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MLong operator/(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
        i64 v;
        is >> v;
        a = MLong(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MLong lhs, MLong rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MLong lhs, MLong rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
i64 MLong<0LL>::Mod = i64(1E18) + 9;

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}
    
    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
int MInt<0>::Mod = 998244353;

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m, k;
    std::cin >> n >> m >> k;
    
    std::vector<int> x(k), y(k);
    for (int i = 0; i < k; i++) {
        std::cin >> x[i] >> y[i];
        if (n > m) {
            std::swap(x[i], y[i]);
        }
    }
    if (n > m) {
        std::swap(n, m);
    }
    
    std::vector<int> px(k), py(k);
    std::iota(px.begin(), px.end(), 0);
    std::sort(px.begin(), px.end(),
        [&](int i, int j) {
            return x[i] < x[j];
        });
    std::iota(py.begin(), py.end(), 0);
    std::sort(py.begin(), py.end(),
        [&](int i, int j) {
            return y[i] < y[j];
        });
    std::vector<int> qx(k), qy(k);
    for (int i = 0; i < k; i++) {
        qx[px[i]] = i;
        qy[py[i]] = i;
    }
    
    Z ans = Z(n) * (1 + n) * (2 + n) * (Z(-4) + n + Z(3) * n * n - Z(5) * m * (1 + n)) / -60;
    
    auto get = [&](int x1, int y1, int x2, int y2) {
        Z res = 0;
        int bx = std::min(x1, n - x2);
        int dx = x2 - x1 + 1;
        int by = std::min(y1, n - y2);
        int dy = y2 - y1 + 1;
        for (int d = 1; d <= n; d++) {
            res += Z(d) * d * std::max(0, std::min({bx, d - dx, n + 1 - d}))
                * std::max(0, std::min({by, d - dy, m + 1 - d}));
        }
        return res;
    };
    
    for (int i = 0; i < k; i++) {
        ans -= get(x[i], y[i], x[i], y[i]);
    }
    for (int i = 0; i < k; i++) {
        std::vector<int> l(k, -1), r(k, -1);
        int lst = -1;
        for (int j = 0; j < k; j++) {
            if (qx[py[j]] >= i) {
                if (lst != -1) {
                    l[py[j]] = lst;
                    r[lst] = py[j];
                }
                lst = py[j];
            }
        }
        for (int j = k - 1; j > i; j--) {
            int a = px[i], b = px[j];
            int x1 = x[a], x2 = x[b];
            if (qy[a] > qy[b]) {
                std::swap(a, b);
            }
            if (r[a] == b) {
                ans += get(x1, y[a], x2, y[b]);
                if (l[a] != -1) {
                    ans -= get(x1, y[l[a]], x2, y[b]);
                }
                if (r[b] != -1) {
                    ans -= get(x1, y[a], x2, y[r[b]]);
                }
                if (l[a] != -1 && r[b] != -1) {
                    ans -= get(x1, y[l[a]], x2, y[r[b]]);
                }
            }
        }
    }
    
    std::cout << ans << "\n";
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3472kb

input:

3 3 1
2 2

output:

21

result:

ok 1 number(s): "21"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3480kb

input:

5 5 2
2 1
2 4

output:

126

result:

ok 1 number(s): "126"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3444kb

input:

6 6 5
4 1
3 2
2 4
1 5
5 3

output:

998244151

result:

wrong answer 1st numbers differ - expected: '161', found: '998244151'