답안 #956606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956606 2024-04-02T08:24:52 Z sleepntsheep Magic Tree (CEOI19_magictree) C
100 / 100
175 ms 108624 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;
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10596 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 42128 KB Output is correct
2 Correct 38 ms 41108 KB Output is correct
3 Correct 175 ms 108116 KB Output is correct
4 Correct 135 ms 107856 KB Output is correct
5 Correct 146 ms 108624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 57 ms 26364 KB Output is correct
5 Correct 61 ms 31312 KB Output is correct
6 Correct 77 ms 26560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 20372 KB Output is correct
2 Correct 34 ms 20352 KB Output is correct
3 Correct 33 ms 22868 KB Output is correct
4 Correct 38 ms 22392 KB Output is correct
5 Correct 36 ms 26708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10596 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10692 KB Output is correct
10 Correct 45 ms 31300 KB Output is correct
11 Correct 40 ms 26708 KB Output is correct
12 Correct 41 ms 24660 KB Output is correct
13 Correct 38 ms 39004 KB Output is correct
14 Correct 36 ms 26084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 15 ms 19036 KB Output is correct
3 Correct 15 ms 18804 KB Output is correct
4 Correct 15 ms 19036 KB Output is correct
5 Correct 14 ms 18520 KB Output is correct
6 Correct 15 ms 20056 KB Output is correct
7 Correct 14 ms 21336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10596 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10692 KB Output is correct
10 Correct 2 ms 10584 KB Output is correct
11 Correct 2 ms 10584 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 57 ms 26364 KB Output is correct
14 Correct 61 ms 31312 KB Output is correct
15 Correct 77 ms 26560 KB Output is correct
16 Correct 45 ms 31300 KB Output is correct
17 Correct 40 ms 26708 KB Output is correct
18 Correct 41 ms 24660 KB Output is correct
19 Correct 38 ms 39004 KB Output is correct
20 Correct 36 ms 26084 KB Output is correct
21 Correct 16 ms 20276 KB Output is correct
22 Correct 55 ms 39168 KB Output is correct
23 Correct 74 ms 53584 KB Output is correct
24 Correct 118 ms 76664 KB Output is correct
25 Correct 130 ms 107092 KB Output is correct
26 Correct 139 ms 74100 KB Output is correct
27 Correct 108 ms 42324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10596 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10692 KB Output is correct
10 Correct 71 ms 42128 KB Output is correct
11 Correct 38 ms 41108 KB Output is correct
12 Correct 175 ms 108116 KB Output is correct
13 Correct 135 ms 107856 KB Output is correct
14 Correct 146 ms 108624 KB Output is correct
15 Correct 2 ms 10584 KB Output is correct
16 Correct 2 ms 10584 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 57 ms 26364 KB Output is correct
19 Correct 61 ms 31312 KB Output is correct
20 Correct 77 ms 26560 KB Output is correct
21 Correct 37 ms 20372 KB Output is correct
22 Correct 34 ms 20352 KB Output is correct
23 Correct 33 ms 22868 KB Output is correct
24 Correct 38 ms 22392 KB Output is correct
25 Correct 36 ms 26708 KB Output is correct
26 Correct 45 ms 31300 KB Output is correct
27 Correct 40 ms 26708 KB Output is correct
28 Correct 41 ms 24660 KB Output is correct
29 Correct 38 ms 39004 KB Output is correct
30 Correct 36 ms 26084 KB Output is correct
31 Correct 4 ms 13148 KB Output is correct
32 Correct 15 ms 19036 KB Output is correct
33 Correct 15 ms 18804 KB Output is correct
34 Correct 15 ms 19036 KB Output is correct
35 Correct 14 ms 18520 KB Output is correct
36 Correct 15 ms 20056 KB Output is correct
37 Correct 14 ms 21336 KB Output is correct
38 Correct 16 ms 20276 KB Output is correct
39 Correct 55 ms 39168 KB Output is correct
40 Correct 74 ms 53584 KB Output is correct
41 Correct 118 ms 76664 KB Output is correct
42 Correct 130 ms 107092 KB Output is correct
43 Correct 139 ms 74100 KB Output is correct
44 Correct 108 ms 42324 KB Output is correct
45 Correct 17 ms 22616 KB Output is correct
46 Correct 58 ms 41468 KB Output is correct
47 Correct 82 ms 55968 KB Output is correct
48 Correct 137 ms 83536 KB Output is correct
49 Correct 139 ms 107660 KB Output is correct
50 Correct 139 ms 74568 KB Output is correct
51 Correct 100 ms 43020 KB Output is correct