CSES - Shared codeLink to this code:
https://cses.fi/paste/e485eb9a6453cdd2ae1931/
/*
AUTHOR -> KIMJONGOOF (CODEFORCES)
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
const int mod = 1e9 + 7;
const int inf = 1e18;
const int K = 26;
/*
__builtin_popcountll(x): Counts the number of one’s(set bits) in an integer(long/long long).
__builtin_parityll(x): Checks the Parity of a number.Returns true(1) if the number has odd parity(odd number of set bits) else it returns false(0) for even parity(even number of set bits).
__builtin_clzll(x): Counts the leading number of zeros of the integer(long/long long).
__builtin_ctzll(x): Counts the trailing number of zeros of the integer(long/long long).
cout << fixed << setprecision(x) << ans << "\n"; : Use this in case ans is required upto x deciaml precision
kuch nai sooj rha? binary search try kiya ??
*/
// lazy segment tree
struct node
{
int lazy;
int sum;
node()
{
lazy = 0;
sum = -inf;
}
};
node merge(node a, node b)
{
node ans;
ans.sum = max(a.sum, b.sum);
return ans;
}
vector<node> t;
vector<int> pre;
void push(int id, int l, int r)
{
if (t[id].lazy != 0)
{
t[id].sum += (r - l + 1) * t[id].lazy;
if (l != r)
{
t[2 * id].lazy += t[id].lazy;
t[2 * id + 1].lazy += t[id].lazy;
}
t[id].lazy = 0;
}
}
void build(int id, int l, int r)
{
if (l == r)
{
t[id].sum = pre[l];
t[id].lazy = 0;
return;
}
int mid = (l + r) >> 1;
build(2 * id, l, mid);
build(2 * id + 1, mid + 1, r);
t[id] = merge(t[2 * id], t[2 * id + 1]);
}
void update(int id, int l, int r, int lo, int hi, int val)
{
push(id, l, r);
if (lo > r || l > hi)
{
return;
}
if (lo <= l && hi >= r)
{
t[id].lazy += val;
push(id, l, r);
return;
}
int mid = (l + r) >> 1;
update(2 * id, l, mid, lo, hi, val);
update(2 * id + 1, mid + 1, r, lo, hi, val);
t[id] = merge(t[2 * id], t[2 * id + 1]);
}
node query(int id, int l, int r, int lo, int hi)
{
push(id, l, r);
if (lo > r || l > hi)
{
return node();
}
if (lo <= l && hi >= r)
{
return t[id];
}
int mid = (l + r) >> 1;
return merge(query(2 * id, l, mid, lo, hi), query(2 * id + 1, mid + 1, r, lo, hi));
}
int n, q;
vector<int> a;
void solve()
{
cin >> n >> q;
a.assign(n, 0);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
pre.assign(n + 1, 0);
for (int i = 1; i <= n; i++)
{
pre[i] = pre[i - 1] + a[i - 1];
}
t.assign(4 * (n + 1), node());
build(1, 0, n);
while (q--)
{
int tt;
cin >> tt;
if (tt == 1)
{
int k, u;
cin >> k >> u;
int ival = a[k - 1];
int diff = (u - ival);
update(1, 0, n, k, n, diff);
}
else
{
int l, r;
cin >> l >> r;
cout << query(1, 0, n, l - 1, r).sum - query(1, 0, n, l - 1, l - 1).sum << "\n";
}
}
/*for (int i = 1; i <= n; i++)
{
cout << query(1, 1, n, i, i).sum << " ";
}
cout << "\n";*/
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int _ = 1;
// cin >> _;
for (int __ = 1; __ <= _; __++)
{
solve();
}
}