QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#736516#9631. Median ReplacementcaojcWA 0ms3716kbC++203.7kb2024-11-12 11:32:002024-11-12 11:32:00

Judging History

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

  • [2024-11-12 11:32:00]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3716kb
  • [2024-11-12 11:32:00]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pii pair<int, int>
#define lll __int128

#define db(args...){\
    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << '\n';}
void err(istream_iterator<string> it){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << ' ';
    err(++it, args...);
}

const int mod = 1e9 + 7;
void add(ll &x, ll y) {
    x += y;
    if (x >= mod) x -= mod;
}
ll ksm(ll a, ll b = mod - 2) {
    ll res = 1;
    while(b) {
        if(b & 1) res = res * a % mod;
        a = a * a % mod, b /= 2;
    }
    return res;
} 
// 拉格朗日差值,给定点对求一个点值,n方
ll lagrange(vector<ll> x, vector<ll> y, int k) {
    int n = x.size();
    assert(n == y.size());
    ll res = 0;
    for (int i = 0; i < n; i++) {
        ll resi = y[i];
        ll A = 1, B = 1;
        for (int j = 0; j < n; j++) if (i ^ j) {
            A = A * (k - x[j] + mod) % mod;
            B = B * (x[i] - x[j] + mod) % mod;
        }
        resi = resi * A % mod * ksm(B) % mod;
        res += resi;
    }
    return res % mod;
}
void solve() {
    int n;
    cin >> n;

    vector<int> l(n + 1), r(n + 1);
    const int M = 1e9;
    vector<int> b = {0};
    ll all = 1;
    for (int i = 1; i <= n; i++) cin >> l[i];
    for (int i = 1; i <= n; i++) cin >> r[i];

    for (int i = 1; i <= n; i++) {
        all = all * (r[i] - l[i] + 1) % mod;
        b.push_back(l[i] - 1);
        b.push_back(r[i]);
    }
    sort(b.begin(), b.end()); b.erase(unique(b.begin(), b.end()), b.end());

    auto F = [&] (int x) {
        auto get = [&] (int i, int v) {
            ll res;
            if (v == 0) {
                res = x - l[i];
            } else {
                res = r[i] - x + 1;
            }
            res = min(r[i] - l[i] + 1ll, res);
            res = max(0ll, res);
            return res;
        };

        #define a3 array<ll, 3>
        vector<a3> f(n + 1, {0, 0, 0});
        f[0][0] = 1;

        for (int i = 1; i <= n; i++) {
            for (int x = 0; x < 3; x++) {
                for (int v = 0; v < 2; v++) {
                    int nx = x << 1 | v;
                    if (__builtin_popcount(nx) > 1) continue;
                    nx %= 4;
                    add(f[i][nx], f[i - 1][x] * get(i, v) % mod);
                }
            }
        }
        ll res = 0;
        for (int i = 0; i < 3; i++) res += f[n][i];
        return res % mod;
    };

    const int bd = n + 2;
    auto wk = [&] (int L, int R)->ll {
        if (R - L + 1 <= bd) {
            ll res = 0;
            for (int v = L; v <= R; v++) {
                res += F(v);
            }
            res %= mod;
            res = (all - res + mod) % mod; 
            return res;
        } else {
            vector<ll> x, y;
            ll sum = 0;
            for (int v = L; v < L + n + 2; v++) {
                ll B = (all - F(v) + mod) % mod;
                add(sum, B);
                x.push_back(v - L + 1);
                y.push_back(sum);
            }
            return lagrange(x, y, R - L + 1);
        }
    };

    ll res = 0;
    for (int i = 1; i < b.size(); i++) {
        int L = b[i - 1] + 1, R = b[i];
        res += wk(L, R);
    }
    res %= mod;
    cout << res << '\n';
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}

/*
g++ 1.cpp -std=c++20 -o 1 && ./1 < in.txt > out.txt
 */



详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3716kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
999999967
74
265
134
999999970
120
240
0
999999998

result:

wrong answer 2nd lines differ - expected: '170', found: '999999967'