QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140271#4551. 数据交互myee100 ✓292ms39376kbC++116.4kb2023-08-15 16:43:552023-08-15 16:43:58

Judging History

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

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

answer

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

#include <algorithm>
#include <queue>
#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...
struct Info{
    llt A01,A02,A11,A12;
    friend Info operator*(Info a,Info b){
        Info ans;
        ans.A01=std::max(a.A01+b.A11,b.A01);
        ans.A02=std::max({a.A02,a.A01+b.A12,b.A02});
        ans.A11=a.A11+b.A11;
        ans.A12=std::max({a.A12,a.A11+b.A12});
        return ans;
    }
};
std::vector<uint>Way[100005];
uint Fath[100005],Siz[100005],Rot[100005],Dep[100005];
voi dfs(uint p,uint f)
{
    std::vector<uint>S;Siz[p]=1,Fath[p]=f;
    for(auto s:Way[p])if(s!=f)Dep[s]=Dep[p]+1,dfs(s,p),Siz[p]+=Siz[s],S.push_back(s);
    std::sort(S.begin(),S.end(),[&](uint a,uint b){return Siz[a]>Siz[b];}),Way[p]=S;
}
uint Fath_BST[100005],Son_BST[2][100005];Info W_BST[100005];llt Tag_BST[100005];
uint Fath_SonBST[100005],Rot_SonBST[100005],Son_SonBST[2][100005];
llt MaxV_SonBST[100005],SecMaxV_SonBST[100005],A[100005];
voi pushup_BST(uint p)
{
    auto&w=W_BST[p];
    w.A11=A[p];
    w.A01=w.A12=A[p]+(~Rot_SonBST[p]?MaxV_SonBST[Rot_SonBST[p]]:0);
    w.A02=A[p]+(~Rot_SonBST[p]?MaxV_SonBST[Rot_SonBST[p]]+SecMaxV_SonBST[Rot_SonBST[p]]:0);
    if(~Son_BST[0][p])w=W_BST[Son_BST[0][p]]*w;
    if(Tag_BST[p])w.A01+=Tag_BST[p],w.A02+=Tag_BST[p];
    if(~Son_BST[1][p])w=w*W_BST[Son_BST[1][p]];
}
uint build_BST(uint o,uint y)
{
    if(o==y)return-1;
    uint g=-1,m=-1;
    for(uint p=o;p!=y;p=Way[p].size()?Way[p][0]:-1u)
        if(_min(g,std::max(Siz[o]-Siz[p],(Way[p].size()?Siz[Way[p][0]]:0)-(~y?Siz[y]:0))))m=p;
    if(~(Son_BST[0][m]=build_BST(o,m)))Fath_BST[Son_BST[0][m]]=m;
    if(~(Son_BST[1][m]=build_BST(Way[m].size()?Way[m][0]:-1u,y)))Fath_BST[Son_BST[1][m]]=m;
    pushup_BST(m);Fath_BST[m]=-1;return m;
}
voi pushup_SonBST(uint p)
{
    MaxV_SonBST[p]=W_BST[p].A12,SecMaxV_SonBST[p]=0;
    if(~Son_SonBST[0][p])
    {
        if(MaxV_SonBST[Son_SonBST[0][p]]>MaxV_SonBST[p])
            _max(SecMaxV_SonBST[p]=MaxV_SonBST[p],SecMaxV_SonBST[Son_SonBST[0][p]]),
            MaxV_SonBST[p]=MaxV_SonBST[Son_SonBST[0][p]];
        else _max(SecMaxV_SonBST[p],MaxV_SonBST[Son_SonBST[0][p]]);
    }
    if(~Son_SonBST[1][p])
    {
        if(MaxV_SonBST[Son_SonBST[1][p]]>MaxV_SonBST[p])
            _max(SecMaxV_SonBST[p]=MaxV_SonBST[p],SecMaxV_SonBST[Son_SonBST[1][p]]),
            MaxV_SonBST[p]=MaxV_SonBST[Son_SonBST[1][p]];
        else _max(SecMaxV_SonBST[p],MaxV_SonBST[Son_SonBST[1][p]]);
    }
}
uint build_SonBST(std::vector<uint>&V,uint l,uint r)
{
    if(l==r)return-1;
    uint s=0,g=-1,m=-1;
    for(uint j=l;j<r;j++)s+=Siz[V[j]];
    for(uint j=l,t=0;j<r;t+=Siz[V[j++]])if(_min(g,std::max(t,s-=Siz[V[j]])))m=j;
    if(~(Son_SonBST[0][V[m]]=build_SonBST(V,l,m)))Fath_SonBST[Son_SonBST[0][V[m]]]=V[m];
    if(~(Son_SonBST[1][V[m]]=build_SonBST(V,m+1,r)))Fath_SonBST[Son_SonBST[1][V[m]]]=V[m];
    pushup_SonBST(V[m]);return V[m];
}
std::priority_queue<llt>Q1,Q2;
uint dfs2(uint p)
{
    uint r=p;
    while(~p)
    {
        Rot[p]=r,Fath_SonBST[p]=-1;
        if(Way[p].size())
        {
            std::vector<uint>S;for(uint j=1;j<Way[p].size();j++)S.push_back(dfs2(Way[p][j])),Fath_BST[S.back()]=p;
            if(~(Rot_SonBST[p]=build_SonBST(S,0,S.size())))Fath_SonBST[Rot_SonBST[p]]=-1;
            p=Way[p][0];
        }else Rot_SonBST[p]=-1,p=-1;
    }
    Q1.push(0);
    return build_BST(r,-1);
}
uint lca(uint u,uint v)
{
    while(Rot[u]!=Rot[v])
    {
        if(Dep[Rot[u]]<Dep[Rot[v]])std::swap(u,v);
        u=Fath[Rot[u]];
    }
    return Dep[u]<Dep[v]?u:v;
}
voi addA(uint p,llt w)
{
    uint q=p;
    while(~q)
    {
        if(!~Fath_BST[q]||(Son_BST[0][Fath_BST[q]]!=q&&Son_BST[1][Fath_BST[q]]!=q))
            Q2.push(std::max(W_BST[q].A01,W_BST[q].A02));
        q=Fath_BST[q];
    }
    A[p]+=w;
    while(~p)
    {
        pushup_BST(p);
        if(!~Fath_BST[p]||(Son_BST[0][Fath_BST[p]]!=p&&Son_BST[1][Fath_BST[p]]!=p))
        {
            Q1.push(std::max(W_BST[p].A01,W_BST[p].A02));
            q=p;while(~q)pushup_SonBST(q),q=Fath_SonBST[q];
        }
        p=Fath_BST[p];
    }
}
voi addB(uint p,llt w)
{
    uint q=p;
    while(~q)
    {
        if(!~Fath_BST[q]||(Son_BST[0][Fath_BST[q]]!=q&&Son_BST[1][Fath_BST[q]]!=q))
            Q2.push(std::max(W_BST[q].A01,W_BST[q].A02));
        q=Fath_BST[q];
    }
    bol ok=true;
    while(~p)
    {
        if(ok)Tag_BST[p]+=w;
        pushup_BST(p);
        if(!~Fath_BST[p]||(Son_BST[0][Fath_BST[p]]!=p&&Son_BST[1][Fath_BST[p]]!=p))
        {
            Q1.push(std::max(W_BST[p].A01,W_BST[p].A02)),ok=true;
            q=p;while(~q)pushup_SonBST(q),q=Fath_SonBST[q];
        }
        else ok=p==Son_BST[1][Fath_BST[p]];
        p=Fath_BST[p];
    }
}
int main()
{
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#endif
    uint n,q;scanf("%u%u",&n,&q);
    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),Fath_BST[dfs2(0)]=-1;
    for(uint t=0;t<q;t++)
    {
        static chr Op[5];static uint U[100005],V[100005];static llt W[100005];
        scanf("%s",Op);uint u,v;llt w;
        if(*Op=='+')scanf("%u%u%lld",&u,&v,&w),U[t]=--u,V[t]=--v,W[t]=w;else scanf("%u",&u),w=-W[--u],v=V[u],u=U[u];
        uint r=lca(u,v);addA(r,w),addB(u,w),addB(v,w),addB(r,-2*w);
        while(Q2.size()&&Q1.top()==Q2.top())Q1.pop(),Q2.pop();
        printf("%lld\n",Q1.top());
    }
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 1ms
memory: 15432kb

input:

23 45
2 1
2 17
1 4
12 1
22 7
3 7
6 2
14 9
5 1
21 23
18 1
2 3
13 3
20 6
4 11
1 15
4 19
17 21
7 9
8 3
4 16
8 10
+ 16 14 42
+ 2 18 73
+ 9 19 100
+ 17 6 8
+ 20 22 78
+ 10 23 67
+ 15 16 7
+ 14 10 83
- 2
+ 8 22 77
+ 4 13 87
+ 2 4 50
+ 23 18 92
+ 3 23 92
+ 5 10 35
+ 5 1 27
- 13
- 3
+ 14 14 15
+ 18 3 34
+ 3...

output:

42
115
215
223
301
368
375
458
385
462
549
599
691
783
818
845
753
653
668
702
762
786
694
695
765
839
864
914
1010
1038
1079
1158
1240
1315
1341
1291
1346
1398
1494
1499
1458
1513
1564
1581
1662

result:

ok 45 lines

Test #2:

score: 10
Accepted
time: 2ms
memory: 13344kb

input:

78 63
10 4
29 38
9 19
65 55
13 31
42 23
7 15
13 1
28 34
6 4
2 47
77 54
25 1
19 55
57 48
2 35
51 7
24 1
11 23
75 32
25 67
37 69
19 21
62 74
21 27
8 56
3 1
22 58
33 14
36 40
1 5
4 22
22 32
78 76
66 65
70 64
26 1
1 29
62 64
43 15
36 49
19 46
5 73
41 53
65 71
61 46
68 39
1 17
14 7
33 62
60 63
7 9
18 28
...

output:

904
1354
1888
2639
1735
2139
2372
3315
3842
4313
5294
6123
5142
6033
6997
7422
8061
8676
8272
8458
8492
9381
8931
9207
8736
8900
9564
10073
10469
10581
11570
11589
12046
12899
13128
13820
13881
14778
14545
15307
15195
16095
17081
17910
18298
18886
18886
18886
19808
20687
21035
21581
22518
23305
2404...

result:

ok 63 lines

Test #3:

score: 10
Accepted
time: 1ms
memory: 15464kb

input:

89 97
12 21
35 12
27 51
66 72
9 5
51 56
65 52
60 75
34 53
11 30
54 49
89 84
33 43
66 60
15 10
44 47
78 72
82 42
32 13
29 81
6 2
22 18
17 6
40 21
15 23
8 7
11 16
38 2
14 1
15 29
79 84
1 4
25 9
12 39
49 12
15 37
59 24
65 71
46 73
17 20
62 11
87 40
33 28
12 4
5 3
69 36
48 36
42 9
24 45
18 14
27 1
52 48...

output:

1089540208
1759220800
2546484532
4230712681
6371901183
7774014584
5632826082
5925957371
7952282056
8074251425
9519004213
8849323621
10415748675
10690169447
11813499691
11906187227
13991001411
11964676726
12578914249
12578914249
14515987793
15416467423
13850042369
11765228185
12726918299
13983101093
...

result:

ok 97 lines

Test #4:

score: 10
Accepted
time: 232ms
memory: 39372kb

input:

100000 100000
52336 52334
53869 53867
16732 19792
65544 65549
27367 23422
67961 67956
49048 33887
60632 60637
93826 93822
25325 8799
21288 10911
43533 46232
20651 19064
56161 56163
46617 5394
12318 49023
71818 71819
47059 3840
10278 12051
13859 13915
24747 3591
23064 48399
370 1304
11451 28543
11004...

output:

83
633
708
783
1548
2026
2691
3419
4280
4374
5121
6031
6947
7582
8072
8180
8391
8462
8615
9456
9723
10613
11355
12003
12716
12756
13385
13779
13802
14780
14829
15437
15872
16691
17438
18428
19351
19698
19799
20385
20472
20960
21252
21396
21413
21511
21858
21872
22026
22184
22624
23300
24193
25193
25...

result:

ok 100000 lines

Test #5:

score: 10
Accepted
time: 234ms
memory: 32116kb

input:

95677 98173
3340 3973
94173 94170
35297 5013
58731 58728
67614 67618
86454 86451
25595 15771
77275 77277
25332 20520
46497 46498
25941 35331
42294 42292
50457 50452
89704 89701
51426 51424
67050 67045
58437 58436
73785 73782
78312 78317
21909 3074
58903 58899
14297 25726
50060 50064
63798 63794
8628...

output:

1900644205
1946927684
2759299849
3390928670
3627190657
4340157873
6384135762
7966963280
8878768263
10810461477
12698241005
13683799636
15556286164
17160972456
18338672233
19668925940
20133417800
21047167002
22424995453
23350540833
25291671140
25496566536
27386151399
28769001460
30038231490
302037243...

result:

ok 98173 lines

Test #6:

score: 10
Accepted
time: 245ms
memory: 32428kb

input:

99998 99997
363 14746
354 134
58807 58803
99668 99667
87741 87745
1025 1590
2096 1793
65402 65403
56473 56475
73129 73127
94399 94404
71937 71933
89969 89971
1543 7005
1776 13484
96923 96922
47845 47843
90728 90726
8450 6598
81818 81815
36987 36985
51102 51107
97018 97017
7093 8058
59277 59281
36428...

output:

928
1079
1301
1410
1672
2055
2336
3261
3747
4195
4676
5315
5683
6472
6804
7490
7594
8217
8568
9270
9925
9972
10947
11360
11888
12740
12768
12983
13293
13915
14258
14501
14566
14742
15617
16197
17048
17763
18395
19286
19497
19749
20227
21040
21647
22067
22337
22550
22993
23220
23546
24161
24472
24824...

result:

ok 99997 lines

Test #7:

score: 10
Accepted
time: 263ms
memory: 38048kb

input:

98134 97241
69878 69877
18311 9613
90751 90753
35760 17785
69470 69475
3792 10137
23917 26686
94684 94680
95049 95047
88474 88477
28924 48715
79475 79478
97977 97981
12335 7320
80471 80468
69488 69492
94740 94742
96936 96932
29480 37380
28265 45159
22779 20652
64959 64957
69176 69175
80119 80114
185...

output:

2057533475
2293411568
3897759032
3909264883
2304917419
2943495532
3948680786
4236363587
6195907936
4236363587
2178830112
1540251999
1807983129
2108758108
2097252257
2718708571
2812846322
2980211652
2812846322
2576968229
4031194380
4360342160
3355156906
4715945863
5745187483
5847919006
6589762671
794...

result:

ok 97241 lines

Test #8:

score: 10
Accepted
time: 278ms
memory: 37900kb

input:

100000 100000
23954 12295
47529 47524
91373 91378
15638 6925
15454 32962
9038 5615
16793 6606
90092 90088
77082 77080
18944 8974
62796 62797
22321 17972
52896 52898
51414 51417
48800 48804
80453 80458
45838 45833
1027 1838
93017 93021
54313 54310
1139 272
16249 15479
81196 81192
97845 97844
48839 48...

output:

389
807
1120
1996
2269
3071
3303
4293
4749
4784
5439
6325
6645
6872
6640
7108
7224
6756
6982
7572
8519
9480
10359
9970
9514
10033
9147
9987
9674
10600
10957
10438
10735
10860
11520
12031
12940
13164
13290
14091
14285
15149
15646
15818
15646
16390
17252
18049
18903
19211
20081
20908
21366
21331
21760...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 292ms
memory: 33480kb

input:

99938 99173
29210 29209
24805 24806
18902 18901
84977 84976
21151 21150
33914 33911
26605 26606
15309 14605
9229 7297
12797 10600
70506 70511
19355 19360
39615 39611
5058 15762
33588 33587
50269 50264
29467 29470
25154 25151
34950 34947
77168 77166
51154 51153
93763 93761
81266 81270
29625 29629
789...

output:

1986671667
3785768400
5121718492
6046130937
4247034204
6214576849
8118400899
6150858254
8227376394
6150858254
8051397217
8887528821
9337071717
10106783275
10995952205
12130975145
13255736624
12331324179
13139730970
13592218933
12457195993
11568027063
12019696759
12500941036
12913181587
15036478591
1...

result:

ok 99173 lines

Test #10:

score: 10
Accepted
time: 269ms
memory: 39376kb

input:

100000 100000
49975 13565
10650 36102
12276 5900
88308 88304
7795 34194
58024 58027
22072 12288
54859 54863
93061 93057
24812 8732
731 1001
7782 9774
63659 63655
87209 87205
69971 69972
54531 54534
9102 46324
14825 32543
3555 32704
18786 11252
11218 6948
54503 54498
4133 14042
54680 54677
52955 5295...

output:

7
66
59
494
1260
1759
1700
1201
2069
2899
3249
3886
4068
3886
3536
4419
5187
6067
6146
6399
6763
7131
7574
7787
7419
7206
6338
6970
7843
8258
7492
7958
8016
8750
9540
9482
10125
9242
9406
9854
10276
10276
10276
10648
10412
10568
11491
12411
12628
12828
11955
12700
13080
13853
14320
14443
15012
15263...

result:

ok 100000 lines

Extra Test:

score: 0
Extra Test Passed