QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#122219#2065. Cyclic Distance__2018ty22__WA 36ms9860kbC++142.9kb2023-07-09 19:37:112023-07-09 19:37:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-09 19:37:14]
  • 评测
  • 测评结果:WA
  • 用时:36ms
  • 内存:9860kb
  • [2023-07-09 19:37:11]
  • 提交

answer

#include<time.h>
#include<random>
#include<cstdio>
#define MAX 200005
#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));
int T,n,k;
int head[MAX],s[MAX<<1],nxt[MAX<<1],val[MAX<<1];

int tot,pos[MAX],siz[MAX];
ll w[MAX],tag[MAX];
int son[MAX][2];
void down(int x)
{
    if(tag[x])
    {
        w[x]+=tag[x];
        if(son[x][0])tag[son[x][0]]+=tag[x];
        if(son[x][1])tag[son[x][1]]+=tag[x];
        tag[x]=0;
    }
}
void pus(int x)
{
    siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
}
int build(int x)
{
    w[++tot]=x,siz[tot]=1,pos[tot]=rnd();
    tag[tot]=son[tot][0]=son[tot][1]=0;
    return tot;
}
void split(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 splitv(int i,int v,int&x,int&y)
{
    if(!i)
    {
        x=y=0;
        return;
    }
    down(i);
    if(val[i]>=v)//BUG:v->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);
}
int merge(int x,int y)
{
    if(x)down(x);if(y)down(y);
    if(!x||!y)return x|y;
    if(pos[x]>pos[y])x^=y^=x^=y;
    int a,b;
    splitv(y,val[x],a,b);
    son[x][0]=merge(son[x][0],a);
    son[x][1]=merge(son[x][1],b);
    pus(x);
    return x;
}
void change(int i,int l,ll v)
{
    if(l>=siz[i])tag[i]+=v;
    else if(siz[son[i][0]]>l)change(son[i][0],l,v);
    else
    {
        tag[son[i][0]]+=v,l-=siz[son[i][0]];
        if(l)
        {
            w[i]+=v,--l;
            if(l)change(son[i][1],l,v);
        }
    }
}
void print(int i)
{
    down(i);
    if(son[i][0])print(son[i][0]);
    printf("%d_%d ",i,w[i]);
    if(son[i][1])print(son[i][1]);
}
int _;
int dfs(int x,int fa)
{
    int rt=0;
    for(int i=head[x];i;i=nxt[i])
    if(s[i]!=fa)
    {
        int v=dfs(s[i],x);
        change(v,k>>1,val[i]);
        change(v,k+1>>1,val[i]);
        change(v,k,-val[i]);
        rt=merge(v,rt);
    }
    rt=merge(rt,build(0));
    if(siz[rt]>k)split(rt,k,rt,_);
    return rt;
}
ll findans(int i)
{
    down(i);
    ll ans=w[i];
    if(son[i][0])ans+=findans(son[i][0]);
    if(son[i][1])ans+=findans(son[i][1]);
    return ans;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        //clear
        tot=0;
        for(int i=1;i<=n;++i)head[i]=0;
        scanf("%d%d",&n,&k);
        for(int i=1;i<n;++i)
        {
            int x,y,v;
            scanf("%d%d%d",&x,&y,&v);
            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;
            val[i]=val[i+n-1]=v;
        }
        int rt=dfs(1,-1);
        printf("%lld\n",findans(rt)*2);
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 9860kb

input:

1
5 3
1 2 4
1 3 1
1 4 8
4 5 9

output:

44

result:

ok single line: '44'

Test #2:

score: -100
Wrong Answer
time: 36ms
memory: 9856kb

input:

57206
3 2
1 2 574927
2 3 406566
2 2
1 2 308806
4 3
1 2 312588
2 3 500141
2 4 53602
3 3
1 2 797183
2 3 944061
5 3
1 2 624010
2 3 488613
3 4 734514
4 5 497493
5 4
1 2 540278
2 3 488655
2 4 228989
2 5 653896
2 2
1 2 569117
4 2
1 2 821764
2 3 499471
1 4 549060
2 2
1 2 991159
2 2
1 2 482941
5 4
1 2 30462...

output:

1962986
617612
1107486
3482488
4689260
2846326
1138234
1098120
1982318
965882
2809264
3302650
1623414
1885106
1952142
3050630
1691896
3102076
2090122
799102
5697196
5623230
879020
2135168
548150
1358950
1182198
687492
302850
593522
8917452
2373066
2479062
3104226
1917330
3162310
5058442
3669186
6267...

result:

wrong answer 3rd lines differ - expected: '1732662', found: '1107486'