CodeForces - 935F Fafa and Array —— 线段树

2024-04-07 00:58

本文主要是介绍CodeForces - 935F Fafa and Array —— 线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Fafa has an array A of n positive integers, the function f(A) is defined as ∑ i = 1 n − 1 ∣ a i − a i + 1 ∣ \displaystyle\sum_{i=1}^{n-1} |a_{i}-a_{i+1}| i=1n1aiai+1. 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 ti li ri xi .

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
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
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

题意:

给你n个数,对于f(a)的定义见题目,给你两个操作,1 l r x 代表你可以在l到r之间选一个数加上x,让f(a)最大,或者可以不加,2 l r x 代表在l到r之间的所有数都加上x。

题解:

对于第一个操作,我们可以知道,如果 a i − 1 − a i a_{i-1}-a_i ai1ai>0的时候,增加ai会减小f(a),在 a i − a i + 1 a_i-a_{i+1} aiai+1<0的时候,增加ai会减小f(a),那么我们只要维护当 a i − 1 − a i a_{i-1}-a_i ai1ai>0, a i − a i + 1 a_i-a_{i+1} aiai+1<0的时候的和,之后若是小于x,那么就是ans+2*(x-sum).第二个操作,改变的只是l,l-1,r,r+1范围的值,稍微修改一下即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
ll a[N],de[N],sum[N<<2];
void update(int l,int r,int root,int pos)
{if(l==r){ll ans=0;if(de[pos-1]>0)ans+=de[pos-1];if(de[pos]<0)ans-=de[pos];sum[root]=ans;return ;}int mid=l+r>>1;if(mid>=pos)update(l,mid,root<<1,pos);elseupdate(mid+1,r,root<<1|1,pos);sum[root]=min(sum[root<<1],sum[root<<1|1]);
}
ll query(int l,int r,int root,int ql,int qr)
{if(l>=ql&&r<=qr)return sum[root];int mid=l+r>>1;ll ans=1e17;if(mid>=ql)ans=query(l,mid,root<<1,ql,qr);if(mid<qr)ans=min(ans,query(mid+1,r,root<<1|1,ql,qr));return ans;
}
int main()
{int n,m;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);ll all=0;for(int i=1;i<n;i++){de[i]=a[i]-a[i+1];all+=abs(de[i]);if(i>1)update(1,n,1,i);}int op,l,r;ll x;scanf("%d",&m);while(m--){scanf("%d%d%d%lld",&op,&l,&r,&x);if(op==1){if(l==r)printf("%lld\n",all-abs(de[l-1])-abs(de[l])+abs(de[l-1]-x)+abs(de[l]+x));else{ll ans=query(1,n,1,l,r);if(ans>x)printf("%lld\n",all);elseprintf("%lld\n",all+2*(x-ans));}}else{all-=(abs(de[l-1])+abs(de[r]));de[l-1]-=x,de[r]+=x;all+=(abs(de[l-1])+abs(de[r]));update(1,n,1,l),update(1,n,1,r);if(l-1>1)update(1,n,1,l-1);if(r+1<n)update(1,n,1,r+1);}}return 0;
}

这篇关于CodeForces - 935F Fafa and Array —— 线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881273

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m