답안 #684062

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684062 2023-01-20T08:30:40 Z piOOE Measures (CEOI22_measures) C++17
100 / 100
400 ms 25044 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll inf = 3e18;

struct Info {
    ll mx = 0, a = inf, b = -inf; //min_a, max_b

    Info() = default;
    Info(ll a, ll b) : a(a), b(b), mx(b - a) {}
    Info(ll mx, ll a, ll b) : a(a), b(b), mx(mx) {}
};

Info operator+(Info x, Info y) {
    return {max({x.mx, y.mx, y.b - x.a}), min(x.a, y.a), max(x.b, y.b)};
}

vector<Info> t;
vector<ll> tag;
int sz = 1;

void init(int n) {
    sz = 1 << __lg(n) + bool(n & (n - 1));
    t.resize(sz << 1), tag.resize(sz << 1);
}

void apply(Info &a, ll tg) {
    a.a += tg, a.b += tg;
}

void apply(int x, ll tg) {
    apply(t[x], tg);
    tag[x] += tg;
}

void push(int x) {
    apply(x << 1, tag[x]), apply(x << 1 | 1, tag[x]);
    tag[x] = 0;
}

void pull(int x) {
    t[x] = t[x << 1] + t[x << 1 | 1];
}

void rangeAdd(int l, int r, ll D, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) {
        return;
    }
    if (l <= lx && rx <= r) {
        apply(x, D);
        return;
    }
    push(x);

    int mid = (lx + rx) >> 1;
    rangeAdd(l, r, D, x << 1, lx, mid), rangeAdd(l, r, D, x << 1 | 1, mid, rx);

    pull(x);
}

void modify(int i, Info b, int x = 1, int lx = 0, int rx = sz) {
    if (lx + 1 == rx) {
        t[x] = b;
        return;
    }
    push(x);

    int mid = (lx + rx) >> 1;
    if (i < mid) {
        modify(i, b, x << 1, lx, mid);
    } else {
        modify(i, b, x << 1 | 1, mid, rx);
    }

    pull(x);
}

struct Fenwick {
    vector<int> t;
    int n{};

    void init(int m) {
        n = m;
        t.resize(n + 1);
    }

    void modify(int i, int v) {
        for (int x = i + 1; x <= n; x += x & -x) {
            t[x] += v;
        }
    }

    int sum(int i) {
        int ans = 0;
        for (int x = i + 1; x > 0; x -= x & -x) {
            ans += t[x];
        }
        return ans;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, D;
    cin >> n >> m >> D;

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<int> b(m);
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }

    if (n > 0) {
        sort(a.begin(), a.end());

        for (int i = 0; i < m; ++i) {
            a.insert(lower_bound(a.begin(), a.end(), b[i]), b[i]);


            ll ans = numeric_limits<ll>::min(), mx = numeric_limits<ll>::min();
            for (int k = size(a) - 1; k >= 0; --k) {
                mx = max(mx, 1LL * D * k - a[k]);

                ans = max(ans, mx - 1LL * D * k + a[k]);
            }

            if (ans % 2 == 0) {
                cout << ans / 2 << ' ';
            } else {
                cout << ans / 2 << ".5 ";
            }
        }
    } else {
        init(m);

        vector<pair<int, int>> yy(m);
        for (int i = 0; i < m; ++i) {
            yy[i] = {b[i], i};
        }
        sort(yy.begin(), yy.end());

        Fenwick fn;
        fn.init(m);

        for (int i = 0; i < m; ++i) {
            int pos = lower_bound(yy.begin(), yy.end(), pair{b[i], i}) - yy.begin();

            rangeAdd(pos + 1, m, D);

            ll val = 1LL * D * fn.sum(pos - 1);
            Info now = Info(val - b[i], val - b[i]);

            modify(pos, now);
            fn.modify(pos, 1);

            if (t[1].mx % 2 == 0) {
                cout << t[1].mx / 2 << " ";
            } else {
                cout << t[1].mx / 2 << ".5 ";
            }
        }
    }

    return 0;
}

Compilation message

Main.cpp: In constructor 'Info::Info(ll, ll)':
Main.cpp:9:25: warning: 'Info::b' will be initialized after [-Wreorder]
    9 |     ll mx = 0, a = inf, b = -inf; //min_a, max_b
      |                         ^
Main.cpp:9:8: warning:   'll Info::mx' [-Wreorder]
    9 |     ll mx = 0, a = inf, b = -inf; //min_a, max_b
      |        ^~
Main.cpp:12:5: warning:   when initialized here [-Wreorder]
   12 |     Info(ll a, ll b) : a(a), b(b), mx(b - a) {}
      |     ^~~~
Main.cpp: In constructor 'Info::Info(ll, ll, ll)':
Main.cpp:9:25: warning: 'Info::b' will be initialized after [-Wreorder]
    9 |     ll mx = 0, a = inf, b = -inf; //min_a, max_b
      |                         ^
Main.cpp:9:8: warning:   'll Info::mx' [-Wreorder]
    9 |     ll mx = 0, a = inf, b = -inf; //min_a, max_b
      |        ^~
Main.cpp:13:5: warning:   when initialized here [-Wreorder]
   13 |     Info(ll mx, ll a, ll b) : a(a), b(b), mx(mx) {}
      |     ^~~~
Main.cpp: In function 'void init(int)':
Main.cpp:25:23: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   25 |     sz = 1 << __lg(n) + bool(n & (n - 1));
      |               ~~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 356 KB Output is correct
9 Correct 37 ms 3780 KB Output is correct
10 Correct 34 ms 3788 KB Output is correct
11 Correct 26 ms 3764 KB Output is correct
12 Correct 43 ms 3904 KB Output is correct
13 Correct 26 ms 3264 KB Output is correct
14 Correct 31 ms 3780 KB Output is correct
15 Correct 34 ms 3128 KB Output is correct
16 Correct 26 ms 3772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 22756 KB Output is correct
2 Correct 265 ms 24704 KB Output is correct
3 Correct 283 ms 24636 KB Output is correct
4 Correct 255 ms 22560 KB Output is correct
5 Correct 260 ms 23756 KB Output is correct
6 Correct 259 ms 22976 KB Output is correct
7 Correct 259 ms 23884 KB Output is correct
8 Correct 261 ms 22564 KB Output is correct
9 Correct 263 ms 22460 KB Output is correct
10 Correct 259 ms 24880 KB Output is correct
11 Correct 265 ms 23372 KB Output is correct
12 Correct 257 ms 24260 KB Output is correct
13 Correct 261 ms 22720 KB Output is correct
14 Correct 261 ms 24524 KB Output is correct
15 Correct 266 ms 24344 KB Output is correct
16 Correct 252 ms 22188 KB Output is correct
17 Correct 275 ms 23968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 22756 KB Output is correct
2 Correct 265 ms 24704 KB Output is correct
3 Correct 283 ms 24636 KB Output is correct
4 Correct 255 ms 22560 KB Output is correct
5 Correct 260 ms 23756 KB Output is correct
6 Correct 259 ms 22976 KB Output is correct
7 Correct 259 ms 23884 KB Output is correct
8 Correct 261 ms 22564 KB Output is correct
9 Correct 263 ms 22460 KB Output is correct
10 Correct 259 ms 24880 KB Output is correct
11 Correct 265 ms 23372 KB Output is correct
12 Correct 257 ms 24260 KB Output is correct
13 Correct 261 ms 22720 KB Output is correct
14 Correct 261 ms 24524 KB Output is correct
15 Correct 266 ms 24344 KB Output is correct
16 Correct 252 ms 22188 KB Output is correct
17 Correct 275 ms 23968 KB Output is correct
18 Correct 345 ms 22992 KB Output is correct
19 Correct 377 ms 24644 KB Output is correct
20 Correct 268 ms 24652 KB Output is correct
21 Correct 279 ms 22720 KB Output is correct
22 Correct 315 ms 23000 KB Output is correct
23 Correct 269 ms 22960 KB Output is correct
24 Correct 359 ms 23428 KB Output is correct
25 Correct 254 ms 22528 KB Output is correct
26 Correct 342 ms 22460 KB Output is correct
27 Correct 348 ms 25044 KB Output is correct
28 Correct 311 ms 22896 KB Output is correct
29 Correct 320 ms 24336 KB Output is correct
30 Correct 324 ms 22468 KB Output is correct
31 Correct 282 ms 24532 KB Output is correct
32 Correct 295 ms 24444 KB Output is correct
33 Correct 400 ms 22072 KB Output is correct
34 Correct 287 ms 24036 KB Output is correct