Submission #1104948

# Submission time Handle Problem Language Result Execution time Memory
1104948 2024-10-24T21:18:12 Z jerzyk Tree (IOI24_tree) C++17
0 / 100
118 ms 86220 KB
#include <bits/stdc++.h>
#include "tree.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<<18;
int drz[2 * N];
vector<int> aktv;
int pre[N], pos[N], rev[N];

ll wei[N];
ll inc[N * 5], sum[N * 5];
int il[N];
vector<int> ed[N];

void Change(int v, int x)
{
    v += N;
    drz[v] = x;
    v /= 2;
    while(v > 0)
    {
        drz[v] = max(drz[v * 2], drz[v * 2 + 1]);
        v /= 2;
    }
}

void Find(int v, int p, int k, int pz, int kz, int x)
{
    if(drz[v] <= x) return;
    if(p > kz || k < pz) return;
    if(v >= N)
    {
        drz[v] = -1;
        aktv.pb(rev[v - N]);
        return;
    }
    Find(v * 2, p, (p + k) / 2, pz, kz, x);
    Find(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz, x);
    drz[v] = max(drz[v * 2], drz[v * 2 + 1]);
}

bool C(int a, int b)
{
    return (wei[a] > wei[b]);
}

void DFS(int v, int &cnt)
{
    ++cnt; rev[cnt] = v; pre[v] = cnt; pos[v] = cnt;
    il[v] = max(0, (int)ed[v].size() - 1);
    Change(pre[v], wei[v]);

    sum[0] += wei[v];
    //cerr << "DFS: " << v << " " << wei[v] << "\n";

    for(int i = 0; i < (int)ed[v].size(); ++i)
    {
        aktv.clear();
        DFS(ed[v][i], cnt);
        pos[v] = pos[ed[v][i]];
        Find(1, 0, N - 1, pre[ed[v][i]], pos[ed[v][i]], wei[v]);
        sort(aktv.begin(), aktv.end(), C);
        int s = 0;
        for(int j = 0; j < (int)aktv.size(); ++j)
        {
            int ne = aktv[j];
            inc[1 + s] += wei[v] - wei[ne];
            inc[1 + s + il[ne]] -= wei[v] - wei[ne];
            s += il[ne];
        }
        il[v] += s;
    }
}

void init(vector<int> P, vector<int> W)
{
    for(int i = 1; i < 2 * N; ++i) drz[i] = -1;

    int n = (int)P.size() + 1;
    for(int i = 1; i < (int)P.size(); ++i)
        ed[P[i] + 1].pb(i + 1);
    for(int i = 0; i < n; ++i)
        wei[i + 1] = W[i];
    ed[0].pb(1);
    int xd = 0;
    DFS(0, xd);
    for(int i = 1; i <= 1000'000; ++i)
        inc[i] += inc[i - 1];
    for(int i = 1; i <= 1000'000; ++i)
        sum[i] = sum[i - 1] + inc[i - 1];
}

long long query(int L, int R)
{
    ll ans = sum[R / L] + inc[R / L] * (ll)(R % L);
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 109 ms 86220 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 35408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 35408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 58172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 58172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 118 ms 57084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Runtime error 109 ms 86220 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -