#include <bits/stdc++.h>
#define ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const ll inf = 5e18;
const int N = 1e5 + 5;
struct node {
int tl, tr;
ll pref, suf, ans, l, r;
node(ll _v=-inf, int _tl=0, int _tr=0) : tl(_tl), tr(_tr) {
pref = suf = ans = 1;
l = r = _v;
}
};
node merge(node a, node b) {
if(a.l == -inf) return b;
if(b.l == -inf) return a;
node res;
res.pref = a.pref;
res.suf = b.suf;
res.tl = a.tl;
res.tr = b.tr;
res.l = a.l;
res.r = b.r;
res.ans = max(a.ans, b.ans);
if(a.r == b.l) res.ans = max(res.ans, a.suf + b.pref);
if(a.pref == a.tr-a.tl+1 && a.r == b.l) res.pref = a.pref + b.pref;
if(b.suf == b.tr-b.tl+1 && a.r == b.l) res.suf = b.suf + b.suf;
return res;
}
struct segment_tree {
int n;
vector<node> tree;
segment_tree(int _n) : n(_n), tree(4*n) {}
void update(int u, int tl, int tr, int p, int v) {
if(tl == tr) {
tree[u] = node(v, p, p);
} else {
int tm = (tl + tr) / 2;
if(p <= tm) update(2*u, tl, tm, p, v);
else update(2*u+1, tm+1, tr, p, v);
tree[u] = merge(tree[2*u], tree[2*u+1]);
}
// cout << tl << " " << tr << ": " << '\n';
// cout << tree[u].ans << " " << tree[u].l << " " << tree[u].r << endl;
}
node query(int u, int tl, int tr, int l, int r) {
if(tl > r || l > tr) return node();
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return merge( query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r) );
}
void update(int p, int v) { update(1, 1, n, p, v); }
node query(int l, int r) { return query(1, 1, n, l, r); }
};
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q; cin >> n >> q;
vector<ll> a(n+1);
segment_tree sgt(n-1);
for(int i=1; i<=n; i++) {
cin >> a[i];
if(i > 1) sgt.update(i-1, a[i] - a[i-1]);
}
while(q--) {
int t, l, r; cin >> t >> l >> r;
if(t == 3) {
if(l == r) {
cout << 1 << '\n';
continue;
}
cout << sgt.query(l, r-1).ans + 1 << '\n';
}
}
}
# | 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... |