您的位置:首页 > 其它

poj_2750 Potted Flower(环形区间+线段树+单点更新)

2017-01-15 17:02 661 查看
Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4775 Accepted: 1820
DescriptionThe little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer number (possibly negative) denotinghow attractive it is. See the following graph as an example:(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.)The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot bea total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest amongall possible constructions.Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which meanschanging the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction.InputThere will be a single test data in the input. You are given an integer N (4 <= N <= 100000) in the first input line.The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position.A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above.Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer.OutputFor each instruction, output a single line with the maximum sum of attractive values for the optimum cane-chair.Sample Input
5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1
Sample Output
4
4
3
5
求环形区间中长度最小为1,最大为n-1的最大连续和。
——————————
可以转化为求普通区间的 最大连续和 与 区间总和减去最小连续和,取两者较大者。
不过由于长度不能为n,所以当所有值都为整数时,即当最大连续和长度为n时,要减去区间最小值。
同样的,由于长度最小为1,所以当最大连续和长度为0时,最大连续和只能为区间最大值。
——————————
这样我们构造的线段树就得维护 区间总和、 区间最大连续和,区间最小连续和,区间最大最小值。
为得出最大最小连续和,还得维护 区间最大最小左/右连续和 来辅助。
区间最大左连续和 = Max(左孩子区间总和+右孩子区间最大左连续和,左孩子区间最大左连续和),其他同理。
则 区间最大连续和 = Max(左孩子区间最大连续和,右孩子区间最大连续和,左孩子区间最大右连续和+右孩子区间最大左连续和)。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long long#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;struct Node{int l, r;int ma, mi; //最大值、最小值int s_ma, s_mi; //连续最大值、连续最小和int ls_ma, ls_mi; //左连续最大值、左连续最小和int rs_ma, rs_mi; //右连续最大值、右连续最小和int sum; //所有数之和int s_p; //正数个数int mid(){ return l+r>>1; }}tree[maxn<<4];void build(int l, int r, int rt){tree[rt].l = l, tree[rt].r = r;if(l == r) return ;int m = l+r>>1;build(lson);build(rson);}void update(int pos, int val, int rt){if(tree[rt].l == tree[rt].r){tree[rt].sum = tree[rt].ma = tree[rt].mi = val;tree[rt].s_ma = tree[rt].ls_ma = tree[rt].rs_ma = val>0 ? val : 0;tree[rt].s_mi = tree[rt].ls_mi = tree[rt].rs_mi = val<0 ? val : 0;tree[rt].s_p = val>0 ? 1 : 0;return ;}int m = tree[rt].mid();if(pos <= m) update(pos, val, rt<<1);else update(pos, val, rt<<1|1);tree[rt].ma = max(tree[rt<<1].ma, tree[rt<<1|1].ma);tree[rt].mi = min(tree[rt<<1].mi, tree[rt<<1|1].mi);tree[rt].ls_ma = max(tree[rt<<1].ls_ma, tree[rt<<1].sum + tree[rt<<1|1].ls_ma);tree[rt].ls_mi = min(tree[rt<<1].ls_mi, tree[rt<<1].sum + tree[rt<<1|1].ls_mi);tree[rt].rs_ma = max(tree[rt<<1|1].rs_ma, tree[rt<<1|1].sum + tree[rt<<1].rs_ma);tree[rt].rs_mi = min(tree[rt<<1|1].rs_mi, tree[rt<<1|1].sum + tree[rt<<1].rs_mi);tree[rt].s_ma = max(tree[rt<<1].rs_ma + tree[rt<<1|1].ls_ma, max(tree[rt<<1].s_ma, tree[rt<<1|1].s_ma));tree[rt].s_mi = min(tree[rt<<1].rs_mi + tree[rt<<1|1].ls_mi, min(tree[rt<<1].s_mi, tree[rt<<1|1].s_mi));tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;tree[rt].s_p = tree[rt<<1].s_p + tree[rt<<1|1].s_p;}int n, m;int main(){while(~scanf("%d", &n)){build(1, n, 1);int pos, val;for(int i = 1; i <= n; i++){scanf("%d", &val);update(i, val, 1);}scanf("%d", &m);for(int i = 1; i <= m; i++){scanf("%d%d", &pos, &val);update(pos, val, 1);if(tree[1].s_p == n) printf("%d\n", tree[1].sum - tree[1].mi);else if(tree[1].s_p == 0) printf("%d\n", tree[1].ma);else printf("%d\n", max(tree[1].s_ma, tree[1].sum-tree[1].s_mi));}}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: