Submission #635177

# Submission time Handle Problem Language Result Execution time Memory
635177 2022-08-25T15:22:13 Z phathnv Radio Towers (IOI22_towers) C++17
31 / 100
1516 ms 96224 KB
#include "towers.h"

#include <bits/stdc++.h>
using namespace std;

struct Node {
    Node* nLeft;
    Node* nRight;
    int l, r, sum, minPos, maxPos, minVal;
    Node(int _l, int _r, int val) {
        nLeft = nRight = nullptr;
        l = _l;
        r = _r;
        sum = 0;
        minPos = 1e9;
        maxPos = -1e9;
        minVal = val;
        assert(l <= r);
    }
};

Node* mergeNode(Node* a, Node* b) {
    assert(a->r + 1 == b->l);
    Node* res = new Node(a->l, b->r, 1e9);
    res->nLeft = a;
    res->nRight = b;
    res->sum = a->sum + b->sum;
    res->minPos = min(a->minPos, b->minPos);
    res->maxPos = max(a->maxPos, b->maxPos);
    res->minVal = min(a->minVal, a->minVal);
    return res;
}

Node* build(int l, int r, const vector<int>& h, const vector<bool>& mark) {
    if (l == r) {
        Node* n = new Node(l, r, h[l]);
        if (mark[l]) {
            n->sum = 1;
            n->minPos = n->maxPos = l;
        }
        return n;
    }
    int mid = (l + r) >> 1;
    return mergeNode(build(l, mid, h, mark), build(mid + 1, r, h, mark));
}

Node* update(Node* n, int pos) {
    if (pos < n->l || n->r < pos) {
        return n;
    }
    if (n->l == n->r) {
        assert(n->sum == 1);
        return new Node(n->l, n->r, n->minVal);
    }
    return mergeNode(update(n->nLeft, pos), update(n->nRight, pos));
}

int getMin(Node*n, int l, int r) {
    if (r < n->l || n->r < l) {
        return 1e9;
    }
    if (l <= n->l && n->r <= r) {
        return n->minVal;
    }
    return min(getMin(n->nLeft, l, r), getMin(n->nRight, l, r));
}

void get(Node*n, int l, int r, int &x, int &y, int &sum) {
    if (r < n->l || n->r < l) {
        return;
    }
    if (l <= n->l && n->r <= r) {
        sum += n->sum;
        x = min(x, n->minPos);
        y = max(y, n->maxPos);
        return;
    }
    get(n->nLeft, l, r, x, y, sum);
    get(n->nRight, l, r, x, y, sum);
}

int n;
vector<int> h;
vector<int> type, changingPoint;
vector<Node*> roots;

void init(int _n, vector<int> _h) {
    n = _n;
    h = _h;

    set<int> s;
    s.insert(0);
    for (int i = 1; i < n; ++i) {
        if (s.size() & 1) {
            if (h[i] > h[*s.rbegin()]) {
                s.insert(i);
            } else {
                s.erase(*s.rbegin());
                s.insert(i);
            }
        } else {
            if (h[i] < h[*s.rbegin()]) {
                s.insert(i);
            } else {
                s.erase(*s.rbegin());
                s.insert(i);
            }
        }
    }
    if (s.size() % 2 == 0) {
        s.erase(*s.rbegin());
    }

    //for (auto x : s) {
        //cerr << x << ' ';
    //}
    //cerr << endl;

    vector<bool> mark(n, false);
    for (auto x : s) {
        mark[x] = true;
    }
    changingPoint.push_back(0);
    roots.push_back(build(0, n - 1, h, mark));

    priority_queue<array<int, 3>> pq;
    type.assign(n, -1);
    int pre = -1, t = 0;
    for (int cur : s) {
        type[cur] = t;
        t ^= 1;
        if (pre != -1) {
            pq.push({-abs(h[pre] - h[cur]), pre, cur});
        }
        pre = cur;
    }

    int last = 0;
    while (!pq.empty()) {
        auto [d, l, r] = pq.top();
        pq.pop();
        if (!mark[l] || !mark[r]) {
            continue;
        }

        d = -d;
        if (d != last) {
            changingPoint.push_back(d);
            roots.push_back(roots.back());
            last = d;
        }

        int a[5] = {-1, -1, -1, -1, -1};

        if (h[l] > h[r]) {
            a[1] = l;
            a[2] = r;
            auto it = s.lower_bound(l);
            if (it != s.begin()) {
                a[0] = *(--it);
            }
            it = s.lower_bound(r);
            ++it;
            if (it != s.end()) {
                a[3] = *it;
                ++it;
            }
            if (it != s.end()) {
                a[4] = *it;
                ++it;
            }
        } else {
            a[2] = l;
            a[3] = r;
            auto it = s.lower_bound(l);
            if (it != s.begin()) {
                a[1] = *(--it);
            }
            if (it != s.begin()) {
                a[0] = *(--it);
            }
            it = s.lower_bound(r);
            ++it;
            if (it != s.end()) {
                a[4] = *it;
                ++it;
            }
        }

        roots.back() = update(roots.back(), a[2]);
        mark[a[2]] = false;
        s.erase(a[2]);
        if (a[1] == -1 || a[3] == -1) {
            if (a[1] != -1) {
                roots.back() = update(roots.back(), a[1]);
                mark[a[1]] = false;
                s.erase(a[1]);
            }
            if (a[3] != -1) {
                roots.back() = update(roots.back(), a[3]);
                mark[a[3]] = false;
                s.erase(a[3]);
            }
        } else {
            if (h[a[1]] > h[a[3]]) {
                roots.back() = update(roots.back(), a[3]);
                mark[a[3]] = false;
                s.erase(a[3]);
                pq.push({-abs(h[a[1]] - h[a[4]]), a[1], a[4]});
            } else {
                roots.back() = update(roots.back(), a[1]);
                mark[a[1]] = false;
                s.erase(a[1]);
                pq.push({-abs(h[a[0]] - h[a[3]]), a[0], a[3]});
            }
        }

        //cerr << "change " << d << ' ' << l << ' ' << r << endl;
        for (int i = 0; i < 5; ++i) {
            //cerr << a[i] << ' ';
        }
        //cerr << endl;
    }
}

int max_towers(int l, int r, int d) {
    Node* root = roots[lower_bound(changingPoint.begin(), changingPoint.end(), d) - changingPoint.begin() - 1];

    int x = 1e9, y = -1e9, res = 0;
    get(root, l, r, x, y, res);

    if (x > y) {
        return 1;
    }

    assert(type[x] != -1 && type[y] != -1);

    if (type[x] == 1) {
        if (h[x] - d >= getMin(root, l, x - 1)) {
            ++res;
        } else {
            --res;
        }
    }
    if (type[y] == 1) {
        if (h[y] - d >= getMin(root, y + 1, r)) {
            ++res;
        } else {
            --res;
        }
    }

    //cerr << "query " << l << ' ' << r << ' ' << d << ' ' << res << endl;
    return max(1, (res + 1) / 2);
}
# Verdict Execution time Memory Grader output
1 Incorrect 614 ms 6828 KB 23320th lines differ - on the 1st token, expected: '2', found: '1'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 4 ms 1232 KB Output is correct
3 Correct 3 ms 1232 KB Output is correct
4 Correct 3 ms 1616 KB Output is correct
5 Correct 3 ms 1616 KB Output is correct
6 Correct 4 ms 1616 KB Output is correct
7 Incorrect 3 ms 1616 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 4 ms 1232 KB Output is correct
3 Correct 3 ms 1232 KB Output is correct
4 Correct 3 ms 1616 KB Output is correct
5 Correct 3 ms 1616 KB Output is correct
6 Correct 4 ms 1616 KB Output is correct
7 Incorrect 3 ms 1616 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1097 ms 67116 KB Output is correct
2 Correct 1292 ms 67828 KB Output is correct
3 Correct 1414 ms 67704 KB Output is correct
4 Correct 1516 ms 96028 KB Output is correct
5 Correct 1070 ms 96080 KB Output is correct
6 Correct 1164 ms 96028 KB Output is correct
7 Correct 1372 ms 96224 KB Output is correct
8 Correct 1187 ms 11160 KB Output is correct
9 Correct 991 ms 11212 KB Output is correct
10 Correct 1203 ms 11184 KB Output is correct
11 Correct 1032 ms 11184 KB Output is correct
12 Correct 1121 ms 11180 KB Output is correct
13 Correct 990 ms 11164 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 204 ms 67648 KB Output is correct
18 Correct 286 ms 96116 KB Output is correct
19 Correct 304 ms 96036 KB Output is correct
20 Correct 25 ms 11160 KB Output is correct
21 Correct 26 ms 11224 KB Output is correct
22 Correct 211 ms 67668 KB Output is correct
23 Correct 263 ms 96076 KB Output is correct
24 Correct 263 ms 96056 KB Output is correct
25 Correct 24 ms 11188 KB Output is correct
26 Correct 27 ms 11156 KB Output is correct
27 Correct 3 ms 1232 KB Output is correct
28 Correct 3 ms 1616 KB Output is correct
29 Correct 5 ms 1616 KB Output is correct
30 Correct 2 ms 464 KB Output is correct
31 Correct 2 ms 464 KB Output is correct
32 Correct 3 ms 1232 KB Output is correct
33 Correct 5 ms 1616 KB Output is correct
34 Correct 4 ms 1660 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 294 ms 14860 KB Output is correct
2 Correct 1165 ms 67732 KB Output is correct
3 Correct 1076 ms 67804 KB Output is correct
4 Correct 1084 ms 96012 KB Output is correct
5 Correct 1283 ms 96108 KB Output is correct
6 Correct 1225 ms 96116 KB Output is correct
7 Correct 1357 ms 96004 KB Output is correct
8 Correct 962 ms 11172 KB Output is correct
9 Correct 939 ms 11192 KB Output is correct
10 Correct 618 ms 11208 KB Output is correct
11 Correct 854 ms 11168 KB Output is correct
12 Correct 198 ms 67708 KB Output is correct
13 Correct 245 ms 96168 KB Output is correct
14 Correct 265 ms 96024 KB Output is correct
15 Correct 25 ms 11196 KB Output is correct
16 Correct 29 ms 11208 KB Output is correct
17 Correct 167 ms 65396 KB Output is correct
18 Correct 189 ms 67768 KB Output is correct
19 Correct 200 ms 68020 KB Output is correct
20 Correct 261 ms 96024 KB Output is correct
21 Correct 253 ms 96040 KB Output is correct
22 Correct 277 ms 96032 KB Output is correct
23 Correct 288 ms 96016 KB Output is correct
24 Correct 30 ms 11208 KB Output is correct
25 Correct 22 ms 11208 KB Output is correct
26 Correct 32 ms 11208 KB Output is correct
27 Correct 33 ms 11208 KB Output is correct
28 Correct 2 ms 1232 KB Output is correct
29 Correct 4 ms 1616 KB Output is correct
30 Correct 3 ms 1616 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 592 KB Output is correct
34 Correct 3 ms 1232 KB Output is correct
35 Correct 2 ms 1232 KB Output is correct
36 Correct 3 ms 1616 KB Output is correct
37 Correct 3 ms 1616 KB Output is correct
38 Correct 3 ms 1616 KB Output is correct
39 Correct 3 ms 1616 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 4 ms 1232 KB Output is correct
3 Correct 3 ms 1232 KB Output is correct
4 Correct 3 ms 1616 KB Output is correct
5 Correct 3 ms 1616 KB Output is correct
6 Correct 4 ms 1616 KB Output is correct
7 Incorrect 3 ms 1616 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 614 ms 6828 KB 23320th lines differ - on the 1st token, expected: '2', found: '1'
2 Halted 0 ms 0 KB -