제출 #1325512

#제출 시각아이디문제언어결과실행 시간메모리
1325512zwezdinvIntergalactic ship (IZhO19_xorsum)C++20
100 / 100
1437 ms21132 KiB
#include <bits/stdc++.h>

const int mod = 1e9 + 7;
int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
    return a >= b ? a - b : a + mod - b;
}
int mul(int a, int b) {
    return 1ll * a * b % mod;
}

const int N = 1001;
int n, v[N], mask[4][N][N];

void init() {
    for (int c = 0; c < 4; ++c) {
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) {
                mask[c][i][j] = 0;
            }
        }
    }
}
void apply(int c, int lx, int rx, int ly, int ry) {
    if (lx > rx || ly > ry) return;
    ++rx, ++ry;
    ++mask[c][lx][ly], ++mask[c][rx][ry];
    --mask[c][lx][ry], --mask[c][rx][ly];
}
void build() {
    for (int c = 0; c < 4; ++c) {
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) {
                if (i > 0) mask[c][i][j] += mask[c][i - 1][j];
                if (j > 0) mask[c][i][j] += mask[c][i][j - 1];
                if (i > 0 && j > 0) mask[c][i][j] -= mask[c][i - 1][j - 1];
            }
        }
    }
}

const int Q = 1e6;
int dp[4], ndp[4], pw2[Q];

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    pw2[0] = 1;
    for (int i = 1; i < Q; ++i) pw2[i] = add(pw2[i - 1], pw2[i - 1]);

    std::cin >> n;
    for (int i = 0; i < n; ++i) std::cin >> v[i];
    int q;
    std::cin >> q;
    std::vector<std::array<int, 3>> ques(q);
    for (auto& [l, r, x] : ques) {
        std::cin >> l >> r >> x;
        --l, --r;
    }
    int ans = 0;
    for (int a = 0; a < 7; ++a) {
        for (int b = a; b < 7; ++b) {
            init();
            for (auto [l, r, x] : ques) {
                int ca = x >> a & 1, cb = x >> b & 1;
                apply(0, 0, l - 1, 0, l - 1);
                apply(ca, l, r, 0, l - 1);
                apply(0, r + 1, n - 1, 0, l - 1);
                apply(2 * cb, 0, l - 1, l, r);
                apply(ca + 2 * cb, l, r, l, r);
                apply(2 * cb, r + 1, n - 1, l, r);
                apply(0, 0, l - 1, r + 1, n - 1);
                apply(ca, l, r, r + 1, n - 1);
                apply(0, r + 1, n - 1, r + 1, n - 1);
            }
            build();
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    int ca = v[i] >> a & 1, cb = v[j] >> b & 1;
                    std::fill(dp, dp + 4, 0);
                    dp[ca + 2 * cb] = 1;
                    int pw = 0;
                    for (int k = 0; k < 4; ++k) {
                        int cnt = mask[k][i][j];
                        if (cnt == 0) continue;
                        pw += cnt - 1;
                        std::copy(dp, dp + 4, ndp);
                        for (int c = 0; c < 4; ++c) {
                            ndp[c] = add(ndp[c], dp[c ^ k]);
                        }
                        std::copy(ndp, ndp + 4, dp);
                    }
                    int coef = mul(mul(std::min(i, j) + 1, n - std::max(i, j)), 1 << (a + b));
                    coef = mul(coef, pw2[pw]);
                    if (a != b) coef = mul(coef, 2);
                    ans = add(ans, mul(dp[3], coef));
                }
            }
        }
    }
    std::cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...