Submission #937840

#TimeUsernameProblemLanguageResultExecution timeMemory
937840nguyentunglamFish 2 (JOI22_fish2)C++17
100 / 100
2443 ms15264 KiB
#include<bits/stdc++.h> #define all(v) v.begin(), v.end() #define endl "\n" #define int long long using namespace std; const int N = 1e5 + 10; int n; long long bit[N], a[N]; struct IT1 { int T[N << 2]; void up(int s, int l, int r, int pos, int val) { if (l == r) { T[s] = val; return; } int mid = l + r >> 1; if (pos <= mid) up(s << 1, l, mid, pos, val); else up(s << 1 | 1, mid + 1, r, pos, val); T[s] = max(T[s << 1], T[s << 1 | 1]); } int find_right(int s, int l, int r, int from, int to, int val) { if (l > to || r < from) return 0; if (from <= l && r <= to) { if (T[s] <= val) return 0; while (l != r) { int mid = l + r >> 1; if (T[s << 1] > val) { s = s << 1; r = mid; } else { s = s << 1 | 1; l = mid + 1; } } return l; } int mid = l + r >> 1; int ret = find_right(s << 1, l, mid, from, to, val); if (ret) return ret; return find_right(s << 1 | 1, mid + 1, r, from, to, val); } int find_left(int s, int l, int r, int from, int to, int val) { if (l > to || r < from) return 0; if (from <= l && r <= to) { if (T[s] <= val) return 0; while (l != r) { int mid = l + r >> 1; if (T[s << 1 | 1] > val) { s = s << 1 | 1; l = mid + 1; } else { s = s << 1; r = mid; } } return l; } int mid = l + r >> 1; int ret = find_left(s << 1 | 1, mid + 1, r, from, to, val); if (ret) return ret; return find_left(s << 1, l, mid, from, to, val); } } it1; struct IT2 { pair<int, int> T[N << 2]; int lazy[N << 2]; pair<int, int> gather(pair<int, int> x, pair<int, int> y) { if (x.first == y.first) return make_pair(x.first, x.second + y.second); return min(x, y); } void build(int s, int l, int r) { T[s].second = r - l + 1; if (l != r) { int mid = l + r >> 1; build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r); } } void push(int s, int l, int r) { if (!lazy[s]) return; T[s].first += lazy[s]; if (l != r) { lazy[s << 1] += lazy[s]; lazy[s << 1 | 1] += lazy[s]; } lazy[s] = 0; } void up(int s, int l, int r, int from, int to, int val) { push(s, l, r); if (l > to || r < from) return; if (from <= l && r <= to) { lazy[s] = val; push(s, l, r); return; } int mid = l + r >> 1; up(s << 1, l, mid, from, to, val); up(s << 1 | 1, mid + 1, r, from, to, val); T[s] = gather(T[s << 1], T[s << 1 | 1]); } pair<int, int> get(int s, int l, int r, int from, int to) { push(s, l, r); if (l > to || r < from) return make_pair(1e9, 0); if (from <= l && r <= to) return T[s]; int mid = l + r >> 1; return gather(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to)); } } it2; void up_bit(int pos, int val) { while (pos <= n) { bit[pos] += val; pos += pos & -pos; } } long long get_bit(int l, int r) { long long ret = 0; for(int pos = r; pos; pos -= pos & -pos) ret += bit[pos]; for(int pos = l - 1; pos; pos -= pos & -pos) ret -= bit[pos]; return ret; } vector<pair<int, int> > expand (int pos) { vector<pair<int, int> > ret; if (pos == 0 || pos == n + 1) return ret; int l = pos, r = pos, sum = a[pos]; while (1) { int _r = it1.find_right(1, 1, n, r + 1, n, sum); if (_r == 0) _r = _r = n + 1; int _l = it1.find_left(1, 1, n, 1, l - 1, sum); if (_l == 0 && _r == n + 1) return ret; sum = get_bit(_l + 1, _r - 1); if (sum < a[_l] && sum < a[_r]) ret.emplace_back(_l + 1, _r - 1); if (a[_l] <= a[_r]) l = _l; else r = _r; } return ret; } vector<pair<int, int> > _expand (int pos) { vector<pair<int, int> > ret = expand(pos); vector<pair<int, int> > left = expand(pos - 1); vector<pair<int, int> > right = expand(pos + 1); for(auto &[l, r] : left) if (r == pos - 1) ret.emplace_back(l, r); for(auto &[l, r] : right) if (l == pos + 1) ret.emplace_back(l, r); return ret; } int32_t main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen("task.inp", "r")) { freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); } if (fopen(task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } cin >> n; a[0] = a[n + 1] = 1e18; it2.build(1, 1, n); auto add = [&] (int pos, int val) { vector<pair<int, int> > old = _expand(pos); for(auto &[l, r] : old) { it2.up(1, 1, n, l, r, -1); // cout << pos << " " << l << " " << r << "#\n"; } up_bit(pos, val - a[pos]); a[pos] = val; it1.up(1, 1, n, pos, val); vector<pair<int, int> > cur = _expand(pos); for(auto &[l, r] : cur) { it2.up(1, 1, n, l, r, 1); // cout << pos << " " << l << " " << r << "@\n"; } }; auto query = [&] (int l, int r) { int j = r, last = r; long long sum = a[r]; while (j > l) { j = it1.find_left(1, 1, n, 1, j - 1, sum); if (j < l) break; sum = get_bit(j + 1, r); if (sum < a[j]) last = j; sum += a[j]; } r = last; j = l; last = l; sum = a[l]; while (j < r) { j = it1.find_right(1, 1, n, j + 1, n, sum); if (j == 0 || j > r) break; sum = get_bit(l, j - 1); if (sum < a[j]) last = j; sum += a[j]; } l = last; pair<int, int> tmp = it2.get(1, 1, n, l, r); return tmp.second; }; for(int i = 1; i <= n; i++) { int x; cin >> x; add(i, x); } int q; cin >> q; while (q--) { int t, x, y; cin >> t >> x >> y; if (t == 1) add(x, y); else cout << query(x, y) << endl; } }

Compilation message (stderr)

fish2.cpp: In member function 'void IT1::up(long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:21:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In member function 'long long int IT1::find_right(long long int, long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:32:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |         int mid = l + r >> 1;
      |                   ~~^~~
fish2.cpp:44:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In member function 'long long int IT1::find_left(long long int, long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |         int mid = l + r >> 1;
      |                   ~~^~~
fish2.cpp:67:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In member function 'void IT2::build(long long int, long long int, long long int)':
fish2.cpp:86:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |       int mid = l + r >> 1;
      |                 ~~^~~
fish2.cpp: In member function 'void IT2::up(long long int, long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:110:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  110 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In member function 'std::pair<long long int, long long int> IT2::get(long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:120:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  120 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In function 'std::vector<std::pair<long long int, long long int> > expand(long long int)':
fish2.cpp:145:21: warning: operation on '_r' may be undefined [-Wsequence-point]
  145 |     if (_r == 0) _r = _r = n + 1;
      |                  ~~~^~~~~~~~~~~~
fish2.cpp: In function 'int32_t main()':
fish2.cpp:172:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |     freopen("task.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:173:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |     freopen("task.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:177:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |     freopen (task".inp", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:178:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |     freopen (task".out", "w", stdout);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...