QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132604 | #4815. Flower's Land | 2024zll | WA | 2ms | 9108kb | C++14 | 3.2kb | 2023-07-30 18:38:55 | 2023-07-30 18:38:58 |
Judging History
answer
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<set>
namespace IO{
const int ARR_SIZE=1<<16;
#define gc() ((IO::si!=IO::ti||(IO::ti=(IO::si=IO::input)+fread(IO::input,1,IO::ARR_SIZE,stdin))),IO::si!=IO::ti?*(IO::si++):EOF)
#define pc(ch) ((IO::o.so!=IO::o.to||(fwrite(IO::o.output,1,IO::ARR_SIZE,stdout),IO::o.so=IO::o.output)),*(IO::o.so++)=ch)
char input[ARR_SIZE],*si=input,*ti=input;
struct Output_Stream{
char output[ARR_SIZE],*so=output,*to=output+ARR_SIZE;
~Output_Stream(){
if(so==output)return;
fwrite(output,1,so-output,stdout);
so=output;
}
}o;
template<typename T>
void read(T&num){
int ch=gc();
num=0;
while(ch<48||ch>57)ch=gc();
while(ch>=48&&ch<=57)num=(num<<3)+(num<<1)+(ch^48),ch=gc();
}
template<typename T>
void write(T a){
static int ch[50],cnt=0;
if(a<0)pc('-'),a=-a;
if(a==0)pc('0');
while(a)ch[++cnt]=a%10|48,a/=10;
while(cnt)pc(ch[cnt--]);
}
}
using IO::read;
using IO::write;
typedef long long ll;
const int maxn=200000;
int n,k,a[maxn+1];
int head[maxn+1],total;
struct Edge{
int to,next;
}e[(maxn-1)*2+1];
void add(const int u,const int v){
e[++total]={v,head[u]};
head[u]=total;
}
ll f[maxn+1],g[maxn+1],G[maxn+1],ans[maxn+1];
struct DS{
std::multiset<ll>S1,S2;
ll sum;
void insert(const ll x){
if(!x)return;
S1.insert(x);
if((int)S1.size()>k){
auto it=S1.begin();
S2.insert(*it);
sum+=*it;
S1.erase(it);
}
}
void erase(const ll x){
if(!x)return;
auto it_1=S2.find(x);
if(it_1!=S2.end())sum-=x,S2.erase(it_1);
else{
if(S1.find(x)==S1.end())exit(1);
S1.erase(S1.find(x));
if(!S2.empty()){
auto it_2=--S2.end();
S1.insert(*it_2);
sum-=*it_2;
S2.erase(it_2);
}
}
}
}S;
struct OP{
int type;
ll x;
};
struct OP_Stack{
OP stack[maxn*4+1];
int top;
void insert(const ll x){
stack[++top]=OP{1,x};
S.insert(x);
}
void erase(const ll x){
stack[++top]=OP{2,x};
S.erase(x);
}
void roll_back(const int lim){
while(top>lim){
if(stack[top].type==1)S.erase(stack[top].x);
else S.insert(stack[top].x);
top--;
}
}
}s;
int id[maxn+1][2];
void dfs_1(const int u,const int from){
int*ID=id[u];
f[u]=a[u];
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==from)continue;
dfs_1(v,u);
f[u]+=f[v];
if(!ID[0]||g[ID[0]]<g[v])ID[1]=ID[0],ID[0]=v;
else if(!ID[1]||g[ID[1]]<g[v])ID[1]=v;
}
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==from||v==ID[0])continue;
S.insert(g[v]);
}
g[u]=g[ID[0]]+f[u];
if(!from)S.insert(g[u]-f[u]);
}
void dfs_2(const int u,const int from){
int*ID=id[u];
ans[u]=S.sum;
for(int i=head[u],lim;i;i=e[i].next){
const int v=e[i].to;
if(v==from)continue;
lim=s.top;
if(v!=ID[0])G[v]=std::max(G[u],g[u]);
else G[v]=std::max(G[u],f[ID[1]]+g[ID[1]]);
G[v]+=f[1]-f[v];
s.erase(g[v]+f[v]);
s.insert(g[v]);
s.erase(G[v]-(f[1]-f[v]));
s.insert(G[v]);
dfs_2(v,u);
s.roll_back(lim);
}
}
int main(){
read(n),read(k);
for(int i=1;i<=n;i++)read(a[i]);
for(int i=1,u,v;i<n;i++){
read(u),read(v);
add(u,v),add(v,u);
}
dfs_1(1,0);
for(int i=1;i<=n;i++)g[i]-=f[i];
dfs_2(1,0);
for(int i=1;i<=n;i++)write(ans[i]),pc('\n');
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9108kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
0 0 0 0 0
result:
wrong answer 1st numbers differ - expected: '20', found: '0'