QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#627951#8237. Sugar Sweet IIzzfs#WA 403ms11664kbC++144.3kb2024-10-10 17:48:592024-10-10 17:49:00

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-10 17:49:00]
  • 评测
  • 测评结果:WA
  • 用时:403ms
  • 内存:11664kb
  • [2024-10-10 17:48:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define mod_n 1000000007
#define mod(x) ((x) % mod_n)
#define int int64_t
#define max_len 500010

int qpow(int x, int n)
{
    if (n == 0)
        return 1;
    else if (n == 1)
        return mod(x);
    else if (n % 2 == 0)
        return qpow(mod(x * x), n / 2);
    else
        return mod(qpow(mod(x * x), n / 2) * x);
}

int inv(int x)
{
    return qpow(x, mod_n - 2);
}

int inv_jc_arr[max_len];
void init_inv_jc()
{
    inv_jc_arr[0] = inv_jc_arr[1] = 1;
    for (int i = 2; i < max_len; i++)
    {
        inv_jc_arr[i] = mod(inv_jc_arr[i - 1] * inv(i));
    }
}

struct node
{
    int p_next;
    int have_sugar;
    int will_add;

    int E_val;
    int len;

    bool dfs1_vis;
    bool dfs2_vis;
    bool duan;
} node_arr[max_len];

int in_stack[max_len];
vector<int> stk_vec;

void dfs1(int now_i)
{
    node_arr[now_i].dfs1_vis = true;
    if (in_stack[now_i] == 0)
    {
        in_stack[now_i] = stk_vec.size();
        stk_vec.push_back(now_i);

        dfs1(node_arr[now_i].p_next);

        stk_vec.pop_back();
        in_stack[now_i] = 0;
    }
    else // huan
    {
        int huan_len = stk_vec.size() - in_stack[now_i];
        int base_len = in_stack[now_i];
        for (int i = 0; i < huan_len; i++)
        {
            int ni = stk_vec[base_len + (huan_len + i) % huan_len];
            int ni_nx = node_arr[ni].p_next;

            if (node_arr[ni].have_sugar < node_arr[ni_nx].have_sugar)
            // if (node_arr[ni].have_sugar >= node_arr[ni_nx].have_sugar +
            //                                    node_arr[ni_nx].will_add)
            {
                node_arr[ni].duan = true;
                node_arr[ni].p_next = ni;
                break;
            }
        }
    }
}

int dfs2(int now_i)
{
    node_arr[now_i].dfs2_vis = true;
    if (node_arr[now_i].len != -1)
    {
        return node_arr[now_i].len;
    }
    if (node_arr[now_i].p_next == now_i)
    {
        node_arr[now_i].E_val = node_arr[now_i].have_sugar;
        if (node_arr[now_i].duan == true)
            node_arr[now_i].E_val += node_arr[now_i].will_add;
        node_arr[now_i].E_val = mod(node_arr[now_i].E_val);
        node_arr[now_i].len = 0;
        return 0;
    }
    else
    {
        int len = dfs2(node_arr[now_i].p_next);
        int pnx = node_arr[now_i].p_next;
        node_arr[now_i].E_val = node_arr[now_i].have_sugar;
        if (node_arr[now_i].have_sugar >= node_arr[pnx].have_sugar)
        {
            if (node_arr[now_i].have_sugar < node_arr[pnx].have_sugar +
                                                 node_arr[pnx].will_add)
            {
                node_arr[now_i].E_val +=
                    mod(inv_jc_arr[len + 2] * node_arr[now_i].will_add);

                node_arr[now_i].E_val = mod(node_arr[now_i].E_val);

                len = len + 1;
            }
            else
            {
                len = 0;
            }
        }
        else
        {
            node_arr[now_i].E_val += node_arr[now_i].will_add;
            node_arr[now_i].E_val = mod(node_arr[now_i].E_val);
            len = 0;
        }
        node_arr[now_i].len = len;
        return len;
    }
}

void init_arr(int N)
{
    for (int i = 0; i < N + 10; i++)
    {
        node_arr[i].len = -1;
        node_arr[i].E_val = 0;

        node_arr[i].dfs1_vis = false;
        node_arr[i].dfs2_vis = false;
        node_arr[i].duan = false;
    }
}

void sol()
{
    int N;
    cin >> N;
    init_arr(N);

    for (int i = 1; i <= N; i++)
    {
        cin >> node_arr[i].have_sugar;
    }
    for (int i = 1; i <= N; i++)
    {
        cin >> node_arr[i].p_next;
    }
    for (int i = 1; i <= N; i++)
    {
        cin >> node_arr[i].will_add;
    }
    for (int i = 1; i <= N; i++)
    {
        if (node_arr[i].dfs1_vis == false)
        {
            dfs1(i);
        }
    }
    for (int i = 1; i <= N; i++)
    {
        if (node_arr[i].dfs2_vis == false)
        {
            dfs2(i);
        }
    }
    for (int i = 1; i <= N; i++)
    {
        cout << node_arr[i].E_val << (i == N ? '\n' : ' ');
    }
}

int32_t main()
{
    init_inv_jc();
    int T;
    cin >> T;
    while (T--)
    {
        sol();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 403ms
memory: 11664kb

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:

854665334 904343293 590444253 906393935 859123744
863886789
871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959
902284038 240323331 132646723 421298438 601054667 99438820 94...

result:

wrong answer 25th numbers differ - expected: '665922935', found: '902284038'