7 const int maxn
= 2e5
+ 10, inf
= 1 << 25;
12 int lx
, rx
, mx
, sum
, val
;
13 inline bool dir() { return this == p
->ch
[1]; }
14 inline void setc(node
*, int);
19 node
*nil
= pool
, *tot
= nil
+ 1, *root
;
21 inline void node::setc(node
*c
, int d
) {
26 inline void node::update() {
27 size
= ch
[0]->size
+ 1 + ch
[1]->size
;
28 sum
= ch
[0]->sum
+ val
+ ch
[1]->sum
;
29 lx
= max(ch
[0]->lx
, ch
[0]->sum
+ val
+ max(ch
[1]->lx
, 0));
30 rx
= max(max(ch
[0]->rx
, 0) + val
+ ch
[1]->sum
, ch
[1]->rx
);
31 mx
= max(max(ch
[0]->mx
, ch
[1]->mx
), max(ch
[0]->rx
, 0) + val
+ max(ch
[1]->lx
, 0));
34 inline node
* makenode(int x
) {
35 tot
->ch
[0] = tot
->ch
[1] = tot
->p
= nil
;
37 tot
->lx
= tot
->rx
= tot
->mx
= tot
->sum
= tot
->val
= x
;
41 inline void rotate(node
*u
) {
44 v
->p
->setc(u
, v
->dir());
45 v
->setc(u
->ch
[d
^1], d
);
49 if (v
== root
) root
= u
;
52 inline void splay(node
*u
, node
*target
= nil
) {
53 while (u
->p
!= target
) {
54 if (u
->p
->p
== target
)
57 (u
->dir() == u
->p
->dir()) ? (rotate(u
->p
), rotate(u
)) : (rotate(u
), rotate(u
));
62 inline node
* select(int pos
) {
65 int k
= u
->ch
[0]->size
;
81 nil
->ch
[0] = nil
->ch
[1] = nil
->p
= nil
;
82 nil
->mx
= nil
->lx
= nil
->rx
= nil
->val
= -inf
;
83 root
= makenode(-inf
);
85 for (int i
= 1, x
; i
<= n
; ++i
) {
96 inline void ins(int pos
, int val
) {
97 node
*u
= select(pos
- 1), *v
= makenode(val
);
104 inline void del(int pos
) {
105 node
*u
= select(pos
- 1), *v
= select(pos
+ 1);
112 inline void replace(int pos
, int val
) {
113 node
*u
= select(pos
);
119 inline int query(int l
, int r
) {
120 node
*u
= select(l
- 1), *v
= select(r
+ 1);
134 scanf(" %c%d", &op
, &a
);
146 printf("%d\n", query(a
, b
));