JDOJ-1260: VIJOS-P1083 Little White Walks in the Park

1260: VIJOS-P1083 Xiaobai Walking in the Park

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 329  Solved: 94
[Submit][Status][Web Board]

Description

        Xiaoxin often accompanies Xiaobai to play in the park, which is the so-called dog walking... There is a "Park Road" near Xiaoxin's house. There are n parks lined up on one side of the road from south to north. Xiaobai has long seen flowers. I don't know which parks to go to. At the beginning, Xiaobai gave each park a score based on the park's scenery. In order to save trouble, Xiaoxin will prescribe a range in advance every time he walks his dog. Xiaobai can only choose between the ath and bth parks (including the two parks a and b) and choose some consecutive parks to play. Of course, Xiaobai hopes that the total score of the selected parks will be as high as possible. At the same time, because the landscape of some parks will change, Xiaobai's score may also change. So, please come and help Xiaobai choose a park.

Input

        The first line, two integers N and M, represent the number of parks and the total number of operations (walking the dog or changing the score), respectively. The next N lines, each with an integer, give Xiaobai's score for the park at the beginning. Next M lines, each with three integers. The first integer K, 1 or 2. K=1 means that Xiaoxin wants to take Xiaobai out to play, and the next two integers a and b give the range of choosing the park (1≤a, b≤N); K=2 means that Xiaobai has changed the right The score of a certain park, the next two integers p and s, means that Xiaobai's score for the pth park becomes s (1≤p≤N). Among them, 1≤N≤500 000, 1≤M≤100 000, all scores are integers whose absolute value does not exceed 1000.

Output

        Every time Xiaobai goes out to play, he will output a line corresponding to only one integer, which represents the maximum value of the sum of the park scores that Xiaobai can choose.

Sample Input

5 3 1 2 -3 4 5 1 2 3 2 2 -1 1 2 3

Sample Output

2 -1
Summary: line segment tree, maintains 4 values ​​of the continuous maximum sum on the left of mxl, the continuous maximum sum on the right of mxr, the maximum continuous sum of the val interval, and the sum of the interval
Single-point modification, pay attention to the query, two complete intervals are queried but they are not the same parent node, and the message needs to be merged
#include<bits/stdc++.h>

using namespace std;
const int maxn = 2000005;

#define inf 10000007
int val[maxn], n, m, opt, a, b, A[maxn];
int mxl[maxn], mxr[maxn], sum[maxn];

struct Node{
	int s, m, lm, rm;
} ;
void update(int o, int l, int r) {
	sum[o] = sum[o << 1] + sum[o << 1 | 1];
	val[o] = max(val[o << 1], max(val[o << 1 | 1], mxr[o << 1] + mxl[o << 1 | 1]));
	mxl[o] = max(mxl[o << 1], sum[o << 1] + mxl[o << 1 | 1]);
	mxr[o] = max(mxr[o << 1 | 1], sum[o << 1 | 1] + mxr[o << 1]);
}
void build(int o, int l, int r) {
	if(l == r) {
		val [o] = mxl [o] = mxr [o] = A [l];
		sum[o] = A[l]; return;
	} int mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	update(o, l, r);
}
void modify (int o, int l, int r, int ql, int c) {
	if(l == r) {
		val [o] = mxl [o] = mxr [o] = c;
		sum[o] = c; return;
	} int mid = (l + r) >> 1;
	if(ql <= mid) modify(o << 1, l, mid, ql, c);
	else modify(o << 1 | 1, mid + 1, r, ql, c);
	update(o, l, r);
}
Query node (int o, int l, int r, int ql, int qr) {
	if(l == ql && r == qr) {
		Node a; a.m = val[o];
		as = sum [o]; a.lm = mxl [o]; a.rm = mxr [o];
		return a;
	}
	int mid = (l + r) >> 1;
	if(qr <= mid) return query(o << 1, l, mid, ql, qr);
	else if(ql > mid) return query(o << 1 | 1, mid + 1, r, ql, qr);
	else {
		Node t1 = query(o << 1, l, mid, ql, mid);
		Node t2 = query(o << 1 | 1, mid + 1, r, mid + 1, qr);
		Node a;
		a.m = max(max(t1.m, t2.m), t1.rm + t2.lm);
		a.rm = max(t2.rm, t1.rm + t2.s);
		a.lm = max(t1.lm, t1.s + t2.lm);
		return a;
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) scanf("%d", &A[i]);
	build(1, 1, n);
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d", &opt, &a, &b);
		if(opt == 1) printf("%d\n", query(1, 1, n, min(a, b), max(a, b)).m);
		else modify(1, 1, n, a, b);
	}
	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324907581&siteId=291194637