답안 #956612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956612 2024-04-02T08:27:35 Z sleepntsheep Magic Tree (CEOI19_magictree) C++17
100 / 100
203 ms 106344 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.cpp: In function 'int main()':
magictree.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d%d%d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
magictree.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' 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.cpp:76:40: warning: ignoring return value of 'int scanf(const char*, ...)' 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 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 41988 KB Output is correct
2 Correct 51 ms 40228 KB Output is correct
3 Correct 203 ms 105748 KB Output is correct
4 Correct 133 ms 105556 KB Output is correct
5 Correct 197 ms 106344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10592 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 55 ms 24612 KB Output is correct
5 Correct 60 ms 29216 KB Output is correct
6 Correct 77 ms 24504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 20376 KB Output is correct
2 Correct 35 ms 18320 KB Output is correct
3 Correct 46 ms 20884 KB Output is correct
4 Correct 32 ms 20632 KB Output is correct
5 Correct 32 ms 24456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 52 ms 29780 KB Output is correct
11 Correct 41 ms 25428 KB Output is correct
12 Correct 37 ms 23212 KB Output is correct
13 Correct 46 ms 37972 KB Output is correct
14 Correct 35 ms 24412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13148 KB Output is correct
2 Correct 14 ms 18268 KB Output is correct
3 Correct 16 ms 18456 KB Output is correct
4 Correct 14 ms 18268 KB Output is correct
5 Correct 10 ms 18412 KB Output is correct
6 Correct 18 ms 19548 KB Output is correct
7 Correct 19 ms 20824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 2 ms 10592 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 12632 KB Output is correct
13 Correct 55 ms 24612 KB Output is correct
14 Correct 60 ms 29216 KB Output is correct
15 Correct 77 ms 24504 KB Output is correct
16 Correct 52 ms 29780 KB Output is correct
17 Correct 41 ms 25428 KB Output is correct
18 Correct 37 ms 23212 KB Output is correct
19 Correct 46 ms 37972 KB Output is correct
20 Correct 35 ms 24412 KB Output is correct
21 Correct 17 ms 20060 KB Output is correct
22 Correct 48 ms 37896 KB Output is correct
23 Correct 72 ms 52296 KB Output is correct
24 Correct 139 ms 74896 KB Output is correct
25 Correct 127 ms 105556 KB Output is correct
26 Correct 122 ms 72060 KB Output is correct
27 Correct 95 ms 40532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 69 ms 41988 KB Output is correct
11 Correct 51 ms 40228 KB Output is correct
12 Correct 203 ms 105748 KB Output is correct
13 Correct 133 ms 105556 KB Output is correct
14 Correct 197 ms 106344 KB Output is correct
15 Correct 2 ms 10592 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 2 ms 12632 KB Output is correct
18 Correct 55 ms 24612 KB Output is correct
19 Correct 60 ms 29216 KB Output is correct
20 Correct 77 ms 24504 KB Output is correct
21 Correct 38 ms 20376 KB Output is correct
22 Correct 35 ms 18320 KB Output is correct
23 Correct 46 ms 20884 KB Output is correct
24 Correct 32 ms 20632 KB Output is correct
25 Correct 32 ms 24456 KB Output is correct
26 Correct 52 ms 29780 KB Output is correct
27 Correct 41 ms 25428 KB Output is correct
28 Correct 37 ms 23212 KB Output is correct
29 Correct 46 ms 37972 KB Output is correct
30 Correct 35 ms 24412 KB Output is correct
31 Correct 5 ms 13148 KB Output is correct
32 Correct 14 ms 18268 KB Output is correct
33 Correct 16 ms 18456 KB Output is correct
34 Correct 14 ms 18268 KB Output is correct
35 Correct 10 ms 18412 KB Output is correct
36 Correct 18 ms 19548 KB Output is correct
37 Correct 19 ms 20824 KB Output is correct
38 Correct 17 ms 20060 KB Output is correct
39 Correct 48 ms 37896 KB Output is correct
40 Correct 72 ms 52296 KB Output is correct
41 Correct 139 ms 74896 KB Output is correct
42 Correct 127 ms 105556 KB Output is correct
43 Correct 122 ms 72060 KB Output is correct
44 Correct 95 ms 40532 KB Output is correct
45 Correct 19 ms 22108 KB Output is correct
46 Correct 56 ms 40020 KB Output is correct
47 Correct 85 ms 54272 KB Output is correct
48 Correct 156 ms 80928 KB Output is correct
49 Correct 126 ms 105608 KB Output is correct
50 Correct 138 ms 72060 KB Output is correct
51 Correct 99 ms 40324 KB Output is correct