Submission #628872

# Submission time Handle Problem Language Result Execution time Memory
628872 2022-08-13T18:46:33 Z imeimi2000 Radio Towers (IOI22_towers) C++17
17 / 100
1358 ms 147556 KB
#include "towers.h"

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

const int inf = 1e9 + 5;
struct MaxSeg {
    int seg[1 << 18];
    MaxSeg() {
        fill(seg, seg + (1 << 18), -inf);
    }
    void update(int i, int s, int e, int x, int v) {
        if (s == e) {
            seg[i] = v;
            return;
        }
        int m = (s + e) / 2;
        if (x <= m) update(i + i, s, m, x, v);
        else update(i + i + 1, m + 1, e, x, v);
        seg[i] = max(seg[i + i], seg[i + i + 1]);
    }
    int query(int i, int s, int e, int x, int y) const {
        if (e < x || y < s) return -inf;
        if (x <= s && e <= y) return seg[i];
        int m = (s + e) / 2;
        return max(query(i + i, s, m, x, y), query(i + i + 1, m + 1, e, x, y));
    }
    int find(int i, int s, int e, int x, int v) const {
        if (x < s) return 0;
        if (e <= x && seg[i] <= v) return 0;
        if (s == e) return s;
        int m = (s + e) / 2;
        int r = find(i + i + 1, m + 1, e, x, v);
        if (r) return r;
        return find(i + i, s, m, x, v);
    }
};

struct Pst {
    struct Node {
        int L, R, S;
    };
    int root;
    vector<Node> node;
    vector<int> snapshots;
    Pst() {
        root = 0;
        node.push_back(Node { 0, 0, 0 });
        snapshots.push_back(0);
    }
    int update(int i, int s, int e, int x, int v) {
        int j = node.size();
        node.push_back(node[i]);
        node[j].S += v;
        if (s < e) {
            int m = (s + e) / 2;
            if (x <= m) node[j].L = update(node[j].L, s, m, x, v);
            else node[j].R = update(node[j].R, m + 1, e, x, v);
        }
        return j;
    }
    void update(int x, int v) {
        if (x == 0) return;
        root = update(root, 1, inf, x, v);
    }
    int query(int i, int s, int e, int x, int y) const {
        if (!i || e < x || y < s) return 0;
        if (x <= s && e <= y) return node[i].S;
        int m = (s + e) / 2;
        return query(node[i].L, s, m, x, y) + query(node[i].R, m + 1, e, x, y);
    }
    int query(int t, int x, int y) const {
        return query(snapshots[t], 1, inf, x, y);
    }
    void snapshot() {
        snapshots.push_back(root);
    }
};

int n;
struct Tower {
    MaxSeg min_seg, max_seg;
    int maxD[100005];
    int Lmin[100005];
    int Lpos[100005];
    int Lpar[20][100005];
    int Ldep[100005];
    int V[100005];
    Pst pst;
    void init(vector<int> H) {
        H.insert(H.begin(), inf);
        for (int i = 1; i <= n; ++i) {
            V[i] = H[i];
            min_seg.update(1, 1, n, i, -H[i]);
            max_seg.update(1, 1, n, i, H[i]);
        }
        vector<int> R = { 0, 1 };
        vector<int> L;
        pst.snapshot();
        Lmin[0] = Lmin[1] = inf;
        for (int i = 2; i <= n; ++i) {
            int poped = 0;
            Lmin[i] = inf;
            while (H[R.back()] < H[i]) {
                Lmin[i] = min(Lmin[i], min(Lmin[R.back()], H[R.back()]));
                if (!L.empty() && L.back() == R.back()) L.pop_back();
                R.pop_back();
                ++poped;
            }
            if (!poped) {
                while (!L.empty()) {
                    int x = L.back();
                    pst.update(maxD[x], -1);
                    if (Lmin[x] < H[i]) {
                        maxD[x] = H[x] - H[i];
                        pst.update(maxD[x], 1);
                        break;
                    }
                    else {
                        maxD[x] = H[x] - Lmin[x];
                        pst.update(maxD[x], 1);
                    }
                    L.pop_back();
                }
            }
            else {
                if (!L.empty()) Lpar[0][i] = L.back();
                Ldep[i] = Ldep[Lpar[0][i]] + 1;
                L.push_back(i);
            }
            Lpos[i] = L.empty() ? 0 : L.back();
            R.push_back(i);
            pst.snapshot();
        }
        for (int i = 1; i < 20; ++i) {
            for (int j = 1; j <= n; ++j) Lpar[i][j] = Lpar[i - 1][Lpar[i - 1][j]];
        }
    }
    int query(int m, int r, int d) const {
        // printf("m = %d, r = %d\n", m, r);
        if (m == r) return 0;
        int ans = pst.query(r, d, inf) - pst.query(m, d, inf);
        int mn = -min_seg.query(1, 1, n, m + 1, r);
        int x = Lpos[m];
        if (!x) return ans;
        int p = Lpar[0][x];
        if (Lmin[x] + d <= V[x] && mn + d <= V[x]);
        else if (p && Lmin[p] + d <= V[p] && min(mn, Lmin[x]) + d <= V[p]) x = p;
        else {
            for (int i = 20; i--; ) {
                int p = Lpar[i][x];
                int g = Lpar[0][p];
                if (!g) continue;
                if (Lmin[g] + d <= V[g] && min(mn, Lmin[p]) + d <= V[g]);
                else x = p;
            }
            x = Lpar[0][Lpar[0][x]];
        }
        ans -= Ldep[x];
        x = Lpos[m];
        p = Lpar[0][x];
        if (p && Lmin[p] + d <= V[p] && Lmin[x] + d <= V[p]) x = p;
        else {
            for (int i = 20; i--; ) {
                int p = Lpar[i][x];
                int g = Lpar[0][p];
                if (!g) continue;
                if (Lmin[g] + d <= V[g] && Lmin[p] + d <= V[g]);
                else x = p;
            }
            x = Lpar[0][Lpar[0][x]];
        }
        ans += Ldep[x];
        return ans;
    }
} L, R;

void init(int N, vector<int> H) {
    n = N;
    L.init(H);
    reverse(H.begin(), H.end());
    R.init(H);
}

int max_towers(int L, int R, int D) {
    ++L;
    ++R;
    int V = ::L.max_seg.query(1, 1, n, L, R);
    int M = ::L.max_seg.find(1, 1, n, R, V - 1);
    return ::L.query(M, R, D) + ::R.query(n + 1 - M, n + 1 - L, D) + (V - D >= -::L.min_seg.query(1, 1, n, L, M - 1) && V - D >= -::L.min_seg.query(1, 1, n, M + 1, R)) + 1;
}
# Verdict Execution time Memory Grader output
1 Incorrect 732 ms 81072 KB 1st lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5080 KB Output is correct
2 Correct 6 ms 6632 KB Output is correct
3 Correct 7 ms 6632 KB Output is correct
4 Correct 6 ms 6624 KB Output is correct
5 Correct 6 ms 6648 KB Output is correct
6 Correct 6 ms 6632 KB Output is correct
7 Correct 6 ms 6632 KB Output is correct
8 Correct 3 ms 5052 KB Output is correct
9 Correct 5 ms 4944 KB Output is correct
10 Correct 5 ms 6484 KB Output is correct
11 Correct 6 ms 6224 KB Output is correct
12 Correct 2 ms 4560 KB Output is correct
13 Incorrect 5 ms 6856 KB 1st lines differ - on the 1st token, expected: '1', found: '0'
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5080 KB Output is correct
2 Correct 6 ms 6632 KB Output is correct
3 Correct 7 ms 6632 KB Output is correct
4 Correct 6 ms 6624 KB Output is correct
5 Correct 6 ms 6648 KB Output is correct
6 Correct 6 ms 6632 KB Output is correct
7 Correct 6 ms 6632 KB Output is correct
8 Correct 3 ms 5052 KB Output is correct
9 Correct 5 ms 4944 KB Output is correct
10 Correct 5 ms 6484 KB Output is correct
11 Correct 6 ms 6224 KB Output is correct
12 Correct 2 ms 4560 KB Output is correct
13 Incorrect 5 ms 6856 KB 1st lines differ - on the 1st token, expected: '1', found: '0'
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1155 ms 146940 KB 59096th lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 377 ms 39356 KB Output is correct
2 Correct 1265 ms 147520 KB Output is correct
3 Correct 1273 ms 147516 KB Output is correct
4 Correct 1269 ms 147440 KB Output is correct
5 Correct 1349 ms 147444 KB Output is correct
6 Correct 1358 ms 147432 KB Output is correct
7 Correct 1251 ms 147448 KB Output is correct
8 Correct 910 ms 24996 KB Output is correct
9 Correct 709 ms 24672 KB Output is correct
10 Correct 1018 ms 74088 KB Output is correct
11 Correct 958 ms 106084 KB Output is correct
12 Correct 203 ms 147412 KB Output is correct
13 Correct 193 ms 147496 KB Output is correct
14 Correct 200 ms 147556 KB Output is correct
15 Correct 52 ms 24680 KB Output is correct
16 Correct 118 ms 69176 KB Output is correct
17 Correct 198 ms 144468 KB Output is correct
18 Correct 193 ms 147456 KB Output is correct
19 Correct 205 ms 147412 KB Output is correct
20 Correct 192 ms 147432 KB Output is correct
21 Correct 200 ms 147504 KB Output is correct
22 Correct 203 ms 147528 KB Output is correct
23 Correct 193 ms 147528 KB Output is correct
24 Correct 62 ms 25144 KB Output is correct
25 Correct 54 ms 24732 KB Output is correct
26 Correct 152 ms 116664 KB Output is correct
27 Correct 59 ms 29104 KB Output is correct
28 Correct 5 ms 6624 KB Output is correct
29 Correct 5 ms 6632 KB Output is correct
30 Correct 5 ms 6632 KB Output is correct
31 Correct 3 ms 4944 KB Output is correct
32 Correct 4 ms 5460 KB Output is correct
33 Correct 4 ms 5644 KB Output is correct
34 Correct 5 ms 6620 KB Output is correct
35 Correct 6 ms 6688 KB Output is correct
36 Correct 5 ms 6632 KB Output is correct
37 Correct 5 ms 6632 KB Output is correct
38 Correct 5 ms 6620 KB Output is correct
39 Correct 5 ms 6620 KB Output is correct
40 Correct 3 ms 4968 KB Output is correct
41 Correct 3 ms 4944 KB Output is correct
42 Correct 4 ms 5580 KB Output is correct
43 Correct 5 ms 6088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5080 KB Output is correct
2 Correct 6 ms 6632 KB Output is correct
3 Correct 7 ms 6632 KB Output is correct
4 Correct 6 ms 6624 KB Output is correct
5 Correct 6 ms 6648 KB Output is correct
6 Correct 6 ms 6632 KB Output is correct
7 Correct 6 ms 6632 KB Output is correct
8 Correct 3 ms 5052 KB Output is correct
9 Correct 5 ms 4944 KB Output is correct
10 Correct 5 ms 6484 KB Output is correct
11 Correct 6 ms 6224 KB Output is correct
12 Correct 2 ms 4560 KB Output is correct
13 Incorrect 5 ms 6856 KB 1st lines differ - on the 1st token, expected: '1', found: '0'
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 732 ms 81072 KB 1st lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -