QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#390947#4924. 蜘蛛爬树Graphcity9 1150ms122084kbC++206.4kb2024-04-16 09:25:072024-04-16 09:25:08

Judging History

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

  • [2024-04-16 09:25:08]
  • 评测
  • 测评结果:9
  • 用时:1150ms
  • 内存:122084kb
  • [2024-04-16 09:25:07]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define i128 __int128
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=2e5,lim=1e9;
const ll inf=8e18;

int n,m,q,a[Maxn+5]; ll ans[Maxn+5],dis[Maxn+5],dxv[Maxn+5];
int fa[Maxn+5],dfn[Maxn+5],st[Maxn+5][20],dx[Maxn+5],cur;
int siz[Maxn+5],dep[Maxn+5],son[Maxn+5],top[Maxn+5];
vector<pair<int,ll>> v[Maxn+5],w[Maxn+5],w2[Maxn+5];
vector<array<int,3>> qr[Maxn+5]; vector<int> w1[Maxn+5];
struct Line{int k; ll b; inline ll F(int x){return 1ll*k*x+b;}};
inline Line operator-(Line x,Line y) {return Line{x.k-y.k,x.b-y.b};}
inline bool operator<(Line x,Line y) {return x.k<y.k || (x.k==y.k && x.b<y.b);}
inline i128 operator*(Line x,Line y) {return (i128)x.k*y.b-(i128)y.k*x.b;}
inline bool chk(Line x,Line y,int k) {return (y.b-x.b)<=1ll*k*(y.k-x.k);}
struct Node{int l,r; Line k;};
struct LCT
{
    Node t[Maxn*200+5]; int tot,tmp;
    #define ls(x) t[x].l
    #define rs(x) t[x].r
    inline void Insert(int &p,int l,int r,Line k)
    {
        if(!p) {t[p=++tot]=Node{0,0,k}; return;}
        int mid=(l+r)>>1; if(t[p].k.F(mid)>k.F(mid)) swap(t[p].k,k);
        if(k.F(l)<t[p].k.F(l)) Insert(ls(p),l,mid,k);
        if(k.F(r)<t[p].k.F(r)) Insert(rs(p),mid+1,r,k);
    }
    inline ll Find(int p,int l,int r,int x)
    {
        if(!p) return inf; if(l==r) return t[p].k.F(x); int mid=(l+r)>>1;
        return min(t[p].k.F(x),(x<=mid)?Find(ls(p),l,mid,x):Find(rs(p),mid+1,r,x));
    }
    #undef ls
    #undef rs
} T;

inline int GetID(int x,int y) {return dfn[x]<dfn[y]?x:y;}
inline int LCA(int x,int y)
{
    if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y);
    int len=__lg(y-x++); return GetID(st[x][len],st[y-(1<<len)+1][len]);
}
inline ll Dis(int x,int y) {return dis[x]+dis[y]-2*dis[LCA(x,y)];}
inline void dfs1(int x,int f)
{
    fa[x]=f,dep[x]=dep[f]+1,siz[x]=1;
    for(auto [y,z]:v[x]) if(y!=f)
    {
        dis[y]=dis[x]+z,dfs1(y,x),siz[x]+=siz[y];
        if(siz[y]>siz[son[x]]) son[x]=y;
    }
}
inline void dfs2(int x,int t)
{
    dfn[x]=++cur,st[cur][0]=fa[x],top[x]=t;
    if(son[x]) dfs2(son[x],t);
    for(auto [y,z]:v[x]) if(y!=fa[x] && y!=son[x]) dfs2(y,y);
}
inline void Work(int x,int y,int id)
{
    while(top[x]!=top[y]) w[x].emplace_back(id,0ll),w1[x].push_back(id),x=fa[top[x]];
    qr[top[y]].push_back({y,x,id});
}
inline void Upd(int x,int id)
{
    for(int i=x;i;i=fa[top[i]])
        w[i].emplace_back(id,dis[x]-dis[i]),w2[i].emplace_back(id,dis[x]-dis[i]);
}
namespace DSU
{
    int rt;
    inline void Add(int x) {T.Insert(rt,0,lim,Line{a[x],dis[x]});}
    inline void dfs2(int x) {Add(x); for(auto [y,z]:v[x]) if(y!=fa[x]) dfs2(y);}
    inline void dfs1(int x,int tp)
    {
        for(auto [y,z]:v[x]) if(y!=fa[x] && y!=son[x]) dfs1(y,0);
        if(son[x]) dfs1(son[x],1); Add(x);
        for(auto [y,z]:v[x]) if(y!=fa[x] && y!=son[x]) dfs2(y);
        if(!w[x].empty()) for(auto [id,z]:w[x])
            ans[id]=min(ans[id],T.Find(rt,0,lim,dx[id])-dis[x]+z);
        if(!tp) rt=T.tot=0;
    }
}
struct Polygon
{
    vector<Line> v;
    inline void Add(Line k)
    {
        while(v.size()>1)
        {
            if((k-v.back())*(v.back()-v[v.size()-2])>=0) v.pop_back();
            else break;
        } v.push_back(k);
    }
    inline ll Get(int k)
    {
        int sz=v.size(),l=0,r=sz-1; while(l<r)
        {
            int mid=(l+r)/2;
            if(chk(v[mid],v[mid+1],k)) l=mid+1; else r=mid;
        } return v[l].F(-k);
    }
    inline void Clear() {vector<Line>().swap(v);}
};
struct SegTree
{
    Polygon t[Maxn*4+5];
    #define ls(x) (x<<1)
    #define rs(x) (x<<1|1)
    inline void Build(int l,int r,int p)
    {
        t[p].Clear(); if(l==r) return;
        int mid=(l+r)>>1; Build(l,mid,ls(p)),Build(mid+1,r,rs(p));
    }
    inline void Insert(int l,int r,int p,int pos,Line k)
    {
        t[p].Add(k); if(l==r) return; int mid=(l+r)>>1;
        if(pos<=mid) Insert(l,mid,ls(p),pos,k); else Insert(mid+1,r,rs(p),pos,k);
    }
    inline ll Count(int nl,int nr,int l,int r,int p,int k)
    {
        if(l<=nl && nr<=r) return t[p].Get(k);
        int mid=(nl+nr)>>1; ll res=inf;
        if(l<=mid) res=min(res,Count(nl,mid,l,r,ls(p),k));
        if(r>mid) res=min(res,Count(mid+1,nr,l,r,rs(p),k));
        return res;
    }
    #undef ls
    #undef rs
} Tr;
namespace Chain
{
    int r1,r2; vector<pair<Line,int>> vx;
    inline void Add(int x,ll k1,ll k2,int id)
    {
        vx.emplace_back(Line{-a[x],k1},id);
        T.Insert(r1,0,lim,Line{a[x],k1}),T.Insert(r2,0,lim,Line{a[x],k2});
    }
    inline void dfs1(int x,ll k1,ll k2,int id) {Add(x,k1,k2,id); for(auto [y,z]:v[x]) if(y!=fa[x]) dfs1(y,k1+z,k2+z,id);}
    inline void dfs(int x)
    {
        static int st[Maxn+5]; int top=0; r1=0,r2=0,T.tot=0;
        for(int i=x;i;i=son[i]) st[++top]=i; int lw=st[top];
        vector<pair<Line,int>>().swap(vx);
        For(_,1,top)
        {
            int i=st[_]; Add(i,0ll,dis[lw]-dis[i],_);
            for(auto [j,z]:v[i]) if(j!=fa[i] && j!=son[i]) dfs1(j,z,dis[lw]-dis[i]+z,_);
            for(auto id:w1[i]) ans[id]=min(ans[id],T.Find(r1,0,lim,dx[id]));
            for(auto [id,z]:w2[i]) ans[id]=min(ans[id],T.Find(r2,0,lim,dx[id])+z-(dis[lw]-dis[i]));
        }
        sort(vx.begin(),vx.end()); Tr.Build(1,top,1);
        for(auto [k,i]:vx) Tr.Insert(1,top,1,i,k);
        for(auto [l,r,id]:qr[x])
        {
            l=dfn[l]-dfn[x]+1,r=dfn[r]-dfn[x]+1;
            ans[id]=min(ans[id],Tr.Count(1,top,l,r,1,dx[id]));
        }
        for(int i=x;i;i=son[i]) for(auto [j,z]:v[i])
            if(j!=fa[i] && j!=son[i]) dfs(j);
    }
}

int main()
{
    // freopen("1.in","r",stdin);

    cin>>n>>m>>q;
    For(i,1,n) cin>>a[i];
    For(i,1,n-1)
    {
        int a,b; ll c; cin>>a>>b>>c; c*=2;
        v[a].emplace_back(b,c),v[b].emplace_back(a,c);
    }
    dfs1(1,0),dfs2(1,1);
    For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
        st[i][j]=GetID(st[i][j-1],st[i+(1<<j-1)][j-1]);
    For(i,1,q)
    {
        ll s,t; cin>>s>>t;
        int p=(s-1)/n,x=s-1ll*p*n,q=(t-1)/n,y=t-1ll*q*n,l=LCA(x,y);
        ans[i]=inf,dx[i]=abs(p-q),dxv[i]=Dis(x,y)/2;
        if(x!=l) Work(x,l,i); if(y!=l) Work(y,l,i); Upd(l,i);
    }
    DSU::dfs1(1,1); Chain::dfs(1);
    For(i,1,q) printf("%lld\n",ans[i]+dxv[i]);
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 4ms
memory: 8152kb

input:

97 99 1000
763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...

output:

6130845802041
10806758605627
3440559556796
5426989115608
4458875959622
1566659300400
7908007295597
1846030561682
5112206409383
6968388472340
4706970599850
5270158948178
4633066810868
3176122148295
2331646415266
961435137842
14353365296423
9675072605938
4256954122467
7333255321628
8376795894537
12319...

result:

ok 1000 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 8232kb

input:

96 100 1000
31199641 534644486 57980198 794620020 322548308 524438614 467991232 68617179 243820212 229414440 471419229 316085673 271698528 136252783 926625435 615138376 200446739 551914057 483498389 879166147 512229523 45850421 337184110 799426876 46405170 427929494 235848997 861270528 291868400 616...

output:

2436336301732
4467388472930
6498834342013
6450642313333
4049880787954
7509100670176
5831628235154
4150554274586
3112250048344
202594784082
2974050754796
8714737807242
7727115169865
1321297431165
3071603311467
4662413775237
5469193332429
2306749862693
6240860740176
1297819731517
5602374629655
5876108...

result:

ok 1000 lines

Test #3:

score: -3
Wrong Answer
time: 5ms
memory: 12328kb

input:

96 100 1000
557703101 38662752 91559144 406758463 248251717 279124287 667387330 272252891 892434115 281731667 162886140 786660171 350559478 909940602 476034354 78826379 748607300 381191755 708777514 223906483 954905399 405424569 356033791 565979037 5787205 21241249 399771402 280058652 527147793 5875...

output:

82007488946
86777173467
35481695596
11498756054
87983213070
37171191055
33172639202
31451029430
111662176795
31626589074
105218154775
57116127519
14488184465
20368758481
41150521804
57639739744
45269689956
24620398400
51392609182
47597028824
72097558763
13572235163
78364419227
40255815091
1195379951...

result:

wrong answer 1st lines differ - expected: '80606469890', found: '82007488946'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #20:

score: 0
Wrong Answer
time: 1150ms
memory: 122084kb

input:

200000 20 200000
679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...

output:

920563165
270738856
355012553
363898450
515535908
734168762
81197110
448355845
204186827
966151314
377621564
856252543
311456222
368700872
197258906
567302636
172379629
579171621
1043838058
244996663
621435809
278057792
727965316
573783312
395879848
500677226
891900111
1031612062
771021332
691010101...

result:

wrong answer 23rd lines differ - expected: '727463012', found: '727965316'

Subtask #4:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 870ms
memory: 112252kb

input:

200000 1000000000 200000
28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...

output:

29294995135992468
9003943574137677
39324997066279292
37544709020512848
57388992119827952
54425124319330092
19450449300737912
25838911017710871
2608104102967357
32395369352281774
5765752637876701
65609495812941401
57820177390587134
1971831067795873
19213682025389514
30244870693646792
3672338761985429...

result:

wrong answer 41st lines differ - expected: '36481368792729553', found: '36504635587802551'

Subtask #5:

score: 9
Accepted

Test #36:

score: 9
Accepted
time: 579ms
memory: 89604kb

input:

199918 1000000000 199903
1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...

output:

1352416884531
1380463318391
923920163167
1525224977139
1405019709299
869269749781
715671043328
876194052054
1358007874327
127994985855
1230162209719
1532026808855
611656467332
1023855959729
414792924571
1316679734677
827308370883
1265411315424
821484360433
1051517948640
837509712760
582943943131
457...

result:

ok 199903 lines

Test #37:

score: 0
Accepted
time: 559ms
memory: 89060kb

input:

200000 1000 200000
809770918 700177243 142627650 840666719 799717263 288840787 130614153 965150450 584417569 833256629 453961603 553430999 842122932 156970995 233405993 462368588 449589390 97217337 576814616 526506175 16887352 919946415 588340411 47310125 508028561 746882745 289969878 38349443 85588...

output:

1585694392495
706038536805
586801212025
729763504879
1121912701709
602929530934
1384874490966
932809860298
1786651350814
1173133997984
642188971333
1847564817170
874110129257
1634207197990
1165001912684
860420326934
364758620851
736767366986
901294347345
1499330839732
451636930949
1002710230684
1556...

result:

ok 200000 lines

Test #38:

score: 0
Accepted
time: 700ms
memory: 90084kb

input:

200000 1000000000 200000
399998482 399998882 643012112 481981456 399998451 475990021 399997292 399997409 399996369 399998092 399998185 399998416 399998701 399997027 399996347 1000000000 411997672 399996237 399997188 402404134 399996973 399998072 459327897 399997196 399997360 606704265 399997369 3999...

output:

56425935917250335
348929904541748910
43150321666095229
218746357373815571
108846332361563136
211578526026780722
142755080047590213
244555928973138123
59355666550218703
274305014995294225
171177308635990844
94566903236734112
84270300399685207
317423517245573254
902979060499211
14514565807335715
18696...

result:

ok 200000 lines

Test #39:

score: 0
Accepted
time: 785ms
memory: 107732kb

input:

199989 1000000000 199949
5101893 2534711 252776 33497 4575476 620658 35790 1061631 1362697 834917 2062598 2789238 2540552 2557066 725856 2407848 4266144 1731334 653868 4676650 235573 2010805 1576557 922173 617762 1140093 387911 618359 2084018 2717580 9938 4014950 411349 3801906 341206 665844 2556003...

output:

376419619600
353028349944
783455928283
427146243318
508001272847
231025894377
614377184831
496116219491
384142701402
337878147372
528478063399
414595122323
604998898988
244135680083
319848781263
358386785447
481117281935
464006706964
356458898506
260105342030
610113746365
259007651455
414991108424
2...

result:

ok 199949 lines

Test #40:

score: 0
Accepted
time: 779ms
memory: 103208kb

input:

200000 1000000000 200000
1101540 573488 61066 1014872 39283 626062 84341 591377 109026 505272 1339 74452 729192 49315 521939 959958 249731 940337 56264 1071790 609623 239862 57448 809987 464526 111430 226312 124386 673550 421690 211347 45875 138962 705453 739456 464892 44238 52980 905593 205558 5198...

output:

655436303263
616441802310
638361564730
586321577191
519122088245
660130086237
389806954608
241891011597
423594953230
510963332372
630353140994
627262451077
339051346548
308888235187
550167732447
354951509166
308776095000
597351022439
625625736560
772346222022
549689478477
667370706484
319926160326
2...

result:

ok 200000 lines

Test #41:

score: 0
Accepted
time: 0ms
memory: 14164kb

input:

1 2 5
1000000000
1 1
1 2
2 1
2 2
1 1

output:

0
1000000000
1000000000
0
0

result:

ok 5 lines

Test #42:

score: 0
Accepted
time: 0ms
memory: 12080kb

input:

2 1 5
2 1
1 2 1000000000000
1 1
1 2
2 2
1 1
2 1

output:

0
1000000000000
0
0
1000000000000

result:

ok 5 lines

Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 967ms
memory: 96356kb

input:

200000 1000000000 200000
81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...

output:

516260625003899
380880451347644
183401242058615
56975236749493
349851829300288
188845759476214
188011317678919
414887287533565
111834744858133
305218494040213
227244584301956
365579485207024
201761449059479
246263150359463
468212144364502
389353276591541
207814284476264
341801277159919
4270404442188...

result:

wrong answer 30th lines differ - expected: '301617487719160', found: '304182269225954'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%