제출 #1137467

#제출 시각아이디문제언어결과실행 시간메모리
1137467icoder178Harbingers (CEOI09_harbingers)C++20
90 / 100
113 ms37192 KiB
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
const int maxn = 100005;
int dp[maxn];
signed n,dst[maxn],rmv[maxn],s[maxn],v[maxn],ncnt = 0,rcnt = 0,chcnt = 0,connl[maxn],connr[maxn];
pair<signed,int> rmvd[maxn],cht[maxn];
pair<signed,signed> nconn[maxn];
pair<signed,pair<signed,signed> > conn[maxn*2];
inline void fdp(signed pos) {
    signed l = -5, r = chcnt+5;
    while(l+1 < r) {
        signed mid = (l+r)/2;
        bool isDecr;
        if(mid < 0) isDecr = true;
        else if(mid >= chcnt-1) isDecr = false;
        else {
            int vl1 = cht[mid].second-(int) v[pos]*(int) cht[mid].first;
            int vl2 = cht[mid+1].second-(int) v[pos]*(int) cht[mid+1].first;
            isDecr = (vl2 < vl1);
        }
        if(isDecr == true) l = mid;
        else r = mid;
    }
    dp[pos] = cht[r].second-(int) v[pos]*(int) cht[r].first+(int) v[pos]*(int) dst[pos]+(int) s[pos];
}
inline void prmv(signed pos) {
    signed l = -5, r = chcnt+5;
    while(l+1 < r) {
        signed mid = (l+r)/2;
        bool isV;
        if(mid < 0) isV = true;
        else if(mid >= (chcnt-1)) isV = false;
        else {
            __int128_t x1 = cht[mid].first;
            __int128_t y1 = cht[mid].second;
            __int128_t x2 = cht[mid+1].first;
            __int128_t y2 = cht[mid+1].second;
            __int128_t x3 = dst[pos];
            __int128_t y3 = dp[pos];
            __int128_t v1 = (y3-y2)*(x2-x1);
            __int128_t v2 = (y2-y1)*(x3-x2);
            isV = (v1 > v2);
        }
        if(isV) l = mid;
        else r = mid;
    }
    rmv[pos] = r+1;
}
void dfs(signed pos, signed prevP) {
    signed ncl = ncnt;
    for(int i = connl[pos]; i < connr[pos]; i++) {
        signed targP = conn[i].second.first;
        signed newV = conn[i].second.second;
        if(targP != prevP) {
            dst[targP] = dst[pos]+newV;
            fdp(targP);
            prmv(targP);
            nconn[ncnt].first = rmv[targP];
            nconn[ncnt].second = targP;
            ncnt++;
        }
    }
    signed ncr = ncnt;
    sort(nconn+ncl,nconn+ncr);
    signed rcl = rcnt;
    for(signed i = ncr-1; i >= ncl; i--) {
        while(chcnt > nconn[i].first) {
            rmvd[rcnt] = cht[chcnt-1];
            rcnt++;
            chcnt--;
        }
        signed targP = nconn[i].second;
        cht[chcnt].first = dst[targP];
        cht[chcnt].second = dp[targP];
        chcnt++;
        dfs(targP,pos);
        chcnt--;
    }
    ncnt = ncl;
    signed rcr = rcnt;
    for(int i = rcr-1; i >= rcl; i--) {
        cht[chcnt] = rmvd[i];
        chcnt++;
    }
    rcnt = rcl;
}
signed main () {
    scanf("%d",&n);
    for(int i = 0; i < n-1; i++) {
        int a,b,c;
        scanf("%d%d%lld",&conn[i*2].first,&conn[i*2].second.first,&conn[i*2].second.second);
        conn[i*2+1].first = conn[i*2].second.first;
        conn[i*2+1].second.first = conn[i*2].first;
        conn[i*2+1].second.second = conn[i*2].second.second;
    }
    sort(conn,conn+n*2-2);
    for(int i = 0; i < n*2-2; i++) {
        connr[conn[i].first] = i+1;
    }
    for(int i = n*2-3; i >= 0; i--) {
        connl[conn[i].first] = i;
    }
    for(int i = 2; i <= n; i++) {
        scanf("%d%d",&s[i],&v[i]);
    }
    chcnt++;
    dfs(1,-1);
    for(int i = 2; i <= n; i++) {
        printf("%lld ",dp[i]);
    }
    printf("\n");
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

harbingers.cpp: In function 'int main()':
harbingers.cpp:93:23: warning: format '%lld' expects argument of type 'long long int*', but argument 4 has type 'int*' [-Wformat=]
   93 |         scanf("%d%d%lld",&conn[i*2].first,&conn[i*2].second.first,&conn[i*2].second.second);
      |                    ~~~^                                           ~~~~~~~~~~~~~~~~~~~~~~~~
      |                       |                                           |
      |                       long long int*                              int*
      |                    %d
harbingers.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
harbingers.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         scanf("%d%d%lld",&conn[i*2].first,&conn[i*2].second.first,&conn[i*2].second.second);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         scanf("%d%d",&s[i],&v[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...