QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#823330#9905. 哈夫曼树xay54210 760ms26772kbC++142.8kb2024-12-20 21:51:562024-12-20 21:51:58

Judging History

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

  • [2024-12-20 21:51:58]
  • 评测
  • 测评结果:0
  • 用时:760ms
  • 内存:26772kb
  • [2024-12-20 21:51:56]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define D(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
using LL=long long;
const int N=200005;
int n,Q,fa[N],son[N][2],dep[N];
LL sum[N];
struct cmp{
    bool operator()(const int &x,const int&y)const{return sum[x]!=sum[y]?sum[x]<sum[y]:x<y;}
};
set<int,cmp>S[N];
int mx,tot,need;
bool solve(){
    int flag=1;
    rep(i,1,mx){
        flag&=sum[*--S[i].end()]<=sum[*S[i-1].begin()];
    }
    if(!flag)return 0;
    return tot==need;
}
int F(int u,int v){
    if(u<=n||v<=n)return 1;
    return max(sum[son[u][0]],sum[son[u][1]])<=min(sum[son[v][0]],sum[son[v][1]]);
}
void del(int p){
    
    auto it=S[dep[p]].find(p);
    if(it!=S[dep[p]].begin()){
        tot-=F(*prev(it),*it);
    }
    if(next(it)!=S[dep[p]].end()){
        tot-=F(*it,*next(it));
    }
    if(it!=S[dep[p]].begin()&&next(it)!=S[dep[p]].end()){
        tot+=F(*prev(it),*next(it));
    }
    S[dep[p]].erase(it);
}
void ins(int p){
    auto it=S[dep[p]].insert(p).first;
    if(it!=S[dep[p]].begin()){
        tot+=F(*prev(it),*it);
    }
    if(next(it)!=S[dep[p]].end()){
        tot+=F(*it,*next(it));
    }
    if(it!=S[dep[p]].begin()&&next(it)!=S[dep[p]].end()){
        tot-=F(*prev(it),*next(it));
    }
}
void add(int p,LL v){
	int old_p=p;
    while(p){
//        D("p=%d v=%lld\n",p,v);
        del(p);
        p=fa[p];
	}
	p=old_p;
	while(p){
        sum[p]+=v;
        ins(p);
        p=fa[p];
    }
}
int main(){
#ifdef xay5421
    freopen("a.in","r",stdin);
#endif
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>Q;
    rep(i,1,n)cin>>sum[i];
    rep(i,n+1,n*2-1){
        cin>>son[i][0]>>son[i][1];
        fa[son[i][0]]=i;
        fa[son[i][1]]=i;
        sum[i]=sum[son[i][0]]+sum[son[i][1]];
    }
    per(i,n*2-1,0){
        dep[son[i][0]]=dep[i]+1;
        dep[son[i][1]]=dep[i]+1;
    }
    mx=*max_element(dep+1,dep+n*2);
    rep(i,1,n*2-1){
        S[dep[i]].insert(i);
    }
    rep(i,0,mx){
        for(auto it=S[i].begin();it!=S[i].end();++it){
            if(it!=S[i].begin()){
                tot+=F(*prev(it),*it);
                need+=1;
            }
        }
    }
    rep(tc,1,Q){
        if(tc>1){
            int p;
            LL v;
            cin>>p>>v;
            v-=sum[p];
            add(p,v);
        }
//        D("tc=%d need=%d tot=%d\n",tc,need,tot);
//        rep(i,0,mx){
//            for(auto it=S[i].begin();it!=S[i].end();++it){
//                D("(%d,sum=%lld,[%lld,%lld])",*it,sum[*it],min(sum[son[*it][0]],sum[son[*it][1]]),max(sum[son[*it][0]],sum[son[*it][1]]));
//            }
//            D("\n");
//        }
        puts(solve()?"YES":"NO");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 30
Accepted
time: 3ms
memory: 16760kb

input:

3 4
1 1 1
2 3
1 4
2 2
1 2
3 3

output:

YES
NO
YES
NO

result:

ok 4 token(s): yes count is 2, no count is 2

Test #2:

score: 30
Accepted
time: 0ms
memory: 15904kb

input:

8 5
5 3 4 2 2 6 5 5
1 8
4 5
10 3
11 9
7 2
6 13
14 12
7 3
6 8
4 2
2 5

output:

NO
YES
YES
YES
NO

result:

ok 5 token(s): yes count is 3, no count is 2

Test #3:

score: 30
Accepted
time: 2ms
memory: 16968kb

input:

5 1000
193989534544158 57483670601746 183281373434588 92196008024549 197513473286508
1 5
4 2
7 3
8 6
2 65545142774024
4 67957472319495
5 131478473459166
2 102185858570152
3 191441353035940
5 186000528093501
2 63201184033501
2 77481806092413
3 159789430863849
4 92773786021894
1 194598667478593
3 1458...

output:

YES
YES
YES
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
YES
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
YES
YES
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
...

result:

ok 1000 token(s): yes count is 375, no count is 625

Test #4:

score: 30
Accepted
time: 0ms
memory: 17024kb

input:

7 1000
88159166205053 95998544558881 48231159865354 231786835189365 84291070100955 225941839972605 33315221625793
2 5
6 4
7 3
1 10
8 11
9 12
6 150843468162951
2 75759088055460
1 86133344610051
4 140694127444493
1 63070113756930
1 90150689680608
6 147790469610032
7 46561924657801
2 103953340734616
6 ...

output:

NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 1000 token(s): yes count is 86, no count is 914

Test #5:

score: 30
Accepted
time: 3ms
memory: 16588kb

input:

7 1000
5 6 7 3 2 5 5
4 7
3 5
1 2
10 6
8 9
12 11
5 2
5 1
5 1
1 2
3 2
5 1
5 2
6 3
4 2
4 1
2 1
5 1
5 1
5 1
7 1
1 2
5 1
5 1
6 3
6 3
5 1
2 2
7 2
7 1
7 1
2 2
1 2
4 2
4 1
1 1
3 1
5 1
2 2
2 2
4 1
2 1
7 2
6 1
2 1
6 2
5 2
1 1
1 1
6 3
7 2
6 3
4 1
1 1
5 1
2 2
7 2
5 1
4 2
5 2
7 2
7 1
4 1
3 2
3 2
1 1
7 2
5 2
1 1
...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
YES
YES
YES
NO
YES
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

ok 1000 token(s): yes count is 199, no count is 801

Test #6:

score: 30
Accepted
time: 0ms
memory: 16788kb

input:

8 1000
172480280419267 212072146993988 23147786306112 161006134777989 37963466387491 50018942108886 18649770050090 50499101532214
7 3
2 1
5 8
9 6
12 11
4 13
10 14
5 57899673021751
7 22087918088240
1 236990257757485
3 21994370580136
7 26427284121647
8 54391900162086
7 27709763713585
6 59631479929866
...

output:

NO
NO
NO
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES...

result:

ok 1000 token(s): yes count is 119, no count is 881

Test #7:

score: 30
Accepted
time: 0ms
memory: 16164kb

input:

6 1000
20326746134626 98517002313418 165556087937310 116347939244398 130872245741329 136674587230071
2 3
6 7
8 1
9 5
10 4
4 31902652070998
2 20697837065544
3 14389422108471
5 45879772826060
2 23505088168423
6 7588643182717
2 13873224035289
4 3462171437780
6 12963735380517
6 3348685354818
3 141429739...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO...

result:

ok 1000 token(s): yes count is 12, no count is 988

Test #8:

score: 30
Accepted
time: 0ms
memory: 15316kb

input:

72 1000
2 10946 610 987 139583862445 20365011074 5 1597 196418 377 267914296 44945570212853 956722026041 5702887 39088169 75025 27777890035288 12586269025 4181 1 53316291173 144 1548008755920 63245986 1 225851433717 6557470319842 832040 2504730781961 102334155 55 3 46368 7778742049 6765 165580141 72...

output:

YES
NO
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
YES
YES
YES
YES
N...

result:

ok 1000 token(s): yes count is 598, no count is 402

Test #9:

score: 30
Accepted
time: 2ms
memory: 15720kb

input:

70 1000
31322671994689 1078810887856 579849 254672704401 136883 4713 4569916255830 11964196083506 19970 22963823428 157396387326 488813369 12341 1 61 1799 162 82003819900560 221480 5421023350 44076273 37 261 347373755513422 6430641 938216 71316908 687 7394279827673 302103276 2456284 32311 1745552683...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
...

result:

ok 1000 token(s): yes count is 986, no count is 14

Test #10:

score: 0
Wrong Answer
time: 3ms
memory: 16556kb

input:

1000 1000
696394773309 108383178182 819893348793 723784375461 931503257800 952660789032 100236017416 539829053718 209653090976 981815062655 964960614840 961643834067 369486282052 800646303984 387416608811 261777906950 172691588923 485971148690 453102872226 770561490733 375803723200 861225005889 1185...

output:

YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
YES
NO
NO
YES
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
NO
NO
YES
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
NO
NO
NO
N...

result:

wrong answer expected NO, found YES [83rd token]

Subtask #2:

score: 0
Wrong Answer

Test #16:

score: 0
Wrong Answer
time: 35ms
memory: 17332kb

input:

10000 10000
85117964119 41472951000 61693640396 66409648221 91978532661 62922448518 92497200794 43837348258 45577855926 38256001396 79446271832 95289903258 62510175551 97599809584 56162244722 87617641117 64010325734 56604859803 58544571483 40687963085 38627694508 64665875035 62273927372 73014847094 ...

output:

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

wrong answer expected NO, found YES [303rd token]

Subtask #3:

score: 0
Wrong Answer

Test #20:

score: 0
Wrong Answer
time: 341ms
memory: 21608kb

input:

50000 50000
16394396247 17456058492 11358090355 13208121167 8612535629 2853817504 18100755237 18603321637 1618810635 7615832530 13631222424 7817630977 10963098997 19654927084 645638016 9352759139 17939720223 15106346489 14475376391 2122412099 15482023637 11224675857 15931143509 4240408932 1270948838...

output:

YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
...

result:

wrong answer expected NO, found YES [30th token]

Subtask #4:

score: 0
Wrong Answer

Test #24:

score: 20
Accepted
time: 205ms
memory: 16184kb

input:

70 100000
66748 126 1 91045172 3605661959574 274077743637349 147314183 8209537 740253 6920630855 25494 1377240316614 15756 6 108000 18118446805 169389361127761 29316262755 48 2643445763 5834083602536 3 9439745562111 29 3719 10 47434709561 11197815949 6018 325122336074 851181326345 1633739329 1527382...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
...

result:

ok 100000 token(s): yes count is 98093, no count is 1907

Test #25:

score: 0
Wrong Answer
time: 760ms
memory: 26772kb

input:

100000 100000
7549638646 8066727970 9672316362 9615802181 6376690689 416134043 5288164622 2316444041 8423663950 1806779510 3010692396 7782858557 4229348735 1361364214 9005774175 6382408188 8174082766 8406340542 8599848784 3178078732 1395839441 5310497981 6807939596 2743315129 3143071583 3636776799 5...

output:

YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
...

result:

wrong answer expected NO, found YES [453rd token]