Submission #586872

# Submission time Handle Problem Language Result Execution time Memory
586872 2022-06-30T20:55:58 Z TheEvilBird Snowball (JOI21_ho_t2) C++17
100 / 100
777 ms 13180 KB
#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <numeric>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <random>
#include <ctime>
#include <chrono>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)((x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128_t int128;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const char en = '\n';
const int INF = 1e9 + 7;
const ll INFLL = 1e18;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

#ifdef __APPLE__
#include "debug.h"
//#define debug(...) 42
#else
#define debug(...) 42
#endif

//#define int ll

void solve() {
    int n, q;
    cin >> n >> q;
    vector<ll> x(n + 2), qs(q);
    for (int i = 1; i <= n; ++i) {
        cin >> x[i];
    }
    x[0] = -INFLL;
    x[n + 1] = INFLL;
    ll go_left = 0, go_right = 0, d = 0;
    vector<ll> jumps_left(1, 0), jumps_right(1, 0);
    vector<int> id_left(1, -1), id_right(1, -1);
    for (int i = 0; i < q; ++i) {
        cin >> qs[i];
        d += qs[i];
        if (d < go_left) {
            go_left = d;
            jumps_left.emplace_back(abs(d));
            id_left.emplace_back(i);
        }
        if (d > go_right) {
            go_right = d;
            jumps_right.emplace_back(d);
            id_right.emplace_back(i);
        }
    }
    vector<ll> l(n + 2), r(n + 2);
    for (int i = 0; i < n + 2; ++i) {
        l[i] = x[i] + go_left;
        r[i] = x[i] + go_right;
    }
    vector<ll> ans(n + 2, 0);
    ans[1] += abs(go_left);
    ans[n] += go_right;
    for (int i = 2; i <= n; ++i) {
        if (r[i - 1] <= l[i]) {
            ans[i - 1] += go_right;
            ans[i] += abs(go_left);
        }
        else if (r[i - 1] == x[i - 1] && l[i] <= x[i - 1]) {
            ans[i - 1] += go_right;
            ans[i] += (x[i] - x[i - 1]);
        }
        else {
            ll lx = l[i], rx = min(r[i - 1], x[i]) + 1;
            ans[i - 1] += (lx - x[i - 1]);
            while (rx - lx > 1) {
                ll mx = lx + (rx - lx) / 2;
                int id1 = (int)(lower_bound(all(jumps_right), mx - x[i - 1]) - jumps_right.begin());
                int id2 = (int)(lower_bound(all(jumps_left), x[i] - mx + 1) - jumps_left.begin());
                int ev1 = id_right[id1], ev2 = id_left[id2];
                if (ev1 < ev2) {
                    lx = mx;
                }
                else {
                    rx = mx;
                }
            }
            ans[i - 1] += (lx - l[i]);
            ans[i] += (x[i] - lx);
        }
    }
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << en;
    }
}

int32_t main() {
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 452 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 3 ms 432 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 5 ms 468 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 452 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 3 ms 432 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 5 ms 468 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 30 ms 5016 KB Output is correct
21 Correct 31 ms 4712 KB Output is correct
22 Correct 26 ms 4628 KB Output is correct
23 Correct 31 ms 4792 KB Output is correct
24 Correct 100 ms 5164 KB Output is correct
25 Correct 767 ms 12800 KB Output is correct
26 Correct 769 ms 12712 KB Output is correct
27 Correct 706 ms 12112 KB Output is correct
28 Correct 691 ms 12020 KB Output is correct
29 Correct 572 ms 10736 KB Output is correct
30 Correct 193 ms 8924 KB Output is correct
31 Correct 55 ms 8460 KB Output is correct
32 Correct 51 ms 8524 KB Output is correct
33 Correct 55 ms 1516 KB Output is correct
34 Correct 436 ms 9852 KB Output is correct
35 Correct 492 ms 9824 KB Output is correct
36 Correct 777 ms 12788 KB Output is correct
37 Correct 671 ms 11880 KB Output is correct
38 Correct 704 ms 12348 KB Output is correct
39 Correct 689 ms 12116 KB Output is correct
40 Correct 317 ms 12452 KB Output is correct
41 Correct 27 ms 5164 KB Output is correct
42 Correct 46 ms 8524 KB Output is correct
43 Correct 345 ms 12852 KB Output is correct
44 Correct 27 ms 4916 KB Output is correct
45 Correct 57 ms 12388 KB Output is correct
46 Correct 293 ms 12828 KB Output is correct
47 Correct 258 ms 11204 KB Output is correct
48 Correct 75 ms 13180 KB Output is correct