QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379018#4924. 蜘蛛爬树27455185850 972ms1012308kbC++205.6kb2024-04-06 15:49:552024-04-06 15:49:56

Judging History

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

  • [2024-04-06 15:49:56]
  • 评测
  • 测评结果:0
  • 用时:972ms
  • 内存:1012308kb
  • [2024-04-06 15:49:55]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1000001;
int n,m,q,b[N];
ll d[N],d2[N];
vector<pair<int,ll>> a[N];
vector<int> e[N];
vector<pair<pair<int,int>,pair<ll,int>>> c;
struct pt
{
    ll x,y;
    pt() {}
    pt(ll x,ll y):x(x),y(y) {}
};
bool cmp(const pt &x,const pt &y)
{
    return x.x<y.x;
}
void insert(vector<pt> &s,const pt &p)
{
    while(s.size()>1)
    {
        pt p1=s.back(),p2=*prev(prev(s.end()));
        if((p1.y-p2.y)*(p.x-p2.x)<(p.y-p2.y)*(p1.x-p2.x)) s.pop_back();
        else break;
    }
    s.push_back(p);
}
void query(const vector<pt> &s,const vector<pair<pair<ll,ll>,int>> &x)
{
    if(s.empty()) return;
    auto p=s.begin();
    for(auto i:x)
    {
        while(next(p)!=s.end())
        {
            auto p2=next(p);
            if((p2->y-p->y)>i.first.first*(p2->x-p->x)) ++p;
            else break;
        }
        d[i.second]=min(d[i.second],p->x*i.first.first-p->y+i.first.second);
    }
}
struct sgt
{
    vector<pt> a[N];
    struct tree
    {
        int l,r;
        vector<pt> s;
        vector<pair<pair<ll,ll>,int>> q;
    }T[N<<2];
    void pushup(int x)
    {
        auto p1=T[x<<1].s.begin(),p2=T[x<<1|1].s.begin();
        while(p1!=T[x<<1].s.end()||p2!=T[x<<1|1].s.end())
        {
            if(p1!=T[x<<1].s.end()&&(p2==T[x<<1|1].s.end()||p1->x<=p2->x)) insert(T[x].s,*p1),++p1;
            else insert(T[x].s,*p2),++p2;
        }
    }
    void build(int x,int l,int r)
    {
        T[x].l=l,T[x].r=r;
        if(l==r)
        {
            sort(a[l].begin(),a[l].end(),cmp);
            for(auto i:a[l]) insert(T[x].s,pt(i.x,-i.y));
            return;
        }
        int z=l+r>>1;
        build(x<<1,l,z);
        build(x<<1|1,z+1,r);
        pushup(x);
    }
    void add(int x,int l,int r,ll q,ll k,int t)
    {
        if(T[x].l>=l&&T[x].r<=r)
        {
            T[x].q.push_back(make_pair(make_pair(q,k),t));
            return;
        }
        int z=T[x].l+T[x].r>>1;
        if(l<=z) add(x<<1,l,z,q,k,t);
        if(r>z) add(x<<1|1,l,r,q,k,t);
    }
    void sum(int x)
    {
        // printf("%d %d:\n",T[x].l,T[x].r);
        // for(auto i:T[x].s) printf("(%d,%d) ",i.x,i.y);printf("\n");
        // for(auto i:T[x].q) printf("%lld %lld %d\n",i.first.first,i.first.second,i.second);printf("\n");
        query(T[x].s,T[x].q);
        if(T[x].l==T[x].r) return;
        sum(x<<1);
        sum(x<<1|1);
    }
}T1,T2,T3;
namespace tc
{
    int tot;
    struct tree
    {
        int f,s,d,t,z,b,bm;
        ll h;
    }T[N];
    void dfs1(int x)
    {
        T[x].s=1;
        T[x].d=T[T[x].f].d+1;
        for(auto i:a[x])
        {
            if(i.first==T[x].f) continue;
            T[i.first].f=x;
            T[i.first].h=T[x].h+i.second;
            dfs1(i.first);
            T[x].s+=T[i.first].s;
            if(T[i.first].s>T[T[x].z].s) T[x].z=i.first;
        }
    }
    void dfs2(int x,int t)
    {
        T[x].b=++tot;
        T[x].t=t;
        if(T[x].z) dfs2(T[x].z,t);
        for(auto i:a[x])
        {
            if(i.first==T[x].f||i.first==T[x].z) continue;
            dfs2(i.first,i.first);
        }
        T[x].bm=tot;
    }
    void solve(int x,int y,ll p,int t)
    {
        int px=x,py=y;
        while(T[x].t!=T[y].t)
        {
            if(T[T[x].t].d>T[T[y].t].d)
            {
                T1.add(1,T[T[x].t].b,T[x].b,p,0,t);
                x=T[T[x].t].f;
            }
            else
            {
                T1.add(1,T[T[y].t].b,T[y].b,p,0,t);
                y=T[T[y].t].f;
            }
        }
        if(T[x].d>T[y].d) swap(x,y);
        T1.add(1,T[x].b,T[y].b,p,0,t);
        int z=x;
        while(T[x].t!=1)
        {
            T2.add(1,T[T[x].t].b,T[x].b,p,T[z].h*2,t);
            x=T[T[x].t].f;
        }
        T3.add(1,T[px].b,T[px].bm,p,-T[px].h*2,t);
        T3.add(1,T[py].b,T[py].bm,p,-T[py].h*2,t);
        d2[t]+=T[px].h+T[py].h-T[z].h*2;
    }
    void dfs(int x)
    {
        if(T[x].z)
        {
            dfs(T[x].z);
            swap(e[x],e[T[x].z]);
        }
        for(auto i:a[x])
        {
            if(i.first==T[x].f||i.first==T[x].z) continue;
            dfs(i.first);
            for(auto j:e[i.first])
            {
                e[x].push_back(j);
                T1.a[T[x].b].push_back(pt(b[j],(T[j].h-T[x].h)*2));
                T2.a[T[x].b].push_back(pt(b[j],(T[j].h-T[x].h)*2-T[x].h*2));
            }
        }
        T3.a[T[x].b].push_back(pt(b[x],T[x].h*2));
    }
}
bool cmp2(const pair<pair<int,int>,pair<ll,int>> &a,const pair<pair<int,int>,pair<ll,int>> &b)
{
    return a.second.first>b.second.first;
}
int main()
{
    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n-1;++i)
    {
        int x,y;
        ll w;
        scanf("%d%d%lld",&x,&y,&w);
        a[x].push_back(make_pair(y,w));
        a[y].push_back(make_pair(x,w));
    }
    tc::dfs1(1);
    tc::dfs2(1,1);
    tc::dfs(1);
    T1.build(1,1,n);
    T2.build(1,1,n);
    T3.build(1,1,n);
    for(int i=1;i<=q;++i)
    {
        ll x,y;
        scanf("%lld%lld",&x,&y);
        c.push_back(make_pair(make_pair((x-1)%n+1,(y-1)%n+1),make_pair(abs((x-1)/n-(y-1)/n),i)));
        d[i]=1e18;
    }
    sort(c.begin(),c.end(),cmp2);
    for(auto i:c)
    {
        tc::solve(i.first.first,i.first.second,i.second.first,i.second.second);
    }
    T1.sum(1);
    T2.sum(1);
    T3.sum(1);
    for(int i=1;i<=q;++i)
    {
        printf("%lld\n",d[i]+d2[i]);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 97ms
memory: 739512kb

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:

-2800666168374
5193135868730
-24639918402
-727494876406
-2266062068813
-875904043398
5180657804587
-1612635544268
1579562268765
6816887152082
2947098176148
-3022535892548
-3660413713798
3184132438661
-2280893908887
-698645872120
10850093765785
3580001748433
-1360312210583
3950766801337
2762853666871...

result:

wrong answer 1st lines differ - expected: '6130845802041', found: '-2800666168374'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Memory Limit Exceeded

Test #20:

score: 0
Memory Limit Exceeded

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:

35798224
23031550
37768293
-130104316
-554884880
253997548
-126089628
-664424705
198846742
-97998622
2551186
-158480383
-217449422
-187149534
-586419062
-231287680
30616135
395424968
-171024038
-128026893
377180805
-559305886
-148447314
21299082
161419316
225057218
-62607059
-180540484
-328510329
-2...

result:


Subtask #4:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 935ms
memory: 995824kb

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:

30879707146720545
10256847659574969
39779162642086856
37874259713077280
4572254107007228
21036894538073105
19643034267278932
-10299643483869450
-67051224049267249
-26440387533986253
-36775824192377784
43240415729103085
-7539259761884765
-67284504905127611
20103636996478280
30494908103472596
31225634...

result:

wrong answer 1st lines differ - expected: '29294995135992468', found: '30879707146720545'

Subtask #5:

score: 0
Memory Limit Exceeded

Test #36:

score: 0
Memory Limit Exceeded

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:

-508528305937
-393823227341
-371409669943
-416962948145
-315700811759
-118075218123
-418998904464
-553667286695
-217738479019
-49630859017
-270235672431
19935820264
18911742964
-946293976823
-342534900669
-653624152696
-65292892144
-157115368402
-452680916651
-824297781396
-646565747008
-13740811267...

result:


Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 972ms
memory: 1012308kb

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:

185275174611557
167625925039084
156773348583308
18346574552169
176477352654424
165294367998912
77251557172423
272154304424547
12958160584791
263872049532303
81484308130674
333587540978134
153578669250347
87558525056526
235060041073986
282694295250912
221932443234545
355194197483023
271921842532355
7...

result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%