codeforces Destroying Array 线段树维护区间
来源:互联网 发布:汇天下p2p源码 编辑:程序博客网 时间:2024/05/21 08:01
/* 题目描述:给出n个数的序列a[1] , a[2] , ... ,a[n] ,每次将其中的一个数破坏掉,每一次破坏掉以后问连续数 的最大值是多少 方法:利用线段树维护,线段树的每一个节点(l , r , x)包含4个信息:(l , r)中的总和,(l , r)中的最大值,(l , r)中最大前缀, (l , r)中最大后缀,每次修改后输出t[1].summax(1节点的最大值)即可,每次更新的pushup具体操作见代码*/#pragma warning(disable:4786)#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<cmath>#include<string>#include<sstream>#define LL long long#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define mem(a,x) memset(a,x,sizeof(a))#define lson l,m,x<<1#define rson m+1,r,x<<1|1using namespace std;const LL INF = 1e15 + 5 ;const int mod = 1e9 + 7;const double PI = acos(-1.0);const double eps=1e-6;const int maxn = 1e5 + 5 ;struct node{ LL sum , summax , lmax , rmax ;}t[4 * maxn];LL a[maxn] ;int op[maxn] ;LL Max(const LL a , const LL b){ return a > b ? a : b ;}void pushup(int l , int r , int x){ t[x].sum = t[x<<1].sum + t[x << 1 | 1].sum ; //总和等于左右两节点总和 t[x].summax = Max(t[x<<1].summax , Max(t[x<<1|1].summax , t[x<<1].rmax + t[x <<1 | 1].lmax) ) ; //最大值的三种可能:左子节点最大值,右子节点最大值,左子节点最大后缀+右子节点最大前缀 t[x].lmax = Max(t[x<<1].sum + t[x<<1|1].lmax , t[x<<1].lmax ) ; //最大前缀二种可能:左子节点最大前缀 , 左子节点总和+右子节点最大前缀 t[x].rmax = Max( t[x<<1].rmax + t[x<<1|1].sum , t[x<<1|1].rmax ) ; //最大后缀二种可能:右子节点最大后缀 , 右子节点总和+左子节点最大后缀 if(t[x].sum < 0) t[x].sum = -INF; //<0就置于-INF是因为害怕-INF加多了会爆LL型 if(t[x].summax < 0) t[x].summax = -INF; if(t[x].lmax < 0) t[x].lmax = -INF; if(t[x].rmax < 0) t[x].rmax = -INF;}void build(int l , int r , int x){ if(l == r){ t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = a[l] ; return ; } int m = l + (r - l ) / 2 ; build(lson); build(rson); pushup(l , r , x);}void modify(int pos , LL val , int l , int r , int x){ if(pos==l && l == r){ t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = val ; return ; } int m = l + (r - l) / 2; if(pos <= m) modify(pos , val , lson); else modify(pos , val , rson); pushup(l , r , x) ;}int main(){ int n ; scanf("%d",&n); for(int i = 1 ; i<= n ; i++) scanf("%lld",&a[i]); for(int i = 1 ; i<= n ; i++) scanf("%d",&op[i]); build(1 , n , 1 ); for(int i = 1 ; i <= n ; i++){ modify( op[i] , -INF , 1 , n , 1 ) ; LL ans = t[1].summax ; if(ans > 0) printf("%lld\n",ans); else printf("0\n"); } return 0;}
0 0
- codeforces Destroying Array 线段树维护区间
- codeforces 722C Destroying Array 【线段树好题】
- CodeForces 722C Destroying Array(数组删除区间最大值)
- 722C - Destroying Array 线段树
- Destroying Array CodeForces
- Codeforces 718C Sasha and Array(线段树维护矩阵)
- codeforces 482B. Interesting Array【线段树区间更新】
- codeforces 482 B Interesting Array 线段树区间更新
- 线段树区间维护upcoj
- 线段树区间维护hdu3308
- 线段树区间维护hdu3397
- 线段树区间维护cf46D
- POJ3468 线段树区间维护
- codeforces 722C Destroying Array
- codeforces(722c)-Destroying Array
- codeforces-772C-Destroying Array
- Codeforces 722C. Destroying Array
- codeforces 722C. Destroying Array
- 从功能模块角度看ARM Cortex-M各处理器区别
- window环境中0.python1.Numpy 2. Scipy 3. Matplotlib 4. Scikit-Learn的安装
- ucos-II(四)
- Linux线程3种同步方法
- Exchange Online 邮件迁移最佳实践(二)
- codeforces Destroying Array 线段树维护区间
- 图像偏色检测算法,速度快,效果好,共享给大家。
- android ScrollView嵌套viewpager,viewpager嵌套gridview,解决内嵌无法显示的问题
- ubuntu下安装flash player
- 从性能指标角度看ARM Cortex-M各处理器区别
- Decode String(leetcode),solved by DFS
- 剑指offer之顺时针打印矩阵
- 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
- 角点绘制2