답안 #679582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679582 2023-01-08T15:25:11 Z elkernos Fish 2 (JOI22_fish2) C++17
100 / 100
2778 ms 12312 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define vt vector
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int nax = 1 << 16;
const ll oo = 1e17;

int n;
#define lc 2 * v
#define rc 2 * v + 1
#define m (l + r) / 2
struct CMin {
    struct Node {
        pii p = {0, 1};
        int lazy = 0;
        void dod(int x)
        {
            p.first += x;
            lazy += x;
        }
    };
    Node nothing = {{n + 213, 0}, 0};
    friend Node operator+(const Node &l, const Node &r)
    {
        Node ret;
        ret.p = min(l.p, r.p);
        if (l.p.first == r.p.first) ret.p.second = l.p.second + r.p.second;
        return ret;
    }
    Node tree[4 * nax];
    void push(int v)
    {
        for (auto to : {lc, rc})
            tree[to].dod(tree[v].lazy);
        tree[v].lazy = 0;
    }
    void build(int v = 1, int l = 0, int r = n + 1)
    {
        if (l == r) {
            tree[v].p.second = 1;
            return;
        }
        build(lc, l, m), build(rc, m + 1, r);
        tree[v] = tree[lc] + tree[rc];
    }
    void add(int ql, int qr, int x, int v = 1, int l = 0, int r = n + 1)
    {
        if (r < ql || qr < l) return;
        if (ql <= l && r <= qr) {
            tree[v].dod(x);
            return;
        }
        push(v);
        add(ql, qr, x, lc, l, m), add(ql, qr, x, rc, m + 1, r);
        tree[v] = tree[lc] + tree[rc];
    }
    Node query(int ql, int qr, int v = 1, int l = 0, int r = n + 1)
    {
        if (r < ql || qr < l) return nothing;
        if (ql <= l && r <= qr) return tree[v];
        push(v);
        return query(ql, qr, lc, l, m) + query(ql, qr, rc, m + 1, r);
    }
};
struct Sum {
    ll s[2 * nax];
    void update(int pos, int dif)
    {
        for (; pos < 2 * nax; pos |= pos + 1)
            s[pos] += dif;
    }
    ll query(int pos)
    {
        ll res = 0;
        for (; pos > 0; pos &= pos - 1)
            res += s[pos - 1];
        return res;
    }
    ll query(int a, int b)
    {
        return query(b + 1) - query(a);
    }
};
struct Max {
    ll tree[4 * nax];
    void update(int pos, ll to, int v = 1, int l = 0, int r = n + 1)
    {
        if (pos < l || r < pos) return;
        if (l == r) {
            tree[v] = to;
            return;
        }
        update(pos, to, lc, l, m), update(pos, to, rc, m + 1, r);
        tree[v] = max(tree[lc], tree[rc]);
    }
    int maxPrawo(int ogr, ll od, int v = 1, int l = 0, int r = n + 1)
    {
        if (tree[v] <= od) return -1;
        if (l == r) return l;
        if (ogr <= m) return maxPrawo(ogr, od, lc, l, m);
        int ret = maxPrawo(ogr, od, rc, m + 1, r);
        return ret != -1 ? ret : maxPrawo(ogr, od, lc, l, m);
    }
    int maxLewo(int ogr, ll od, int v = 1, int l = 0, int r = n + 1)
    {
        if (tree[v] <= od) return -1;
        if (l == r) return l;
        if (m < ogr) return maxLewo(ogr, od, rc, m + 1, r);
        int ret = maxLewo(ogr, od, lc, l, m);
        return ret != -1 ? ret : maxLewo(ogr, od, rc, m + 1, r);
    }
};

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    CMin intervals;
    Sum getsum;
    Max getmax;
    vt<ll> a(n + 2);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    a[0] = a[n + 1] = oo;
    for (int i = 0; i <= n + 1; i++) {
        getsum.update(i, a[i]);
        getmax.update(i, a[i]);
    }
    intervals.build();
    set<pair<int, int>> alive;
    auto good = [&](int l, int r) {
        if (r - l + 1 <= 2) return false;
        return getsum.query(l + 1, r - 1) < min(a[l], a[r]);
    };
    auto wsadz = [&](int l, int r, int x) {
        if (x == +1) {
            if (!alive.count({l, r})) {
                alive.emplace(l, r);
                intervals.add(l + 1, r - 1, +1);
            }
        }
        if (x == -1) {
            if (alive.count({l, r})) {
                alive.erase({l, r});
                intervals.add(l + 1, r - 1, -1);
            }
        }
    };
    auto wezL = [&](int l, int r) {
        return getmax.maxPrawo(l - 1, getsum.query(l, r - 1));
    };
    auto wezR = [&](int l, int r) {
        return getmax.maxLewo(r + 1, getsum.query(l + 1, r));
    };
    auto findboth = [&](int p, int x) {
        int l = p - 1, r = p + 1;
        while (1) {
            if (good(l, r)) wsadz(l, r, x);
            if (l == 0 && r == n + 1) break;
            if (a[l] < a[r])
                l = wezL(l, r);
            else
                r = wezR(l, r);
        }
    };
    auto findpresuf = [&](int p, int x) {
        int r = wezR(p, p);
        while (1) {
            if (good(p, r)) wsadz(p, r, x);
            if (r == n + 1) break;
            r = wezR(p, r);
        }
        int l = wezL(p, p);
        while (1) {
            if (good(l, p)) wsadz(l, p, x);
            if (l == 0) break;
            l = wezL(l, p);
        }
    };
    auto wezupd = [&](int i, int x) {
        findboth(i, -1);
        findpresuf(i, -1);
        getsum.update(i, x - a[i]);
        a[i] = x;
        getmax.update(i, x);
        findboth(i, +1);
        findpresuf(i, +1);
    };
    for (int i = 1; i <= n; i++) {
        findboth(i, +1);
        findpresuf(i, +1);
    }
    int q;
    cin >> q;
    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int i, x;
            cin >> i >> x;
            wezupd(i, x);
        }
        else {
            int l, r;
            cin >> l >> r;
            int real_l = l, real_r = r;
            int lp = l + 1, rp = r - 1;
            while (lp <= r) {
                if (getsum.query(l, lp - 1) < a[lp]) real_l = lp;
                lp = wezR(l, lp);
            }
            while (l <= rp) {
                if (getsum.query(rp + 1, r) < a[rp]) real_r = rp;
                rp = wezL(rp, r);
            }
            cout << intervals.query(real_l, real_r).p.second << endl;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 3 ms 6356 KB Output is correct
3 Correct 3 ms 6356 KB Output is correct
4 Correct 3 ms 6356 KB Output is correct
5 Correct 12 ms 6496 KB Output is correct
6 Correct 8 ms 6484 KB Output is correct
7 Correct 10 ms 6484 KB Output is correct
8 Correct 8 ms 6484 KB Output is correct
9 Correct 8 ms 6472 KB Output is correct
10 Correct 7 ms 6484 KB Output is correct
11 Correct 4 ms 6484 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 8 ms 6412 KB Output is correct
16 Correct 6 ms 6484 KB Output is correct
17 Correct 10 ms 6484 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 6 ms 6484 KB Output is correct
20 Correct 5 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 602 ms 11164 KB Output is correct
3 Correct 821 ms 10700 KB Output is correct
4 Correct 690 ms 11216 KB Output is correct
5 Correct 789 ms 10868 KB Output is correct
6 Correct 187 ms 9188 KB Output is correct
7 Correct 455 ms 8332 KB Output is correct
8 Correct 187 ms 9176 KB Output is correct
9 Correct 442 ms 8448 KB Output is correct
10 Correct 601 ms 9136 KB Output is correct
11 Correct 767 ms 8896 KB Output is correct
12 Correct 242 ms 9076 KB Output is correct
13 Correct 301 ms 9012 KB Output is correct
14 Correct 248 ms 10496 KB Output is correct
15 Correct 263 ms 10276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 3 ms 6356 KB Output is correct
3 Correct 3 ms 6356 KB Output is correct
4 Correct 3 ms 6356 KB Output is correct
5 Correct 12 ms 6496 KB Output is correct
6 Correct 8 ms 6484 KB Output is correct
7 Correct 10 ms 6484 KB Output is correct
8 Correct 8 ms 6484 KB Output is correct
9 Correct 8 ms 6472 KB Output is correct
10 Correct 7 ms 6484 KB Output is correct
11 Correct 4 ms 6484 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 8 ms 6412 KB Output is correct
16 Correct 6 ms 6484 KB Output is correct
17 Correct 10 ms 6484 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 6 ms 6484 KB Output is correct
20 Correct 5 ms 6484 KB Output is correct
21 Correct 3 ms 6356 KB Output is correct
22 Correct 602 ms 11164 KB Output is correct
23 Correct 821 ms 10700 KB Output is correct
24 Correct 690 ms 11216 KB Output is correct
25 Correct 789 ms 10868 KB Output is correct
26 Correct 187 ms 9188 KB Output is correct
27 Correct 455 ms 8332 KB Output is correct
28 Correct 187 ms 9176 KB Output is correct
29 Correct 442 ms 8448 KB Output is correct
30 Correct 601 ms 9136 KB Output is correct
31 Correct 767 ms 8896 KB Output is correct
32 Correct 242 ms 9076 KB Output is correct
33 Correct 301 ms 9012 KB Output is correct
34 Correct 248 ms 10496 KB Output is correct
35 Correct 263 ms 10276 KB Output is correct
36 Correct 662 ms 11716 KB Output is correct
37 Correct 812 ms 11032 KB Output is correct
38 Correct 732 ms 10456 KB Output is correct
39 Correct 674 ms 11744 KB Output is correct
40 Correct 800 ms 10408 KB Output is correct
41 Correct 214 ms 9256 KB Output is correct
42 Correct 203 ms 9320 KB Output is correct
43 Correct 417 ms 8456 KB Output is correct
44 Correct 478 ms 8400 KB Output is correct
45 Correct 684 ms 9512 KB Output is correct
46 Correct 614 ms 9224 KB Output is correct
47 Correct 689 ms 8164 KB Output is correct
48 Correct 268 ms 9096 KB Output is correct
49 Correct 242 ms 9040 KB Output is correct
50 Correct 259 ms 10316 KB Output is correct
51 Correct 273 ms 10292 KB Output is correct
52 Correct 244 ms 10428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 602 ms 11164 KB Output is correct
3 Correct 821 ms 10700 KB Output is correct
4 Correct 690 ms 11216 KB Output is correct
5 Correct 789 ms 10868 KB Output is correct
6 Correct 187 ms 9188 KB Output is correct
7 Correct 455 ms 8332 KB Output is correct
8 Correct 187 ms 9176 KB Output is correct
9 Correct 442 ms 8448 KB Output is correct
10 Correct 601 ms 9136 KB Output is correct
11 Correct 767 ms 8896 KB Output is correct
12 Correct 242 ms 9076 KB Output is correct
13 Correct 301 ms 9012 KB Output is correct
14 Correct 248 ms 10496 KB Output is correct
15 Correct 263 ms 10276 KB Output is correct
16 Correct 4 ms 6364 KB Output is correct
17 Correct 1238 ms 11272 KB Output is correct
18 Correct 1079 ms 12196 KB Output is correct
19 Correct 1249 ms 11440 KB Output is correct
20 Correct 1205 ms 11264 KB Output is correct
21 Correct 1185 ms 11180 KB Output is correct
22 Correct 1062 ms 12312 KB Output is correct
23 Correct 1149 ms 11144 KB Output is correct
24 Correct 1293 ms 11640 KB Output is correct
25 Correct 1243 ms 11372 KB Output is correct
26 Correct 1297 ms 11500 KB Output is correct
27 Correct 376 ms 9792 KB Output is correct
28 Correct 369 ms 9920 KB Output is correct
29 Correct 373 ms 9832 KB Output is correct
30 Correct 760 ms 8812 KB Output is correct
31 Correct 767 ms 8664 KB Output is correct
32 Correct 1202 ms 9436 KB Output is correct
33 Correct 853 ms 9440 KB Output is correct
34 Correct 1206 ms 9168 KB Output is correct
35 Correct 1028 ms 8904 KB Output is correct
36 Correct 1069 ms 9740 KB Output is correct
37 Correct 417 ms 9292 KB Output is correct
38 Correct 401 ms 9432 KB Output is correct
39 Correct 468 ms 10940 KB Output is correct
40 Correct 455 ms 10924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 602 ms 11164 KB Output is correct
3 Correct 821 ms 10700 KB Output is correct
4 Correct 690 ms 11216 KB Output is correct
5 Correct 789 ms 10868 KB Output is correct
6 Correct 187 ms 9188 KB Output is correct
7 Correct 455 ms 8332 KB Output is correct
8 Correct 187 ms 9176 KB Output is correct
9 Correct 442 ms 8448 KB Output is correct
10 Correct 601 ms 9136 KB Output is correct
11 Correct 767 ms 8896 KB Output is correct
12 Correct 242 ms 9076 KB Output is correct
13 Correct 301 ms 9012 KB Output is correct
14 Correct 248 ms 10496 KB Output is correct
15 Correct 263 ms 10276 KB Output is correct
16 Correct 3 ms 6356 KB Output is correct
17 Correct 2676 ms 11408 KB Output is correct
18 Correct 2013 ms 11388 KB Output is correct
19 Correct 2231 ms 11100 KB Output is correct
20 Correct 1750 ms 11660 KB Output is correct
21 Correct 2395 ms 11424 KB Output is correct
22 Correct 2031 ms 11496 KB Output is correct
23 Correct 2505 ms 11180 KB Output is correct
24 Correct 1927 ms 11592 KB Output is correct
25 Correct 2287 ms 11128 KB Output is correct
26 Correct 666 ms 9508 KB Output is correct
27 Correct 866 ms 9344 KB Output is correct
28 Correct 1226 ms 10380 KB Output is correct
29 Correct 745 ms 9460 KB Output is correct
30 Correct 862 ms 9416 KB Output is correct
31 Correct 1411 ms 10376 KB Output is correct
32 Correct 1860 ms 10708 KB Output is correct
33 Correct 1272 ms 8952 KB Output is correct
34 Correct 1716 ms 11092 KB Output is correct
35 Correct 1131 ms 9256 KB Output is correct
36 Correct 1710 ms 10404 KB Output is correct
37 Correct 1182 ms 10252 KB Output is correct
38 Correct 854 ms 10180 KB Output is correct
39 Correct 871 ms 10788 KB Output is correct
40 Correct 597 ms 10824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6356 KB Output is correct
2 Correct 3 ms 6356 KB Output is correct
3 Correct 3 ms 6356 KB Output is correct
4 Correct 3 ms 6356 KB Output is correct
5 Correct 12 ms 6496 KB Output is correct
6 Correct 8 ms 6484 KB Output is correct
7 Correct 10 ms 6484 KB Output is correct
8 Correct 8 ms 6484 KB Output is correct
9 Correct 8 ms 6472 KB Output is correct
10 Correct 7 ms 6484 KB Output is correct
11 Correct 4 ms 6484 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 8 ms 6412 KB Output is correct
16 Correct 6 ms 6484 KB Output is correct
17 Correct 10 ms 6484 KB Output is correct
18 Correct 6 ms 6484 KB Output is correct
19 Correct 6 ms 6484 KB Output is correct
20 Correct 5 ms 6484 KB Output is correct
21 Correct 3 ms 6356 KB Output is correct
22 Correct 602 ms 11164 KB Output is correct
23 Correct 821 ms 10700 KB Output is correct
24 Correct 690 ms 11216 KB Output is correct
25 Correct 789 ms 10868 KB Output is correct
26 Correct 187 ms 9188 KB Output is correct
27 Correct 455 ms 8332 KB Output is correct
28 Correct 187 ms 9176 KB Output is correct
29 Correct 442 ms 8448 KB Output is correct
30 Correct 601 ms 9136 KB Output is correct
31 Correct 767 ms 8896 KB Output is correct
32 Correct 242 ms 9076 KB Output is correct
33 Correct 301 ms 9012 KB Output is correct
34 Correct 248 ms 10496 KB Output is correct
35 Correct 263 ms 10276 KB Output is correct
36 Correct 662 ms 11716 KB Output is correct
37 Correct 812 ms 11032 KB Output is correct
38 Correct 732 ms 10456 KB Output is correct
39 Correct 674 ms 11744 KB Output is correct
40 Correct 800 ms 10408 KB Output is correct
41 Correct 214 ms 9256 KB Output is correct
42 Correct 203 ms 9320 KB Output is correct
43 Correct 417 ms 8456 KB Output is correct
44 Correct 478 ms 8400 KB Output is correct
45 Correct 684 ms 9512 KB Output is correct
46 Correct 614 ms 9224 KB Output is correct
47 Correct 689 ms 8164 KB Output is correct
48 Correct 268 ms 9096 KB Output is correct
49 Correct 242 ms 9040 KB Output is correct
50 Correct 259 ms 10316 KB Output is correct
51 Correct 273 ms 10292 KB Output is correct
52 Correct 244 ms 10428 KB Output is correct
53 Correct 4 ms 6364 KB Output is correct
54 Correct 1238 ms 11272 KB Output is correct
55 Correct 1079 ms 12196 KB Output is correct
56 Correct 1249 ms 11440 KB Output is correct
57 Correct 1205 ms 11264 KB Output is correct
58 Correct 1185 ms 11180 KB Output is correct
59 Correct 1062 ms 12312 KB Output is correct
60 Correct 1149 ms 11144 KB Output is correct
61 Correct 1293 ms 11640 KB Output is correct
62 Correct 1243 ms 11372 KB Output is correct
63 Correct 1297 ms 11500 KB Output is correct
64 Correct 376 ms 9792 KB Output is correct
65 Correct 369 ms 9920 KB Output is correct
66 Correct 373 ms 9832 KB Output is correct
67 Correct 760 ms 8812 KB Output is correct
68 Correct 767 ms 8664 KB Output is correct
69 Correct 1202 ms 9436 KB Output is correct
70 Correct 853 ms 9440 KB Output is correct
71 Correct 1206 ms 9168 KB Output is correct
72 Correct 1028 ms 8904 KB Output is correct
73 Correct 1069 ms 9740 KB Output is correct
74 Correct 417 ms 9292 KB Output is correct
75 Correct 401 ms 9432 KB Output is correct
76 Correct 468 ms 10940 KB Output is correct
77 Correct 455 ms 10924 KB Output is correct
78 Correct 3 ms 6356 KB Output is correct
79 Correct 2676 ms 11408 KB Output is correct
80 Correct 2013 ms 11388 KB Output is correct
81 Correct 2231 ms 11100 KB Output is correct
82 Correct 1750 ms 11660 KB Output is correct
83 Correct 2395 ms 11424 KB Output is correct
84 Correct 2031 ms 11496 KB Output is correct
85 Correct 2505 ms 11180 KB Output is correct
86 Correct 1927 ms 11592 KB Output is correct
87 Correct 2287 ms 11128 KB Output is correct
88 Correct 666 ms 9508 KB Output is correct
89 Correct 866 ms 9344 KB Output is correct
90 Correct 1226 ms 10380 KB Output is correct
91 Correct 745 ms 9460 KB Output is correct
92 Correct 862 ms 9416 KB Output is correct
93 Correct 1411 ms 10376 KB Output is correct
94 Correct 1860 ms 10708 KB Output is correct
95 Correct 1272 ms 8952 KB Output is correct
96 Correct 1716 ms 11092 KB Output is correct
97 Correct 1131 ms 9256 KB Output is correct
98 Correct 1710 ms 10404 KB Output is correct
99 Correct 1182 ms 10252 KB Output is correct
100 Correct 854 ms 10180 KB Output is correct
101 Correct 871 ms 10788 KB Output is correct
102 Correct 597 ms 10824 KB Output is correct
103 Correct 2778 ms 10592 KB Output is correct
104 Correct 1893 ms 11740 KB Output is correct
105 Correct 1534 ms 11404 KB Output is correct
106 Correct 1325 ms 11700 KB Output is correct
107 Correct 2462 ms 11000 KB Output is correct
108 Correct 1835 ms 11800 KB Output is correct
109 Correct 1599 ms 11136 KB Output is correct
110 Correct 1542 ms 11752 KB Output is correct
111 Correct 1423 ms 11500 KB Output is correct
112 Correct 1321 ms 11628 KB Output is correct
113 Correct 753 ms 9824 KB Output is correct
114 Correct 445 ms 9804 KB Output is correct
115 Correct 1448 ms 10200 KB Output is correct
116 Correct 1079 ms 10388 KB Output is correct
117 Correct 502 ms 9708 KB Output is correct
118 Correct 842 ms 9752 KB Output is correct
119 Correct 835 ms 9280 KB Output is correct
120 Correct 1313 ms 10408 KB Output is correct
121 Correct 996 ms 10416 KB Output is correct
122 Correct 1839 ms 10580 KB Output is correct
123 Correct 1227 ms 8696 KB Output is correct
124 Correct 1273 ms 10400 KB Output is correct
125 Correct 1036 ms 8536 KB Output is correct
126 Correct 1184 ms 9844 KB Output is correct
127 Correct 1202 ms 10348 KB Output is correct
128 Correct 598 ms 9616 KB Output is correct
129 Correct 875 ms 10628 KB Output is correct
130 Correct 611 ms 10784 KB Output is correct