Untitled

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> using namespace std; struct node { int l,r,maxn,minn,maxc,minc; }; int a[200010]; node tree[800010]; void pushup(node &u,node l,node r) { if (l.maxn == r.maxn) { u.maxn = l.maxn; u.maxc = l.maxc + r.maxc; if (l.minn == r.minn) { u.minn = l.minn; u.minc = l.minc + r.minc; } else if (l.minn > r.minn) { u.minn = l.minn; u.minc = l.minc; } else if (l.minn < r.minn) { u.minn = r.minn; u.minc = r.minc; } } else if (l.maxn > r.maxn) { u.maxn = l.maxn; u.maxc = l.maxc; if (l.minn == r.maxn) { u.minn = l.minn; u.minc = l.minc + r.maxc; } else if (l.minn > r.maxn) { u.minn = l.minn; u.minc = l.minc; } else if (l.minn < r.maxn) { u.minn = r.maxn; u.minc = r.maxc; } } else if (l.maxn < r.maxn) { u.maxn = r.maxn; u.maxc = r.maxc; if (l.maxn == r.minn) { u.minn = l.maxn; u.minc = l.maxc + r.minc; } else if (l.maxn > r.minn) { u.minn = l.maxn; u.minc = l.maxc; } else if (l.maxn < r.minn) { u.minn = r.minn; u.minc = r.minc; } } } void build(int u,int l,int r) { if (l == r) { tree[u] = {l,r,a[l],0,1,0}; return ; } tree[u] = {l,r}; int mid = (l + r) >> 1; build(u << 1,l,mid); build(u << 1 | 1,mid + 1,r); pushup(tree[u],tree[u << 1],tree[u << 1 | 1]); } void update(int u,int x,int y) { if (tree[u].l == tree[u].r) { tree[u] = {tree[u].l,tree[u].r,y,0,1,0}; return ; } int mid = (tree[u].l + tree[u].r) >> 1; if (x <= mid) { update(u << 1,x,y); } else { update(u << 1 | 1,x,y); } pushup(tree[u],tree[u << 1],tree[u << 1 | 1]); } node query(int u,int l,int r) { if (l <= tree[u].l && tree[u].r <= r) { return tree[u]; } int mid = (tree[u].l + tree[u].r) >> 1; if (r <= mid) { return query(u << 1,l,r); } else if (l > mid) { return query(u << 1 | 1,l,r); } else { node t; pushup(t,query(u << 1,l,r),query(u << 1 | 1,l,r)); return t; } } int main() { int n,q; cin >> n >> q; for (int i = 1;i <= n;i++) { cin >> a[i]; } build(1,1,n); while (q--) { int o,x,y; cin >> o >> x >> y; if (o == 1) { update(1,x,y); } else { cout << query(1,x,y).minc << '\n'; } } return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines