답안 #1099823

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1099823 2024-10-12T05:30:35 Z model_code 트리 (IOI24_tree) C++17
100 / 100
157 ms 30264 KB
// correct/agustin-dual-segtree.cpp

#include "tree.h"

#include <vector>
#include <cassert>

using namespace std;

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define dforn(i,n) for(int i=int(n)-1;i>=0;i--)
#define dforsn(i,s,n) for(int i=int(n)-1;i>=int(s);i--)

#define SIZE(c) int((c).size())

vector<long long> sumCapL, sumCapR;
long long leafWeights;
int N;
vector<int> W;


struct Data
{
    int minIndex; // Index of min in subtree, -1 as neutral element
    int leafCount;
    
    Data() { minIndex = -1; leafCount = 0; }
    Data(int mini, int leaves) { minIndex = mini; leafCount = leaves; }
    
    bool operator ==(const Data &o) const { return minIndex == o.minIndex && leafCount == o.leafCount; }
};

const Data NEUTRAL;
const Data LEAF(-1,1);

Data op(const Data &a, const Data &b)
{
    Data ret;
    ret.leafCount = a.leafCount + b.leafCount;
    if (a.minIndex < 0 || b.minIndex < 0)
        ret.minIndex = a.minIndex + b.minIndex + 1;
    else if (W[a.minIndex] < W[b.minIndex])
        ret.minIndex = a.minIndex;
    else
        ret.minIndex = b.minIndex;
    return ret;
}

vector<Data> st; 
int stBase;

vector<vector<int>> g;
vector<int> startTime, endTime;


int discoveryTime;
void dfsSegtree (int node) {
    startTime[node] = discoveryTime++;
    for (int child : g[node])
        dfsSegtree(child);
    endTime[node] = discoveryTime;
};

void stSet(int i, Data value)
{
    i += stBase;
    st[i] = value;
    while (i > 0)
    {
        i /= 2;
        st[i] = op(st[2*i], st[2*i+1]);
    }
}

Data stGet(int i, int j)
{
    i += stBase;
    j += stBase;
    Data ret;
    while (i < j)
    {
        if (i % 2) ret = op(ret, st[i++]);
        if (j % 2) ret = op(ret, st[--j]);
        i /= 2;
        j /= 2;
    }
    return ret;
}

vector<long long> capByLeafCount;

bool alive(int node) { return !(st[stBase + startTime[node]] == NEUTRAL); }
bool leaf(int node) { return st[stBase + startTime[node]] == LEAF; }

void goGreedy(int node, long long prevSum)
{
    while (true)
    {
        assert(alive(node));
        if (leaf(node))
        {
            stSet(startTime[node], NEUTRAL);
            return;
        }
        Data subtree = stGet(startTime[node], endTime[node]);
        assert(subtree.minIndex >= 0);
        assert(subtree.leafCount > 0);
        long long delta = W[subtree.minIndex] - prevSum;
        assert(delta >= 0);
        prevSum += delta;
        capByLeafCount[subtree.leafCount] += delta;
        for (int child : g[subtree.minIndex])
        if (alive(child))
            goGreedy(child, prevSum);
        stSet(startTime[subtree.minIndex], LEAF);
    }
}

void init(vector<int> P, vector<int> localW) {
    W = localW;
    N = SIZE(P);
    for(stBase = 1; stBase < N; stBase *= 2);
    st = vector<Data>(2*stBase);
    g = vector<vector<int>>(N);
    forsn(i,1,N)
        g[P[i]].push_back(i);
    discoveryTime = 0;
    startTime = vector<int>(N);
    endTime = vector<int>(N);
    dfsSegtree(0);

    vector<bool> leaf(N, true);
    leafWeights = 0;
    dforn(i,N)
    {
        if (leaf[i])
        {
            leafWeights += W[i];
            st[stBase + startTime[i]] = LEAF;
        }
        else
            st[stBase + startTime[i]].minIndex = i;
        if (i > 0)
            leaf[P[i]] = false;
    }
    dforn(i,stBase)
        st[i] = op(st[2*i], st[2*i+1]);
    
    capByLeafCount = vector<long long>(N+1, 0);
    goGreedy(0, 0);
    sumCapL = sumCapR = vector<long long>(N+2, 0);
    dforn(i,N+1)
    {
        sumCapR[i] = sumCapR[i+1] - capByLeafCount[i];
        sumCapL[i] = sumCapL[i+1] + i * capByLeafCount[i];
    }
}

long long query(int L, int R) {
    int k = min(N+1, (R+L-1)/L);
    return leafWeights * L + sumCapL[k] * L + sumCapR[k] * R;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 30264 KB Output is correct
2 Correct 75 ms 27100 KB Output is correct
3 Correct 80 ms 24912 KB Output is correct
4 Correct 76 ms 23396 KB Output is correct
5 Correct 87 ms 23228 KB Output is correct
6 Correct 110 ms 23224 KB Output is correct
7 Correct 77 ms 23120 KB Output is correct
8 Correct 126 ms 23232 KB Output is correct
9 Correct 74 ms 22356 KB Output is correct
10 Correct 47 ms 20168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 428 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 428 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 24 ms 8280 KB Output is correct
10 Correct 25 ms 8268 KB Output is correct
11 Correct 24 ms 8024 KB Output is correct
12 Correct 26 ms 8284 KB Output is correct
13 Correct 27 ms 7772 KB Output is correct
14 Correct 23 ms 7512 KB Output is correct
15 Correct 28 ms 6960 KB Output is correct
16 Correct 26 ms 6996 KB Output is correct
17 Correct 24 ms 7004 KB Output is correct
18 Correct 23 ms 7004 KB Output is correct
19 Correct 23 ms 7004 KB Output is correct
20 Correct 21 ms 6712 KB Output is correct
21 Correct 21 ms 6748 KB Output is correct
22 Correct 13 ms 6104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 28080 KB Output is correct
2 Correct 103 ms 24372 KB Output is correct
3 Correct 96 ms 24320 KB Output is correct
4 Correct 95 ms 24288 KB Output is correct
5 Correct 95 ms 24372 KB Output is correct
6 Correct 112 ms 26524 KB Output is correct
7 Correct 97 ms 23604 KB Output is correct
8 Correct 63 ms 21412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 28080 KB Output is correct
2 Correct 103 ms 24372 KB Output is correct
3 Correct 96 ms 24320 KB Output is correct
4 Correct 95 ms 24288 KB Output is correct
5 Correct 95 ms 24372 KB Output is correct
6 Correct 112 ms 26524 KB Output is correct
7 Correct 97 ms 23604 KB Output is correct
8 Correct 63 ms 21412 KB Output is correct
9 Correct 91 ms 24372 KB Output is correct
10 Correct 117 ms 24372 KB Output is correct
11 Correct 103 ms 24292 KB Output is correct
12 Correct 102 ms 24372 KB Output is correct
13 Correct 119 ms 26448 KB Output is correct
14 Correct 104 ms 23468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 27184 KB Output is correct
2 Correct 91 ms 24892 KB Output is correct
3 Correct 96 ms 24368 KB Output is correct
4 Correct 108 ms 24464 KB Output is correct
5 Correct 100 ms 24372 KB Output is correct
6 Correct 100 ms 24372 KB Output is correct
7 Correct 96 ms 24368 KB Output is correct
8 Correct 122 ms 26676 KB Output is correct
9 Correct 91 ms 23600 KB Output is correct
10 Correct 91 ms 23532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 92 ms 30264 KB Output is correct
3 Correct 75 ms 27100 KB Output is correct
4 Correct 80 ms 24912 KB Output is correct
5 Correct 76 ms 23396 KB Output is correct
6 Correct 87 ms 23228 KB Output is correct
7 Correct 110 ms 23224 KB Output is correct
8 Correct 77 ms 23120 KB Output is correct
9 Correct 126 ms 23232 KB Output is correct
10 Correct 74 ms 22356 KB Output is correct
11 Correct 47 ms 20168 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 428 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 24 ms 8280 KB Output is correct
21 Correct 25 ms 8268 KB Output is correct
22 Correct 24 ms 8024 KB Output is correct
23 Correct 26 ms 8284 KB Output is correct
24 Correct 27 ms 7772 KB Output is correct
25 Correct 23 ms 7512 KB Output is correct
26 Correct 28 ms 6960 KB Output is correct
27 Correct 26 ms 6996 KB Output is correct
28 Correct 24 ms 7004 KB Output is correct
29 Correct 23 ms 7004 KB Output is correct
30 Correct 23 ms 7004 KB Output is correct
31 Correct 21 ms 6712 KB Output is correct
32 Correct 21 ms 6748 KB Output is correct
33 Correct 13 ms 6104 KB Output is correct
34 Correct 91 ms 28080 KB Output is correct
35 Correct 103 ms 24372 KB Output is correct
36 Correct 96 ms 24320 KB Output is correct
37 Correct 95 ms 24288 KB Output is correct
38 Correct 95 ms 24372 KB Output is correct
39 Correct 112 ms 26524 KB Output is correct
40 Correct 97 ms 23604 KB Output is correct
41 Correct 63 ms 21412 KB Output is correct
42 Correct 91 ms 24372 KB Output is correct
43 Correct 117 ms 24372 KB Output is correct
44 Correct 103 ms 24292 KB Output is correct
45 Correct 102 ms 24372 KB Output is correct
46 Correct 119 ms 26448 KB Output is correct
47 Correct 104 ms 23468 KB Output is correct
48 Correct 82 ms 27184 KB Output is correct
49 Correct 91 ms 24892 KB Output is correct
50 Correct 96 ms 24368 KB Output is correct
51 Correct 108 ms 24464 KB Output is correct
52 Correct 100 ms 24372 KB Output is correct
53 Correct 100 ms 24372 KB Output is correct
54 Correct 96 ms 24368 KB Output is correct
55 Correct 122 ms 26676 KB Output is correct
56 Correct 91 ms 23600 KB Output is correct
57 Correct 91 ms 23532 KB Output is correct
58 Correct 93 ms 28332 KB Output is correct
59 Correct 109 ms 28336 KB Output is correct
60 Correct 107 ms 23548 KB Output is correct
61 Correct 109 ms 25652 KB Output is correct
62 Correct 119 ms 24992 KB Output is correct
63 Correct 117 ms 25000 KB Output is correct
64 Correct 109 ms 24980 KB Output is correct
65 Correct 110 ms 25080 KB Output is correct
66 Correct 157 ms 25000 KB Output is correct
67 Correct 124 ms 25004 KB Output is correct
68 Correct 134 ms 27048 KB Output is correct
69 Correct 105 ms 24116 KB Output is correct
70 Correct 110 ms 23208 KB Output is correct
71 Correct 109 ms 21756 KB Output is correct