QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1109#707240#8237. Sugar Sweet IIyumingskyumingskSuccess!2024-11-03 15:17:482024-11-04 16:58:13

详细

Extra Test:

Wrong Answer
time: 3ms
memory: 23968kb

input:

1
2
999999999 1000000000
2 2
8 7

output:

999999999 1000000000 

result:

wrong answer 1st numbers differ - expected: '0', found: '999999999'

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#707240#8237. Sugar Sweet IIyumingsk#WA 130ms44640kbC++203.0kb2024-11-03 15:13:482024-11-04 17:09:36

answer

#pragma GCC optimize(3, "Ofast", "inline")
#include <iostream>
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define L_INF 0x7f3f3f3f3f3f3f3f
#define db cout << "debug\n";

using namespace std;
const int mod = 1e9 + 7;
using ll = long long;
ll a[500005];
int b[500005];
ll w[500005];
ll vis[500005];
ll qpow(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b = b / 2;
    }
    return res;
}
ll ans[500005];
int deg[500005];
int dep[500005];
void dfs(int u)
{
    if (vis[u] == 1)
    {
        ans[u] = a[u];
        return;
    }

    vis[u] = 1;
    if (ans[u] != 0)
    {
        dep[u] = 1;
        vis[u] = 0;
        return;
    }
    dfs(b[u]);
    if (ans[b[u]] == a[b[u]])
        ans[u] = a[u];
    dep[u] = dep[b[u]] + 1;
    vis[u] = 0;
}
ll fac[500005];
ll ifc[500005];
void init()
{
    fac[0] = 1;
    ifc[0] = 1;
    for (int i = 1; i <= 500000; i++)
        fac[i] = fac[i - 1] * i % mod;
    ifc[500000] = qpow(fac[500000], mod - 2);
    for (int i = 500000 - 1; i >= 1; i--)
        ifc[i] = ifc[i + 1] * (i + 1) % mod;
}
void solve()
{
    // cout << ifc[3] << ' ' << qpow(6, mod - 2) << "\n";
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        deg[i] = 0;
        dep[i] = 0;
        ans[i] = 0;
        vis[i] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
    }
    for (int i = 1; i <= n; i++)
    {
        if (i == b[i])
        {
            ans[i] = a[i];
            continue;
        }
        if (a[i] >= a[b[i]] + w[b[i]])
        {
            ans[i] = a[i];
            // a[i] += w[i];
        }
        else if (a[i] >= a[b[i]] && a[i] < a[b[i]] + w[b[i]])
        {
            // i -> b[i];
            // cout << i << '\n';
            deg[b[i]]++;
        }
        else
        {
            ans[i] = a[i] + w[i];
            ans[i] %= mod;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (deg[i] != 0)
            continue;

        dfs(i);
    }
    for (int i = 1; i <= n; i++)
    {
        if (ans[i] != 0)
        {
            cout << ans[i] % mod << " ";
        }
        else
        {
            //
            cout << ((a[i] + w[i] * ifc[dep[i]] * (dep[i] != 0) % mod) % mod) << ' ';
        }
    }
    cout << '\n';
}
int main()
{
    IOS;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#ifndef ONLINE_JUDGE
    clock_t start_time = clock();
#endif
    int t = 1;
    cin >> t;
    init();
    while (t--)
    {
        solve();
    }
#ifndef ONLINE_JUDGE
    // cout << "Used " << (double)(clock() - start_time) << " ms" << endl;
#endif
    return 0;
}