QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431780#4401. PrizeSavu_Stefan_Catalin0 2099ms660564kbC++144.9kb2024-06-06 05:25:552024-06-06 05:25:56

Judging History

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

  • [2024-06-06 05:25:56]
  • 评测
  • 测评结果:0
  • 用时:2099ms
  • 内存:660564kb
  • [2024-06-06 05:25:55]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
#define int long long

using namespace std;
const int NMAX=1e6+505;
int cnt,ine[2][NMAX+5],lv[2][NMAX+5],n,t[2][NMAX+5],cen[2],invine[2][NMAX+5],k,q,T,id[NMAX+5],dp[NMAX+5],rmq[2][25][NMAX+5],imp[NMAX+5],invid[NMAX+5],rr[2][NMAX+5];
vector <int> v[2][NMAX+5];
pair<pair<int,int>,pair<int,int>> ras[NMAX+5];
int sz[NMAX+5],ap[NMAX+5],tt[NMAX+5],tv[NMAX+5],dp2[NMAX+5];
vector <pair<int,int>> vv[NMAX+5];
void dfs(int nod,int tata,int ind)
{
    ++cnt;
    ine[ind][nod]=cnt;
    lv[ind][nod]=lv[ind][tata]+1;
    for (int i=0;i<v[ind][nod].size();++i)
    {
        if (v[ind][nod][i]!=tata)
        {
            dfs(v[ind][nod][i],nod,ind);
        }
    }
}
void read(int ind)
{
    for (int i=1;i<=n;++i)
    {
        cin>>t[ind][i];
        if (t[ind][i]==-1) cen[ind]=i;
        else {v[ind][t[ind][i]].push_back(i);}
    }
    cnt=0;
    dfs(cen[ind],0,ind);
    for (int i=1;i<=n;++i)
    {
        invine[ind][ine[ind][i]]=i;
    }
}
int cmp(int x,int y)
{
    return ine[1][x]<ine[1][y];
}
int lca(int x,int y,int ind)
{
    if (lv[ind][x]>lv[ind][y]) swap(x,y);
    for (int i=20;i>=0;--i) {if (lv[ind][x]+(1<<i)<=lv[ind][y]) {y=rmq[ind][i][y];}}
    if (x==y) return x;
    for (int i=20;i>=0;--i) {if (rmq[ind][i][x]!=rmq[ind][i][y]) {x=rmq[ind][i][x]; y=rmq[ind][i][y];}}
    return x;
}
void bfs(int nod)
{
    sz[nod]=ap[nod];
    for (int i=0;i<v[1][nod].size();++i)
    {
        bfs(v[1][nod][i]);
        sz[nod]+=sz[v[1][nod][i]];
    }
}
void mfs(int nod,int tata)
{
    for (int i=0;i<v[1][nod].size();++i)
    {
        if (sz[nod]==sz[v[1][nod][i]]) {mfs(v[1][nod][i],tata); return ;}
    }
    vv[tata].push_back({nod,0});
    tt[nod]=tata;
    for (int i=0;i<v[1][nod].size();++i)
    {
        if (sz[v[1][nod][i]])
        {
            mfs(v[1][nod][i],nod);
        }
    }
}
int findfirst(int x)
{
    if (ap[x]==1) return x;
    return findfirst(vv[x][0].first);
}
int findlast(int x)
{
    if (vv[x].size()==0) return x;
    return findlast(vv[x][vv[x].size()-1].first);
}
int sum(int x,int stop)
{
    if (x==stop) return 0;
    return tv[x]+sum(tt[x],stop);
}
void lfs(int nod)
{
    if (sz[nod]==1)
    {
        return ;
    }
    for (int i=0;i<vv[nod].size();++i)
    {
        lfs(vv[nod][i].first);
        if (i)
        {
            int x=findlast(vv[nod][i-1].first);
            int y=findfirst(vv[nod][i].first);
            int xx=invid[x],yy=invid[y];
            vv[nod][i-1].second=ras[xx].second.first-sum(x,vv[nod][i-1].first);
            tv[vv[nod][i-1].first]=vv[nod][i-1].second;
            vv[nod][i].second=ras[xx].second.second-sum(y,vv[nod][i].first);
            tv[vv[nod][i].first]=vv[nod][i].second;
        }
    }
    if (vv[nod].size()==1)
    {
        int xx=invid[nod];
        vv[nod][0].second=ras[xx].second.second;
        tv[vv[nod][0].first]=vv[nod][0].second;
    }
}
void ylf(int nod)
{
    for (int i=0;i<vv[nod].size();++i)
    {
        dp2[vv[nod][i].first]=dp2[nod]+vv[nod][i].second;
        ylf(vv[nod][i].first);
    }
}
void solve1()
{
    for (int i=1;i<=k;++i)
    {
        ap[id[i]]=1;
    }
    bfs(cen[1]);
    mfs(cen[1],0);
    int cen=vv[0][0].first;
    lfs(cen);
    ylf(cen);
}
void solve0()
{
    int ii=0;
    for (int i=1;i<=k;++i)
    {
        if (id[i]==cen[0]) {ii=i;}
    }
    dp[ii]=0;
    for (int i=ii+1;i<=k;++i)
    {
        dp[id[i]]=dp[id[i-1]]-ras[i-1].first.first+ras[i-1].first.second;
    }
    for (int i=ii-1;i>=1;--i)
    {
        dp[id[i]]=dp[id[i+1]]-ras[i].first.second+ras[i].first.first;
    }
}
void makermq(int ind)
{
    for (int i=1;i<=n;++i)
    {
        if (t[ind][i]==-1) rmq[ind][0][i]=0;
        else rmq[ind][0][i]=t[ind][i];
    }
    for (int j=1;j<=20;++j)
    {
        for (int i=1;i<=n;++i)
        {
            rmq[ind][j][i]=rmq[ind][j-1][rmq[ind][j-1][i]];
        }
    }
}

signed main()
{
    cin>>n>>k>>q>>T;
    read(0);
    read(1);
    for (int i=1;i<=k;++i)
    {
        imp[i]=invine[0][i];
        cout<<imp[i]<<" ";
    }
        cout<<endl;
    for (int i=1;i<=k;++i)
    {
        id[i]=imp[i];
    }
    sort(id+1,id+k+1,cmp);
    for (int i=1;i<=k;++i)
    {
        invid[id[i]]=i;
    }
    for (int i=1;i<k;++i)
    {
        cout<<"? "<<id[i]<<" "<<id[i+1]<<endl;
    }
    cout<<"!"<<endl;
    for (int i=1;i<k;++i)
    {
        cin>>ras[i].first.first>>ras[i].first.second>>ras[i].second.first>>ras[i].second.second;
    }
    solve0();
    makermq(0);
    makermq(1);
    solve1();
    for (int i=1;i<=T;++i)
    {
        int x,y;
        cin>>x>>y;
        if (x>y) swap(x,y);
        rr[1][i]=dp2[x]+dp2[y]-2*dp2[lca(x,y,1)];
        rr[0][i]=dp[x]+dp[y]-2*dp[lca(x,y,0)];
    }
    for (int i=1;i<=T;++i)
        cout<<rr[0][i]<<" "<<rr[0][i]<<'\n';
    cout.flush();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 1108ms
memory: 486504kb

input:

500000 64682 64681 100000
46115
470589
209303
2979
473162
343535
79503
299539
404621
102085
237721
279170
392890
165201
441593
456314
218991
358478
86614
410800
159785
169761
95368
285837
297549
370283
378974
26449
444381
39320
149913
404523
144109
174828
263837
49847
468694
478535
152644
216598
301...

output:

422989 414496 290928 388223 160563 301045 470257 259625 222733 231286 345214 169817 435263 277447 386014 210139 455433 225855 264772 199736 355788 288506 233893 146148 454958 267562 498596 183745 352665 151125 266374 43142 9414 204593 212097 311775 25324 300764 6643 94847 396968 428563 311355 255767...

result:

ok good job!

Test #2:

score: 0
Accepted
time: 1171ms
memory: 488464kb

input:

500000 90967 90966 100000
122547
312039
290084
118442
352297
175176
294396
496975
127062
90539
132654
408480
493670
419897
53432
141795
264165
60368
473480
5634
253119
64236
85346
422987
28583
262389
111931
271291
13577
415079
132797
256502
76402
265607
11274
289667
398726
32021
302401
410650
369760...

output:

3090 193269 3028 186608 498475 64618 82114 231445 7541 329983 134623 235591 70401 18906 403427 280451 146897 355174 160090 144279 193430 332022 488244 228900 80781 84465 218682 27818 6035 368489 155673 440755 443926 241570 193717 143661 374105 56616 323329 95909 337798 20531 236329 28564 437244 4969...

result:

ok good job!

Test #3:

score: -10
Wrong Answer
time: 1161ms
memory: 448368kb

input:

500000 68287 68286 100000
273928
229768
65518
144983
311611
494773
489379
439644
467893
456131
430188
247387
485565
272285
474827
476962
338340
365804
344570
390867
390170
456217
43185
447057
385874
305750
107742
230530
259907
252254
280920
16831
45761
185191
117450
55891
175190
255615
35904
14855
2...

output:

242387 146602 106115 32426 8390 3821 314935 201979 171459 413397 469146 119187 74265 167902 479051 182695 260054 235048 135315 280891 13044 240704 209304 211564 188960 481631 289686 273785 175837 385737 204887 288861 330677 315423 120726 278204 129910 396267 322633 472675 325914 329277 67326 391455 ...

result:

wrong answer wrong answer on the first integer of query #1: read 11194 but expected 14620

Subtask #2:

score: 0
Wrong Answer

Test #13:

score: 0
Wrong Answer
time: 1242ms
memory: 491344kb

input:

500000 88721 177440 100000
30974
23891
211201
125199
180489
387190
218020
498838
230147
307989
484136
257785
353027
304420
311738
169842
334090
486070
126212
328609
174959
368840
238722
418092
488389
226349
427271
457322
332454
12958
197530
264474
355717
482774
221286
282148
216441
266659
213750
628...

output:

63742 11431 300071 157785 268420 71772 84553 267656 174540 21500 451751 82419 58833 165916 94199 78203 263216 146169 306934 50728 338250 199716 469441 135516 133967 123248 375309 17045 459156 413018 49645 73720 188292 322328 493921 152164 219927 140202 236207 266137 180568 32077 371348 66876 354136 ...

result:

wrong answer wrong answer on the second integer of query #1: read 39120665 but expected 161335632

Subtask #3:

score: 0
Wrong Answer

Test #25:

score: 0
Wrong Answer
time: 936ms
memory: 484012kb

input:

500000 200 199 40000
76296
130139
291501
292412
139543
433345
372726
451574
18315
465578
324564
477223
237354
81532
65170
465332
342130
9670
193303
193680
129668
149532
268907
89969
398275
356210
324593
433492
482232
466692
135343
433758
102545
287283
432859
351864
305769
489532
101532
450535
295762...

output:

20242 414878 185020 125537 353357 496468 308518 188057 254952 120898 414314 11748 435424 326112 345902 271794 473882 337923 135188 438050 45188 88306 260313 116954 457474 435919 366460 431766 397351 392326 178950 199724 227083 282259 70917 121346 109196 193669 242154 12225 466790 155481 287973 15749...

result:

wrong answer wrong answer on the second integer of query #1: read 53295 but expected 11339644

Subtask #4:

score: 0
Wrong Answer

Test #37:

score: 0
Wrong Answer
time: 2099ms
memory: 660564kb

input:

1000000 1000 999 100000
678746
439069
32542
85937
936926
284219
461661
203235
533462
940676
230275
621140
780674
254931
562355
229273
201341
493976
358955
963527
880412
91220
474599
160086
698841
591551
718276
844558
39859
765917
34722
401724
219774
443004
682244
545401
968419
968020
354030
411187
1...

output:

545967 706162 53597 107558 776536 230611 572458 293457 390487 241653 638541 42868 433774 438059 293014 739962 25440 503383 628342 573629 887812 909797 805385 862282 382785 706534 190319 439139 648412 626240 131005 848982 269684 840650 376086 933701 18720 749474 336321 160119 795534 671698 201133 610...

result:

wrong answer wrong answer on the second integer of query #1: read 56703 but expected 820146018

Subtask #5:

score: 0
Skipped

Dependency #4:

0%