This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 12.01.2024 20:25:24 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int INF = 1e9 + 23;
vector <int> a;
void process(void) {
int n; cin >> n;
a.resize(n); cin >> a;
struct Node {
int sum, cnt;
vector <int> pos_l, cnt_l, sum_l, pos_r, cnt_r, sum_r;
Node() {}
Node(int i) {
sum = a[i]; cnt = 1;
pos_l.push_back(i); cnt_l.push_back(0); sum_l.push_back(0);
pos_r.push_back(i); cnt_r.push_back(0); sum_r.push_back(0);
}
void __swap(Node &ot) {
swap(sum, ot.sum); swap(cnt, ot.cnt);
pos_l.swap(ot.pos_r); cnt_l.swap(ot.cnt_r);sum_l.swap(ot.sum_r);
pos_r.swap(ot.pos_l);cnt_r.swap(ot.cnt_l); sum_r.swap(ot.sum_l);
}
Node operator + (Node &ot) {
bool sw = false;
if (sum > ot.sum) sw = true, __swap(ot);
Node res;
res.sum = min(sum + ot.sum, INF);
res.cnt = 0;
res.pos_l = pos_l; res.cnt_l = cnt_l; res.sum_l = sum_l;
res.pos_r = ot.pos_r; res.cnt_r = ot.cnt_r; res.sum_r = ot.sum_r;
REP(i, ot.pos_l.size()) if (sum + ot.sum_l[i] < a[ot.pos_l[i]]) {
res.pos_l.push_back(ot.pos_l[i]);
res.cnt_l.push_back(0);
res.sum_l.push_back(min(sum + ot.sum_l[i], INF));
}
sum_r.push_back(sum); ot.sum_l.push_back(ot.sum);
int tl = 0, tr = 0, pos = cnt_l.size();
auto update = [&] (int cnt) {
while (true) {
if (tl < (int) pos_r.size() && sum_r[tl] + ot.sum_l[tr] >= a[pos_r[tl]]) ++tl;
else if (tr < (int) ot.pos_l.size() && sum_r[tl] + ot.sum_l[tr] >= a[ot.pos_l[tr]]) ++tr;
else break;
}
if (tl == (int) pos_r.size()) {
if (tr == (int) ot.pos_l.size()) res.cnt += cnt;
else {
while (res.pos_l[pos] != ot.pos_l[tr]) ++pos;
res.cnt_l[pos] += cnt;
}
}
};
REP(i, (int) pos_r.size()) {
tl = max(tl, i);
update(cnt_r[i]);
}
tl = pos_r.size(); update(cnt);
tl = tr = 0; pos = cnt_l.size();
REP(i, (int) ot.pos_l.size()) {
tr = max(tr, i);
update(ot.cnt_l[i]);
}
tr = ot.pos_l.size(); update(ot.cnt);
sum_r.pop_back(); ot.sum_l.pop_back();
if (sw) {
res.pos_l.swap(res.pos_r);
res.cnt_l.swap(res.cnt_r);
res.sum_l.swap(res.sum_r);
__swap(ot);
}
return res;
};
};
vector <Node> st(4 * n + 23);
function<void(int, int, int)> build = [&] (int i, int l, int r) {
if (r - l == 1) {
st[i] = Node(l);
return;
}
int m = l + r >> 1;
build(i << 1, l, m);
build(i << 1 | 1, m, r);
st[i] = st[i << 1] + st[i << 1 | 1];
};
function<void(int, int, int, int)> update = [&] (int i, int l, int r, int p) {
if (r - l == 1) {
st[i] = Node(l);
return;
}
int m = l + r >> 1;
if (p < m) update(i << 1, l, m, p);
else update(i << 1 | 1, m, r, p);
st[i] = st[i << 1] + st[i << 1 | 1];
};
function<Node(int, int, int, int, int)> get = [&] (int i, int l, int r, int u, int v) {
if (u <= l && r <= v) return st[i];
int m = l + r >> 1;
Node fl, fr;
if (u < m) fl = get(i << 1, l, m, u, v);
if (v > m) fr = get(i << 1 | 1, m, r, u, v);
if (v <= m) return fl;
if (u >= m) return fr;
return fl + fr;
};
build(1, 0, n);
int q; cin >> q;
while (q--) {
int t, l, r; cin >> t >> l >> r; --l;
if (t == 1) {
a[l] = r;
update(1, 0, n, l);
} else cout << get(1, 0, n, l, r).cnt << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("fish2");
// int t; cin >> t; while (t--)
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
fish2.cpp: In lambda function:
fish2.cpp:122:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
122 | int m = l + r >> 1;
| ~~^~~
fish2.cpp: In lambda function:
fish2.cpp:132:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
132 | int m = l + r >> 1;
| ~~^~~
fish2.cpp: In lambda function:
fish2.cpp:139:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
139 | int m = l + r >> 1;
| ~~^~~
fish2.cpp: In function 'int main()':
fish2.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:160:2: note: in expansion of macro 'file'
160 | file("fish2");
| ^~~~
fish2.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:160:2: note: in expansion of macro 'file'
160 | file("fish2");
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |