QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#823323 | #9905. 哈夫曼树 | xay5421 | 0 | 301ms | 21144kb | C++14 | 2.7kb | 2024-12-20 21:42:48 | 2024-12-20 21:42:56 |
Judging History
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&=cmp()(*--S[i].end(),*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){
while(p){
// D("p=%d v=%lld\n",p,v);
del(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,[%d,%d])",*it,sum[*it],min(sum[son[i][0]],sum[son[i][1]]),max(sum[son[i][0]],sum[son[i][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: 0
Wrong Answer
time: 2ms
memory: 17104kb
input:
3 4 1 1 1 2 3 1 4 2 2 1 2 3 3
output:
NO NO NO NO
result:
wrong answer expected YES, found NO [1st token]
Subtask #2:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 38ms
memory: 18612kb
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 YES YES 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 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 ...
result:
wrong answer expected NO, found YES [14th token]
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 301ms
memory: 21144kb
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 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...
result:
wrong answer expected YES, found NO [17th token]
Subtask #4:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 180ms
memory: 15924kb
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:
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 NO NO NO NO NO NO NO NO NO ...
result:
wrong answer expected YES, found NO [1st token]