QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#617274#8237. Sugar Sweet IINCl3TL 69ms6892kbC++205.5kb2024-10-06 14:41:172024-10-06 14:41:18

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-06 14:41:18]
  • 评测
  • 测评结果:TL
  • 用时:69ms
  • 内存:6892kb
  • [2024-10-06 14:41:17]
  • 提交

answer

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
constexpr int N = 5e5 + 10;
constexpr int inf = 1e9 + 10;
constexpr ll llinf = 1e18 + 10;
constexpr int mod = 1e9 + 7;
void debug() {
    std::cerr << "\n";
}
template<typename T, typename... Args>
void debug(T val, const Args&... args) {
    std::cerr << val << " ";
    debug(args...);
}
template<typename T>
void debug(const std::vector<T>& vec) {
    for (const T& x : vec) std::cerr << x << ' ';std::cerr << "\n";
}
template <typename T>
T power(T a, int b) {
    T ans = 1;
    while (b) {
        if (b & 1) ans *= a;
        a *= a;
        b >>= 1;
    }
    return ans;
}
template <int M>
struct modint {
    modint() : x(0) {}
    template <typename T> modint(T n) { n %= M; if (n < 0) n += M; this->x = n; }

    modint& operator+=(const modint& r) { x += r.x; if (x >= M) x -= M; return *this; }
    modint& operator-=(const modint& r) { x -= r.x; if (x < 0) x += M; return *this; }
    modint& operator*=(const modint& r) { x = (int)((long long)x * r.x % M); return *this; }
    modint inv(long long k = M - 2) const { return power(*this, k); }
    modint& operator/=(const modint& r) { return *this *= r.inv(); }

    modint& operator--() { if (--x == -1) x = M - 1; return *this; }
    modint& operator++() { if (++x == M) x = 0; return *this; }
    modint operator--(int) { modint t = *this; --*this; return t; }
    modint operator++(int) { modint t = *this; ++*this; return t; }

    modint operator-() const { return 0 - *this; }
    modint operator+() const { return *this; }
    constexpr int mod() const { return M; }

    friend modint operator+(modint l, const modint& r) { return l += r; }
    friend modint operator-(modint l, const modint& r) { return l -= r; }
    friend modint operator*(modint l, const modint& r) { return l *= r; }
    friend modint operator/(modint l, const modint& r) { return l /= r; }

    friend bool operator==(const modint& l, const modint& r) { return l.x == r.x; }
    friend bool operator!=(const modint& l, const modint& r) { return l.x != r.x; }
    friend std::istream& operator>>(std::istream& in, modint& r) { return in >> r.x; }
    friend std::ostream& operator<<(std::ostream& out, const modint& r) { return out << r.x; }

    int x;
};
using mint = modint<mod>;

struct Comb
{
    int n, p;
    std::vector<mint> fact, invfact;
    Comb() = default;
    Comb(int _n, int _p) : n(_n), p(_p), fact(n + 1), invfact(n + 1)
    {
        fact[0] = invfact[0] = 1;
        for (int i = 1; i <= n; i++)
        {
            fact[i] = fact[i - 1] * i;
            invfact[i] = power(fact[i], p - 2);
        }
    }
    mint A(int a, int b)
    {
        if (a < 0 || a > n || b < 0 || b > a)
            return 0;
        return  fact[a] * invfact[a - b];
    }
    mint C(int a, int b)
    {
        if (a < 0 || a > n || b < 0 || b > a)
            return 0;
        return fact[a] * invfact[b] * invfact[a - b];
    }
} comb(N, mod);

void ncl3() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> e(n), re(n);
    std::vector<int> a(n), b(n), w(n), ok(n, 1), din(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        std::cin >> b[i];
        b[i]--;
    }
    for (int i = 0; i < n; i++) {
        std::cin >> w[i];
    }
    for (int i = 0; i < n; i++) {
        if (a[b[i]] > a[i]) continue;
        else if (b[i] != i && a[b[i]] + w[b[i]] > a[i]) {
            e[b[i]].push_back(i);
            din[i]++;
            re[i].push_back(b[i]);
        }
        else {
            ok[i] = false;
        }
    }
    std::vector<int> col(n), cir;
    for (int i = 0; i < n; i++) {
        if (!col[i]) {
            int j = i;
            while (!col[j]) {
                col[j] = 1;
                if (re[j].size()) j = re[j][0];
            }
            if (re[j].size()) {
                int jj = j;
                cir.clear();
                while (1) {
                    cir.push_back(jj);
                    jj = re[jj][0];
                    if (jj = j) break;
                }
                for (auto u : cir) ok[u] = false;
            }
        }
    }
    std::function<void(int)> dfs = [&](int u) {
        for (auto v : e[u]) {
            ok[v] = false;
            dfs(v);
        }
    };
    for (int i = 0; i < n; i++) {
        if (!ok[i])
            dfs(i);
    }
    // debug("NCl3");
    std::vector<int> dep(n);
    std::function<void(int, int)> dfs1 = [&](int u, int depth) {
        dep[u] = depth;
        for (auto v : e[u]) {
            dfs1(v, depth + 1);
        }
    };
    for (int i = 0; i < n; i++) {
        if (ok[i] && din[i] == 0) {
            dfs1(i, 1);
        }
    }
    // debug(dep[0], n);
    std::vector<int> p(n);
    for (int i = 0; i < n; i++) {
        if (!ok[i]) std::cout << a[i] << " ";
        else {
            std::cout << comb.invfact[dep[i]] * w[i] + a[i] << " ";
        }
    }
    std::cout << "\n";
}
/*
    预处理、找性质缩小范围、集合思想、正难则反、穷举
*/
int main() {
#ifdef LOCAL
    clock_t time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t = 1;
    std::cin >> t;
    while (t--) {
        ncl3();
    }
#ifdef LOCAL
    std::cout << "Time Used:" << clock() - time << "ms" << "\n";
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 69ms
memory: 6892kb

input:

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

output:

500000007 5 5 6 
5 10 9 
166666673 5 6 
500000006 4 3 4 5 

result:

ok 15 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

50000
5
508432375 168140163 892620793 578579275 251380640
3 4 4 1 3
346232959 736203130 186940774 655629320 607743104
1
863886789
1
364158084
18
864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...

output:


result: