Submission #484486

# Submission time Handle Problem Language Result Execution time Memory
484486 2021-11-03T16:35:12 Z Alexandruabcde Election Campaign (JOI15_election_campaign) C++14
100 / 100
242 ms 31452 KB
#include <bits/stdc++.h>
#define ub(x) (x&(-x))

using namespace std;

constexpr int NMAX = 1e5 + 5;
typedef pair< pair<int, int>, int> PIII;

int N;
vector <int> G[NMAX];
int Parent[20][NMAX];
int Level[NMAX];

vector <int> Euler;
int poz[NMAX];
int sz[NMAX];

vector <PIII> questions[NMAX];

int aib[NMAX];
int ans[NMAX];

int dp[NMAX];
int fara[NMAX];

void Update (int pos, int val) {
    for (int i = pos; i <= N; i += ub(i))
        aib[i] += val;
}

int Query (int pos) {
    int S = 0;
    for (int i = pos; i; i -= ub(i))
        S += aib[i];

    return S;
}

void UpdateInterval (int a, int b, int val) {
    Update(a, val);
    Update(b+1, -val);
}

void Read () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;

    for (int i = 1; i < N; ++ i ) {
        int x, y;
        cin >> x >> y;

        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void Dfs (int Node, int dad = 0) {
    Parent[0][Node] = dad;
    Level[Node] = Level[dad] + 1;
    Euler.push_back(Node);
    sz[Node] = 1;
    poz[Node] = Euler.size();

    for (int lg = 1; lg < 20; ++ lg )
        Parent[lg][Node] = Parent[lg-1][Parent[lg-1][Node]];

    for (auto it : G[Node]) {
        if (it == dad) continue;

        Dfs(it, Node);
        sz[Node] += sz[it];
    }
}

int FindParent (int Node, int what) {
    for (int i = 19; i >= 0; -- i )
        if ((what & (1<<i)))
            Node = Parent[i][Node];

    return Node;
}

int Lca (int a, int b) {
    if (Level[a] < Level[b])
        swap(a, b);

    int dist = Level[a] - Level[b];
    a = FindParent(a, dist);

    if (a == b) return a;

    for (int i = 19; i >= 0; -- i )
        if (Parent[i][a] != Parent[i][b]) {
            a = Parent[i][a];
            b = Parent[i][b];
        }

    a = Parent[0][a];

    return a;
}

void ReadQueries () {
    int Q;
    cin >> Q;

    for (int i = 1; i <= Q; ++ i ) {
        int A, B, X;
        cin >> A >> B >> X;

        questions[Lca(A, B)].push_back({{A, B}, X});
    }
}

void SolveDFs (int Node, int dad = 0) {
    for (auto it : G[Node]) {
        if (it == dad) continue;

        SolveDFs(it, Node);
        fara[Node] += dp[it];
    }

    dp[Node] = fara[Node];

    for (auto q : questions[Node]) {
        int val = fara[Node] + q.second + Query(poz[q.first.first]) - Query(poz[Node]) + Query(poz[q.first.second]) - Query(poz[Node]);
        dp[Node] = max(dp[Node], val);
    }

    UpdateInterval(poz[Node], poz[Node] + sz[Node]-1, -(dp[Node] - fara[Node]));
}

int main () {
    Read();
    Dfs(1);
    ReadQueries();
    SolveDFs(1);

    cout << dp[1] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5288 KB Output is correct
5 Correct 92 ms 20140 KB Output is correct
6 Correct 49 ms 27808 KB Output is correct
7 Correct 94 ms 25016 KB Output is correct
8 Correct 65 ms 20136 KB Output is correct
9 Correct 95 ms 23540 KB Output is correct
10 Correct 78 ms 20056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5068 KB Output is correct
2 Correct 2 ms 5068 KB Output is correct
3 Correct 3 ms 5288 KB Output is correct
4 Correct 92 ms 30912 KB Output is correct
5 Correct 98 ms 30904 KB Output is correct
6 Correct 88 ms 30944 KB Output is correct
7 Correct 93 ms 30900 KB Output is correct
8 Correct 95 ms 30912 KB Output is correct
9 Correct 93 ms 31068 KB Output is correct
10 Correct 93 ms 30940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5068 KB Output is correct
2 Correct 2 ms 5068 KB Output is correct
3 Correct 3 ms 5288 KB Output is correct
4 Correct 92 ms 30912 KB Output is correct
5 Correct 98 ms 30904 KB Output is correct
6 Correct 88 ms 30944 KB Output is correct
7 Correct 93 ms 30900 KB Output is correct
8 Correct 95 ms 30912 KB Output is correct
9 Correct 93 ms 31068 KB Output is correct
10 Correct 93 ms 30940 KB Output is correct
11 Correct 13 ms 6092 KB Output is correct
12 Correct 96 ms 31212 KB Output is correct
13 Correct 129 ms 31244 KB Output is correct
14 Correct 99 ms 31272 KB Output is correct
15 Correct 102 ms 31272 KB Output is correct
16 Correct 89 ms 31268 KB Output is correct
17 Correct 125 ms 31160 KB Output is correct
18 Correct 108 ms 31216 KB Output is correct
19 Correct 88 ms 31208 KB Output is correct
20 Correct 112 ms 31452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 22752 KB Output is correct
2 Correct 85 ms 31012 KB Output is correct
3 Correct 177 ms 27836 KB Output is correct
4 Correct 107 ms 22972 KB Output is correct
5 Correct 189 ms 27308 KB Output is correct
6 Correct 120 ms 22972 KB Output is correct
7 Correct 166 ms 27084 KB Output is correct
8 Correct 145 ms 23056 KB Output is correct
9 Correct 115 ms 30908 KB Output is correct
10 Correct 181 ms 26056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5288 KB Output is correct
5 Correct 92 ms 20140 KB Output is correct
6 Correct 49 ms 27808 KB Output is correct
7 Correct 94 ms 25016 KB Output is correct
8 Correct 65 ms 20136 KB Output is correct
9 Correct 95 ms 23540 KB Output is correct
10 Correct 78 ms 20056 KB Output is correct
11 Correct 4 ms 5196 KB Output is correct
12 Correct 5 ms 5324 KB Output is correct
13 Correct 3 ms 5296 KB Output is correct
14 Correct 3 ms 5324 KB Output is correct
15 Correct 4 ms 5296 KB Output is correct
16 Correct 4 ms 5296 KB Output is correct
17 Correct 3 ms 5324 KB Output is correct
18 Correct 4 ms 5324 KB Output is correct
19 Correct 4 ms 5324 KB Output is correct
20 Correct 4 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5288 KB Output is correct
5 Correct 92 ms 20140 KB Output is correct
6 Correct 49 ms 27808 KB Output is correct
7 Correct 94 ms 25016 KB Output is correct
8 Correct 65 ms 20136 KB Output is correct
9 Correct 95 ms 23540 KB Output is correct
10 Correct 78 ms 20056 KB Output is correct
11 Correct 2 ms 5068 KB Output is correct
12 Correct 2 ms 5068 KB Output is correct
13 Correct 3 ms 5288 KB Output is correct
14 Correct 92 ms 30912 KB Output is correct
15 Correct 98 ms 30904 KB Output is correct
16 Correct 88 ms 30944 KB Output is correct
17 Correct 93 ms 30900 KB Output is correct
18 Correct 95 ms 30912 KB Output is correct
19 Correct 93 ms 31068 KB Output is correct
20 Correct 93 ms 30940 KB Output is correct
21 Correct 13 ms 6092 KB Output is correct
22 Correct 96 ms 31212 KB Output is correct
23 Correct 129 ms 31244 KB Output is correct
24 Correct 99 ms 31272 KB Output is correct
25 Correct 102 ms 31272 KB Output is correct
26 Correct 89 ms 31268 KB Output is correct
27 Correct 125 ms 31160 KB Output is correct
28 Correct 108 ms 31216 KB Output is correct
29 Correct 88 ms 31208 KB Output is correct
30 Correct 112 ms 31452 KB Output is correct
31 Correct 149 ms 22752 KB Output is correct
32 Correct 85 ms 31012 KB Output is correct
33 Correct 177 ms 27836 KB Output is correct
34 Correct 107 ms 22972 KB Output is correct
35 Correct 189 ms 27308 KB Output is correct
36 Correct 120 ms 22972 KB Output is correct
37 Correct 166 ms 27084 KB Output is correct
38 Correct 145 ms 23056 KB Output is correct
39 Correct 115 ms 30908 KB Output is correct
40 Correct 181 ms 26056 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 5 ms 5324 KB Output is correct
43 Correct 3 ms 5296 KB Output is correct
44 Correct 3 ms 5324 KB Output is correct
45 Correct 4 ms 5296 KB Output is correct
46 Correct 4 ms 5296 KB Output is correct
47 Correct 3 ms 5324 KB Output is correct
48 Correct 4 ms 5324 KB Output is correct
49 Correct 4 ms 5324 KB Output is correct
50 Correct 4 ms 5324 KB Output is correct
51 Correct 143 ms 23352 KB Output is correct
52 Correct 100 ms 31240 KB Output is correct
53 Correct 242 ms 26544 KB Output is correct
54 Correct 105 ms 23164 KB Output is correct
55 Correct 146 ms 22968 KB Output is correct
56 Correct 99 ms 31168 KB Output is correct
57 Correct 183 ms 27192 KB Output is correct
58 Correct 111 ms 23100 KB Output is correct
59 Correct 176 ms 23268 KB Output is correct
60 Correct 97 ms 31188 KB Output is correct
61 Correct 164 ms 27312 KB Output is correct
62 Correct 111 ms 22820 KB Output is correct
63 Correct 193 ms 22836 KB Output is correct
64 Correct 104 ms 31164 KB Output is correct
65 Correct 172 ms 27084 KB Output is correct
66 Correct 100 ms 23144 KB Output is correct
67 Correct 184 ms 23208 KB Output is correct
68 Correct 104 ms 31252 KB Output is correct
69 Correct 150 ms 26060 KB Output is correct
70 Correct 107 ms 23232 KB Output is correct