cf935f(线段树+思维)

首先题目主要关注|a[i]-a[i+1]|,所以很容易想到得先进行差分。。然后操作2就直接变成单点操作了,很舒服。。

然后对操作1,主要要判断要在哪个点加x了。。

貌似非常麻烦的样子。。然后参考了下q巨的解法。。竟然是构造分段函数。。。orz

考虑到对某点+x只影响相邻的2个差值,所以可以运用分段函数处理+x后改变的值。。

设差分后的数组为c(c[i]=a[i]-a[i-1]),改变点i,f(a)改变值t=|c[i]+x|+|c[i+1]-x|-|c[i]|-|c[i+1]|

非常像学不等式选讲的函数。。最低处为水平线,值为min=|c[i]+c[i+1]|-|c[i]|-|c[i+1]|(三角不等式)

然后代点求出另外2条一次函数的截距就可以了。。

然后q巨教会窝萌。。想这个函数直接把x代入3个表达式求最大值就可以了。。orz!!!

这样的话函数和函数之间就可以合并了。。对每一段函数直接比较截距的最大值就可以了。。这样可以很优雅地选出f(a)的最大增量。。

合并时当然要用线段树。。然而要注意1和n这2点改变时只改变一个c,需要特判。。。




/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include
#include
#include
#include
#include
#include
#include
#include
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<>1)
#define NM 300005
#define nm 100000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}








int n,m,_x,_y;
ll _t,ans,s,c[NM];
struct node{
	node*l,*r;
	int x,y;ll m,a,b;
	void upd(){m=max(l->m,r->m);a=max(l->a,r->a);b=max(l->b,r->b);}
	void getf(){m=abs(c[x]+c[x+1])-abs(c[x])-abs(c[x+1]);a=m+2*min(-c[x],c[x+1]);b=(m-2*max(-c[x],c[x+1]));}
	node(int x,int y,node*l=0,node*r=0):x(x),y(y),l(l),r(r){getf();if(l)upd();}
	ll get(ll x){return max({-2*x+a,m,2*x+b});}
	node*query(){
		if(_x<=x&&y<=_y)return this;
		if(_y<=mid)return l->query();
		if(_x>mid)return r->query();
		return new node(x,y,l->query(),r->query());
	}
	void mod(){
		if(x==y){getf();return;}
		if(_x<=mid)l->mod();else r->mod();upd();
	}
}*root;
node*build(int x,int y){return x==y?new node(x,y):new node(x,y,build(x,mid),build(mid+1,y));}

int main(){
	//freopen("data.in","r",stdin);
	n=read();
	inc(i,1,n)c[i]=read();
	dec(i,n,1)c[i]-=c[i-1];
	inc(i,2,n)s+=abs(c[i]);
	root=build(2,n-1);
	inc(i,1,n){_x=i;root->mod();}
	m=read();
	while(m--){
		int opt=read();_x=read();_y=read();_t=read();
		if(opt==1){
			ans=0;
			if(_x==1)ans=max(ans,s+abs(c[1]-_t)-abs(c[1])),_x++;
			if(_y==n)ans=max(ans,s+abs(c[n]+_t)-abs(c[n])),_y--;
			if(_x<=_y){
				node*r=root->query();
				ans=max(ans,s+r->get(_t));
			}
			printf("%I64d\n",ans);
		}else{
			if(_x>1)s+=abs(c[_x]+_t)-abs(c[_x]);if(_y1)root->mod();
			_x--;if(_x>1)root->mod();
			_x=_y+1;_t=-_t;
			if(_xmod();
			_x--;if(_x>1&&_xmod();
		}
	}
	return 0;
}







F. Fafa and Array
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Fafa has an array A of n positive integers, the function f(A) is defined as . He wants to do q queries of two types:

  • 1 l r x — find the maximum possible value of f(A), if x is to be added to one element in the range [l,  r]. You can choose to which element to add x.
  • 2 l r x — increase all the elements in the range [l,  r] by value x.

Note that queries of type 1 don't affect the array elements.

Input

The first line contains one integer n (3 ≤ n ≤ 105) — the length of the array.

The second line contains n positive integers a1, a2, ..., an (0 < ai ≤ 109) — the array elements.

The third line contains an integer q (1 ≤ q ≤ 105) — the number of queries.

Then q lines follow, line i describes the i-th query and contains four integers tilirixi .

It is guaranteed that at least one of the queries is of type 1.

Output

For each query of type 1, print the answer to the query.

Examples
Input
Copy
5
1 1 1 1 1
5
1 2 4 1
2 2 3 1
2 4 4 2
2 3 4 1
1 3 3 2
Output
2
8
Input
Copy
5
1 2 3 4 5
4
1 2 4 2
2 2 4 1
2 3 4 1
1 2 4 2
Output
6
10


你可能感兴趣的:(其他算法,线段树)