QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#390945#4924. 蜘蛛爬树Graphcity0 1131ms120516kbC++206.4kb2024-04-16 09:23:342024-04-16 09:23:34

Judging History

This is the latest submission verdict.

  • [2024-04-16 09:23:34]
  • Judged
  • Verdict: 0
  • Time: 1131ms
  • Memory: 120516kb
  • [2024-04-16 09:23:34]
  • Submitted

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=4e18;

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;
        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);
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 3ms
memory: 4108kb

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: -3
Wrong Answer
time: 3ms
memory: 4072kb

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:

wrong answer 385th lines differ - expected: '3401016475409', found: '3400978377471'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #20:

score: 0
Wrong Answer
time: 1131ms
memory: 120516kb

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
270270853
354544550
363430447
511945817
733700759
80159871
445511592
195762164
965683311
377153561
852662452
310988219
368700872
197079367
563712545
172090192
578703618
1043370055
240949924
620967806
274467701
727637767
573783312
395411845
500209223
891432108
1031438888
767431241
691010101...

result:

wrong answer 2nd lines differ - expected: '270738856', found: '270270853'

Subtask #4:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 907ms
memory: 108960kb

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 27th lines differ - expected: '4187509004260718', found: '4069277735247975'

Subtask #5:

score: 0
Wrong Answer

Test #36:

score: 0
Wrong Answer
time: 588ms
memory: 88280kb

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:

1351141202815
1379187636675
923282619411
1523949295423
1404441002703
868933501969
715027534988
874918370338
1357571500103
127658738043
1229016271347
1530976046375
611315978560
1022580278013
414456676759
1316343486865
826823498739
1265074806080
820208678717
1050242266924
836234031044
581668261415
456...

result:

wrong answer 1st lines differ - expected: '1352416884531', found: '1351141202815'

Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 895ms
memory: 93584kb

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:

515388347672413
379547377554312
174698940951287
54467786704776
348518755506956
188298551415788
188011317678919
414887287533565
108926636669457
305218494040213
225504327141627
364871564813423
201214240999053
240924327462873
465854303532819
389353276591541
206301289906173
341801277159919
4270355784623...

result:

wrong answer 1st lines differ - expected: '516260625003899', found: '515388347672413'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%