Submission #924073

# Submission time Handle Problem Language Result Execution time Memory
924073 2024-02-08T11:17:10 Z boris_mihov Designated Cities (JOI19_designated_cities) C++17
100 / 100
339 ms 65916 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int n, q;
struct Edge
{
    int u, a, b;
};

struct SegmentTree
{
    struct Node
    {
        llong max;
        llong lazy;
        int idx;
    
        Node()
        {
            max = lazy = idx = 0;
        }

        friend Node operator + (const Node &left, const Node &right)
        {
            Node res;
            if (left.max > right.max)
            {
                res.max = left.max;
                res.idx = left.idx;
            } else
            {
                res.max = right.max;
                res.idx = right.idx;
            }
            
            return res;
        }
    };

    Node tree[4*MAXN];
    void push(int node, int l, int r)
    {
        if (tree[node].lazy == 0)
        {
            return;
        }

        tree[node].max -= tree[node].lazy;
        if (l < r)
        {
            tree[2*node].lazy += tree[node].lazy;
            tree[2*node + 1].lazy += tree[node].lazy;
        }

        tree[node].lazy = 0;
    }

    void build(int l, int r, int node, llong dists[], const std::vector <int> &tour)
    {
        if (l == r)
        {
            tree[node].max = dists[tour[l]];
            tree[node].idx = l;
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, dists, tour);
        build(mid + 1, r, 2*node + 1, dists, tour);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    void update(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy += queryVal;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR, queryVal);
        update(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    void build(llong dists[], const std::vector <int> &tour)
    {
        build(0, n - 1, 1, dists, tour);
    }

    void update(int l, int r, int val)
    {
        update(0, n - 1, 1, l, r, val);
    }

    std::pair <llong, int> getMAX()
    {
        assert(tree[1].lazy == 0);
        return {tree[1].max, tree[1].idx};
    }
};

llong sumUp;
llong sumOfAll;
llong dists[MAXN];
llong answer[MAXN];
llong ifRoot[MAXN];
llong maxPath[MAXN];
llong maxPath2[MAXN];
int fromPath[MAXN];
int fromPath2[MAXN];
std::vector <Edge> g[MAXN];
int sz[MAXN];

int in[MAXN];
int out[MAXN];
SegmentTree tree;
std::vector <int> tour;

void sumUpDFS(int node, int par)
{
    sz[node] = 1;
    for (const auto &[u, a, b] : g[node])
    {
        if (u == par)
        {
            continue;
        }

        sumUp += b;
        sumUpDFS(u, node);
        sz[node] += sz[u];
    }
}

void findOneAnswer(int node, int par, llong sum)
{
    ifRoot[node] = sum;
    answer[1] = std::max(answer[1], ifRoot[node]);
    for (const auto &[u, a, b]  : g[node])
    {
        if (u == par)
        {
            continue;
        }

        findOneAnswer(u, node, sum + a - b);
    }
}

void findMaxPath(int node, int par)
{
    fromPath[node] = node;
    for (const auto &[u, a, b] : g[node])
    {
        if (u == par)
        {
            continue;
        }

        findMaxPath(u, node);
        if (maxPath[node] < maxPath[u] + a)
        {
            fromPath2[node] = fromPath[node];
            fromPath[node] = fromPath[u];
            maxPath2[node] = maxPath[node];
            maxPath[node] = maxPath[u] + a;
        } else if (maxPath2[node] < maxPath[u] + a)
        {
            fromPath2[node] = fromPath[u];
            maxPath2[node] = maxPath[u] + a;
        }
    }
}

int parent[MAXN];
int parentEdge[MAXN];
void buildDFS(int node, int par)
{
    parent[node] = par; 
    in[node] = tour.size();
    tour.push_back(node);

    for (const auto &[u, a, b] : g[node])
    {
        if (u == par)
        {
            continue;
        }

        parentEdge[u] = a;
        dists[u] = dists[node] + a;
        buildDFS(u, node);
    }

    out[node] = tour.size() - 1;
}

bool vis[MAXN];

void ennactTake(int node)
{
    while (!vis[node])
    {
        tree.update(in[node], out[node], parentEdge[node]);
        vis[node] = true;
        node = parent[node];
    }
}

llong take()
{
    auto [max, idx] = tree.getMAX();
    ennactTake(tour[idx]);
    return max;
}

void solve()
{
    if (n == 2)
    {
        int sum = g[1][0].a + g[1][0].b;
        int max = std::max(g[1][0].a, g[1][0].b);
        answer[1] = max;
        answer[2] = sum;
        return;
    }

    int root = 1;
    while (root <= n && g[root].size() == 1)
    {
        root++;
    }

    assert(root <= n);
    sumUpDFS(root, 0);
    findOneAnswer(root, 0, sumUp);
    findMaxPath(root, 0);

    root = -1;
    llong last = 0;
    for (int i = 1 ; i <= n ; ++i)
    {
        // std::cout << "here: " << answer[2] << ' ' << ifRoot[i] + maxPath[i] + maxPath2[i] << ' ' << last << '\n';
        answer[2] = std::max(answer[2], ifRoot[i] + maxPath[i] + maxPath2[i]);
        if (answer[2] > last)
        {
            last = answer[2];
            root = i;
        }
    }

    assert(root != -1);
    buildDFS(root, 0);
    assert(tour.size() == n);
    tree.build(dists, tour);

    vis[root] = true;
    ennactTake(fromPath[root]);
    ennactTake(fromPath2[root]);

    for (int i = 3 ; i <= n ; ++i)
    {
        answer[i] = answer[i - 1] + take();
    }
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i < n ; ++i)
    {
        int u, v, a, b;
        std::cin >> u >> v >> a >> b;
        g[u].push_back({v, a, b});
        g[v].push_back({u, b, a});
        sumOfAll += a;
        sumOfAll += b;
    }

    std::cin >> q;
}

void print()
{
    for (int i = 1 ; i <= q ; ++i)
    {
        int cnt;
        std::cin >> cnt;
        std::cout << sumOfAll - answer[cnt] << '\n';
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();
    print();

    return 0;   
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from designated_cities.cpp:4:
designated_cities.cpp: In function 'void solve()':
designated_cities.cpp:270:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  270 |     assert(tour.size() == n);
      |            ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29020 KB Output is correct
2 Correct 5 ms 31128 KB Output is correct
3 Correct 7 ms 31196 KB Output is correct
4 Correct 6 ms 31068 KB Output is correct
5 Correct 5 ms 31068 KB Output is correct
6 Correct 5 ms 31068 KB Output is correct
7 Correct 5 ms 31068 KB Output is correct
8 Correct 5 ms 31068 KB Output is correct
9 Correct 5 ms 31080 KB Output is correct
10 Correct 5 ms 31152 KB Output is correct
11 Correct 5 ms 31068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29016 KB Output is correct
2 Correct 263 ms 46448 KB Output is correct
3 Correct 286 ms 57224 KB Output is correct
4 Correct 294 ms 46408 KB Output is correct
5 Correct 247 ms 46596 KB Output is correct
6 Correct 253 ms 48068 KB Output is correct
7 Correct 229 ms 47228 KB Output is correct
8 Correct 298 ms 57796 KB Output is correct
9 Correct 195 ms 48076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 298 ms 46544 KB Output is correct
3 Correct 260 ms 59332 KB Output is correct
4 Correct 256 ms 46640 KB Output is correct
5 Correct 339 ms 46596 KB Output is correct
6 Correct 281 ms 48580 KB Output is correct
7 Correct 201 ms 47476 KB Output is correct
8 Correct 292 ms 53996 KB Output is correct
9 Correct 207 ms 47800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29020 KB Output is correct
2 Correct 5 ms 31128 KB Output is correct
3 Correct 7 ms 31196 KB Output is correct
4 Correct 6 ms 31068 KB Output is correct
5 Correct 5 ms 31068 KB Output is correct
6 Correct 5 ms 31068 KB Output is correct
7 Correct 5 ms 31068 KB Output is correct
8 Correct 5 ms 31068 KB Output is correct
9 Correct 5 ms 31080 KB Output is correct
10 Correct 5 ms 31152 KB Output is correct
11 Correct 5 ms 31068 KB Output is correct
12 Correct 5 ms 29020 KB Output is correct
13 Correct 7 ms 31392 KB Output is correct
14 Correct 6 ms 31320 KB Output is correct
15 Correct 7 ms 31324 KB Output is correct
16 Correct 7 ms 31324 KB Output is correct
17 Correct 7 ms 31324 KB Output is correct
18 Correct 7 ms 31324 KB Output is correct
19 Correct 7 ms 31384 KB Output is correct
20 Correct 7 ms 31324 KB Output is correct
21 Correct 7 ms 31324 KB Output is correct
22 Correct 7 ms 31328 KB Output is correct
23 Correct 7 ms 31324 KB Output is correct
24 Correct 6 ms 31324 KB Output is correct
25 Correct 8 ms 31836 KB Output is correct
26 Correct 7 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29016 KB Output is correct
2 Correct 263 ms 46448 KB Output is correct
3 Correct 286 ms 57224 KB Output is correct
4 Correct 294 ms 46408 KB Output is correct
5 Correct 247 ms 46596 KB Output is correct
6 Correct 253 ms 48068 KB Output is correct
7 Correct 229 ms 47228 KB Output is correct
8 Correct 298 ms 57796 KB Output is correct
9 Correct 195 ms 48076 KB Output is correct
10 Correct 5 ms 29020 KB Output is correct
11 Correct 298 ms 46544 KB Output is correct
12 Correct 260 ms 59332 KB Output is correct
13 Correct 256 ms 46640 KB Output is correct
14 Correct 339 ms 46596 KB Output is correct
15 Correct 281 ms 48580 KB Output is correct
16 Correct 201 ms 47476 KB Output is correct
17 Correct 292 ms 53996 KB Output is correct
18 Correct 207 ms 47800 KB Output is correct
19 Correct 5 ms 29020 KB Output is correct
20 Correct 252 ms 46532 KB Output is correct
21 Correct 275 ms 65916 KB Output is correct
22 Correct 232 ms 51404 KB Output is correct
23 Correct 267 ms 52936 KB Output is correct
24 Correct 254 ms 51892 KB Output is correct
25 Correct 254 ms 52936 KB Output is correct
26 Correct 240 ms 51636 KB Output is correct
27 Correct 271 ms 52980 KB Output is correct
28 Correct 254 ms 54188 KB Output is correct
29 Correct 264 ms 52928 KB Output is correct
30 Correct 241 ms 52308 KB Output is correct
31 Correct 211 ms 53660 KB Output is correct
32 Correct 288 ms 61232 KB Output is correct
33 Correct 177 ms 54136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29020 KB Output is correct
2 Correct 5 ms 31128 KB Output is correct
3 Correct 7 ms 31196 KB Output is correct
4 Correct 6 ms 31068 KB Output is correct
5 Correct 5 ms 31068 KB Output is correct
6 Correct 5 ms 31068 KB Output is correct
7 Correct 5 ms 31068 KB Output is correct
8 Correct 5 ms 31068 KB Output is correct
9 Correct 5 ms 31080 KB Output is correct
10 Correct 5 ms 31152 KB Output is correct
11 Correct 5 ms 31068 KB Output is correct
12 Correct 5 ms 29016 KB Output is correct
13 Correct 263 ms 46448 KB Output is correct
14 Correct 286 ms 57224 KB Output is correct
15 Correct 294 ms 46408 KB Output is correct
16 Correct 247 ms 46596 KB Output is correct
17 Correct 253 ms 48068 KB Output is correct
18 Correct 229 ms 47228 KB Output is correct
19 Correct 298 ms 57796 KB Output is correct
20 Correct 195 ms 48076 KB Output is correct
21 Correct 5 ms 29020 KB Output is correct
22 Correct 298 ms 46544 KB Output is correct
23 Correct 260 ms 59332 KB Output is correct
24 Correct 256 ms 46640 KB Output is correct
25 Correct 339 ms 46596 KB Output is correct
26 Correct 281 ms 48580 KB Output is correct
27 Correct 201 ms 47476 KB Output is correct
28 Correct 292 ms 53996 KB Output is correct
29 Correct 207 ms 47800 KB Output is correct
30 Correct 5 ms 29020 KB Output is correct
31 Correct 7 ms 31392 KB Output is correct
32 Correct 6 ms 31320 KB Output is correct
33 Correct 7 ms 31324 KB Output is correct
34 Correct 7 ms 31324 KB Output is correct
35 Correct 7 ms 31324 KB Output is correct
36 Correct 7 ms 31324 KB Output is correct
37 Correct 7 ms 31384 KB Output is correct
38 Correct 7 ms 31324 KB Output is correct
39 Correct 7 ms 31324 KB Output is correct
40 Correct 7 ms 31328 KB Output is correct
41 Correct 7 ms 31324 KB Output is correct
42 Correct 6 ms 31324 KB Output is correct
43 Correct 8 ms 31836 KB Output is correct
44 Correct 7 ms 31324 KB Output is correct
45 Correct 5 ms 29020 KB Output is correct
46 Correct 252 ms 46532 KB Output is correct
47 Correct 275 ms 65916 KB Output is correct
48 Correct 232 ms 51404 KB Output is correct
49 Correct 267 ms 52936 KB Output is correct
50 Correct 254 ms 51892 KB Output is correct
51 Correct 254 ms 52936 KB Output is correct
52 Correct 240 ms 51636 KB Output is correct
53 Correct 271 ms 52980 KB Output is correct
54 Correct 254 ms 54188 KB Output is correct
55 Correct 264 ms 52928 KB Output is correct
56 Correct 241 ms 52308 KB Output is correct
57 Correct 211 ms 53660 KB Output is correct
58 Correct 288 ms 61232 KB Output is correct
59 Correct 177 ms 54136 KB Output is correct
60 Correct 5 ms 29020 KB Output is correct
61 Correct 288 ms 55500 KB Output is correct
62 Correct 297 ms 65472 KB Output is correct
63 Correct 249 ms 54280 KB Output is correct
64 Correct 290 ms 55500 KB Output is correct
65 Correct 285 ms 54216 KB Output is correct
66 Correct 287 ms 55608 KB Output is correct
67 Correct 264 ms 54212 KB Output is correct
68 Correct 275 ms 55916 KB Output is correct
69 Correct 275 ms 57032 KB Output is correct
70 Correct 294 ms 55520 KB Output is correct
71 Correct 263 ms 55036 KB Output is correct
72 Correct 235 ms 57088 KB Output is correct
73 Correct 333 ms 62152 KB Output is correct
74 Correct 214 ms 58368 KB Output is correct