Submission #991233

# Submission time Handle Problem Language Result Execution time Memory
991233 2024-06-01T15:33:24 Z model_code Flooding Wall (BOI24_wall) C++17
100 / 100
1693 ms 63520 KB
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1000000007;

int n;

vector<long long> powers_of_2;
vector<int> values;

struct Node {
    // sum[0] represents the sum of right(p)+1, sum[1] represents the sum of (-left(p)).
    long long sum[2] = {0, 0};
    // For each type of calculation (right and left), the number of times the value is to be multiplied by 2 and by 0.
    long long update[2][2] = {{0, 0},
                              {0, 0}};

    long long GetValue(int index) {
        // If we have multiplication by zero, the answer is also zero.
        if (update[index][0])return 0;
        return powers_of_2[update[index][1]] * sum[index] % mod;
    }
};

vector<Node> nodes;

void RefreshNode(int node, int index) {
    nodes[node].sum[index] = (nodes[node * 2 + 1].GetValue(index) + nodes[node * 2 + 2].GetValue(index)) % mod;
}

void Update(int node, int beg, int end, int target_start, int target_end, int upd_index, int upd_value) {
    if (beg >= target_start && end <= target_end) {
        if (upd_value == 2) {
            nodes[node].update[upd_index][1]++;
        } else if (upd_value == 1) {
            nodes[node].update[upd_index][0]--;
        } else {
            nodes[node].update[upd_index][0]++;
        }
        return;
    }
    if (target_start <= (beg + end) / 2) {
        Update(node * 2 + 1, beg, (beg + end) / 2, target_start, target_end, upd_index, upd_value);
    }
    if (target_end > (beg + end) / 2) {
        Update(node * 2 + 2, (beg + end) / 2 + 1, end, target_start, target_end, upd_index, upd_value);
    }
    RefreshNode(node, upd_index);
}

// Sets the wall segment to the condition where there are val options smaller than the current height.
void SetValue(int node, int beg, int end, long long wall_segment, int val) {
    if (beg == end) {
        if (node >= nodes.size())nodes.resize(node + 1);
        nodes[node].sum[0] = (wall_segment + 1) * (2 - val) * powers_of_2[wall_segment] % mod;
        nodes[node].sum[1] = (-wall_segment) * (2 - val) * powers_of_2[n - wall_segment - 1] % mod;
        return;
    }
    if (wall_segment <= (beg + end) / 2) {
        SetValue(node * 2 + 1, beg, (beg + end) / 2, wall_segment, val);
    } else {
        SetValue(node * 2 + 2, (beg + end) / 2 + 1, end, wall_segment, val);
    }
    RefreshNode(node, 0);
    RefreshNode(node, 1);
}

int main() {
    cin >> n;
    long long pw = 1;
    for (int i = 0; i <= n; ++i) {
        powers_of_2.push_back(pw);
        pw = (pw * 2) % mod;
    }
    values = vector<int>(n, 0);

    // Pairs of segment height and index. They'll be sorted by height.
    vector<pair<int, int>> sorted_segments;

    long long answer = 0;
    for (int i = 0; i < 2 * n; ++i) {
        int height;
        cin >> height;
        int index = i;
        if (index >= n)index -= n;
        sorted_segments.push_back({height, index});
        answer = (answer - powers_of_2[n - 1] * height) % mod;
    }
    sort(sorted_segments.begin(), sorted_segments.end());

    // We initialize the segment tree to height 0 by doing update queries. We could do it in a single O(n) operation but this still comfortably fits inside time limits so it doesn't matter.

    for (int i = 0; i < n; ++i) {
        SetValue(0, 0, n - 1, i, 0);
    }
    for (int i = 0; i < n; ++i) {
        if (i > 0) {
            Update(0, 0, n - 1, 0, i - 1, 0, 0);
        }
        if (i + 1 < n) {
            Update(0, 0, n - 1, i + 1, n - 1, 1, 0);
        }
    }

    long long prev_height = 0;
    for (auto[height, index] : sorted_segments) {
        long long cur_sum = nodes[0].GetValue(0) + nodes[0].GetValue(1);
        answer = (answer + cur_sum * (height - prev_height)) % mod;
        prev_height = height;;
        int &val = values[index];
        val++;
        SetValue(0, 0, n - 1, index, val);
        if (index > 0) {
            Update(0, 0, n - 1, 0, index - 1, 0, val);
        }
        if (index + 1 < n) {
            Update(0, 0, n - 1, index + 1, n - 1, 1, val);
        }

    }
    cout << (answer + mod) % mod << endl;

    return 0;
}

Compilation message

Main.cpp: In function 'void SetValue(int, int, int, long long int, int)':
Main.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         if (node >= nodes.size())nodes.resize(node + 1);
      |             ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 400 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 400 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 25 ms 2268 KB Output is correct
28 Correct 25 ms 2364 KB Output is correct
29 Correct 21 ms 2268 KB Output is correct
30 Correct 21 ms 2268 KB Output is correct
31 Correct 21 ms 2264 KB Output is correct
32 Correct 23 ms 2268 KB Output is correct
33 Correct 22 ms 2268 KB Output is correct
34 Correct 21 ms 2328 KB Output is correct
35 Correct 26 ms 2264 KB Output is correct
36 Correct 22 ms 2264 KB Output is correct
37 Correct 22 ms 2268 KB Output is correct
38 Correct 22 ms 2268 KB Output is correct
39 Correct 22 ms 2268 KB Output is correct
40 Correct 21 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 400 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 25 ms 2268 KB Output is correct
50 Correct 25 ms 2364 KB Output is correct
51 Correct 21 ms 2268 KB Output is correct
52 Correct 21 ms 2268 KB Output is correct
53 Correct 21 ms 2264 KB Output is correct
54 Correct 23 ms 2268 KB Output is correct
55 Correct 22 ms 2268 KB Output is correct
56 Correct 21 ms 2328 KB Output is correct
57 Correct 26 ms 2264 KB Output is correct
58 Correct 22 ms 2264 KB Output is correct
59 Correct 22 ms 2268 KB Output is correct
60 Correct 22 ms 2268 KB Output is correct
61 Correct 22 ms 2268 KB Output is correct
62 Correct 21 ms 2392 KB Output is correct
63 Correct 26 ms 2268 KB Output is correct
64 Correct 25 ms 2264 KB Output is correct
65 Correct 27 ms 2268 KB Output is correct
66 Correct 22 ms 2264 KB Output is correct
67 Correct 25 ms 2388 KB Output is correct
68 Correct 22 ms 2268 KB Output is correct
69 Correct 24 ms 2520 KB Output is correct
70 Correct 23 ms 2268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 21 ms 2368 KB Output is correct
11 Correct 21 ms 2264 KB Output is correct
12 Correct 20 ms 2640 KB Output is correct
13 Correct 21 ms 2268 KB Output is correct
14 Correct 1434 ms 63512 KB Output is correct
15 Correct 1281 ms 63508 KB Output is correct
16 Correct 1249 ms 63500 KB Output is correct
17 Correct 1229 ms 63416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 400 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 25 ms 2268 KB Output is correct
50 Correct 25 ms 2364 KB Output is correct
51 Correct 21 ms 2268 KB Output is correct
52 Correct 21 ms 2268 KB Output is correct
53 Correct 21 ms 2264 KB Output is correct
54 Correct 23 ms 2268 KB Output is correct
55 Correct 22 ms 2268 KB Output is correct
56 Correct 21 ms 2328 KB Output is correct
57 Correct 26 ms 2264 KB Output is correct
58 Correct 22 ms 2264 KB Output is correct
59 Correct 22 ms 2268 KB Output is correct
60 Correct 22 ms 2268 KB Output is correct
61 Correct 22 ms 2268 KB Output is correct
62 Correct 21 ms 2392 KB Output is correct
63 Correct 26 ms 2268 KB Output is correct
64 Correct 25 ms 2264 KB Output is correct
65 Correct 27 ms 2268 KB Output is correct
66 Correct 22 ms 2264 KB Output is correct
67 Correct 25 ms 2388 KB Output is correct
68 Correct 22 ms 2268 KB Output is correct
69 Correct 24 ms 2520 KB Output is correct
70 Correct 23 ms 2268 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 1 ms 344 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 21 ms 2368 KB Output is correct
81 Correct 21 ms 2264 KB Output is correct
82 Correct 20 ms 2640 KB Output is correct
83 Correct 21 ms 2268 KB Output is correct
84 Correct 1434 ms 63512 KB Output is correct
85 Correct 1281 ms 63508 KB Output is correct
86 Correct 1249 ms 63500 KB Output is correct
87 Correct 1229 ms 63416 KB Output is correct
88 Correct 1693 ms 63520 KB Output is correct
89 Correct 1576 ms 63496 KB Output is correct
90 Correct 1225 ms 63452 KB Output is correct
91 Correct 1339 ms 63484 KB Output is correct
92 Correct 1284 ms 63480 KB Output is correct
93 Correct 1225 ms 63484 KB Output is correct
94 Correct 1306 ms 63500 KB Output is correct
95 Correct 1188 ms 63440 KB Output is correct
96 Correct 1436 ms 63508 KB Output is correct
97 Correct 1454 ms 63480 KB Output is correct