QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#396729#5421. Factories Once More2745518585WA 202ms29312kbC++202.1kb2024-04-23 08:45:162024-04-23 08:45:16

Judging History

你现在查看的是最新测评结果

  • [2024-04-23 08:45:16]
  • 评测
  • 测评结果:WA
  • 用时:202ms
  • 内存:29312kb
  • [2024-04-23 08:45:16]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1000001;
int n,k,tot,rt[N];
vector<pair<int,int>> a[N];
vector<int> c[N];
struct tree
{
    int l,r,s,h;
    ll x,k1,k2;
}T[N];
void pushup(int x)
{
    T[x].s=T[T[x].l].s+T[T[x].r].s+1;
}
void maketag(int x,ll k1,ll k2)
{
    T[x].x+=k1*(T[T[x].l].s+1)+k2;
    T[x].k1+=k1;
    T[x].k2+=k2;
}
void pushdown(int x)
{
    maketag(T[x].l,T[x].k1,T[x].k2);
    maketag(T[x].r,T[x].k1,T[x].k2+(T[T[x].l].s+1)*T[x].k1);
    T[x].k1=T[x].k2=0;
}
void split(int x,ll k,int &x1,int &x2)
{
    if(x==0)
    {
        x1=x2=0;
        return;
    }
    pushdown(x);
    if(T[x].x<=k)
    {
        x1=x;
        split(T[x].r,k,T[x].r,x2);
    }
    else
    {
        x2=x;
        split(T[x].l,k,x1,T[x].l);
    }
    pushup(x);
}
int merge(int x1,int x2)
{
    if(x1==0||x2==0) return x1|x2;
    pushdown(x1);
    pushdown(x2);
    if(T[x1].h<T[x2].h)
    {
        T[x1].r=merge(T[x1].r,x2);
        pushup(x1);
        return x1;
    }
    else
    {
        T[x2].l=merge(x1,T[x2].l);
        pushup(x2);
        return x2;
    }
}
void add(int &rt,ll k)
{
    int x1,x2;
    split(rt,k,x1,x2);
    T[++tot].x=k;
    T[tot].l=T[tot].r=0;
    T[tot].s=1;
    T[tot].h=rand();
    rt=merge(merge(x1,tot),x2);
}
void get(int x,auto p)
{
    if(x==0) return;
    p(T[x].x);
    pushdown(x);
    get(T[x].l,p);
    get(T[x].r,p);
}
void dfs(int x,int fa)
{
    add(rt[x],0);
    for(auto i:a[x])
    {
        if(i.first==fa) continue;
        dfs(i.first,x);
        maketag(rt[i.first],2*i.second,-(k+1)*i.second);
        if(T[rt[i.first]].s>T[rt[x]].s) swap(rt[x],rt[i.first]);
        get(rt[i.first],[&](ll k){add(rt[x],k);});
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n-1;++i)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        a[x].push_back(make_pair(y,w));
        a[y].push_back(make_pair(x,w));
    }
    dfs(1,0);
    ll s=0;
    get(rt[1],[&](ll k){if(k<0) s-=k;});
    printf("%lld",s);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3844kb

input:

6 3
1 2 3
2 3 2
2 4 1
1 5 2
5 6 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3840kb

input:

4 3
1 2 2
1 3 3
1 4 4

output:

18

result:

ok 1 number(s): "18"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3924kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 202ms
memory: 29312kb

input:

100000 17
37253 35652 9892
56367 53643 1120
47896 49255 4547
93065 88999 1745
5251 6742 5031
49828 50972 8974
31548 46729 1032
56341 56287 4812
21896 22838 1682
82124 90557 7307
76289 76949 7028
33834 45380 6856
15499 15064 2265
10127 5251 9920
87208 93945 9487
68990 72637 6891
91640 85004 2259
4748...

output:

5203350610

result:

wrong answer 1st numbers differ - expected: '4915539756', found: '5203350610'