继续种树哈~~
类似之前做的hdu的LCIS,不过这个操作是,某个区间加上一个值。
基本操作和之前那个LCIS差不多,只不多我LCIS结点里没存左右端点的值,直接映射到数组了,但是这个不存的话会很麻烦。。
所以纠结了一会,后来把端点信息存到结点里,改变的时候向上更新就没问题啦。
比LCIS增加了结点信息,这个区间整体加上的值,以及这个区间的左右的端点值。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
using namespace std;
const int MAX = 100010;
int a[MAX];
struct Tnode{ // 一维线段树
int l,r,lval, rval, max, add, ll, rr;
int len() { return r - l;}
int mid() { return MID(l,r);}
bool in(int ll,int rr) { return l >= ll && r <= rr; }
void lr(int ll,int rr){ l = ll; r = rr;}
};
Tnode node[MAX<<2];
void Updata_val(int t)
{
node[t].ll = node[L(t)].ll;
node[t].rr = node[R(t)].rr;
node[t].lval = node[L(t)].lval;
if( node[L(t)].lval == node[L(t)].len()
&& node[R(t)].ll > node[L(t)].rr )
node[t].lval += node[R(t)].lval;
node[t].rval = node[R(t)].rval;
if( node[R(t)].rval == node[R(t)].len()
&& node[R(t)].ll > node[L(t)].rr )
node[t].rval += node[L(t)].rval;
int mval = 0;
if( node[R(t)].ll > node[L(t)].rr )
mval = node[L(t)].rval + node[R(t)].lval;
node[t].max = max(mval, max(node[L(t)].max, node[R(t)].max));
node[t].max = max(node[t].max, max(node[t].lval, node[t].rval));
}
void Build(int t,int l,int r)
{
node[t].add = node[t].lval = node[t].rval = node[t].max = 0;
node[t].lr(l,r);
if( node[t].len() == 1 )
{
node[t].lval = node[t].rval = node[t].max = 1;
node[t].ll = node[t].rr = a[node[t].l];
return ;
}
int mid = MID(l,r);
Build(L(t),l,mid);
Build(R(t),mid,r);
Updata_val(t);
}
void Updata_llrr(int t, int add)
{
node[t].ll += add;
node[t].rr += add;
}
void Push_down(int t)
{
if( node[t].len() == 1 )
{
node[t].add = 0;
return ;
}
if( node[t].add )
{
node[L(t)].add += node[t].add;
Updata_llrr( L(t), node[t].add );
node[R(t)].add += node[t].add;
Updata_llrr( R(t), node[t].add);
node[t].add = 0;
}
}
void Updata(int t,int l,int r,int val)
{
if( node[t].in(l,r) )
{
node[t].add += val;
Updata_llrr(t, val);
Push_down(t);
return ;
}
if( node[t].len() == 1 ) return ;
Push_down(t);
int mid = node[t].mid();
if( l < mid ) Updata(L(t),l,r,val);
if( r > mid ) Updata(R(t),l,r,val);
Updata_val(t);
}
int Query(int t,int l,int r)
{
if( node[t].in(l,r) )
return node[t].max;
if( node[t].len() == 1 ) return 0;
Push_down(t);
int mid = node[t].mid();
int ans = 0;
if( r <= mid )
ans = max(ans, Query(L(t),l,r));
else
if( l >= mid )
ans = max(ans, Query(R(t),l,r));
else
{
ans = max(ans, Query(L(t),l,r));
ans = max(ans, Query(R(t),l,r));
int ll, rr;
if( node[L(t)].r - node[L(t)].rval > l )
ll = node[L(t)].rval;
else
ll = node[L(t)].r - l;
if( node[R(t)].l + node[R(t)].lval > r )
rr = r - node[R(t)].l;
else
rr = node[R(t)].lval;
ans = max(ans, ll);
ans = max(ans, rr);
if( node[R(t)].ll > node[L(t)].rr )
{
int mval = rr + ll;
ans = max(ans, mval);
}
}
Updata_val(t);
return ans;
}
int main()
{
int ncases, n, m, x, y, val, ind = 1;
char op[5];
scanf("%d", &ncases);
while( ncases-- )
{
scanf("%d%d", &n, &m);
FOR(i, 0, n)
scanf("%d", &a[i]);
Build(1, 0, n);
printf("Case #%d:\n", ind++);
while( m-- )
{
scanf("%s", op);
if( op[0] == 'a' )
{
scanf("%d%d%d", &x, &y, &val);
Updata(1, x-1, y, val);
}
else
{
scanf("%d%d", &x, &y);
int ans = Query(1, x-1, y);
printf("%d\n", ans);
}
}
}
return 0;
}