Submission #737966

# Submission time Handle Problem Language Result Execution time Memory
737966 2023-05-08T03:18:03 Z resting Fish 2 (JOI22_fish2) C++17
60 / 100
4000 ms 70528 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int mx = 1e5 + 5;
const ll inf = 1e15;

vector<pair<int, int>> res;
struct segtree {
    int l, r, v, m;
    segtree* lc, * rc;
    segtree* getmem();
    vector<pair<int, int>> lm, rm;
    segtree() : segtree(-1, -1) {};
    segtree(int l, int r) : l(l), r(r) {
        m = (l + r) / 2;
        if (l == r)return;
        lc = getmem(); *lc = segtree(l, m);
        rc = getmem(); *rc = segtree(m + 1, r);
    }
    void del(int ql, int qr) {
        if (qr < m) { lc->del(ql, qr); return; }
        if (ql > m) { rc->del(ql, qr); return; }
        lm.erase(find(lm.begin(), lm.end(), pair<int, int>(ql, qr)));
        rm.erase(find(rm.begin(), rm.end(), pair<int, int>(qr, ql)));
    }
    void add(int ql, int qr) {
        if (qr < m) { lc->add(ql, qr); return; }
        if (ql > m) { rc->add(ql, qr); return; }
        lm.push_back({ ql, qr });
        rm.push_back({ qr, ql });
    }
    void q(int qi) {
        if (l == r)return;
        if (qi <= m)lc->q(qi);
        else rc->q(qi);
        if (qi <= m) {
            for (auto t = lm.begin(); t != lm.end(); t++)if (t->first <= qi)
                res.push_back(*t);
        } else {
            for (auto t = rm.rbegin(); t != rm.rend(); t++)if (t->first >= qi)
                res.push_back({ t->second, t->first });
        }
    }
};
segtree mem[mx * 4];
int memsz = 0;
segtree* segtree::getmem() { return &mem[memsz++]; }

struct segtree2 {
    int l, r, cnt1 = 0, cnt2 = 0, lz = 0;
    segtree2* lc, * rc;
    segtree2* getmem();
    segtree2() : segtree2(-1, -1) {};
    segtree2(int l, int r) : l(l), r(r) {
        if (l == r)return;
        int m = (l + r) / 2;
        lc = getmem(); *lc = segtree2(l, m);
        rc = getmem(); *rc = segtree2(m + 1, r);
    }
    void fix() {
        if (l == r) {
            cnt1 = cnt2 = 0;
            if (lz == 1) cnt1 = 1;
            if (lz > 1) cnt2 = 1;
            return;
        }
        if (lz == 0) {
            cnt1 = lc->cnt1 + rc->cnt1;
            cnt2 = lc->cnt2 + rc->cnt2;
        } else if (lz == 1) {
            cnt2 = lc->cnt1 + rc->cnt1 + lc->cnt2 + rc->cnt2;
            cnt1 = (r - l + 1) - cnt2;
        } else {
            cnt1 = 0;
            cnt2 = (r - l + 1);
        }
    }
    void upd(int ql, int qr, int qv) {
        if (l > qr || r < ql) return;
        if (l >= ql && r <= qr) {
            lz += qv; fix();
            return;
        }
        lc->upd(ql, qr, qv);
        rc->upd(ql, qr, qv);
        fix();
    }
    int q(int ql, int qr, int qlz = 0) {
        if (l > qr || r < ql) return 0;
        lz += qlz; fix();
        int res = 0;
        if (l >= ql && r <= qr) {
            res = cnt2;
        } else {
            res = lc->q(ql, qr, lz) + rc->q(ql, qr, lz);
        }
        lz -= qlz; fix();
        return res;
    }
};
segtree2 mem2[mx * 4];
int memsz2 = 0;
segtree2* segtree2::getmem() { return &mem2[memsz2++]; }

struct segtree3 {
    int l, r; ll mx = 0, sm = 0;
    segtree3* lc, * rc;
    segtree3* getmem();
    segtree3() : segtree3(-1, -1) {};
    segtree3(int l, int r) : l(l), r(r) {
        if (l == r)return;
        int m = (l + r) / 2;
        lc = getmem(); *lc = segtree3(l, m);
        rc = getmem(); *rc = segtree3(m + 1, r);
    }
    void fix() {
        if (l == r) return;
        mx = max(lc->mx, rc->mx);
        sm = lc->sm + rc->sm;
    }
    void upd(int qi, ll qv) {
        if (l > qi || r < qi) return;
        if (l == r) {
            mx = qv; sm = qv;return;
        }
        lc->upd(qi, qv); rc->upd(qi, qv);
        fix();
    }
    int walkl(int ql, ll qv) {
        if (r < ql) return -1;
        if (mx <= qv) return -1;
        if (l == r) return l;
        int v = lc->walkl(ql, qv);
        return v == -1 ? rc->walkl(ql, qv) : v;
    }
    int walkr(int qr, ll qv) {
        if (l > qr) return -1;
        if (mx <= qv) return -1;
        if (l == r) return l;
        int v = rc->walkr(qr, qv);
        return v == -1 ? lc->walkr(qr, qv) : v;
    }
    ll qsum(int ql, int qr) {
        if (l > qr || r < ql) return 0;
        if (l >= ql && r <= qr) return sm;
        return lc->qsum(ql, qr) + rc->qsum(ql, qr);
    }
};
segtree3 mem3[mx * 4];
int memsz3 = 0;
segtree3* segtree3::getmem() { return &mem3[memsz++]; }


segtree ac; segtree2 ac2; segtree3 ac3; vector<ll> ac4;
int n;

void clear(int i) {
    res.clear(); ac.q(i);
    for (auto& x : res) {
        auto [l, r] = x;
        //cout << "DEL " << l << " " << r << endl;
        ac.del(l, r);
        ac2.upd(l, r, -1);
    }
}

void redo(int i, bool extendleft, bool extendright) {
    //cout << "REDO " << i << endl;
    //walking time
    int l = i, r = i;
    while (1) {
        ll t = ac3.qsum(l, r);
        int wr = ac3.walkl(r + 1, t);
        int wl = ac3.walkr(l - 1, t);
        if (wl < i - 1 && !extendleft) break;
        if (wr > i + 1 && !extendright) break;
        //cout << l << "," << r << "," << wl << "," << wr << endl;
        if (wl == -1 || wr == -1) break;
        t = ac3.qsum(wl + 1, wr - 1);
        if (ac4[wl] > t && ac4[wr] > t) {
            //cout << "ADD " << wl + 1 << " " << wr - 1 << " " << t << " " << ac4[wl] << " " << ac4[wr] << endl;
            ac.add(wl + 1, wr - 1);
            ac2.upd(wl + 1, wr - 1, 1);
        }
        l = wl + 1, r = wr - 1;
        if (ac4[wl] > ac4[wr]) {
            if (!extendright) break;
            r++;
        } else {
            if (!extendleft) break;
            l--;
        }
    }
}

void upd(int i, ll v, bool left, bool right) {
    ac4[i] = v; ac3.upd(i, v);
    clear(i);
    if (i < n + 1 && right)clear(i + 1);
    if (i && left)clear(i - 1);

    redo(i, 1, 1);
    if (i < n + 1 && right)redo(i + 1, 0, 1);
    if (i && left)redo(i - 1, 1, 0);
}

void solve() {
    cin >> n;
    vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) cin >> a[i];
    ac = segtree(0, n + 1); ac2 = segtree2(0, n + 1); ac3 = segtree3(0, n + 1); ac4 = vector<ll>(n + 2, 0);
    for (int i = 0; i <= n + 1; i++) upd(i, a[i], 1, 0);
    int q; cin >> q;
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            int x, y; cin >> x >> y; a[x] = y;
            upd(x, y, 1, 1);
        } else {
            int l, r; cin >> l >> r;
            upd(l - 1, inf, 0, 1); upd(r + 1, inf, 1, 0);
            cout << (r - l + 1 - ac2.q(l, r)) << endl;
            upd(l - 1, a[l - 1], 0, 1); upd(r + 1, a[r + 1], 1, 0);
        }
    }
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62940 KB Output is correct
2 Correct 30 ms 62876 KB Output is correct
3 Correct 28 ms 62796 KB Output is correct
4 Correct 28 ms 62852 KB Output is correct
5 Correct 37 ms 62948 KB Output is correct
6 Correct 39 ms 62944 KB Output is correct
7 Correct 33 ms 62932 KB Output is correct
8 Correct 42 ms 62908 KB Output is correct
9 Correct 39 ms 62880 KB Output is correct
10 Correct 32 ms 62872 KB Output is correct
11 Correct 36 ms 62948 KB Output is correct
12 Correct 34 ms 62872 KB Output is correct
13 Correct 37 ms 62952 KB Output is correct
14 Correct 31 ms 62932 KB Output is correct
15 Correct 38 ms 62956 KB Output is correct
16 Correct 35 ms 62888 KB Output is correct
17 Correct 33 ms 62952 KB Output is correct
18 Correct 37 ms 62932 KB Output is correct
19 Correct 34 ms 62924 KB Output is correct
20 Correct 35 ms 62992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62884 KB Output is correct
2 Correct 172 ms 66444 KB Output is correct
3 Correct 183 ms 66544 KB Output is correct
4 Correct 184 ms 66380 KB Output is correct
5 Correct 177 ms 66536 KB Output is correct
6 Correct 154 ms 66420 KB Output is correct
7 Correct 149 ms 65480 KB Output is correct
8 Correct 149 ms 66492 KB Output is correct
9 Correct 147 ms 65524 KB Output is correct
10 Correct 168 ms 65128 KB Output is correct
11 Correct 160 ms 65516 KB Output is correct
12 Correct 149 ms 66252 KB Output is correct
13 Correct 148 ms 66320 KB Output is correct
14 Correct 164 ms 67528 KB Output is correct
15 Correct 166 ms 67144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62940 KB Output is correct
2 Correct 30 ms 62876 KB Output is correct
3 Correct 28 ms 62796 KB Output is correct
4 Correct 28 ms 62852 KB Output is correct
5 Correct 37 ms 62948 KB Output is correct
6 Correct 39 ms 62944 KB Output is correct
7 Correct 33 ms 62932 KB Output is correct
8 Correct 42 ms 62908 KB Output is correct
9 Correct 39 ms 62880 KB Output is correct
10 Correct 32 ms 62872 KB Output is correct
11 Correct 36 ms 62948 KB Output is correct
12 Correct 34 ms 62872 KB Output is correct
13 Correct 37 ms 62952 KB Output is correct
14 Correct 31 ms 62932 KB Output is correct
15 Correct 38 ms 62956 KB Output is correct
16 Correct 35 ms 62888 KB Output is correct
17 Correct 33 ms 62952 KB Output is correct
18 Correct 37 ms 62932 KB Output is correct
19 Correct 34 ms 62924 KB Output is correct
20 Correct 35 ms 62992 KB Output is correct
21 Correct 29 ms 62884 KB Output is correct
22 Correct 172 ms 66444 KB Output is correct
23 Correct 183 ms 66544 KB Output is correct
24 Correct 184 ms 66380 KB Output is correct
25 Correct 177 ms 66536 KB Output is correct
26 Correct 154 ms 66420 KB Output is correct
27 Correct 149 ms 65480 KB Output is correct
28 Correct 149 ms 66492 KB Output is correct
29 Correct 147 ms 65524 KB Output is correct
30 Correct 168 ms 65128 KB Output is correct
31 Correct 160 ms 65516 KB Output is correct
32 Correct 149 ms 66252 KB Output is correct
33 Correct 148 ms 66320 KB Output is correct
34 Correct 164 ms 67528 KB Output is correct
35 Correct 166 ms 67144 KB Output is correct
36 Correct 215 ms 66696 KB Output is correct
37 Correct 222 ms 66720 KB Output is correct
38 Correct 220 ms 66428 KB Output is correct
39 Correct 208 ms 66652 KB Output is correct
40 Correct 217 ms 66456 KB Output is correct
41 Correct 170 ms 66516 KB Output is correct
42 Correct 173 ms 66432 KB Output is correct
43 Correct 180 ms 65612 KB Output is correct
44 Correct 188 ms 65544 KB Output is correct
45 Correct 203 ms 65612 KB Output is correct
46 Correct 188 ms 65172 KB Output is correct
47 Correct 186 ms 64948 KB Output is correct
48 Correct 162 ms 66252 KB Output is correct
49 Correct 176 ms 66252 KB Output is correct
50 Correct 183 ms 67720 KB Output is correct
51 Correct 181 ms 67152 KB Output is correct
52 Correct 187 ms 67572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62884 KB Output is correct
2 Correct 172 ms 66444 KB Output is correct
3 Correct 183 ms 66544 KB Output is correct
4 Correct 184 ms 66380 KB Output is correct
5 Correct 177 ms 66536 KB Output is correct
6 Correct 154 ms 66420 KB Output is correct
7 Correct 149 ms 65480 KB Output is correct
8 Correct 149 ms 66492 KB Output is correct
9 Correct 147 ms 65524 KB Output is correct
10 Correct 168 ms 65128 KB Output is correct
11 Correct 160 ms 65516 KB Output is correct
12 Correct 149 ms 66252 KB Output is correct
13 Correct 148 ms 66320 KB Output is correct
14 Correct 164 ms 67528 KB Output is correct
15 Correct 166 ms 67144 KB Output is correct
16 Correct 29 ms 62920 KB Output is correct
17 Execution timed out 4056 ms 69060 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62884 KB Output is correct
2 Correct 172 ms 66444 KB Output is correct
3 Correct 183 ms 66544 KB Output is correct
4 Correct 184 ms 66380 KB Output is correct
5 Correct 177 ms 66536 KB Output is correct
6 Correct 154 ms 66420 KB Output is correct
7 Correct 149 ms 65480 KB Output is correct
8 Correct 149 ms 66492 KB Output is correct
9 Correct 147 ms 65524 KB Output is correct
10 Correct 168 ms 65128 KB Output is correct
11 Correct 160 ms 65516 KB Output is correct
12 Correct 149 ms 66252 KB Output is correct
13 Correct 148 ms 66320 KB Output is correct
14 Correct 164 ms 67528 KB Output is correct
15 Correct 166 ms 67144 KB Output is correct
16 Correct 31 ms 62932 KB Output is correct
17 Correct 1152 ms 66840 KB Output is correct
18 Correct 1217 ms 70320 KB Output is correct
19 Correct 1821 ms 67080 KB Output is correct
20 Correct 1334 ms 70348 KB Output is correct
21 Correct 1398 ms 67120 KB Output is correct
22 Correct 1233 ms 70528 KB Output is correct
23 Correct 1399 ms 67012 KB Output is correct
24 Correct 1229 ms 70440 KB Output is correct
25 Correct 1502 ms 66908 KB Output is correct
26 Correct 856 ms 67872 KB Output is correct
27 Correct 765 ms 68172 KB Output is correct
28 Correct 1089 ms 68348 KB Output is correct
29 Correct 809 ms 68036 KB Output is correct
30 Correct 765 ms 68148 KB Output is correct
31 Correct 1111 ms 68648 KB Output is correct
32 Correct 1165 ms 69332 KB Output is correct
33 Correct 798 ms 65740 KB Output is correct
34 Correct 1167 ms 69900 KB Output is correct
35 Correct 942 ms 65504 KB Output is correct
36 Correct 1149 ms 69080 KB Output is correct
37 Correct 1166 ms 68280 KB Output is correct
38 Correct 1200 ms 67832 KB Output is correct
39 Correct 932 ms 68876 KB Output is correct
40 Correct 932 ms 68216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 62940 KB Output is correct
2 Correct 30 ms 62876 KB Output is correct
3 Correct 28 ms 62796 KB Output is correct
4 Correct 28 ms 62852 KB Output is correct
5 Correct 37 ms 62948 KB Output is correct
6 Correct 39 ms 62944 KB Output is correct
7 Correct 33 ms 62932 KB Output is correct
8 Correct 42 ms 62908 KB Output is correct
9 Correct 39 ms 62880 KB Output is correct
10 Correct 32 ms 62872 KB Output is correct
11 Correct 36 ms 62948 KB Output is correct
12 Correct 34 ms 62872 KB Output is correct
13 Correct 37 ms 62952 KB Output is correct
14 Correct 31 ms 62932 KB Output is correct
15 Correct 38 ms 62956 KB Output is correct
16 Correct 35 ms 62888 KB Output is correct
17 Correct 33 ms 62952 KB Output is correct
18 Correct 37 ms 62932 KB Output is correct
19 Correct 34 ms 62924 KB Output is correct
20 Correct 35 ms 62992 KB Output is correct
21 Correct 29 ms 62884 KB Output is correct
22 Correct 172 ms 66444 KB Output is correct
23 Correct 183 ms 66544 KB Output is correct
24 Correct 184 ms 66380 KB Output is correct
25 Correct 177 ms 66536 KB Output is correct
26 Correct 154 ms 66420 KB Output is correct
27 Correct 149 ms 65480 KB Output is correct
28 Correct 149 ms 66492 KB Output is correct
29 Correct 147 ms 65524 KB Output is correct
30 Correct 168 ms 65128 KB Output is correct
31 Correct 160 ms 65516 KB Output is correct
32 Correct 149 ms 66252 KB Output is correct
33 Correct 148 ms 66320 KB Output is correct
34 Correct 164 ms 67528 KB Output is correct
35 Correct 166 ms 67144 KB Output is correct
36 Correct 215 ms 66696 KB Output is correct
37 Correct 222 ms 66720 KB Output is correct
38 Correct 220 ms 66428 KB Output is correct
39 Correct 208 ms 66652 KB Output is correct
40 Correct 217 ms 66456 KB Output is correct
41 Correct 170 ms 66516 KB Output is correct
42 Correct 173 ms 66432 KB Output is correct
43 Correct 180 ms 65612 KB Output is correct
44 Correct 188 ms 65544 KB Output is correct
45 Correct 203 ms 65612 KB Output is correct
46 Correct 188 ms 65172 KB Output is correct
47 Correct 186 ms 64948 KB Output is correct
48 Correct 162 ms 66252 KB Output is correct
49 Correct 176 ms 66252 KB Output is correct
50 Correct 183 ms 67720 KB Output is correct
51 Correct 181 ms 67152 KB Output is correct
52 Correct 187 ms 67572 KB Output is correct
53 Correct 29 ms 62920 KB Output is correct
54 Execution timed out 4056 ms 69060 KB Time limit exceeded
55 Halted 0 ms 0 KB -