답안 #679591

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679591 2023-01-08T15:41:29 Z elkernos Fish 2 (JOI22_fish2) C++17
100 / 100
2683 ms 12020 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int nax = 1 << 17;
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 = {{nax, 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[2 * 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);
    }
} intervals;
struct Sum {
    ll s[nax];
    void update(int pos, int dif)
    {
        for (; pos < 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);
    }
} getsum;
struct Max {
    ll tree[2 * 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);
    }
} getmax;

ll a[nax];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &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);
        }
    };
    for (int i = 1; i <= n; i++) {
        findboth(i, +1);
        findpresuf(i, +1);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int type;
        scanf("%d", &type);
        if (type == 1) {
            int i, x;
            scanf("%d%d", &i, &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);
        }
        else {
            int l, r;
            scanf("%d%d", &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);
            }
            printf("%d\n", intervals.query(real_l, real_r).p.second);
        }
    }
}

Compilation message

fish2.cpp: In function 'int main()':
fish2.cpp:123:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
fish2.cpp:125:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         scanf("%lld", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
fish2.cpp:187:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
fish2.cpp:190:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  190 |         scanf("%d", &type);
      |         ~~~~~^~~~~~~~~~~~~
fish2.cpp:193:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  193 |             scanf("%d%d", &i, &x);
      |             ~~~~~^~~~~~~~~~~~~~~~
fish2.cpp:204:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |             scanf("%d%d", &l, &r);
      |             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 10 ms 408 KB Output is correct
6 Correct 5 ms 404 KB Output is correct
7 Correct 8 ms 404 KB Output is correct
8 Correct 6 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 6 ms 396 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 396 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 640 ms 10912 KB Output is correct
3 Correct 772 ms 10436 KB Output is correct
4 Correct 680 ms 10968 KB Output is correct
5 Correct 865 ms 10492 KB Output is correct
6 Correct 210 ms 9000 KB Output is correct
7 Correct 495 ms 8172 KB Output is correct
8 Correct 205 ms 9040 KB Output is correct
9 Correct 460 ms 8152 KB Output is correct
10 Correct 619 ms 8732 KB Output is correct
11 Correct 764 ms 8720 KB Output is correct
12 Correct 261 ms 8804 KB Output is correct
13 Correct 274 ms 8788 KB Output is correct
14 Correct 276 ms 10152 KB Output is correct
15 Correct 265 ms 10100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 10 ms 408 KB Output is correct
6 Correct 5 ms 404 KB Output is correct
7 Correct 8 ms 404 KB Output is correct
8 Correct 6 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 6 ms 396 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 396 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 640 ms 10912 KB Output is correct
23 Correct 772 ms 10436 KB Output is correct
24 Correct 680 ms 10968 KB Output is correct
25 Correct 865 ms 10492 KB Output is correct
26 Correct 210 ms 9000 KB Output is correct
27 Correct 495 ms 8172 KB Output is correct
28 Correct 205 ms 9040 KB Output is correct
29 Correct 460 ms 8152 KB Output is correct
30 Correct 619 ms 8732 KB Output is correct
31 Correct 764 ms 8720 KB Output is correct
32 Correct 261 ms 8804 KB Output is correct
33 Correct 274 ms 8788 KB Output is correct
34 Correct 276 ms 10152 KB Output is correct
35 Correct 265 ms 10100 KB Output is correct
36 Correct 698 ms 11512 KB Output is correct
37 Correct 824 ms 10692 KB Output is correct
38 Correct 791 ms 10216 KB Output is correct
39 Correct 705 ms 11528 KB Output is correct
40 Correct 777 ms 10148 KB Output is correct
41 Correct 207 ms 8964 KB Output is correct
42 Correct 193 ms 9004 KB Output is correct
43 Correct 439 ms 8268 KB Output is correct
44 Correct 420 ms 8172 KB Output is correct
45 Correct 671 ms 9244 KB Output is correct
46 Correct 598 ms 8884 KB Output is correct
47 Correct 623 ms 7884 KB Output is correct
48 Correct 250 ms 8860 KB Output is correct
49 Correct 242 ms 8780 KB Output is correct
50 Correct 244 ms 10060 KB Output is correct
51 Correct 257 ms 9996 KB Output is correct
52 Correct 238 ms 10108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 640 ms 10912 KB Output is correct
3 Correct 772 ms 10436 KB Output is correct
4 Correct 680 ms 10968 KB Output is correct
5 Correct 865 ms 10492 KB Output is correct
6 Correct 210 ms 9000 KB Output is correct
7 Correct 495 ms 8172 KB Output is correct
8 Correct 205 ms 9040 KB Output is correct
9 Correct 460 ms 8152 KB Output is correct
10 Correct 619 ms 8732 KB Output is correct
11 Correct 764 ms 8720 KB Output is correct
12 Correct 261 ms 8804 KB Output is correct
13 Correct 274 ms 8788 KB Output is correct
14 Correct 276 ms 10152 KB Output is correct
15 Correct 265 ms 10100 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1199 ms 10948 KB Output is correct
18 Correct 1052 ms 12020 KB Output is correct
19 Correct 1218 ms 11016 KB Output is correct
20 Correct 1181 ms 10940 KB Output is correct
21 Correct 1165 ms 11020 KB Output is correct
22 Correct 1046 ms 11872 KB Output is correct
23 Correct 1136 ms 10840 KB Output is correct
24 Correct 1237 ms 11212 KB Output is correct
25 Correct 1194 ms 11324 KB Output is correct
26 Correct 1258 ms 11224 KB Output is correct
27 Correct 379 ms 9744 KB Output is correct
28 Correct 374 ms 9548 KB Output is correct
29 Correct 386 ms 9564 KB Output is correct
30 Correct 757 ms 8616 KB Output is correct
31 Correct 758 ms 8512 KB Output is correct
32 Correct 1220 ms 9160 KB Output is correct
33 Correct 863 ms 9368 KB Output is correct
34 Correct 1185 ms 8792 KB Output is correct
35 Correct 982 ms 8352 KB Output is correct
36 Correct 1031 ms 9624 KB Output is correct
37 Correct 408 ms 9072 KB Output is correct
38 Correct 398 ms 9048 KB Output is correct
39 Correct 439 ms 10684 KB Output is correct
40 Correct 436 ms 10720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 640 ms 10912 KB Output is correct
3 Correct 772 ms 10436 KB Output is correct
4 Correct 680 ms 10968 KB Output is correct
5 Correct 865 ms 10492 KB Output is correct
6 Correct 210 ms 9000 KB Output is correct
7 Correct 495 ms 8172 KB Output is correct
8 Correct 205 ms 9040 KB Output is correct
9 Correct 460 ms 8152 KB Output is correct
10 Correct 619 ms 8732 KB Output is correct
11 Correct 764 ms 8720 KB Output is correct
12 Correct 261 ms 8804 KB Output is correct
13 Correct 274 ms 8788 KB Output is correct
14 Correct 276 ms 10152 KB Output is correct
15 Correct 265 ms 10100 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2621 ms 11316 KB Output is correct
18 Correct 2000 ms 11192 KB Output is correct
19 Correct 2199 ms 10776 KB Output is correct
20 Correct 1753 ms 11220 KB Output is correct
21 Correct 2386 ms 10972 KB Output is correct
22 Correct 1997 ms 11340 KB Output is correct
23 Correct 2487 ms 10916 KB Output is correct
24 Correct 1916 ms 11228 KB Output is correct
25 Correct 2284 ms 10708 KB Output is correct
26 Correct 658 ms 9280 KB Output is correct
27 Correct 825 ms 9032 KB Output is correct
28 Correct 1215 ms 10140 KB Output is correct
29 Correct 725 ms 9184 KB Output is correct
30 Correct 823 ms 9232 KB Output is correct
31 Correct 1404 ms 9964 KB Output is correct
32 Correct 1838 ms 10292 KB Output is correct
33 Correct 1190 ms 8828 KB Output is correct
34 Correct 1661 ms 10696 KB Output is correct
35 Correct 1077 ms 9124 KB Output is correct
36 Correct 1658 ms 10288 KB Output is correct
37 Correct 1169 ms 10120 KB Output is correct
38 Correct 875 ms 9924 KB Output is correct
39 Correct 841 ms 10396 KB Output is correct
40 Correct 539 ms 10572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 10 ms 408 KB Output is correct
6 Correct 5 ms 404 KB Output is correct
7 Correct 8 ms 404 KB Output is correct
8 Correct 6 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 6 ms 396 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 396 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 640 ms 10912 KB Output is correct
23 Correct 772 ms 10436 KB Output is correct
24 Correct 680 ms 10968 KB Output is correct
25 Correct 865 ms 10492 KB Output is correct
26 Correct 210 ms 9000 KB Output is correct
27 Correct 495 ms 8172 KB Output is correct
28 Correct 205 ms 9040 KB Output is correct
29 Correct 460 ms 8152 KB Output is correct
30 Correct 619 ms 8732 KB Output is correct
31 Correct 764 ms 8720 KB Output is correct
32 Correct 261 ms 8804 KB Output is correct
33 Correct 274 ms 8788 KB Output is correct
34 Correct 276 ms 10152 KB Output is correct
35 Correct 265 ms 10100 KB Output is correct
36 Correct 698 ms 11512 KB Output is correct
37 Correct 824 ms 10692 KB Output is correct
38 Correct 791 ms 10216 KB Output is correct
39 Correct 705 ms 11528 KB Output is correct
40 Correct 777 ms 10148 KB Output is correct
41 Correct 207 ms 8964 KB Output is correct
42 Correct 193 ms 9004 KB Output is correct
43 Correct 439 ms 8268 KB Output is correct
44 Correct 420 ms 8172 KB Output is correct
45 Correct 671 ms 9244 KB Output is correct
46 Correct 598 ms 8884 KB Output is correct
47 Correct 623 ms 7884 KB Output is correct
48 Correct 250 ms 8860 KB Output is correct
49 Correct 242 ms 8780 KB Output is correct
50 Correct 244 ms 10060 KB Output is correct
51 Correct 257 ms 9996 KB Output is correct
52 Correct 238 ms 10108 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1199 ms 10948 KB Output is correct
55 Correct 1052 ms 12020 KB Output is correct
56 Correct 1218 ms 11016 KB Output is correct
57 Correct 1181 ms 10940 KB Output is correct
58 Correct 1165 ms 11020 KB Output is correct
59 Correct 1046 ms 11872 KB Output is correct
60 Correct 1136 ms 10840 KB Output is correct
61 Correct 1237 ms 11212 KB Output is correct
62 Correct 1194 ms 11324 KB Output is correct
63 Correct 1258 ms 11224 KB Output is correct
64 Correct 379 ms 9744 KB Output is correct
65 Correct 374 ms 9548 KB Output is correct
66 Correct 386 ms 9564 KB Output is correct
67 Correct 757 ms 8616 KB Output is correct
68 Correct 758 ms 8512 KB Output is correct
69 Correct 1220 ms 9160 KB Output is correct
70 Correct 863 ms 9368 KB Output is correct
71 Correct 1185 ms 8792 KB Output is correct
72 Correct 982 ms 8352 KB Output is correct
73 Correct 1031 ms 9624 KB Output is correct
74 Correct 408 ms 9072 KB Output is correct
75 Correct 398 ms 9048 KB Output is correct
76 Correct 439 ms 10684 KB Output is correct
77 Correct 436 ms 10720 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 2621 ms 11316 KB Output is correct
80 Correct 2000 ms 11192 KB Output is correct
81 Correct 2199 ms 10776 KB Output is correct
82 Correct 1753 ms 11220 KB Output is correct
83 Correct 2386 ms 10972 KB Output is correct
84 Correct 1997 ms 11340 KB Output is correct
85 Correct 2487 ms 10916 KB Output is correct
86 Correct 1916 ms 11228 KB Output is correct
87 Correct 2284 ms 10708 KB Output is correct
88 Correct 658 ms 9280 KB Output is correct
89 Correct 825 ms 9032 KB Output is correct
90 Correct 1215 ms 10140 KB Output is correct
91 Correct 725 ms 9184 KB Output is correct
92 Correct 823 ms 9232 KB Output is correct
93 Correct 1404 ms 9964 KB Output is correct
94 Correct 1838 ms 10292 KB Output is correct
95 Correct 1190 ms 8828 KB Output is correct
96 Correct 1661 ms 10696 KB Output is correct
97 Correct 1077 ms 9124 KB Output is correct
98 Correct 1658 ms 10288 KB Output is correct
99 Correct 1169 ms 10120 KB Output is correct
100 Correct 875 ms 9924 KB Output is correct
101 Correct 841 ms 10396 KB Output is correct
102 Correct 539 ms 10572 KB Output is correct
103 Correct 2683 ms 10444 KB Output is correct
104 Correct 1820 ms 11596 KB Output is correct
105 Correct 1449 ms 11136 KB Output is correct
106 Correct 1325 ms 11464 KB Output is correct
107 Correct 2389 ms 10720 KB Output is correct
108 Correct 1782 ms 11468 KB Output is correct
109 Correct 1570 ms 10712 KB Output is correct
110 Correct 1500 ms 11448 KB Output is correct
111 Correct 1417 ms 11212 KB Output is correct
112 Correct 1326 ms 11368 KB Output is correct
113 Correct 731 ms 9220 KB Output is correct
114 Correct 434 ms 9552 KB Output is correct
115 Correct 1381 ms 9932 KB Output is correct
116 Correct 1078 ms 10284 KB Output is correct
117 Correct 491 ms 9500 KB Output is correct
118 Correct 834 ms 9364 KB Output is correct
119 Correct 834 ms 9256 KB Output is correct
120 Correct 1309 ms 10040 KB Output is correct
121 Correct 1000 ms 10024 KB Output is correct
122 Correct 1888 ms 10416 KB Output is correct
123 Correct 1209 ms 8424 KB Output is correct
124 Correct 1276 ms 9904 KB Output is correct
125 Correct 1031 ms 8520 KB Output is correct
126 Correct 1160 ms 9600 KB Output is correct
127 Correct 1189 ms 10096 KB Output is correct
128 Correct 594 ms 9400 KB Output is correct
129 Correct 886 ms 10272 KB Output is correct
130 Correct 610 ms 10516 KB Output is correct