QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276696#4607. Hello world!SoyTony100 ✓4243ms360616kbC++149.0kb2023-12-06 09:37:052023-12-06 09:37:06

Judging History

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

  • [2023-12-06 09:37:06]
  • 评测
  • 测评结果:100
  • 用时:4243ms
  • 内存:360616kb
  • [2023-12-06 09:37:05]
  • 提交

answer

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

typedef long long ll;
typedef pair<int,int> pii;
#define fir first
#define sec second
const int maxn=5e4+10;
const int maxB=55;
const int B=50;

int n,q;
ll a[maxn];
struct edge{
    int to,nxt;
}e[maxn<<1];
int head[maxn],cnt;
inline void add_edge(int u,int v){
    e[++cnt].to=v,e[cnt].nxt=head[u],head[u]=cnt;
    e[++cnt].to=u,e[cnt].nxt=head[v],head[v]=cnt;
}
int fa[maxn],dep[maxn],siz[maxn],son[maxn];
int top[maxn],ed[maxn],dfn[maxn],orddfn[maxn],dfncnt;
void dfs1(int u,int f,int d){
    fa[u]=f,dep[u]=d,siz[u]=1;
    for(int i=head[u],v;i;i=e[i].nxt){
        v=e[i].to;
        if(v==f) continue;
        dfs1(v,u,d+1);
        siz[u]+=siz[v];
        if(!son[u]||siz[v]>siz[son[u]]) son[u]=v;
    }
}
void dfs2(int u,int t){
    top[u]=t,ed[t]=u,dfn[u]=++dfncnt,orddfn[dfncnt]=u;
    if(!son[u]) return;
    dfs2(son[u],t);
    for(int i=head[u],v;i;i=e[i].nxt){
        v=e[i].to;
        if(v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}
inline int get_LCA(int u,int v){
    while(top[u]!=top[v]){
        if(dep[top[u]]>dep[top[v]]) swap(u,v);
        v=fa[top[v]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    return u;
}
inline int get_kthfa(int u,int k){
    if(k>dep[u]) return 0;
    while(1){
        if(dfn[u]-dfn[top[u]]<k){
            k-=(dfn[u]-dfn[top[u]]+1);
            u=fa[top[u]];
        }    
        else return orddfn[dfn[u]-k];
    }
}

int tot[maxn];
int ord[maxB][maxn],rk[maxB][maxn];
int L[maxB][maxB],R[maxB][maxB];
struct SegmentTree{
#define mid ((l+r)>>1)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
    ll sum[maxn<<2];
    inline void push_up(int rt){
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void build(int rt,int l,int r,int id){
        if(l==r) return sum[rt]=a[ord[id][l]],void();
        build(lson,id),build(rson,id);
        push_up(rt);
    }
    void update(int rt,int l,int r,int p,ll k){
        if(l==r) return sum[rt]=k,void();
        if(p<=mid) update(lson,p,k);
        else update(rson,p,k);
        push_up(rt);
    }
    ll query(int rt,int l,int r,int p){
        if(l==r) return sum[rt];
        if(p<=mid) return query(lson,p);
        else return query(rson,p);
    }
    ll query_range(int rt,int l,int r,int pl,int pr){
        if(pl<=l&&r<=pr) return sum[rt];
        ll res=0;
        if(pl<=mid) res+=query_range(lson,pl,pr);
        if(pr>mid) res+=query_range(rson,pl,pr);
        return res;
    }
#undef mid
#undef lson
#undef rson
}ST[maxB];
set<pii> S[maxB][maxn];

vector<int> ER;
inline void update1(int u,int lca,int k,int i,bool type){
    set<pii>::iterator it;
    while(top[u]!=top[lca]){
        it=S[k][top[u]].lower_bound(make_pair(i,0));
        while(it!=S[k][top[u]].end()&&(*it).fir==i&&(*it).sec<=dfn[u]){
            ll now=ST[1].query(1,1,n,rk[1][orddfn[(*it).sec]]);
            now=sqrt(now);
            for(int j=1;j<=B;++j) ST[j].update(1,1,n,rk[j][orddfn[(*it).sec]],now);
            if(now==1) ER.push_back(orddfn[(*it).sec]);
            ++it;
        }
        u=fa[top[u]];
    }
    if(!type) lca=fa[lca];
    it=S[k][top[u]].upper_bound(make_pair(i,dfn[lca]));
    while(it!=S[k][top[u]].end()&&(*it).fir==i&&(*it).sec<=dfn[u]){
        ll now=ST[1].query(1,1,n,rk[1][orddfn[(*it).sec]]);
        now=sqrt(now);
        for(int j=1;j<=B;++j) ST[j].update(1,1,n,rk[j][orddfn[(*it).sec]],now);
        if(now==1) ER.push_back(orddfn[(*it).sec]);
        ++it;
    }
    for(int u:ER){
        for(int j=1;j<=B;++j) S[j][top[u]].erase(make_pair(dep[u]%j,dfn[u]));
    }
    ER.clear();
}
inline void update_l(int u,int v,int k){
    int lca=get_LCA(u,v),dis=dep[u]+dep[v]-2*dep[lca];
    if((dep[u]-dep[lca])%k==0) update1(lca,lca,k,dep[lca]%k,0); 
    update1(u,lca,k,dep[u]%k,1);
    if(dis%k) update1(v,v,k,dep[v]%k,0);
    update1(v,lca,k,((2*dep[lca]-dep[u])%k+k)%k,1);
}
inline void update2(int u){
    ll now=ST[1].query(1,1,n,rk[1][u]);
    if(now==1) return;
    now=sqrt(now);
    for(int j=1;j<=B;++j) ST[j].update(1,1,n,rk[j][u],now);
    if(now!=1) return;
    for(int j=1;j<=B;++j) S[j][top[u]].erase(make_pair(dep[u]%j,dfn[u]));
}
inline void update_g(int u,int v,int k){
    int lca=get_LCA(u,v),dis=dep[u]+dep[v]-2*dep[lca];
    if((dep[u]-dep[lca])%k==0) update2(lca);
    while(dfn[u]>dfn[lca]){
        update2(u);
        u=get_kthfa(u,k);
    }
    if(dis%k){
        update2(v);
        v=get_kthfa(v,dis%k);
    }
    while(dfn[v]>dfn[lca]){
        update2(v);
        v=get_kthfa(v,k);
    }
}

inline ll query1(int u,int lca,int k,int i,bool type){
    // cerr<<"u:"<<u<<" lca:"<<lca<<" k:"<<k<<" i:"<<i<<endl;
    if(L[k][i]==-1) return 0;
    ll res=0;
    while(top[u]!=top[lca]){
        int l=L[k][i],r=R[k][i],lpos=-1,rpos=-1;
        // cerr<<l<<" "<<r<<endl;
        while(l<=r){
            int mid=(l+r)>>1;
            // cerr<<"lpos mid:"<<mid<<" "<<ord[k][mid]<<" "<<dfn[top[u]]<<endl;
            if(dfn[ord[k][mid]]>=dfn[top[u]]) lpos=mid,r=mid-1;
            else l=mid+1;
        }
        l=L[k][i],r=R[k][i];
        while(l<=r){
            int mid=(l+r)>>1;
            // cerr<<"rpos mid:"<<mid<<endl;
            if(dfn[ord[k][mid]]<=dfn[u]) rpos=mid,l=mid+1;
            else r=mid-1;
        }
        // cerr<<lpos<<" "<<rpos<<endl;
        if(lpos!=-1&&lpos<=rpos) res+=ST[k].query_range(1,1,n,lpos,rpos);
        u=fa[top[u]];
    }
    int l=L[k][i],r=R[k][i],lpos=-1,rpos=-1;
    // cerr<<"lca:"<<lca<<" u:"<<u<<endl;
    while(l<=r){
        int mid=(l+r)>>1;
        // cerr<<"lpos mid:"<<mid<<endl;
        if(dfn[ord[k][mid]]>=dfn[lca]) lpos=mid,r=mid-1;
        else l=mid+1;
    }
    l=L[k][i],r=R[k][i];
    while(l<=r){
        int mid=(l+r)>>1;
        // cerr<<"rpos mid:"<<mid<<endl;
        if(dfn[ord[k][mid]]<=dfn[u]) rpos=mid,l=mid+1;
        else r=mid-1;
    }
    // cerr<<lpos<<" "<<rpos<<endl;
    if(lpos!=-1&&type&&ord[k][lpos]==lca) ++lpos;
    if(lpos!=-1&&lpos<=rpos){
        res+=ST[k].query_range(1,1,n,lpos,rpos);
    }
    return res;
}
inline void query_l(int u,int v,int k){
    // cerr<<"u:"<<u<<" v:"<<v<<" k:"<<k<<endl;
    ll res=0;
    int lca=get_LCA(u,v),dis=dep[u]+dep[v]-2*dep[lca];
    if((dep[u]-dep[lca])%k==0) res+=query1(lca,lca,k,dep[lca]%k,0); 
    // cerr<<"res:"<<res<<endl;
    res+=query1(u,lca,k,dep[u]%k,1);
    // cerr<<"res:"<<res<<endl;
    if(dis%k) res+=query1(v,v,k,dep[v]%k,0);
    // cerr<<"res:"<<res<<endl;
    res+=query1(v,lca,k,((2*dep[lca]-dep[u])%k+k)%k,1);
    // cerr<<"res:"<<res<<endl;
    printf("%lld\n",res);
}
inline ll query2(int u){
    return ST[1].query(1,1,n,rk[1][u]);
}
inline void query_g(int u,int v,int k){
    // cerr<<"u:"<<u<<" v:"<<v<<" k:"<<k<<endl;
    ll res=0;
    int lca=get_LCA(u,v),dis=dep[u]+dep[v]-2*dep[lca];
    if((dep[u]-dep[lca])%k==0) res+=query2(lca);
    while(dfn[u]>dfn[lca]){
        res+=query2(u);
        // cerr<<"u:"<<u<<" res:"<<res<<endl;
        u=get_kthfa(u,k);
    }
    // cerr<<dep[u]<<" "<<dep[lca]<<endl;
    // cerr<<(dep[u]-dep[lca])%k<<endl;
    // cerr<<"res:"<<res<<endl;
    if(dis%k){
        res+=query2(v);
        v=get_kthfa(v,dis%k);
    }
    // cerr<<"res:"<<res<<endl;
    while(dfn[v]>dfn[lca]){
        res+=query2(v);
        // cerr<<"v:"<<v<<" res:"<<res<<endl;
        v=get_kthfa(v,k);
    }
    printf("%lld\n",res);
}

int main(){
    // freopen("d.in","r",stdin);
    // freopen("d.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
    for(int i=1,u,v;i<n;++i){
        scanf("%d%d",&u,&v);
        add_edge(u,v);
    }
    dfs1(1,0,0);
    dfs2(1,1);
    // for(int i=1;i<=n;++i) cerr<<"i:"<<i<<" fa:"<<fa[i]<<" top:"<<top[i]<<" dfn:"<<dfn[i]<<endl;
    for(int k=1;k<=B;++k){
        for(int i=0;i<k;++i) tot[i]=0,L[k][i]=R[k][i]=-1;
        for(int i=1;i<=n;++i) ++tot[dep[orddfn[i]]%k];
        for(int i=1;i<k;++i) tot[i]+=tot[i-1];
        for(int i=n;i>=1;--i) ord[k][tot[dep[orddfn[i]]%k]--]=orddfn[i];
        for(int i=1;i<=n;++i) rk[k][ord[k][i]]=i;
        for(int i=1;i<=n;++i){
            if(i==1||dep[ord[k][i]]%k!=dep[ord[k][i-1]]%k) L[k][dep[ord[k][i]]%k]=i;
            R[k][dep[ord[k][i]]%k]=i;
        }
        ST[k].build(1,1,n,k);
        for(int u=1;u<=n;++u) S[k][top[u]].insert(make_pair(dep[u]%k,dfn[u]));
        if(k>n) continue;
        // cerr<<"k:"<<k<<endl;
        // cerr<<"ord:"<<endl;
        // for(int i=1;i<=n;++i) cerr<<ord[k][i]<<" ";
        // cerr<<endl;
        // cerr<<"rk:"<<endl;
        // for(int i=1;i<=n;++i) cerr<<rk[k][i]<<" ";
        // cerr<<endl;
        // for(int i=0;i<k;++i) cerr<<"["<<L[k][i]<<","<<R[k][i]<<"] ";
        // cerr<<endl;
    }
    scanf("%d",&q);
    while(q--){
        int opt,u,v,k;
        scanf("%d%d%d%d",&opt,&u,&v,&k);
        if(!opt){
            if(k<=B) update_l(u,v,k);
            else update_g(u,v,k);
        }
        else{
            if(k<=B) query_l(u,v,k);
            else query_g(u,v,k);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 12
Accepted
time: 68ms
memory: 237268kb

input:

2000
2303903480321 5781389899155 3190545907553 3071647373889 508555808969 9840447775707 2497180409217 9664886116213 6214859274809 9609544570693 2154151804097 7975716574177 1787719635873 2263067923489 4726600312917 2516670601041 3303532684135 4655815226849 8788279457553 1784776583937 385024107539 518...

output:

3761290186872767
112821372473362
37483813149326
102117005711372
1846668376560916
124418062674481
89075481153064
3358026786143100
380867873719735
1111940510177397
44759103650168
1116347542051075
1212237842454014
90158551755837
1746080219504777
109197623497344
109004080706904
1214856494492229
18784462...

result:

ok 2523 numbers

Test #2:

score: 14
Accepted
time: 2896ms
memory: 333392kb

input:

40000
1839380803809 5986819474641 8388566664193 269512605297 4315213736593 6306426469957 8327979728929 8902208229353 5398140157057 6196104366769 4333834127773 1043863499377 1990884823105 7921774419777 2577662676429 6938953844225 2059575677761 4314548232605 7312171366465 3245120568569 8212009729561 1...

output:

406846728903614
6247271396375072
194227934630643
15238260274428799
34730231897452372
32318719715910
317799829490448
99706246925551
350432398142488
3297388341200056
2595953400809410
12350397603348878
3274711470626714
8085245926454205
596331367045525
351255735405543
316576261874879
1014423424438015
31...

result:

ok 199874 numbers

Test #3:

score: 14
Accepted
time: 3248ms
memory: 345016kb

input:

45000
370494722817 2274035277501 5295464663233 7972306507041 1765294180665 1581282817707 2978672143233 8282780215847 9871515474304 7972720958141 6979394187137 5816070843684 9216493892185 5742895947121 3946860734373 4219806017537 5301020579357 2117059185345 5402369771553 4626350626691 7561290718797 6...

output:

2221485173124667
270078419803964
5328371941157586
261457803405091
7165691999075991
78558851320638452
904700523331553
23376853744225882
17042486510096
10297143152188722
20676540996164080
51075278771399501
29193965029592808
23657544410889698
4977486824140640
844279415155815
12088224986781054
517354211...

result:

ok 200122 numbers

Test #4:

score: 17
Accepted
time: 4243ms
memory: 360616kb

input:

50000
4682617620677 9672865907041 7003397684537 572457269305 2343774317554 7990946533675 7922899621433 688625530923 1875537202085 1569512405235 8233538766913 3758862270203 8940096768065 3056695454113 3298282391265 922626243585 5596665701791 7063494657877 9152169281959 7991556909457 7014947748289 707...

output:

5235790676149816
45990181652376833
11744936391066909
16502462033273073
614021361359863
165675732278276166
397410433391452
63165093407502339
370947303833182
37802859900488356
36515795204630270
32187202259167830
587397039446129
4680789770085812
55067628715464374
19753435277409988
61057278956084076
444...

result:

ok 199964 numbers

Test #5:

score: 7
Accepted
time: 2379ms
memory: 355460kb

input:

50000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

101
8679
9412
95
6330
906
1029
19025
18
328
9048
842
936
18491
86
1022
2818
396
132
1035
805
43
136
38
54
8758
129
6224
8887
1045
735
97
5607
19935
61
25
7
4601
98
18750
17765
190
1775
3724
52
1355
148
431
88
47
77
4735
17212
17374
8416
1006
93
9
47
946
2056
58
14393
18635
19655
96
95
515
85
273
46
...

result:

ok 200528 numbers

Test #6:

score: 13
Accepted
time: 2784ms
memory: 357600kb

input:

50000
2206367061365 8888509988977 3088159216223 474665948371 6023855838657 8127837515185 3553129538785 184267506201 4598762011291 9615306678057 3919697530201 1823403688081 4377903007581 9640730244989 5974907661143 2274190708737 2138174066195 6463826022593 7365279718721 710302060187 1194512684597 419...

output:

88572249240093202
4376462023957
8117294166298060
3812284140794
10388558597855603
10647719823472460
5033870027936144
4093559264626453
11422405602922605
6596195776161906
10905020708
17463258886691
4140807197844808
3476104711847494
4436512747345943
9214138504983
7008969018118
6928560106712265
264216350...

result:

ok 199898 numbers

Test #7:

score: 23
Accepted
time: 3594ms
memory: 358128kb

input:

50000
9021805289381 1690779861907 7852603332623 2176962640129 4012452875777 5036049181476 5108865184769 3577298802913 5017434132449 8187410440985 9821875376593 2388910184247 3462802293952 6085256713985 7504270899313 7990195710545 2210484303689 7285793027969 1235979822081 8816677352337 8561703320021 ...

output:

1338125970753330
388215884120724
15857940247112356
43000630305235133
42037374985116005
4683236704290060
410600314000419
14658347684906997
43955236696675028
394466445656176
29824238351503461
28117195196762335
429239644517135
655820625904147
7069848184318
86873672705012751
5849487637724698
80559571588...

result:

ok 200082 numbers

Extra Test:

score: 0
Extra Test Passed