Submission #938887

# Submission time Handle Problem Language Result Execution time Memory
938887 2024-03-05T18:45:53 Z danikoynov Two Antennas (JOI19_antennas) C++14
0 / 100
3000 ms 85584 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


const ll maxn = 2e5 + 10;

struct query
{
    ll l, r, idx;

    query(ll _l = 0, ll _r = 0, ll _idx = 0)
    {
        l = _l;
        r = _r;
        idx = _idx;
    }
} task[maxn];

ll n, q;
ll h[maxn], a[maxn], b[maxn];
vector < query > spot[maxn];
ll ans[maxn];

void input()
{
    cin >> n;
    for (ll i = 1; i <= n; i ++)
    {
        cin >> h[i] >> a[i] >> b[i];
    }
    cin >> q;
    for (ll i = 1; i <= q; i ++)
        ans[i] = -1;

    for (ll i = 1; i <= q; i ++)
    {
        cin >> task[i].l >> task[i].r;
        task[i].idx = i;
        spot[task[i].r].push_back(task[i]);
    }
}

const ll inf = 2e9 + 10;
ll act[maxn], low[maxn];
vector < ll > upd[2][2 * maxn];

struct node
{
    ll max_dif, max_low, sec_low;
    ll res;

    node()
    {
        max_dif = -inf;
        max_low = inf;
        sec_low = -inf;
        res = -inf;
    }
};

node merge_nodes(node lf, node rf)
{
    /// careful for free node

    node mf;
    mf.max_low = max(lf.max_low, rf.max_low);
    if (lf.max_low != mf.max_low && lf.max_low > mf.sec_low)
        mf.sec_low = lf.max_low;
    if (lf.sec_low != mf.max_low && lf.sec_low > mf.sec_low)
        mf.sec_low = lf.sec_low;

    if (rf.max_low != mf.max_low && rf.max_low > mf.sec_low)
        mf.sec_low = rf.max_low;
    if (rf.sec_low != mf.max_low && rf.sec_low > mf.sec_low)
        mf.sec_low = rf.sec_low;

    if (lf.max_low == mf.max_low && lf.max_dif > mf.max_dif)
        mf.max_dif = lf.max_dif;
    if (rf.max_low == mf.max_low && rf.max_dif > mf.max_dif)
        mf.max_dif = rf.max_dif;

    mf.res = max(lf.res, rf.res);

    return mf;
}

node tree[4 * maxn];

void build(ll root, ll left, ll right)
{
    if (left == right)
    {
        tree[root] = node();
        tree[root].max_dif = 0 - inf;
        tree[root].max_low = inf;
        tree[root].sec_low = -inf;
        tree[root].res = tree[root].max_dif;
        return;
    }

    ll mid = (left + right) / 2;
    build(root * 2, left, mid);
    build(root * 2 + 1, mid + 1, right);

    tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}

ll lazy[4 * maxn];
ll tag[4 * maxn];

void push_lazy(ll root, ll left, ll right)
{
    if (tag[root] == 0 || lazy[root] > tree[root].max_low)
        return;
        //cout << "push_lazy " << endl;
    ll delta = tree[root].max_low - lazy[root];
    tree[root].max_dif += delta;
    tree[root].res = max(tree[root].res, tree[root].max_dif);
    tree[root].max_low = lazy[root];

    if (left != right)
    {
        lazy[root * 2] = lazy[root];
        lazy[root * 2 + 1] = lazy[root];
        tag[root * 2] = tag[root * 2 + 1] = 1;
    }
    tag[root] = 0;
    lazy[root] = 0;
}

void chmin(ll root, ll left, ll right, ll qleft, ll qright, ll val)
{
    push_lazy(root, left, right);
    if (left > qright || right < qleft || tree[root].max_low < val)
        return;

    if (left >= qleft && right <= qright && tree[root].sec_low < val)
    {
        //cout << "chmin " << left << " " << right << " " << val << endl;
        lazy[root] = val;
        tag[root] = 1;
        push_lazy(root, left, right);
        return;
    }

    ll mid = (left + right) / 2;
    chmin(root * 2, left, mid, qleft, qright, val);
    chmin(root * 2 + 1, mid + 1, right, qleft, qright, val);

    tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}

void point_update(ll root, ll left, ll right, ll pivot, ll val)
{
    push_lazy(root, left, right);

    if (left == right)
    {
        tree[root] = node();
            tree[root].max_dif = val - inf;
        tree[root].max_low = inf;
        tree[root].sec_low = -inf;
        tree[root].res = tree[root].max_dif;
        return;
    }

    ll mid = (left + right) / 2;
    if (pivot <= mid)
    {
        push_lazy(root * 2 + 1, mid + 1, right);
        point_update(root * 2, left, mid, pivot, val);
    }
    else
    {
        push_lazy(root * 2, left, mid);
        point_update(root * 2 + 1, mid + 1, right, pivot, val);
    }

    tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}

ll point_query(ll root, ll left, ll right, ll pivot)
{
    push_lazy(root, left, right);
    if (left == right)
        return tree[root].res;
    ll mid = (left + right) / 2;
    if (pivot <= mid)
        return point_query(root * 2, left, mid, pivot);
    else
        return point_query(root * 2 + 1, mid + 1, right, pivot);
}

ll range_query(ll root, ll left, ll right, ll qleft, ll qright)
{
    push_lazy(root, left, right);
    if (left > qright || right < qleft)
        return -inf;

    if (left >= qleft && right <= qright)
        return tree[root].res;

    ll mid = (left + right) / 2;
    return max(range_query(root * 2, left, mid, qleft, qright),
               range_query(root * 2 + 1, mid + 1, right, qleft, qright));
}

ll values[maxn];
void sweep_line()
{
    ///cerr << task[49].l << " : " << task[49].r << endl;
    for (ll i = 1; i <= n; i ++)
    {
        low[i] = inf;
        act[i] = 0;
        values[i] = -inf;
        upd[0][i + a[i]].push_back(i);
        upd[1][i + b[i]].push_back(i);
    }

    build(1, 1, n);
    for (ll i = 1; i <= n; i ++)
    {
        for (ll cur : upd[0][i])
        {
            act[cur] = 1;
            point_update(1, 1, n, cur, h[cur]);
        }

        ll lf = max((ll)1, i - b[i]), rf = max((ll)0, i - a[i]);
        chmin(1, 1, n, lf, rf, h[i]);
        for (ll j = lf; j <= rf; j ++)
        {
            if (act[j])
                low[j] = min(low[j], h[i]);
        }

        /**for (int i = 1; i <= n; i ++)
            cout << act[i] << " ";
        cout << endl;
        for (int i = 1; i <= n; i ++)
            cout << low[i] << " ";
        cout << endl;

        for (int j = 1; j <= n; j ++)
            cout << point_query(1 ,1 ,n, j) << " ";
        cout << endl;cout << "--------------" << endl;*/
        for (ll cur : upd[1][i])
        {
            act[cur] = 0;
            values[cur] = point_query(1 ,1, n, cur);
            point_update(1, 1, n, cur, 0);
        }

        for (query cur : spot[i])
        {
            for (ll j = cur.l; j <= cur.r; j ++)
            {
                ans[cur.idx] = max(ans[cur.idx], values[j]);
            }
            int cs = range_query(1, 1, n, cur.l, cur.r);

            ans[cur.idx] = max(ans[cur.idx], range_query(1, 1, n, cur.l, cur.r));
        }

    }

    ///cout << "FINISH" << endl;
}

void reverse_data()
{
    reverse(h + 1, h + n + 1);
    reverse(a + 1, a + n + 1);
    reverse(b + 1, b + n + 1);

    for (ll i = 1; i <= n; i ++)
    {
        spot[i].clear();
        upd[0][i].clear();
        upd[1][i].clear();
    }

    for (ll i = 1; i <= q; i ++)
    {
        ll nl = n - task[i].r + 1, nr = n - task[i].l + 1;
        ///cout << i << " : " << nl << " " << nr << endl;
        task[i].l = nl;
        task[i].r = nr;
        spot[task[i].r].push_back(task[i]);
    }
}

void prll()
{
    for (ll i = 1; i <= q; i ++)
    {

        cout << ans[i]  << endl;

    }
}
void solve()
{
    input();

    sweep_line();
    reverse_data();
    sweep_line();
    prll();
}

int main()
{
    speed();
    ///freopen("output.txt", "w", stdout);
    solve();
    return 0;
}
/**
5
10 2 4
1 1 1
2 1 3
1 1 1
100 1 1
1
1 2


111
2342163 25 76
738276997 50 52
1669890 26 40
14902411 56 81
899007094 32 85
422634516 2 71
936109680 79 100
638713463 109 110
119468142 28 104
713258492 104 107
267306336 1 39
973810399 87 90
835929417 43 86
335127775 12 104
840490095 39 66
459253103 11 104
706538155 4 101
194912428 82 96
949222000 73 96
910118043 85 95
226986709 46 100
827920993 75 83
768390729 46 87
314695927 26 30
230872586 49 82
995156805 60 93
497044719 3 103
827805975 75 83
499889872 95 110
590726845 66 73
607078123 59 99
370425117 26 76
490217622 18 32
431150446 77 89
9267221 13 81
693001694 64 95
866349101 26 93
844742196 6 63
655634427 33 88
829473857 67 92
311419714 15 56
900123935 42 87
665050273 42 59
792230034 12 34
847490718 11 21
177697704 66 81
198679758 24 54
668213379 14 45
45659509 82 91
327099919 21 82
118001173 81 85
738608951 80 101
214209461 38 97
324473845 107 109
879267283 109 109
190386778 43 77
881527758 25 44
18672166 13 21
733749641 73 92
653264399 103 110
870234612 104 106
101340654 33 40
710067113 71 104
616465791 8 14
610725519 6 58
376168562 89 109
93289534 79 107
973278654 55 85
183001646 37 50
70495079 47 89
383449644 76 77
239592436 26 49
561740304 21 27
326305893 94 101
376448990 28 85
558483855 107 110
58797040 8 97
479096372 76 90
435216279 42 87
583841698 92 106
277031377 63 110
56594141 34 38
381128173 40 95
275114013 110 110
716940824 48 85
805968236 44 55
997032121 87 92
958531100 90 104
459788004 39 85
618435405 34 97
833464071 87 99
527989265 92 92
708140049 65 87
707609457 2 78
937680650 71 105
969561745 81 102
960172678 49 65
56883909 7 40
602118268 53 69
980631740 95 101
795277058 19 26
572270576 13 89
440358047 25 79
467566899 16 24
634175510 30 38
673050101 21 60
768461341 54 107
331110483 58 87
218341925 68 77
419836470 30 77
925521790 29 36
68
87 89
81 90
38 95
73 93
69 70
4 98
77 93
73 110
49 54
101 102
107 109
20 63
55 94
61 95
104 107
80 101
73 101
44 51
92 94
75 82
15 83
47 59
32 79
30 62
48 79
18 45
33 86
74 90
107 109
37 87
60 78
1 48
93 99
78 89
38 96
61 107
6 61
110 111
81 92
98 103
76 95
43 107
11 81
56 65
49 102
70 87
84 85
44 109
66 111
110 111
6 59
6 12
8 64
7 42
21 81
70 88
53 104
46 103
8 44
103 106
34 38
72 103
39 45
38 51
80 88
7 79
105 107
17 28

*/

Compilation message

antennas.cpp: In function 'void sweep_line()':
antennas.cpp:271:17: warning: unused variable 'cs' [-Wunused-variable]
  271 |             int cs = range_query(1, 1, n, cur.l, cur.r);
      |                 ^~
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 66652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 66652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3050 ms 85584 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 66652 KB Output isn't correct
2 Halted 0 ms 0 KB -