Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef long long ll;
- const int maxn = 50005;
- const int INF = 2e9;
- struct node_data {
- ll sum, prefix, suffix, res;
- };
- int n, a[maxn];
- node_data merge_nodes(node_data A, node_data B) {
- node_data C;
- C.sum = A.sum + B.sum;
- C.prefix = max(A.prefix, A.sum + B.prefix);
- C.suffix = max(B.suffix, B.sum + A.suffix);
- C.res = max(C.prefix, max(C.suffix, max(A.res, max(B.res, A.suffix + B.prefix))));
- return C;
- }
- node_data make_node(int value) {
- node_data C;
- C.sum = value;
- C.prefix = max(-INF, value);
- C.suffix = max(-INF, value);
- C.res = max(-INF, value);
- return C;
- }
- node_data segment_tree[3 * maxn];
- void build_tree(int L, int R, int node) {
- if(L == R) {
- segment_tree[node] = make_node(a[L]);
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * node);
- build_tree(middle + 1, R, 2 * node + 1);
- segment_tree[node] = merge_nodes(segment_tree[2 * node], segment_tree[2 * node + 1]);
- }
- }
- //L R i L R j L r
- node_data query(int i, int j, int L, int R, int node) {
- if(i <= L and R <= j) {
- return segment_tree[node];
- }
- if(R < i or j < L) {
- return make_node(-INF);
- }
- int middle = (L + R) / 2;
- return merge_nodes(query(i, j, L, middle, 2 * node), query(i, j, middle + 1, R, 2 * node + 1));
- }
- void update(int idx, int new_value, int L, int R, int node) {
- if(L == R) {
- segment_tree[node] = make_node(new_value);
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(idx, new_value, L, middle, 2 * node);
- }
- else {
- update(idx, new_value, middle + 1, R, 2 * node + 1);
- }
- segment_tree[node] = merge_nodes(segment_tree[2 * node], segment_tree[2 * node + 1]);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build_tree(0, n - 1, 1);
- int q;
- cin >> q;
- for(int i = 0; i < q; i++) {
- int type;
- cin >> type;
- if(type == 0) {
- int x, y;
- cin >> x >> y;
- update(x - 1, y, 0, n - 1, 1);
- }
- else {
- int x, y;
- cin >> x >> y;
- cout << query(x - 1, y - 1, 0, n - 1, 1).res << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment