QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#821624 | #9905. 哈夫曼树 | Fesdrer | 0 | 968ms | 24700kb | C++17 | 2.7kb | 2024-12-19 16:59:53 | 2024-12-19 16:59:54 |
Judging History
answer
#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
const int N=1e5+5,M=80;
int n,q,mxdep,fa[N<<1],dep[N<<1],ls[N<<1],rs[N<<1];
long long val[N<<1],a[N];
multiset<pair<long long,long long>> s;
multiset<long long> sd[M+5];
int cnt;
inline bool check(pair<long long,long long> x,pair<long long,long long> y){
return (y.second<x.first);
}
inline int cp(int x){
return (ls[fa[x]]==x?rs[fa[x]]:ls[fa[x]]);
}
inline pair<long long,long long> get(int p){
return {val[p],val[cp(p)]};
}
inline void erase(pair<long long,long long> x){
if(x.first<=x.second) return;
auto it=s.find(x);
if(s.size()!=1){
if(x==*s.begin()) cnt-=check(x,*++s.begin());
else if(x==*s.rbegin()) cnt-=check(*--s.rbegin(),x);
else{
pair<long long,long long> l=*(--it);it++;
pair<long long,long long> r=*(++it);it--;
cnt-=check(l,x)+check(x,r)-check(l,r);
}
}
s.erase(it);
}
inline void insert(pair<long long,long long> x){
if(x.first<=x.second) return;
auto it=s.insert(x);
if(s.size()!=1){
if(x==*s.begin()) cnt+=check(x,*++s.begin());
else if(x==*s.rbegin()) cnt+=check(*--s.rbegin(),x);
else{
pair<long long,long long> l=*(--it);it++;
pair<long long,long long> r=*(++it);
cnt+=check(l,x)+check(x,r)-check(l,r);
}
}
}
inline void changeval(int p,long long v){
sd[dep[p]].erase(sd[dep[p]].find(val[p])),sd[dep[p]].insert(v);
if(dep[p]==1){val[p]=v;return;}
pair<long long,long long> e1=get(p),e2=get(cp(p));
val[p]=v;
pair<long long,long long> i1=get(p),i2=get(cp(p));
erase(e1),erase(e2),insert(i1),insert(i2);
}
inline void change(int p,long long v){
changeval(p,v);
while(p!=n*2-1) p=fa[p],changeval(p,val[ls[p]]+val[rs[p]]);
}
inline bool solve(){
for(int i=1;i<mxdep;i++) if(*sd[i].begin()<*(--sd[i+1].end())) return 0;
return cnt==0;
}
int main(){
// freopen("ex_huffman4.in","r",stdin);
// freopen("ex_huffman4.my","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>q;
if(n==1){
while(q--) puts("YES");
return 0;
}
for(int i=1;i<=n;i++) cin>>a[i],val[i]=a[i];
for(int i=n+1;i<n*2;i++){
cin>>ls[i]>>rs[i];
fa[ls[i]]=i,fa[rs[i]]=i,val[i]=val[ls[i]]+val[rs[i]];
}
dep[n*2-1]=1;
mxdep=1;
for(int i=n*2-1;i>=n+1;i--){
dep[ls[i]]=dep[i]+1,dep[rs[i]]=dep[i]+1;
mxdep=max(mxdep,dep[ls[i]]),mxdep=max(mxdep,dep[rs[i]]);
}
if(mxdep>M){
while(q--) puts("NO");
return 0;
}
for(int i=1;i<=n*2-1;i++) sd[dep[i]].insert(val[i]);
for(int i=1;i<=n*2-2;i++) insert(get(i));
puts(solve()?"YES":"NO");
q--;
while(q--){
int p;
long long v;
cin>>p>>v;
change(p,v);
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: 1ms
memory: 7716kb
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: 0
Wrong Answer
time: 1ms
memory: 7700kb
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:
YES YES YES YES YES
result:
wrong answer expected NO, found YES [1st token]
Subtask #2:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 58ms
memory: 9448kb
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 [704th token]
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 415ms
memory: 16288kb
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 NO YES NO YES YES 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 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 NO NO...
result:
wrong answer expected NO, found YES [2798th token]
Subtask #4:
score: 0
Wrong Answer
Test #24:
score: 20
Accepted
time: 439ms
memory: 7732kb
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: 968ms
memory: 24700kb
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 [2472nd token]