Submission #956611

# Submission time Handle Problem Language Result Execution time Memory
956611 2024-04-02T08:27:24 Z vjudge1 Magic Tree (CEOI19_magictree) C
100 / 100
176 ms 106484 KB
#include<stdio.h>

long long min_(long long a,long long b){return a<b?a:b;}
long long max_(long long a,long long b){return a>b?a:b;}

#define M 4000000
long long LIT[M],A[M],B[M];
int L[M],R[M],alc,trash[M],to;

int alloc() {int p=to?trash[--to]:++alc;L[p]=R[p]=A[p]=B[p]=LIT[p]=0;return p;}
void delet(int v){if(!v)return;trash[to++]=v;delet(L[v]);delet(R[v]);}

void pull(int v)
{
    A[v]=min_(A[L[v]]+LIT[L[v]],A[R[v]]+LIT[R[v]]);
    B[v]=max_(B[L[v]]+LIT[L[v]],B[R[v]]+LIT[R[v]]);
    if(A[v]==B[v])delet(L[v]),delet(R[v]),L[v]=0,R[v]=0;
}

int same(int v){return A[v]==B[v];}

int merge(int p, int q) {
    if(!p||!q)return p^q;
    if(same(p)){LIT[q]+=A[p]+LIT[p];return q;}
    if(same(q)){LIT[p]+=A[q]+LIT[q];return p;}
    L[p]=merge(L[p],L[q]);
    R[p]=merge(R[p],R[q]);
    pull(p);
    LIT[p]+=LIT[q];
    return p;
}

long long qry(int v,int l,int r,int p)
{
    if(same(v)) return LIT[v]+A[v];
    if(p<=(l+r)/2)return LIT[v]+qry(L[v],l,(l+r)/2,p);
    return LIT[v]+qry(R[v],(l+r)/2+1,r,p);
}

void chmax(int v,int l,int r,int x,int y,long long k)
{
    k-=LIT[v];
    if(A[v]>=k)return;
    if(x<=l&&r<=y&&B[v]<=k)
    {
        A[v]=B[v]=k;
        delet(R[v]);delet(L[v]);L[v]=R[v]=0;
        return;
    }
    if(same(v))
    {
        L[v]=alloc();R[v]=alloc();
        A[L[v]]=A[R[v]]=B[R[v]]=B[L[v]]=B[v];
    }
    if(x<=(l+r)/2) chmax(L[v],l,(l+r)/2,x,y,k);
    if(y>(l+r)/2) chmax(R[v],(l+r)/2+1,r,x,y,k);
    pull(v);
}

#define N 100005

int n,m,k,e[N][2],eo,h[N],d[N],w[N],t[N];

void dfs(int u,int p)
{
    t[u]=alloc();
    for(int v,j=h[u];j;j=e[j][1])if((v=e[j][0])^p) dfs(v,u),t[u]=merge(t[u],t[v]);
    if(~d[u]) chmax(t[u],1,k,d[u],k,w[u]+qry(t[u],1,k,d[u]));
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;++i)d[i]=-1;
    for(int p,i=2;i<=n;++i) scanf("%d",&p),e[++eo][0]=i,e[eo][1]=h[p],h[p]=eo;
    for(int vv,dd,ww,i=0;i<m;++i) scanf("%d%d%d",&vv,&dd,&ww), d[vv]=dd,w[vv]=ww;
    dfs(1,1);
    printf("%lld",LIT[t[1]]+B[t[1]]);
}

Compilation message

magictree.c: In function 'main':
magictree.c:73:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d%d%d",&n,&m,&k);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
magictree.c:75:29: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     for(int p,i=2;i<=n;++i) scanf("%d",&p),e[++eo][0]=i,e[eo][1]=h[p],h[p]=eo;
      |                             ^~~~~~~~~~~~~~
magictree.c:76:35: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     for(int vv,dd,ww,i=0;i<m;++i) scanf("%d%d%d",&vv,&dd,&ww), d[vv]=dd,w[vv]=ww;
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10584 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10828 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 42132 KB Output is correct
2 Correct 51 ms 40016 KB Output is correct
3 Correct 176 ms 106064 KB Output is correct
4 Correct 145 ms 105552 KB Output is correct
5 Correct 151 ms 106484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 55 ms 24472 KB Output is correct
5 Correct 60 ms 29168 KB Output is correct
6 Correct 74 ms 24400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 20568 KB Output is correct
2 Correct 35 ms 18260 KB Output is correct
3 Correct 36 ms 20816 KB Output is correct
4 Correct 30 ms 20632 KB Output is correct
5 Correct 47 ms 24492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10584 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10828 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 60 ms 29776 KB Output is correct
11 Correct 40 ms 25424 KB Output is correct
12 Correct 37 ms 23196 KB Output is correct
13 Correct 42 ms 37972 KB Output is correct
14 Correct 35 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 16 ms 18452 KB Output is correct
3 Correct 21 ms 18292 KB Output is correct
4 Correct 15 ms 18268 KB Output is correct
5 Correct 11 ms 18452 KB Output is correct
6 Correct 14 ms 19548 KB Output is correct
7 Correct 14 ms 20720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10584 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10828 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 55 ms 24472 KB Output is correct
14 Correct 60 ms 29168 KB Output is correct
15 Correct 74 ms 24400 KB Output is correct
16 Correct 60 ms 29776 KB Output is correct
17 Correct 40 ms 25424 KB Output is correct
18 Correct 37 ms 23196 KB Output is correct
19 Correct 42 ms 37972 KB Output is correct
20 Correct 35 ms 24412 KB Output is correct
21 Correct 16 ms 20060 KB Output is correct
22 Correct 69 ms 37972 KB Output is correct
23 Correct 79 ms 52372 KB Output is correct
24 Correct 133 ms 74900 KB Output is correct
25 Correct 126 ms 105676 KB Output is correct
26 Correct 123 ms 72068 KB Output is correct
27 Correct 105 ms 40536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10584 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10828 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 54 ms 42132 KB Output is correct
11 Correct 51 ms 40016 KB Output is correct
12 Correct 176 ms 106064 KB Output is correct
13 Correct 145 ms 105552 KB Output is correct
14 Correct 151 ms 106484 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 55 ms 24472 KB Output is correct
19 Correct 60 ms 29168 KB Output is correct
20 Correct 74 ms 24400 KB Output is correct
21 Correct 50 ms 20568 KB Output is correct
22 Correct 35 ms 18260 KB Output is correct
23 Correct 36 ms 20816 KB Output is correct
24 Correct 30 ms 20632 KB Output is correct
25 Correct 47 ms 24492 KB Output is correct
26 Correct 60 ms 29776 KB Output is correct
27 Correct 40 ms 25424 KB Output is correct
28 Correct 37 ms 23196 KB Output is correct
29 Correct 42 ms 37972 KB Output is correct
30 Correct 35 ms 24412 KB Output is correct
31 Correct 4 ms 13148 KB Output is correct
32 Correct 16 ms 18452 KB Output is correct
33 Correct 21 ms 18292 KB Output is correct
34 Correct 15 ms 18268 KB Output is correct
35 Correct 11 ms 18452 KB Output is correct
36 Correct 14 ms 19548 KB Output is correct
37 Correct 14 ms 20720 KB Output is correct
38 Correct 16 ms 20060 KB Output is correct
39 Correct 69 ms 37972 KB Output is correct
40 Correct 79 ms 52372 KB Output is correct
41 Correct 133 ms 74900 KB Output is correct
42 Correct 126 ms 105676 KB Output is correct
43 Correct 123 ms 72068 KB Output is correct
44 Correct 105 ms 40536 KB Output is correct
45 Correct 17 ms 22104 KB Output is correct
46 Correct 50 ms 39864 KB Output is correct
47 Correct 73 ms 54356 KB Output is correct
48 Correct 140 ms 80976 KB Output is correct
49 Correct 138 ms 105724 KB Output is correct
50 Correct 148 ms 72076 KB Output is correct
51 Correct 98 ms 40432 KB Output is correct