QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#122209#2065. Cyclic Distance__2018ty22__WA 35ms11900kbC++142.9kb2023-07-09 19:19:522023-07-09 19:19:55

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:19:55]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:11900kb
  • [2023-07-09 19:19:52]
  • 提交

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]<=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||!y)return x|y;
    down(x);down(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 ",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]);
        merge(v,rt);
    }
    if(siz[rt]>k)split(rt,k,rt,_);
    else if(siz[rt]<k)merge(rt,build(0));
    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: 2ms
memory: 11900kb

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: 35ms
memory: 7824kb

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
2580598
4313260
7795748
12485008
16308644
17446878
21187468
23169786
24135668
27554172
32580734
34204148
36089254
38041396
41092026
42783922
45885998
48266930
51343200
55631882
59807792
60686812
63187024
66800878
68159828
69342026
71615688
73947248
75629212
81161366
83534432
86697474
8980170...

result:

wrong answer 2nd lines differ - expected: '617612', found: '2580598'