답안 #1046626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046626 2024-08-06T18:36:05 Z juicy Fish 2 (JOI22_fish2) C++17
100 / 100
1974 ms 9576 KB
// https://oj.uz/submission/937840
// https://codeforces.com/blog/entry/101003?#comment-898608
// https://www2.ioi-jp.org/camp/2022/2022-sp-tasks/contest4/fish2-review.pdf

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5, inf = 1e9 + 7;

int n, q;
int a[N], ma[4 * N],  lz[4 * N];
array<int, 2> s[4 * N];

array<int, 2> operator + (const array<int, 2> &lt, const array<int, 2> &rt) {
  array<int, 2> res{};
  res[0] = min(lt[0], rt[0]);
  if (res[0] == lt[0]) {
    res[1] += lt[1];
  }
  if (res[0] == rt[0]) {
    res[1] += rt[1];
  }
  return res;
}

namespace ft {
  long long ft[N];

  void upd(int i, int x) {
    for (; i <= n; i += i & -i) {
      ft[i] += x;
    }
  }

  long long qry(int i) {
    long long res = 0;
    for (; i; i -= i & -i) {
      res += ft[i];
    }
    return res;
  }

  long long qry(int l, int r) {
    return qry(r) - qry(l - 1);
  }
}

void upd(int i, int x, int id = 1, int l = 1, int r = n) {
  if (l == r) {
    ma[id] = x;
    return;
  }
  int md = (l + r) / 2;
  if (i <= md) {
    upd(i, x, id * 2, l, md);
  } else {
    upd(i, x, id * 2 + 1, md + 1, r);
  }
  ma[id] = max(ma[id * 2], ma[id * 2 + 1]);
}

int walklt(int i, long long k, int id = 1, int l = 1, int r = n) {
  if (l >= i || ma[id] <= k) {
    return 0;
  }
  if (l == r) {
    return l;
  }
  int md = (l + r) / 2;
  auto res = walklt(i, k, id * 2 + 1, md + 1, r);
  return res == 0 ? walklt(i, k, id * 2, l, md) : res;
}

int walkrt(int i, long long k, int id = 1, int l = 1, int r = n) {
  if (r <= i || ma[id] <= k) {
    return n + 1;
  }
  if (l == r) {
    return l;
  }
  int md = (l + r) / 2;
  auto res = walkrt(i, k, id * 2, l, md);
  return res == n + 1 ? walkrt(i, k, id * 2 + 1, md + 1, r) : res;
}

void app(int id, int x) {
  s[id][0] += x;
  lz[id] += x;
}

void psh(int id) {
  if (lz[id]) {
    app(id * 2, lz[id]);
    app(id * 2 + 1, lz[id]);
    lz[id] = 0;
  }
}

void bld(int id = 1, int l = 1, int r = n) {
  if (l == r) {
    s[id] = {0, 1};
    return;
  }
  int md = (l + r) / 2;
  bld(id * 2, l, md);
  bld(id * 2 + 1, md + 1, r);
  s[id] = s[id * 2] + s[id * 2 + 1];
}

void add(int u, int v, int x, int id = 1, int l = 1, int r = n) {
  if (u <= l && r <= v) {
    app(id, x);
    return;
  }
  psh(id);
  int md = (l + r) / 2;
  if (u <= md) {
    add(u, v, x, id * 2, l, md);
  }
  if (md < v) {
    add(u, v, x, id * 2 + 1, md + 1, r);
  }
  s[id] = s[id * 2] + s[id * 2 + 1];
}

array<int, 2> get(int u, int v, int id = 1, int l = 1, int r = n) {
  if (u <= l && r <= v) {
    return s[id];
  }
  psh(id);
  int md = (l + r) / 2;
  if (v <= md) {
    return get(u, v, id * 2, l, md);
  }
  if (md < u) {
    return get(u, v, id * 2 + 1, md + 1, r);
  }
  return get(u, v, id * 2, l, md) + get(u, v, id * 2 + 1, md + 1, r); 
}

bool check(int l, int r, long long &sum) {
  sum = ft::qry(l + 1, r - 1);
  return sum < min(a[l], a[r]);
}

vector<array<int, 2>> getcands(int p) {
  if (p == 0 || p == n + 1) {
    return {};
  }
  int l = p, r = p;
  long long cur = a[p];
  vector<array<int, 2>> cands;
  while (1) {
    int s = walklt(l, cur), e = walkrt(r, cur);
    if (s == 0 && e == n + 1) {
      break;
    }
    if (check(s, e, cur)) {
      cands.push_back({s + 1, e - 1});
    }
    if (a[s] <= a[e]) {
      l = s;
    } else {
      r = e;
    }
  }
  return cands; 
}

void up(int i, int x) {
  auto cands = getcands(i);
  auto lt = getcands(i - 1);
  auto rt = getcands(i + 1);
  for (auto [l, r] : cands) {
    add(l, r, x);
  }
  for (auto [l, r] : lt) {
    if (r == i - 1) {
      add(l, r, x);
    }
  }
  for (auto [l, r] : rt) {
    if (l == i + 1) {
      add(l, r, x);
    }
  }
}

void chg(int i, int x) {
  up(i, -1);
  ft::upd(i, x - a[i]);
  a[i] = x;
  upd(i, x);
  up(i, 1);
}

int findlt(int l, int r) {
  int res = l, p = l;
  long long cur = a[l];
  while (1) {
    p = walkrt(p, cur);
    if (p > r) {
      break;
    }
    cur = ft::qry(l, p - 1);
    if (cur < a[p]) {
      res = p;
    }
  }
  return res;
}

int findrt(int l, int r) {
  int res = r, p = r;
  long long cur = a[r];
  while (1) {
    p = walklt(p, cur);
    if (p < l) {
      break;
    }
    cur = ft::qry(p + 1, r);
    if (cur < a[p]) {
      res = p;
    }
  }
  return res;
}

int qry(int l, int r) {
  int s = findlt(l, r), e = findrt(l, r);
  if (s > e) {
    return 0;
  }
  return get(s, e)[1];
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  bld();
  a[0] = a[n + 1] = inf;
  for (int i = 1; i <= n; ++i) {
    int x; cin >> x;
    chg(i, x);
  }
  cin >> q;
  while (q--) {
    int t; cin >> t;
    if (t == 1) {
      int x, y; cin >> x >> y;
      chg(x, y);
    } else {
      int s, e; cin >> s >> e;
      cout << qry(s, e) << "\n";
    }
  }
  return 0;
}

// an interesting observation that I haven't taken advantage of
// A >= sum(x - 1) - sum(l - 1)
// -> A - sum(x - 1) >= -sum(l - 1) (can be solved with lazy segtree)
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 5 ms 2396 KB Output is correct
6 Correct 5 ms 2396 KB Output is correct
7 Correct 5 ms 2392 KB Output is correct
8 Correct 8 ms 2396 KB Output is correct
9 Correct 6 ms 2392 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 2 ms 2560 KB Output is correct
12 Correct 4 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 4 ms 2396 KB Output is correct
15 Correct 5 ms 2396 KB Output is correct
16 Correct 4 ms 2396 KB Output is correct
17 Correct 3 ms 2556 KB Output is correct
18 Correct 2 ms 2560 KB Output is correct
19 Correct 2 ms 2392 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2516 KB Output is correct
2 Correct 332 ms 7252 KB Output is correct
3 Correct 659 ms 6936 KB Output is correct
4 Correct 507 ms 7252 KB Output is correct
5 Correct 614 ms 7032 KB Output is correct
6 Correct 258 ms 7720 KB Output is correct
7 Correct 806 ms 6992 KB Output is correct
8 Correct 258 ms 7764 KB Output is correct
9 Correct 805 ms 6864 KB Output is correct
10 Correct 959 ms 7260 KB Output is correct
11 Correct 1266 ms 6992 KB Output is correct
12 Correct 443 ms 6964 KB Output is correct
13 Correct 413 ms 6992 KB Output is correct
14 Correct 355 ms 7364 KB Output is correct
15 Correct 351 ms 7252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 5 ms 2396 KB Output is correct
6 Correct 5 ms 2396 KB Output is correct
7 Correct 5 ms 2392 KB Output is correct
8 Correct 8 ms 2396 KB Output is correct
9 Correct 6 ms 2392 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 2 ms 2560 KB Output is correct
12 Correct 4 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 4 ms 2396 KB Output is correct
15 Correct 5 ms 2396 KB Output is correct
16 Correct 4 ms 2396 KB Output is correct
17 Correct 3 ms 2556 KB Output is correct
18 Correct 2 ms 2560 KB Output is correct
19 Correct 2 ms 2392 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 0 ms 2516 KB Output is correct
22 Correct 332 ms 7252 KB Output is correct
23 Correct 659 ms 6936 KB Output is correct
24 Correct 507 ms 7252 KB Output is correct
25 Correct 614 ms 7032 KB Output is correct
26 Correct 258 ms 7720 KB Output is correct
27 Correct 806 ms 6992 KB Output is correct
28 Correct 258 ms 7764 KB Output is correct
29 Correct 805 ms 6864 KB Output is correct
30 Correct 959 ms 7260 KB Output is correct
31 Correct 1266 ms 6992 KB Output is correct
32 Correct 443 ms 6964 KB Output is correct
33 Correct 413 ms 6992 KB Output is correct
34 Correct 355 ms 7364 KB Output is correct
35 Correct 351 ms 7252 KB Output is correct
36 Correct 323 ms 7252 KB Output is correct
37 Correct 664 ms 6920 KB Output is correct
38 Correct 645 ms 6936 KB Output is correct
39 Correct 303 ms 7252 KB Output is correct
40 Correct 674 ms 7060 KB Output is correct
41 Correct 273 ms 7764 KB Output is correct
42 Correct 261 ms 7568 KB Output is correct
43 Correct 764 ms 6876 KB Output is correct
44 Correct 838 ms 6844 KB Output is correct
45 Correct 1117 ms 7204 KB Output is correct
46 Correct 1067 ms 7252 KB Output is correct
47 Correct 1364 ms 6980 KB Output is correct
48 Correct 515 ms 7072 KB Output is correct
49 Correct 378 ms 6992 KB Output is correct
50 Correct 423 ms 7248 KB Output is correct
51 Correct 392 ms 7248 KB Output is correct
52 Correct 394 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2516 KB Output is correct
2 Correct 332 ms 7252 KB Output is correct
3 Correct 659 ms 6936 KB Output is correct
4 Correct 507 ms 7252 KB Output is correct
5 Correct 614 ms 7032 KB Output is correct
6 Correct 258 ms 7720 KB Output is correct
7 Correct 806 ms 6992 KB Output is correct
8 Correct 258 ms 7764 KB Output is correct
9 Correct 805 ms 6864 KB Output is correct
10 Correct 959 ms 7260 KB Output is correct
11 Correct 1266 ms 6992 KB Output is correct
12 Correct 443 ms 6964 KB Output is correct
13 Correct 413 ms 6992 KB Output is correct
14 Correct 355 ms 7364 KB Output is correct
15 Correct 351 ms 7252 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 904 ms 8776 KB Output is correct
18 Correct 538 ms 9076 KB Output is correct
19 Correct 880 ms 8836 KB Output is correct
20 Correct 933 ms 8784 KB Output is correct
21 Correct 904 ms 8788 KB Output is correct
22 Correct 531 ms 9044 KB Output is correct
23 Correct 855 ms 8636 KB Output is correct
24 Correct 936 ms 8752 KB Output is correct
25 Correct 896 ms 8788 KB Output is correct
26 Correct 983 ms 8784 KB Output is correct
27 Correct 347 ms 9576 KB Output is correct
28 Correct 348 ms 9556 KB Output is correct
29 Correct 359 ms 9560 KB Output is correct
30 Correct 1056 ms 8524 KB Output is correct
31 Correct 1004 ms 8528 KB Output is correct
32 Correct 1578 ms 8708 KB Output is correct
33 Correct 1136 ms 9040 KB Output is correct
34 Correct 1598 ms 8532 KB Output is correct
35 Correct 1468 ms 8588 KB Output is correct
36 Correct 1249 ms 9040 KB Output is correct
37 Correct 521 ms 8792 KB Output is correct
38 Correct 533 ms 8528 KB Output is correct
39 Correct 467 ms 9044 KB Output is correct
40 Correct 461 ms 9044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2516 KB Output is correct
2 Correct 332 ms 7252 KB Output is correct
3 Correct 659 ms 6936 KB Output is correct
4 Correct 507 ms 7252 KB Output is correct
5 Correct 614 ms 7032 KB Output is correct
6 Correct 258 ms 7720 KB Output is correct
7 Correct 806 ms 6992 KB Output is correct
8 Correct 258 ms 7764 KB Output is correct
9 Correct 805 ms 6864 KB Output is correct
10 Correct 959 ms 7260 KB Output is correct
11 Correct 1266 ms 6992 KB Output is correct
12 Correct 443 ms 6964 KB Output is correct
13 Correct 413 ms 6992 KB Output is correct
14 Correct 355 ms 7364 KB Output is correct
15 Correct 351 ms 7252 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1758 ms 8204 KB Output is correct
18 Correct 1176 ms 8828 KB Output is correct
19 Correct 1691 ms 8276 KB Output is correct
20 Correct 1121 ms 8784 KB Output is correct
21 Correct 1770 ms 8248 KB Output is correct
22 Correct 1202 ms 8764 KB Output is correct
23 Correct 1830 ms 8016 KB Output is correct
24 Correct 1162 ms 8784 KB Output is correct
25 Correct 1743 ms 8276 KB Output is correct
26 Correct 502 ms 9296 KB Output is correct
27 Correct 500 ms 9300 KB Output is correct
28 Correct 1113 ms 8548 KB Output is correct
29 Correct 453 ms 9296 KB Output is correct
30 Correct 521 ms 9300 KB Output is correct
31 Correct 1322 ms 8524 KB Output is correct
32 Correct 1800 ms 8592 KB Output is correct
33 Correct 1593 ms 8304 KB Output is correct
34 Correct 1437 ms 8936 KB Output is correct
35 Correct 1370 ms 8532 KB Output is correct
36 Correct 1683 ms 8784 KB Output is correct
37 Correct 871 ms 8504 KB Output is correct
38 Correct 737 ms 8532 KB Output is correct
39 Correct 597 ms 9040 KB Output is correct
40 Correct 493 ms 8960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 5 ms 2396 KB Output is correct
6 Correct 5 ms 2396 KB Output is correct
7 Correct 5 ms 2392 KB Output is correct
8 Correct 8 ms 2396 KB Output is correct
9 Correct 6 ms 2392 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 2 ms 2560 KB Output is correct
12 Correct 4 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 4 ms 2396 KB Output is correct
15 Correct 5 ms 2396 KB Output is correct
16 Correct 4 ms 2396 KB Output is correct
17 Correct 3 ms 2556 KB Output is correct
18 Correct 2 ms 2560 KB Output is correct
19 Correct 2 ms 2392 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 0 ms 2516 KB Output is correct
22 Correct 332 ms 7252 KB Output is correct
23 Correct 659 ms 6936 KB Output is correct
24 Correct 507 ms 7252 KB Output is correct
25 Correct 614 ms 7032 KB Output is correct
26 Correct 258 ms 7720 KB Output is correct
27 Correct 806 ms 6992 KB Output is correct
28 Correct 258 ms 7764 KB Output is correct
29 Correct 805 ms 6864 KB Output is correct
30 Correct 959 ms 7260 KB Output is correct
31 Correct 1266 ms 6992 KB Output is correct
32 Correct 443 ms 6964 KB Output is correct
33 Correct 413 ms 6992 KB Output is correct
34 Correct 355 ms 7364 KB Output is correct
35 Correct 351 ms 7252 KB Output is correct
36 Correct 323 ms 7252 KB Output is correct
37 Correct 664 ms 6920 KB Output is correct
38 Correct 645 ms 6936 KB Output is correct
39 Correct 303 ms 7252 KB Output is correct
40 Correct 674 ms 7060 KB Output is correct
41 Correct 273 ms 7764 KB Output is correct
42 Correct 261 ms 7568 KB Output is correct
43 Correct 764 ms 6876 KB Output is correct
44 Correct 838 ms 6844 KB Output is correct
45 Correct 1117 ms 7204 KB Output is correct
46 Correct 1067 ms 7252 KB Output is correct
47 Correct 1364 ms 6980 KB Output is correct
48 Correct 515 ms 7072 KB Output is correct
49 Correct 378 ms 6992 KB Output is correct
50 Correct 423 ms 7248 KB Output is correct
51 Correct 392 ms 7248 KB Output is correct
52 Correct 394 ms 7424 KB Output is correct
53 Correct 0 ms 2396 KB Output is correct
54 Correct 904 ms 8776 KB Output is correct
55 Correct 538 ms 9076 KB Output is correct
56 Correct 880 ms 8836 KB Output is correct
57 Correct 933 ms 8784 KB Output is correct
58 Correct 904 ms 8788 KB Output is correct
59 Correct 531 ms 9044 KB Output is correct
60 Correct 855 ms 8636 KB Output is correct
61 Correct 936 ms 8752 KB Output is correct
62 Correct 896 ms 8788 KB Output is correct
63 Correct 983 ms 8784 KB Output is correct
64 Correct 347 ms 9576 KB Output is correct
65 Correct 348 ms 9556 KB Output is correct
66 Correct 359 ms 9560 KB Output is correct
67 Correct 1056 ms 8524 KB Output is correct
68 Correct 1004 ms 8528 KB Output is correct
69 Correct 1578 ms 8708 KB Output is correct
70 Correct 1136 ms 9040 KB Output is correct
71 Correct 1598 ms 8532 KB Output is correct
72 Correct 1468 ms 8588 KB Output is correct
73 Correct 1249 ms 9040 KB Output is correct
74 Correct 521 ms 8792 KB Output is correct
75 Correct 533 ms 8528 KB Output is correct
76 Correct 467 ms 9044 KB Output is correct
77 Correct 461 ms 9044 KB Output is correct
78 Correct 1 ms 2392 KB Output is correct
79 Correct 1758 ms 8204 KB Output is correct
80 Correct 1176 ms 8828 KB Output is correct
81 Correct 1691 ms 8276 KB Output is correct
82 Correct 1121 ms 8784 KB Output is correct
83 Correct 1770 ms 8248 KB Output is correct
84 Correct 1202 ms 8764 KB Output is correct
85 Correct 1830 ms 8016 KB Output is correct
86 Correct 1162 ms 8784 KB Output is correct
87 Correct 1743 ms 8276 KB Output is correct
88 Correct 502 ms 9296 KB Output is correct
89 Correct 500 ms 9300 KB Output is correct
90 Correct 1113 ms 8548 KB Output is correct
91 Correct 453 ms 9296 KB Output is correct
92 Correct 521 ms 9300 KB Output is correct
93 Correct 1322 ms 8524 KB Output is correct
94 Correct 1800 ms 8592 KB Output is correct
95 Correct 1593 ms 8304 KB Output is correct
96 Correct 1437 ms 8936 KB Output is correct
97 Correct 1370 ms 8532 KB Output is correct
98 Correct 1683 ms 8784 KB Output is correct
99 Correct 871 ms 8504 KB Output is correct
100 Correct 737 ms 8532 KB Output is correct
101 Correct 597 ms 9040 KB Output is correct
102 Correct 493 ms 8960 KB Output is correct
103 Correct 1951 ms 8124 KB Output is correct
104 Correct 809 ms 9040 KB Output is correct
105 Correct 1077 ms 8728 KB Output is correct
106 Correct 931 ms 8856 KB Output is correct
107 Correct 1974 ms 8020 KB Output is correct
108 Correct 816 ms 9060 KB Output is correct
109 Correct 1196 ms 8528 KB Output is correct
110 Correct 1022 ms 8788 KB Output is correct
111 Correct 1064 ms 8788 KB Output is correct
112 Correct 940 ms 8816 KB Output is correct
113 Correct 518 ms 9412 KB Output is correct
114 Correct 366 ms 9552 KB Output is correct
115 Correct 1187 ms 8528 KB Output is correct
116 Correct 1050 ms 8788 KB Output is correct
117 Correct 382 ms 9460 KB Output is correct
118 Correct 954 ms 8680 KB Output is correct
119 Correct 513 ms 9296 KB Output is correct
120 Correct 1146 ms 8532 KB Output is correct
121 Correct 1065 ms 8788 KB Output is correct
122 Correct 1802 ms 8784 KB Output is correct
123 Correct 1646 ms 8272 KB Output is correct
124 Correct 1594 ms 8780 KB Output is correct
125 Correct 1462 ms 8532 KB Output is correct
126 Correct 1550 ms 8788 KB Output is correct
127 Correct 919 ms 8528 KB Output is correct
128 Correct 610 ms 8560 KB Output is correct
129 Correct 621 ms 9044 KB Output is correct
130 Correct 546 ms 9232 KB Output is correct