pku2750
//参考:
http://hi.baidu.com/fandywang_jlu/blog/item/505b40f4c864bddff3d38574.html
http://blog.csdn.net/ldyhot/archive/2008/10/29/3173535.aspx
![pku2750_第1张图片](http://img.e-com-net.com/image/product/48d8a3ed6a354169804f1f4263cc5f4a.jpg)
1
/**/
/*
2
线段树+DP
3
出题者的简单解题报告:把环从一个地方,切断拉成一条直线,
4
用线段树记录当前区间的非空最大子列和当前区间的非空最小
5
子列。如果环上的数都是正整数,答案是:环上数的总和-根
6
结点的非空最小子列;否则,答案是:max{根结点的非空最大
7
子列, 环上数的总和-根结点的非空最小子列},每次问答的
8
复杂度是O(logN)。
9
*/
10![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
#include
<
iostream
>
12
using
namespace
std;
13
#define
MAXN 100000
14
#define
MAXM 100000
15
#define
MAX 262145
16![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
17
struct
Node
18![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
19
int left;
20
int right;
21
int sum; //该区间 数的总和
22
int max,min; //该区间 最大子列和 与 最小子列和
23
int lmax,rmax; //该区间 从左端点开始的最大子列和 与 到右端点结束的最小子列和
24
int lmin,rmin; //该区间 从左端点开始的最小子列和 与 到右端点结束的最小子列和
25
}
;
26![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
27
Node segtree[MAX];
28
int
value[MAXN
+
1
];
29
//
int vi;
30
31
void
update (
int
v)
32![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
33
segtree[v].sum=segtree[2*v].sum+segtree[2*v+1].sum;
34
segtree[v].max=max(max(segtree[2*v].max,segtree[2*v+1].max),segtree[2*v].rmax+segtree[2*v+1].lmax);
35
segtree[v].min=min(min(segtree[2*v].min,segtree[2*v+1].min),segtree[2*v].rmin+segtree[2*v+1].lmin);
36
segtree[v].lmax=max(segtree[2*v].lmax, segtree[2*v].sum+segtree[2*v+1].lmax);
37
segtree[v].rmax=max(segtree[2*v+1].rmax, segtree[2*v+1].sum+segtree[2*v].rmax);
38
segtree[v].lmin=min(segtree[2*v].lmin,segtree[2*v].sum+segtree[2*v+1].lmin);
39
segtree[v].rmin=min(segtree[2*v+1].rmin,segtree[2*v+1].sum+segtree[2*v].rmin);
40
}
41![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
42
void
build(
int
v,
int
l,
int
r)
43![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
44
segtree[v].left=l;
45
segtree[v].right=r;
46![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
47
if(l == r )
48![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
{
49
segtree[v].sum =
50
segtree[v].max = segtree[v].min =
51
segtree[v].lmax = segtree[v].rmax =
52
segtree[v].lmin = segtree[v].rmin =value[l];
53
54
}else
55![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
{
56
57
int mid=(segtree[v].left+segtree[v].right)>>1;
58
build(2*v,l,mid);
59
build(2*v+1,mid+1,r);
60
update(v);
61
}
62
63
}
64![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
65
void
insert(
int
v,
int
index)
66![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
67
if( segtree[v].left == segtree[v].right && segtree[v].left == index)
68![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
{
69
segtree[v].sum =
70
segtree[v].max = segtree[v].min =
71
segtree[v].lmax = segtree[v].rmax =
72
segtree[v].lmin = segtree[v].rmin =value[index];
73
}else
74![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
{
75
int mid=(segtree[v].left+segtree[v].right)>>1;
76
if(index <= mid) insert(2*v,index);
77
else insert(2*v+1,index);
78
//if(index <= segtree[2*v].right) insert(2*v,index);
79
//else if(index >= segtree[2*v+1].left) insert(2*v+1,index);
80
update(v);
81
}
82
}
83![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
84
int
main()
85![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
86
int n,k,i,index;
87
scanf("%d",&n);
88
for(i=1;i<=n;i++)
89
scanf("%d",&value[i]);
90![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
91
build(1,1,n);
92![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
93
scanf("%d",&k);
94
while(k--)
95![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
{
96
scanf("%d",&index);
97
scanf("%d",&value[index]);
98
insert(1,index);
99
if(segtree[1].sum==segtree[1].max)
100
printf("%d\n",segtree[1].sum-segtree[1].min);
101
else printf("%d\n",max(segtree[1].max,segtree[1].sum-segtree[1].min));
102
}
103
return 0;
104
}
105
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
2
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
3
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
4
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
5
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
6
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
7
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
8
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
9
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
10
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
12
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
17
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
18
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
19
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
20
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
21
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
22
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
23
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
24
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
25
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
26
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
27
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
28
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
29
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
30
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
31
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
32
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
33
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
34
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
35
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
37
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
39
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
40
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
41
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
42
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
43
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
44
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
45
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
46
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
47
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
48
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
49
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
50
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
51
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
52
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
53
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
54
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
55
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
56
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
57
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
58
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
59
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
60
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
61
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
62
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
63
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
64
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
65
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
66
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
67
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
68
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
69
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
70
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
71
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
72
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
73
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
74
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
75
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
76
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
77
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
78
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
79
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
80
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
81
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
82
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
83
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
84
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
85
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
86
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
87
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
88
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
89
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
90
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
91
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
92
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
93
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
94
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
95
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
96
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
97
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
98
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
99
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
100
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
101
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
102
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
103
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
104
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
105
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)