Submission #1072544

# Submission time Handle Problem Language Result Execution time Memory
1072544 2024-08-23T21:35:22 Z jerzyk Truck Driver (IOI23_deliveries) C++17
100 / 100
1912 ms 50152 KB
#include <bits/stdc++.h>
#include "deliveries.h"

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1<<17;
int n;
ll tab[N], sum[N], pth[N];
ll al = 0LL;
vector<pair<int, ll>> ed[N];
bool vis[N];

int cnt = 0, clrcnt = 0;
int pos[N], revp[N], frs[N], sp[N], oj[N], il[N];
ll drzs[2 * N], dod[2 * N], l1[2 * N];
ll drzc[2 * N], l2[2 * N];

inline void PushC(int v)
{
    drzc[v * 2] += l2[v]; l2[v * 2] += l2[v];
    drzc[v * 2 + 1] += l2[v]; l2[v * 2 + 1] += l2[v];
    l2[v] = 0;
}

void AddC(int v, int p, int k, int pz, int kz, ll x)
{
    if(p > kz || k < pz) return;
    if(p >= pz && k <= kz)
    {
        drzc[v] += x; l2[v] += x;
        return;
    }
    AddC(v * 2, p, (p + k) / 2, pz, kz, x);
    AddC(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz, x);
    drzc[v] = max(drzc[v * 2], drzc[v * 2 + 1]) + l2[v];
}

int FindC(ll x)
{
    int v = 1;
    while(v < N)
    {
        PushC(v);
        v *= 2; ++v;
        if(drzc[v] < x) --v;
    }
    return v - N;
}

inline void PushS(int v)
{
    drzs[v * 2] += dod[v * 2] * l1[v]; l1[v * 2] += l1[v];
    drzs[v * 2 + 1] += dod[v * 2 + 1] * l1[v]; l1[v * 2 + 1] += l1[v];
    l1[v] = 0LL;
}

void AddS(int v, int p, int k, int pz, int kz, ll x)
{
    if(p > kz || k < pz) return;
    if(p >= pz && k <= kz)
    {
        drzs[v] += dod[v] * x; l1[v] += x;
        return;
    }
    PushS(v);
    AddS(v * 2, p, (p + k) / 2, pz, kz, x);
    AddS(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz, x);
    drzs[v] = drzs[v * 2] + drzs[v * 2 + 1];
}

ll QueryS(int v, int p, int k, int pz, int kz)
{
    if(p > kz || k < pz) return 0LL;
    if(p >= pz && k <= kz)
        return drzs[v];
    ll ans;
    PushS(v);
    ans = QueryS(v * 2, p, (p + k) / 2, pz, kz);
    ans += QueryS(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz);
    return ans;
}

void DFS(int v)
{
    vis[v] = true;
    sum[v] = tab[v]; il[v] = 1;
    for(int i = 0; i < (int)ed[v].size(); ++i)
    {
        if(vis[ed[v][i].st]) continue;
        pth[ed[v][i].st] = pth[v] + ed[v][i].nd;
        oj[ed[v][i].st] = v;
        DFS(ed[v][i].st);
        sum[v] += sum[ed[v][i].st];
        il[v] += il[ed[v][i].st];
    }
    //cerr << answer << " dfs: " << v - 1 << " " << sum[v] << " " << al << " " << ev << "\n";
    
    vis[v] = false;
}

void DFSH(int v, int clr, ll ev)
{
    drzs[pos[v] + N] = ev * sum[v];
    dod[pos[v] + N] = ev;
    drzc[pos[v] + N] = sum[v];

    //cerr << "HLD " << v << " " << clr << " " << pos[v] << " " << ev << " " << sum[v] << "\n";
    revp[pos[v]] = v;
    pair<int, int> ma = make_pair(-1, -1);
    sp[v] = clr;
    for(int i = 0; i < (int)ed[v].size(); ++i)
        if(sp[ed[v][i].st] == 0)
            ma = max(ma, make_pair(il[ed[v][i].st], i));
    if(ma == make_pair(-1, -1)) return;
    int ne = ed[v][ma.nd].st;
    pos[ne] = pos[v] + 1; ++cnt;
    DFSH(ne, clr, ed[v][ma.nd].nd);
    for(int i = 0; i < (int)ed[v].size(); ++i)
    {
        int ne = ed[v][i].st;
        if(sp[ne]) continue;
        ++clrcnt;
        ++cnt; pos[ne] = cnt;
        frs[clrcnt] = ne;
        DFSH(ne, clrcnt, ed[v][i].nd);
    }

}

void HLDChange(int v, ll x)
{
    while(v > 0)
    {
        int a = pos[frs[sp[v]]], b = pos[v];
        AddC(1, 0, N - 1, a, b, x);
        AddS(1, 0, N - 1, a, b, x);
        v = oj[frs[sp[v]]];
    }
}

ll HLDQuery(int v)
{
    ll s = 0LL;
    while(v > 0)
    {
        int a = pos[frs[sp[v]]], b = pos[v];
        s += QueryS(1, 0, N - 1, a, b);
        v = oj[frs[sp[v]]];
    }
    return s;
}

void InitTrees()
{
    for(int v = N - 1; v >= 1; --v)
    {
        drzc[v] = max(drzc[v * 2], drzc[v * 2 + 1]);

        drzs[v] = drzs[v * 2] + drzs[v * 2 + 1];
        dod[v] = dod[v * 2] + dod[v * 2 + 1];
    }
}

void init(int _N, vector<int> U, vector<int> V, vector<int> T, vector<int> W)
{
    n = _N;
    for(int i = 1; i <= n; ++i) tab[i] = W[i - 1];
    tab[1] += 1LL;
    for(int i = 1; i <= n; ++i) al += tab[i];
    for(int i = 0; i < n - 1; ++i)
    {
        ed[U[i] + 1].pb(make_pair(V[i] + 1, (ll)T[i]));
        ed[V[i] + 1].pb(make_pair(U[i] + 1, (ll)T[i]));
    }
    oj[1] = 1;
    DFS(1);
    cnt = 1; clrcnt = 1;
    pos[1] = 1; frs[1] = 1;
    DFSH(1, 1, 0LL);
    oj[1] = 0;
    InitTrees();
    //cerr << "After Init " << drzs[1] << "\n";
}

long long max_time(int S, int X)
{
    ++S; if(S == 1) ++X;
    ll ch = X - tab[S];
    tab[S] += ch; al += ch;
    HLDChange(S, ch);
    ll answer = drzs[1];
    //cout << "Beg " << answer << "\n";
    int cen = revp[FindC((al + 1LL) / 2LL)];
    //cout << "centroid " << cen << "\n";
    answer -= 2LL * HLDQuery(cen);
    answer += pth[cen] * al;
    //cerr << "nquery\n";
    
    return answer * 2LL;
}
# Verdict Execution time Memory Grader output
1 Correct 427 ms 28756 KB Output is correct
2 Correct 424 ms 28500 KB Output is correct
3 Correct 414 ms 28792 KB Output is correct
4 Correct 423 ms 28760 KB Output is correct
5 Correct 422 ms 28788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 18916 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 5 ms 19088 KB Output is correct
6 Correct 4 ms 19036 KB Output is correct
7 Correct 5 ms 18912 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 4 ms 19076 KB Output is correct
10 Correct 5 ms 19036 KB Output is correct
11 Correct 6 ms 19036 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19036 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 5 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 427 ms 28756 KB Output is correct
2 Correct 424 ms 28500 KB Output is correct
3 Correct 414 ms 28792 KB Output is correct
4 Correct 423 ms 28760 KB Output is correct
5 Correct 422 ms 28788 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 13 ms 19036 KB Output is correct
8 Correct 137 ms 20820 KB Output is correct
9 Correct 1912 ms 39272 KB Output is correct
10 Correct 1783 ms 39216 KB Output is correct
11 Correct 1718 ms 39220 KB Output is correct
12 Correct 1454 ms 41632 KB Output is correct
13 Correct 1467 ms 41704 KB Output is correct
14 Correct 1481 ms 41644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 427 ms 28756 KB Output is correct
2 Correct 424 ms 28500 KB Output is correct
3 Correct 414 ms 28792 KB Output is correct
4 Correct 423 ms 28760 KB Output is correct
5 Correct 422 ms 28788 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 8 ms 19036 KB Output is correct
8 Correct 64 ms 21660 KB Output is correct
9 Correct 655 ms 46896 KB Output is correct
10 Correct 672 ms 46900 KB Output is correct
11 Correct 661 ms 46884 KB Output is correct
12 Correct 681 ms 50152 KB Output is correct
13 Correct 645 ms 49972 KB Output is correct
14 Correct 609 ms 46380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 427 ms 28756 KB Output is correct
2 Correct 424 ms 28500 KB Output is correct
3 Correct 414 ms 28792 KB Output is correct
4 Correct 423 ms 28760 KB Output is correct
5 Correct 422 ms 28788 KB Output is correct
6 Correct 4 ms 18780 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 5 ms 18916 KB Output is correct
9 Correct 5 ms 19036 KB Output is correct
10 Correct 5 ms 19088 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 18912 KB Output is correct
13 Correct 4 ms 19036 KB Output is correct
14 Correct 4 ms 19076 KB Output is correct
15 Correct 5 ms 19036 KB Output is correct
16 Correct 6 ms 19036 KB Output is correct
17 Correct 5 ms 19036 KB Output is correct
18 Correct 5 ms 19036 KB Output is correct
19 Correct 5 ms 19036 KB Output is correct
20 Correct 5 ms 19036 KB Output is correct
21 Correct 3 ms 18780 KB Output is correct
22 Correct 13 ms 19036 KB Output is correct
23 Correct 137 ms 20820 KB Output is correct
24 Correct 1912 ms 39272 KB Output is correct
25 Correct 1783 ms 39216 KB Output is correct
26 Correct 1718 ms 39220 KB Output is correct
27 Correct 1454 ms 41632 KB Output is correct
28 Correct 1467 ms 41704 KB Output is correct
29 Correct 1481 ms 41644 KB Output is correct
30 Correct 3 ms 18780 KB Output is correct
31 Correct 8 ms 19036 KB Output is correct
32 Correct 64 ms 21660 KB Output is correct
33 Correct 655 ms 46896 KB Output is correct
34 Correct 672 ms 46900 KB Output is correct
35 Correct 661 ms 46884 KB Output is correct
36 Correct 681 ms 50152 KB Output is correct
37 Correct 645 ms 49972 KB Output is correct
38 Correct 609 ms 46380 KB Output is correct
39 Correct 3 ms 18780 KB Output is correct
40 Correct 9 ms 19152 KB Output is correct
41 Correct 67 ms 21588 KB Output is correct
42 Correct 790 ms 43468 KB Output is correct
43 Correct 799 ms 44336 KB Output is correct
44 Correct 776 ms 45348 KB Output is correct
45 Correct 761 ms 45916 KB Output is correct
46 Correct 724 ms 46640 KB Output is correct
47 Correct 721 ms 45800 KB Output is correct
48 Correct 700 ms 46708 KB Output is correct
49 Correct 703 ms 47412 KB Output is correct
50 Correct 664 ms 48172 KB Output is correct
51 Correct 707 ms 48692 KB Output is correct
52 Correct 1006 ms 41292 KB Output is correct
53 Correct 1018 ms 41268 KB Output is correct
54 Correct 1049 ms 41268 KB Output is correct
55 Correct 655 ms 45108 KB Output is correct
56 Correct 645 ms 45824 KB Output is correct
57 Correct 642 ms 45616 KB Output is correct