QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#467812#2429. Conquer The WorldxuqinAC ✓1809ms168132kbC++142.0kb2024-07-08 17:33:372024-07-08 17:33:37

Judging History

你现在查看的是最新测评结果

  • [2024-07-08 17:33:37]
  • 评测
  • 测评结果:AC
  • 用时:1809ms
  • 内存:168132kb
  • [2024-07-08 17:33:37]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#include<set>
#include<utility>
#define eb emplace_back

using namespace std;
const int maxn=2.5e5+10, inf=1e9+10;
const double eps=1e-10;
typedef long long LL;
typedef pair<int, int> pii;

inline int read() {
    int x=0, f=1; char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=0;
    for(; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
    return f?x:-x;
}
struct pj{
    int y, nxt, d;
} g[maxn<<1];
int len, la[maxn];
inline void add_e(int x, int y, int d) {g[++len].y=y; g[len].nxt=la[x]; la[x]=len; g[len].d=d;}
int a[maxn]; LL dis[maxn];

void dfs(int x, int F) {
    for(int i=la[x]; i; i=g[i].nxt) {
        int y=g[i].y;
        if(y!=F) dis[y]=dis[x]+g[i].d, dfs(y, x);
    }
}
const LL M=5e12;
//small
multiset<LL> up[maxn], dn[maxn];

inline void merge(multiset<LL>& x, multiset<LL>& y) {
    if(x.size()<y.size()) swap(x, y);
    for(auto t:y) x.insert(t);
    y.clear();
}
LL ans;
void sol(int x, int F) {
   // printf("x=%d dis[x]=%lld\n", x, dis[x]);
    for(int i=la[x]; i; i=g[i].nxt) {
        int y=g[i].y;
        if(y!=F) sol(y, x), merge(up[x], up[y]), merge(dn[x], dn[y]);
    }
    if(a[x]>0) {
        for(int i=1; i<=a[x]; ++i) up[x].insert(dis[x]);
    } else if(a[x]<0) {
        for(int i=1; i<=-a[x]; ++i) dn[x].insert(dis[x]-M);
    }
    while(up[x].size()&&dn[x].size()) {
        LL u=*up[x].begin(), v=*dn[x].begin();
        if(u+v-2*dis[x]>=0) break;
        ans+=u+v-2*dis[x];
        up[x].erase(up[x].begin()); up[x].insert(2*dis[x]-v);
        dn[x].erase(dn[x].begin()); dn[x].insert(2*dis[x]-u);
    }
}

int main() {
    int n=read();
    for(int i=1, x, y, d; i<n; ++i) {
        x=read(), y=read(), d=read();
        add_e(x, y, d); add_e(y, x, d);
    }
    int s=0;
    for(int i=1; i<=n; ++i) {
        a[i]=read(), a[i]-=read();
        if(a[i]<0) s+=-a[i];
    }
    dfs(1, 0);
    sol(1, 0);
    printf("%lld", ans+M*s);
    return 0;
}

Details

Test #1:

score: 100
Accepted
time: 7ms
memory: 28232kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 28552kb

Test #3:

score: 0
Accepted
time: 6ms
memory: 29716kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 29288kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 30152kb

Test #6:

score: 0
Accepted
time: 7ms
memory: 29304kb

Test #7:

score: 0
Accepted
time: 0ms
memory: 29144kb

Test #8:

score: 0
Accepted
time: 4ms
memory: 29740kb

Test #9:

score: 0
Accepted
time: 127ms
memory: 61308kb

Test #10:

score: 0
Accepted
time: 172ms
memory: 59840kb

Test #11:

score: 0
Accepted
time: 236ms
memory: 79944kb

Test #12:

score: 0
Accepted
time: 7ms
memory: 28640kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 29048kb

Test #14:

score: 0
Accepted
time: 0ms
memory: 31280kb

Test #15:

score: 0
Accepted
time: 4ms
memory: 30092kb

Test #16:

score: 0
Accepted
time: 0ms
memory: 29440kb

Test #17:

score: 0
Accepted
time: 9ms
memory: 29092kb

Test #18:

score: 0
Accepted
time: 12ms
memory: 32384kb

Test #19:

score: 0
Accepted
time: 62ms
memory: 46380kb

Test #20:

score: 0
Accepted
time: 453ms
memory: 95508kb

Test #21:

score: 0
Accepted
time: 42ms
memory: 36180kb

Test #22:

score: 0
Accepted
time: 34ms
memory: 35912kb

Test #23:

score: 0
Accepted
time: 39ms
memory: 36956kb

Test #24:

score: 0
Accepted
time: 27ms
memory: 35372kb

Test #25:

score: 0
Accepted
time: 32ms
memory: 34712kb

Test #26:

score: 0
Accepted
time: 50ms
memory: 37680kb

Test #27:

score: 0
Accepted
time: 529ms
memory: 83656kb

Test #28:

score: 0
Accepted
time: 371ms
memory: 58952kb

Test #29:

score: 0
Accepted
time: 705ms
memory: 101756kb

Test #30:

score: 0
Accepted
time: 1389ms
memory: 121924kb

Test #31:

score: 0
Accepted
time: 1167ms
memory: 98176kb

Test #32:

score: 0
Accepted
time: 1003ms
memory: 94964kb

Test #33:

score: 0
Accepted
time: 905ms
memory: 101380kb

Test #34:

score: 0
Accepted
time: 1257ms
memory: 112404kb

Test #35:

score: 0
Accepted
time: 279ms
memory: 85232kb

Test #36:

score: 0
Accepted
time: 415ms
memory: 92492kb

Test #37:

score: 0
Accepted
time: 1809ms
memory: 124644kb

Test #38:

score: 0
Accepted
time: 921ms
memory: 168108kb

Test #39:

score: 0
Accepted
time: 926ms
memory: 149908kb

Test #40:

score: 0
Accepted
time: 470ms
memory: 99512kb

Test #41:

score: 0
Accepted
time: 584ms
memory: 118376kb

Test #42:

score: 0
Accepted
time: 401ms
memory: 102908kb

Test #43:

score: 0
Accepted
time: 410ms
memory: 106032kb

Test #44:

score: 0
Accepted
time: 407ms
memory: 111148kb

Test #45:

score: 0
Accepted
time: 551ms
memory: 127612kb

Test #46:

score: 0
Accepted
time: 0ms
memory: 29652kb

Test #47:

score: 0
Accepted
time: 223ms
memory: 77536kb

Test #48:

score: 0
Accepted
time: 3ms
memory: 29428kb

Test #49:

score: 0
Accepted
time: 767ms
memory: 101520kb

Test #50:

score: 0
Accepted
time: 48ms
memory: 36936kb

Test #51:

score: 0
Accepted
time: 0ms
memory: 29320kb

Test #52:

score: 0
Accepted
time: 0ms
memory: 29256kb

Test #53:

score: 0
Accepted
time: 5ms
memory: 28464kb

Test #54:

score: 0
Accepted
time: 5ms
memory: 29628kb

Test #55:

score: 0
Accepted
time: 5ms
memory: 29548kb

Test #56:

score: 0
Accepted
time: 0ms
memory: 28688kb

Test #57:

score: 0
Accepted
time: 3ms
memory: 30000kb

Test #58:

score: 0
Accepted
time: 6ms
memory: 30928kb

Test #59:

score: 0
Accepted
time: 122ms
memory: 46564kb

Test #60:

score: 0
Accepted
time: 353ms
memory: 61700kb

Test #61:

score: 0
Accepted
time: 1313ms
memory: 120692kb

Test #62:

score: 0
Accepted
time: 1216ms
memory: 126500kb

Test #63:

score: 0
Accepted
time: 892ms
memory: 130764kb

Test #64:

score: 0
Accepted
time: 872ms
memory: 130836kb

Test #65:

score: 0
Accepted
time: 894ms
memory: 130836kb

Test #66:

score: 0
Accepted
time: 863ms
memory: 130684kb

Test #67:

score: 0
Accepted
time: 716ms
memory: 154332kb

Test #68:

score: 0
Accepted
time: 852ms
memory: 168132kb

Test #69:

score: 0
Accepted
time: 898ms
memory: 161088kb

Test #70:

score: 0
Accepted
time: 908ms
memory: 160052kb