This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e5 + 7;
const ll oo = 1e18;
ll a[N];
int n;
namespace cover {
pair<char, int> t[N * 4];
char lz[N * 4];
pair<char, int> add(pair<char, int> a, pair<char, int> b) {
if(a.F != b.F) return min(a, b);
a.S += b.S;
return a;
}
void apply(int v, int x) {
t[v].F += x;
lz[v] += x;
}
void push(int v) {
apply(v * 2, lz[v]);
apply(v * 2 + 1, lz[v]);
lz[v] = 0;
}
void build(int v = 1, int tl = 1, int tr = n + 1) {
if(tr - tl == 1) {
t[v] = {0, 1};
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
t[v] = add(t[v * 2], t[v * 2 + 1]);
}
void add(int l, int r, int x, int v = 1, int tl = 1, int tr = n + 1) {
if(l <= tl && tr <= r) {
apply(v, x);
return;
}
push(v);
int tm = (tl + tr) / 2;
if(l < tm) add(l, r, x, v * 2, tl, tm);
if(r > tm) add(l, r, x, v * 2 + 1, tm, tr);
t[v] = add(t[v * 2], t[v * 2 + 1]);
}
pair<char, char> qry(int l, int r, int v = 1, int tl = 1, int tr = n + 1) {
if(l <= tl && tr <= r)
return t[v];
push(v);
int tm = (tl + tr) / 2;
pair<char, int> res = {100, 100};
if(l < tm) res = add(res, qry(l, r, v * 2, tl, tm));
if(r > tm) res = add(res, qry(l, r, v * 2 + 1, tm, tr));
return res;
}
}
namespace seg {
struct node {
V<pair<int, ll>> bad_pref, bad_suff;
V<pi> bad_seg;
ll sum;
} t[N * 4];
void pull(int v) {
for(auto p:t[v * 2].bad_pref)
t[v].bad_pref.PB(p);
for(auto p:t[v * 2 + 1].bad_suff)
t[v].bad_suff.PB(p);
for(auto[pos, sum]:t[v * 2 + 1].bad_pref) if(sum + t[v * 2].sum < a[pos + 1])
t[v].bad_pref.EB(pos, sum + t[v * 2].sum);
for(auto[pos, sum]:t[v * 2].bad_suff) if(sum + t[v * 2 + 1].sum < a[pos - 1])
t[v].bad_suff.EB(pos, sum + t[v * 2 + 1].sum);
for(int i = 0, j = 0; i < SZ(t[v * 2].bad_suff) && j < SZ(t[v * 2 + 1].bad_pref); ) {
auto he = t[v * 2].bad_suff[i], be = t[v * 2 + 1].bad_pref[j];
if(he.S + be.S < min(a[he.F - 1], a[be.F + 1]))
t[v].bad_seg.EB(he.F, be.F);
if(a[he.F - 1] < a[be.F + 1])
i++;
else
j++;
}
t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
}
void build(int v = 1, int tl = 1, int tr = n + 1) {
if(tr - tl == 1) {
t[v].sum = a[tl];
if(a[tl] < a[tl + 1])
t[v].bad_pref.EB(tl, a[tl]);
if(a[tl] < a[tl - 1])
t[v].bad_suff.EB(tl, a[tl]);
if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1])
t[v].bad_seg.EB(tl, tl);
} else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
pull(v);
}
for(auto[l, r]:t[v].bad_seg)
cover::add(l, r + 1, 1);
}
void upd(int pos, int v = 1, int tl = 1, int tr = n + 1) {
for(auto[l, r]:t[v].bad_seg)
cover::add(l, r + 1, -1);
t[v].bad_seg.clear(), t[v].bad_pref.clear(), t[v].bad_suff.clear();
if(tr - tl == 1) {
t[v].sum = a[tl];
if(a[tl] < a[tl + 1])
t[v].bad_pref.EB(tl, a[tl]);
if(a[tl] < a[tl - 1])
t[v].bad_suff.EB(tl, a[tl]);
if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1])
t[v].bad_seg.EB(tl, tl);
} else {
int tm = (tl + tr) / 2;
if(pos - 1 < tm)
upd(pos, v * 2, tl, tm);
if(pos + 1 >= tm)
upd(pos, v * 2 + 1, tm, tr);
pull(v);
}
for(auto[l, r]:t[v].bad_seg)
cover::add(l, r + 1, 1);
}
}
void upd(int pos, ll val) {
a[pos] = val;
seg::upd(pos);
}
signed main()
{
IO_OP;
cin >> n;
a[0] = a[n + 1] = oo;
for(int i = 1; i <= n; i++)
cin >> a[i];
cover::build();
seg::build();
int q;
cin >> q;
while(q--) {
int op;
cin >> op;
if(op == 1) {
int x, y;
cin >> x >> y;
upd(x, y);
} else {
int l, r;
cin >> l >> r;
ll tl = a[l - 1], tr = a[r + 1];
if(l - 1 >= 1) upd(l - 1, 1e15);
if(r + 1 <= n) upd(r + 1, 1e15);
auto p = cover::qry(l, r + 1);
cout << p.S << '\n';
if(l - 1 >= 1) upd(l - 1, tl);
if(r + 1 <= n) upd(r + 1, tr);
}
}
}
Compilation message (stderr)
fish2.cpp: In function 'void seg::pull(int)':
fish2.cpp:86:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for(auto[pos, sum]:t[v * 2 + 1].bad_pref) if(sum + t[v * 2].sum < a[pos + 1])
| ^
fish2.cpp:88:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | for(auto[pos, sum]:t[v * 2].bad_suff) if(sum + t[v * 2 + 1].sum < a[pos - 1])
| ^
fish2.cpp: In function 'void seg::build(int, int, int)':
fish2.cpp:117:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
117 | for(auto[l, r]:t[v].bad_seg)
| ^
fish2.cpp: In function 'void seg::upd(int, int, int, int)':
fish2.cpp:121:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
121 | for(auto[l, r]:t[v].bad_seg)
| ^
fish2.cpp:141:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
141 | for(auto[l, r]:t[v].bad_seg)
| ^
# | 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... |