QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#128810 | #5421. Factories Once More | __2018ty22__ | WA | 0ms | 5832kb | C++14 | 3.3kb | 2023-07-21 15:54:31 | 2023-07-21 16:06:30 |
Judging History
answer
#include<time.h>
#include<random>
#include<cstdio>
#define MAX 100005
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
typedef long long ll;
using namespace std;
mt19937 rnd(time(0));
namespace Treap{
int tot,pos[MAX],siz[MAX];
int son[MAX][2];
ll v[MAX],tag1[MAX],tag2[MAX];
void down(int x)
{
if(!tag1[x]&&!tag2[x])return;
v[x]+=tag2[x]+tag1[x]*siz[son[x][0]];
if(son[x][0])
{
tag1[son[x][0]]+=tag1[x];
tag2[son[x][0]]+=tag2[x];
}
if(son[x][1])
{
tag1[son[x][1]]+=tag1[x];
tag2[son[x][1]]+=tag2[x]+tag1[x]*(siz[son[x][0]]+1);
}
tag1[x]=tag2[x]=0;
}
void pus(int x)
{
siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
}
int build(int x)
{
v[++tot]=x,siz[tot]=1,pos[tot]=rnd();
return tot;
}
int merge(int x,int y)
{
if(!x||!y)return x|y;
down(x);down(y);
if(pos[x]<pos[y])
{
son[x][1]=merge(son[x][1],y);
pus(x);
return x;
}
son[y][0]=merge(x,son[y][0]);
pus(y);
return y;
}
void split(int i,ll k,int&x,int&y)
{
if(!i)
{
x=y=0;
return;
}
down(i);
if(v[i]>=k)
x=i,split(son[i][1],k-siz[son[i][0]]-1,son[i][1],y);
else
y=i,split(son[i][0],k,x,son[i][0]);
pus(i);
}
void split1(int i,int k,int&x,int&y)
{
if(!i)
{
x=y=0;
return;
}
down(i);
if(siz[son[i][0]]<k)
x=i,split(son[i][1],k-siz[son[i][0]]-1,son[i][1],y);
else
y=i,split(son[i][0],k,x,son[i][0]);
pus(i);
}
void add(int&rt,ll x)
{
int a,b=build(x),c;
split(rt,x,a,c);
rt=merge(a,merge(b,c));
}
void add1(int&rt,int x)
{
int a,c;
split(rt,v[x],a,c);
rt=merge(a,merge(x,c));
}
void merge1(int x,int&y)
{
down(x);
if(son[x][0])merge1(son[x][0],y),son[x][0]=0;
if(son[x][1])merge1(son[x][1],y),son[x][1]=0;
siz[x]=1;
add1(y,x);
}
ll ask(int x)
{
down(x);
if(son[x][0])v[x]+=ask(son[x][0]);
if(son[x][1])v[x]+=ask(son[x][1]);
return v[x];
}
};
int n,k,head[100001],s[200001],nxt[200001],v[200001];
int dfs(int x,int fa)
{
int rt=Treap::build(0);
for(int i=head[x];i;i=nxt[i])
if(s[i]!=fa)
{
int son=dfs(s[i],x);
Treap::tag2[son]+=v[i]*ll(k-1);
Treap::tag1[son]-=v[i]*2ll;
if(Treap::siz[rt]<Treap::siz[son])
Treap::merge1(rt,son),rt=son;
else
Treap::merge1(son,rt);
}
int r;
Treap::split1(rt,k,rt,r);
return rt;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<n;++i)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
s[i]=y;nxt[i]=head[x];head[x]=i;
s[i+n-1]=x;nxt[i+n-1]=head[y];head[y]=i+n-1;
v[i]=v[i+n-1]=w;
}
int rt=dfs(1,-1);
printf("%lld",Treap::ask(rt));
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5832kb
input:
6 3 1 2 3 2 3 2 2 4 1 1 5 2 5 6 3
output:
20
result:
wrong answer 1st numbers differ - expected: '22', found: '20'