QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140284#4607. Hello world!myee100 ✓354ms23912kbC++115.6kb2023-08-15 16:55:542023-08-15 16:55:58

Judging History

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

  • [2023-08-15 16:55:58]
  • 评测
  • 测评结果:100
  • 用时:354ms
  • 内存:23912kb
  • [2023-08-15 16:55:54]
  • 提交

answer

// 那就是希望。
// 即便需要取模,也是光明。

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
    T ans=1%mod;
    while(index)
    {
        if(index&1)ans=ans*base%mod;
        base=base*base%mod,index>>=1;
    }
    return ans;
}
// Heaven and Earth... My guiding star...
// Akira Complex R.I.P.
ullt SQRT(ullt v)
{
    ullt w=sqrtl((long double)v);
    while(w*w<v)w++;
    while(w*w>v)w--;
    return w;
}
const uint B=8,Lim=50000;
std::vector<uint>Way[Lim+1];
uint T[Lim+1],F[Lim+1],Rot[Lim+1],Dep[Lim+1];ullt A[Lim+1],U[Lim+1];
uint Bgn[B+1][Lim+1],End[B+1][Lim+1],n,q;
voi dfs(uint p,uint f)
{
    std::vector<uint>S;T[p]=1;
    for(auto&s:Way[p])if(s!=f){S.push_back(s),dfs(s,p),T[p]+=T[s];if(T[s]>T[S[0]])std::swap(S[0],S.back());}
    Way[p]=S;
}
voi dfs2(uint p)
{
    static uint cnt=0,Cnt[B+1][B+1];A[T[p]=cnt++]=U[p];
    for(uint i=1;i<=B;i++)Bgn[i][T[p]]=Cnt[i][Dep[T[p]]%i]++;
    for(auto s:Way[p])Rot[cnt]=s==Way[p][0]?Rot[T[p]]:cnt,F[cnt]=T[p],Dep[cnt]=Dep[T[p]]+1,dfs2(s);
    for(uint i=1;i<=B;i++)End[i][T[p]]=Cnt[i][Dep[T[p]]%i];
    if(!p)for(uint i=1;i<=B;i++)
    {
        for(uint j=1;j<i;j++)Cnt[i][j]+=Cnt[i][j-1];
        for(uint j=0;j<n;j++)if(Dep[j]%i)Bgn[i][j]+=Cnt[i][Dep[j]%i-1],End[i][j]+=Cnt[i][Dep[j]%i-1];
    }
}
uint kth(uint p,uint k)
{
    if(Dep[p]<k)return-1;
    while(p-Rot[p]<k)k-=p-Rot[p]+1,p=F[Rot[p]];
    return p-k;
}
uint lca(uint u,uint v)
{
    while(Rot[u]!=Rot[v])
    {
        if(Rot[u]<Rot[v])std::swap(u,v);
        u=F[Rot[u]];
    }
    return u<v?u:v;
}
namespace Block
{
    const uint T=200;
    ullt A[B+1][Lim+1],S[B+1][Lim/T+1];
    inline voi add(uint c,uint l,uint r,ullt w){A[c][l]+=w,S[c][l/T]+=w,A[c][r]-=w,S[c][r/T]-=w;}
    ullt find(uint c,uint p)
    {
        ullt ans=0;p++;
        for(uint i=p/T-1;~i;i--)ans+=S[c][i];
        for(uint i=p/T*T;i<p;i++)ans+=A[c][i];
        return ans;
    }
}
voi upd(uint p)
{
    if(A[p]==1)return;
    ullt g=SQRT(A[p])-A[p];A[p]+=g;
    for(uint i=1;i<=B;i++)Block::add(i,Bgn[i][p],End[i][p],g);
}
uint kRot[B+1][Lim+1];
uint getf(uint p,uint k)
{
    if(~kRot[k][p]&&A[kRot[k][p]]==1)kRot[k][p]=getf(kRot[k][p],k);
    return kRot[k][p];
}
int main()
{
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#endif
    scanf("%u",&n);for(uint i=0;i<n;i++)scanf("%llu",U+i);
    for(uint i=1,u,v;i<n;i++)scanf("%u%u",&u,&v),Way[--u].push_back(--v),Way[v].push_back(u);
    dfs(0,-1),dfs2(0),F[0]=-1;
    for(uint i=1;i<=B;i++)for(uint p=0;p<n;p++)kRot[i][p]=kth(p,i),Block::add(i,Bgn[i][p],End[i][p],A[p]);
    scanf("%u",&q);
    while(q--)
    {
        uint o,u,v,r,k;scanf("%u%u%u%u",&o,&u,&v,&k),r=lca(u=T[u-1],v=T[v-1]);
        if(o)
        {
            ullt ans=0;
            if((Dep[u]+Dep[v]-2*Dep[r])%k)
            {
                ans=A[v];
                if(Dep[v]-Dep[r]>=(Dep[u]+Dep[v]-2*Dep[r])%k)v=kth(v,(Dep[u]+Dep[v]-2*Dep[r])%k);
                else r=v=kth(u,(Dep[u]+Dep[v]-2*Dep[r])/k*k);
            }
            if(k<=B)
            {
                if(Dep[u]-Dep[r]>=k)
                    ans+=Block::find(k,Bgn[k][u]),ans-=Block::find(k,Bgn[k][u=kth(u,(Dep[u]-Dep[r])/k*k)]);
                ans+=A[u];
                if(v!=r)
                {
                    if(Dep[v]-Dep[r]>k)
                        ans+=Block::find(k,Bgn[k][v]),ans-=Block::find(k,Bgn[k][v=kth(v,(Dep[v]-Dep[r]-1)/k*k)]);
                    ans+=A[v];
                }
            }
            else
            {
                while(true)
                {
                    ans+=A[u];if(Dep[u]-Dep[r]<k)break;
                    u=kth(u,k);
                }
                if(v!=r)while(true)
                {
                    ans+=A[v];if(Dep[v]-Dep[r]<=k)break;
                    v=kth(v,k);
                }
            }
            printf("%llu\n",ans);
        }
        else
        {
            if((Dep[u]+Dep[v]-2*Dep[r])%k)
            {
                upd(v);
                if(Dep[v]-Dep[r]>=(Dep[u]+Dep[v]-2*Dep[r])%k)v=kth(v,(Dep[u]+Dep[v]-2*Dep[r])%k);
                else r=v=kth(u,(Dep[u]+Dep[v]-2*Dep[r])/k*k);
            }
            while(true)
            {
                upd(u);if(Dep[u]-Dep[r]<k)break;
                if(k<=B)
                {
                    uint t=getf(u,k);
                    if(!~t||Dep[t]<Dep[r])t=kth(u,(Dep[u]-Dep[r])/k*k);
                    u=t;
                }else u=kth(u,k);
            }
            if(v!=r)while(true)
            {
                upd(v);if(Dep[v]-Dep[r]<=k)break;
                if(k<=B)
                {
                    uint t=getf(v,k);
                    if(!~t||Dep[t]<=Dep[r])t=kth(v,(Dep[v]-Dep[r]-1)/k*k);
                    v=t;
                }else v=kth(v,k);
            }
        }
    }
    return 0;
}

// 那就是希望。
// 即便需要取模,也是光明。

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 12
Accepted
time: 3ms
memory: 13812kb

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: 244ms
memory: 16932kb

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: 271ms
memory: 18404kb

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: 354ms
memory: 23912kb

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: 257ms
memory: 18036kb

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: 167ms
memory: 18028kb

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: 274ms
memory: 18984kb

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