Submission #570140

# Submission time Handle Problem Language Result Execution time Memory
570140 2022-05-28T18:57:07 Z timreizin Two Antennas (JOI19_antennas) C++17
2 / 100
3000 ms 7496 KB
#include <iostream>
#include <vector>
#include <numeric>
#include <cmath>
#include <set>
#include <array>
#include <algorithm>

using namespace std;

using ll = long long;

const ll INF = 1e15;

struct segtree
{
    int n;
    vector<ll> mx, mod, tree;
    
    void push(int v)
    {
        mod[v << 1] = max(mod[v << 1], mod[v]);
        mod[v << 1 | 1] = max(mod[v << 1 | 1], mod[v]);
        tree[v << 1] = max(tree[v << 1], mx[v << 1] + mod[v]);
        tree[v << 1 | 1] = max(tree[v << 1 | 1], mx[v << 1 | 1] + mod[v]);
    }
    
    segtree(int n) : n(n)
    {
        mx.resize(4 * n, -INF);
        mod.resize(4 * n, 0);
        tree.resize(4 * n, -INF);
    }
    
    ll get(int a, int b, int l, int r, int v)
    {
        if (a > r || b < l) return -INF;
        if (a == l && b == r) return tree[v];
        int m = (l + r) >> 1;
        push(v);
        return max(get(a, min(b, m), l, m, v << 1), get(max(a, m + 1), b, m + 1, r, v << 1 | 1));
    }
    
    ll get(int a, int b)
    {
        return get(a, b, 0, n - 1, 1);
    }
    
    void updateH(int p, ll val, int l, int r, int v)
    {
        if (l > r) return;
        if (l == r)
        {
            mx[v] = val;
            tree[v] = val + mod[v];
            return;
        }
        int m = (l + r) >> 1;
        push(v);
        if (p <= m) updateH(p, val, l, m, v << 1);
        else updateH(p, val, m + 1, r, v << 1 | 1);
        tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
        mx[v] = max(mx[v << 1], mx[v << 1 | 1]);
    }
    
    void updateH(int p, ll val)
    {
        updateH(p, val, 0, n - 1, 1);
    }
    
    void updateM(int a, int b, ll val, int l, int r, int v)
    {
        if (a > r || b < l) return;
        if (a == l && b == r)
        {
            mod[v] = max(mod[v], val);
            tree[v] = max(tree[v], mx[v] + mod[v]);
            return;
        }
        int m = (l + r) >> 1;
        push(v);
        updateM(a, min(b, m), val, l, m, v << 1);
        updateM(max(a, m + 1), b, val, m + 1, r, v << 1 | 1);
        tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
        mx[v] = max(mx[v << 1], mx[v << 1 | 1]);
    }
    
    void updateM(int a, int b, ll val)
    {
        updateM(a, b, val, 0, n - 1, 1);
    }
    
};

vector<ll> solve(vector<ll> h, vector<pair<int, int>> ab, vector<pair<int, int>> queries)
{
    int n = (int)h.size();
    int q = (int)queries.size();
    vector<int> inds(q);
    vector<pair<pair<int, int>, int>> events;
    segtree st(n);
    events.reserve(2 * n);
    iota(inds.begin(), inds.end(), 0);
    sort(inds.begin(), inds.end(), [&queries](int a, int b){ return queries[a].second < queries[b].second; });
    for (int i = 0; i < n; ++i)
    {
        events.emplace_back(pair(i + ab[i].first, 0), i);
        events.emplace_back(pair(i + ab[i].second, 1), i);
    }
    sort(events.begin(), events.end());
    vector<ll> result(q, -1);
    for (int i = 0, indE = 0, indQ = 0; i < n; ++i)
    {
        while (indE < 2 * n && events[indE].first <= pair(i, 0))
        {
            st.updateH(events[indE].second, -h[events[indE].second]);
            ++indE;
        }
        if (i - ab[i].first >= 0) st.updateM(max(0, i - ab[i].second), i - ab[i].first, h[i]);
        while (indQ < q && queries[inds[indQ]].second == i)
        {
            result[inds[indQ]] = max(result[inds[indQ]], st.get(queries[inds[indQ]].first, queries[inds[indQ]].second));
            ++indQ;
        }
        while (indE < 2 * n && events[indE].first <= pair(i, 1))
        {
            st.updateH(events[indE].second, -INF);
            ++indE;
        }
        
    }
    reverse(h.begin(), h.end());
    reverse(ab.begin(), ab.end());
    for (auto &i : queries) i = {n - i.second - 1, n - i.first - 1};
    events.clear();
    sort(inds.begin(), inds.end(), [&queries](int a, int b){ return queries[a].second < queries[b].second; });
    for (int i = 0; i < n; ++i)
    {
        events.emplace_back(pair(i + ab[i].first, 0), i);
        events.emplace_back(pair(i + ab[i].second, 1), i);
    }
    sort(events.begin(), events.end());
    st = segtree(n);
    for (int i = 0, indE = 0, indQ = 0; i < n; ++i)
    {
        while (indE < 2 * n && events[indE].first <= pair(i, 0))
        {
            st.updateH(events[indE].second, -h[events[indE].second]);
            ++indE;
        }
        if (i - ab[i].first >= 0) st.updateM(max(0, i - ab[i].second), i - ab[i].first, h[i]);
        while (indQ < q && queries[inds[indQ]].second == i)
        {
            result[inds[indQ]] = max(result[inds[indQ]], st.get(queries[inds[indQ]].first, queries[inds[indQ]].second));
            ++indQ;
        }
        while (indE < 2 * n && events[indE].first <= pair(i, 1))
        {
            st.updateH(events[indE].second, -INF);
            ++indE;
        }
    }
    return result;
}

vector<ll> brute(vector<ll> h, vector<pair<int, int>> ab, vector<pair<int, int>> queries)
{
    int n = (int)h.size();
    int q = (int)queries.size();
    vector<ll> res(q, -1);
    for (int i = 0; i < q; ++i)
    {
        for (int j = queries[i].first; j <= queries[i].second; ++j)
        {
            for (int k = j + ab[j].first; k <= min(queries[i].second, j + ab[j].second); ++k)
            {
                if (k - j >= ab[k].first && k - j <= ab[k].second) res[i] = max(res[i], abs(h[j] - h[k]));
            }
        }
        cout << res[i] << '\n';
    }
    return res;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<ll> h(n);
    vector<pair<int, int>> ab(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> h[i] >> ab[i].first >> ab[i].second;
    }
    int q;
    cin >> q;
    vector<pair<int, int>> queries(q);
    for (auto &[l, r] : queries)
    {
        cin >> l >> r;
        --l;
        --r;
    }
    brute(h, ab, queries);
    return 0;
}
/*
 5
 10 2 4
 1 1 1
 2 1 3
 1 1
 1
 100 1 1
 5
 1 2
 2 3
 1 3
 1 4
 1 5
 */

/*
2
10 2 4
1 1 1
1
1 2
*/
//10 1 2 1 100

Compilation message

antennas.cpp: In function 'std::vector<long long int> brute(std::vector<long long int>, std::vector<std::pair<int, int> >, std::vector<std::pair<int, int> >)':
antennas.cpp:168:9: warning: unused variable 'n' [-Wunused-variable]
  168 |     int n = (int)h.size();
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 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 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 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 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2528 ms 6220 KB Output is correct
26 Correct 1126 ms 1256 KB Output is correct
27 Execution timed out 3079 ms 7496 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3066 ms 5948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 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 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2528 ms 6220 KB Output is correct
26 Correct 1126 ms 1256 KB Output is correct
27 Execution timed out 3079 ms 7496 KB Time limit exceeded
28 Halted 0 ms 0 KB -