QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#865063 | #9905. 哈夫曼树 | yinhee | 0 | 0ms | 7764kb | C++17 | 3.0kb | 2025-01-21 14:27:21 | 2025-01-21 14:27:22 |
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define mems(x,y) memset(x,y,sizeof x)
#define Mp make_pair
#define eb emplace_back
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define il inline
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define drep(i,a,b) for(int i=(a);i>=(b);i--)
#define go(i,u) for(int i=head[u];i;i=e[i].nxt)
typedef long long ll;
typedef pair<int,int> pii;
template<typename T>
il void read(T &x){
int f=1;x=0;char c=gc();
while(c<'0'||c>'9'){
if(c=='-'){
f=-1;
}
c=gc();
}
while(c>='0'&&c<='9'){
x=x*10+c-48,c=gc();
}
x*=f;
}
template<typename T,typename ...Args>
void read(T &x,Args &...args){
read(x),read(args...);
}
template<typename T>
il void write(T x){
char buf[43];int len=0;
if(x<0){
pc('-'),x=-x;
}
do{
buf[++len]=x%10,x/=10;
}while(x);
while(len){
pc(buf[len--]+'0');
}
}
}
using namespace my_std;
const int N=2e5+7,M=-1,inf=0x3f3f3f3f,mod=0;
int n,m,q,cnt,a[N],fa[N],dep[N],ls[N],rs[N];
bool vis[N];
ll s[N];
multiset<pair<ll,ll>> st;
il void insert(int x,int y){
auto it=st.emplace(x,y);
if(it!=st.begin()&&it!=--st.end()&&prev(it)->se>next(it)->fi){
cnt--;
}
if(it!=st.begin()&&prev(it)->se>x){
cnt++;
}
if(it!=--st.end()&&y>next(it)->fi){
cnt++;
}
}
il void erase(int x,int y){
// printf("==%d %d\n",x,y);
// for(auto [x,y]:st){
// printf("%d %d\n",x,y);
// }
// return;
auto it=st.erase(st.find(Mp(x,y)));
if(it!=st.begin()&&it!=st.end()&&prev(it)->se>it->fi){
cnt++;
}
if(it!=st.begin()&&prev(it)->se>x){
cnt--;
}
if(it!=st.end()&&y>it->fi){
cnt--;
}
}
void dfs(int u,int f){
fa[u]=f,dep[u]=dep[f]+1;
if(dep[u]>80){
while(q--){
puts("NO");
}
exit(0);
}
if(u<=n){
s[u]=a[u];
return;
}
dfs(ls[u],u),dfs(rs[u],u);
s[u]+=s[ls[u]]+s[rs[u]];
ll x=s[ls[u]],y=s[rs[u]];
if(x>y){
swap(x,y);
}
insert(x,y);
}
void Yorushika(){
read(n,q);
rep(i,1,n){
read(a[i]);
}
rep(i,n+1,n+n-1){
read(ls[i],rs[i]);
vis[ls[i]]=vis[rs[i]]=1;
}
int rt=0;
rep(i,n+1,n+n-1){
if(!vis[i]){
rt=i;
}
}
dfs(rt,0);
puts(cnt?"NO":"YES");
q--;
while(q--){
int x,y;read(x,y);
int u=x;
while(u){
// printf("**%d %d %d\n",u,ls[fa[u]],rs[fa[u]]);
if(u==ls[fa[u]]){
int l=s[u],r=s[rs[fa[u]]];
bool fl=0;
if(l>r){
swap(l,r),fl^=1;
}
erase(l,r);
if(fl){
r+=y-a[x];
}else{
l+=y-a[x];
}
if(l>r){
swap(l,r);
}
insert(l,r);
}else if(u==rs[fa[u]]){
int l=s[u],r=s[ls[fa[u]]];
bool fl=0;
if(l>r){
swap(l,r),fl^=1;
}
erase(l,r);
if(fl){
r+=y-a[x];
}else{
l+=y-a[x];
}
if(l>r){
swap(l,r);
}
insert(l,r);
}
s[u]+=y-a[x];
u=fa[u];
}
a[x]=y;
puts(cnt?"NO":"YES");
}
}
signed main(){
int t=1;
//read(t);
while(t--){
Yorushika();
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 30
Accepted
time: 0ms
memory: 7760kb
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: 7760kb
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: 0
Wrong Answer
time: 0ms
memory: 7764kb
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:
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO 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]
Subtask #2:
score: 0
Runtime Error
Test #16:
score: 0
Runtime Error
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:
result:
Subtask #3:
score: 0
Runtime Error
Test #20:
score: 0
Runtime Error
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:
result:
Subtask #4:
score: 0
Runtime Error
Test #24:
score: 0
Runtime Error
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...