QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#764007#8237. Sugar Sweet IIgray114514TL 0ms22436kbC++142.1kb2024-11-19 23:19:122024-11-19 23:19:12

Judging History

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

  • [2024-11-19 23:19:12]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:22436kb
  • [2024-11-19 23:19:12]
  • 提交

answer

#include<cstdio>
#include<bitset>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 5e5+10;
const ll mod = 1e9 + 7;
ll a[N] , b[N] , w[N] ,E[N] ,  n , T , inv2 , fac[N];
int h[N << 1] , nxt[N << 1] , to[N << 1] , tot , dep[N];
int tmp[N] , idx;
bitset<N> vis , st;
ll qmi(ll x,ll q)
{
    ll res = 1;
    while(q){
        if(q & 1) res = res * x % mod;
        x = x * x % mod;
        q >>= 1ll;
    }
    return res;
}
void add(int u,int v)
{
    nxt[++tot] = h[u] , to[tot] = v , h[u] = tot;
}
void dfs(int u,int fa)
{
    if(st[u]) return ;
    if(dep[fa]) dep[u] = dep[fa] + 1;
    else dep[u] = 0;
    E[u] = (a[u] + qmi(fac[dep[u]],mod-2) * w[u] % mod) % mod;
    st.set(u);
    for(int i=h[u];i;i=nxt[i]){
        int v = to[i];
        dfs(v,u);
    }
    return ;
}
int main()
{
    scanf("%d",&T);
    fac[1] = 1;
    for(int i=2;i<N;i++)
        fac[i] = (fac[i-1] * (ll)i) % mod;
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        int cur;
        for(int i=1;i<=n;i++){
            if(vis[i]) continue;
            idx = 0 , cur = i;
            while(!vis[cur]){
                vis[cur] = 1;
                if(b[cur] == cur) dep[cur] = 0 , E[cur] = a[cur] , st[cur] = 1;
                if(a[cur] >= a[b[cur]]+w[b[cur]])  dep[cur] = 0 , E[cur] = a[cur] , st[cur] = 1;
                else if(a[cur] < a[b[cur]]) dep[cur] = 1 , E[cur] = a[cur] + w[cur] , st[cur] = 1;
                add(b[cur],cur);
                tmp[++idx] = cur;
                cur = b[cur];
            }
        }
        for(int u=1;u<=n;u++)
            for(int i=h[u];i;i=nxt[i]){
                int v = to[i];
                if(!st.test(v) && st.test(u))
                    dfs(v,u);
        }
        for(int i=1;i<=n;i++) 
            printf("%d ",E[i]);
        puts("");
        tot = 0;
        memset(h,0,sizeof(h));
        st.reset();
        vis.reset();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 22436kb

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:

854665334 904343293 590444253 906393935 859123744 
863886789 
871186919 814243920 968784984 1206455481 17527050 1449261420 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 482247153 963066959 
665922935 577926775 132646723 421298438 601054667 10994...

result: