Codeforces Round #381 (Div. 2) E. Alyona and towers
来源:互联网 发布:ubuntu paichu fuzhi 编辑:程序博客网 时间:2024/06/06 13:58
E. Alyona and towers
Description
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from
Let the sequence
After each addition of
Input
The first line contain single integer
The second line contain n integers
The third line contain single integer
The next m lines contain 3 integers each. The i-th of these lines contains integers
Output
Print
题意
对于一系列塔高
塔高
分析
针对区间修改及维护的问题,很容易想到线段树。
可以想到
此时问题即可转换为求满足
而区间更新即可转换为对
#include<bits/stdc++.h>using namespace std;const int maxn = 3e5+10;long long sub[maxn],a[maxn];struct Node{ int left,right,lmx,rmx,mx;}seg[maxn<<2];void upmx(int l,int r,int p){ int mid = (l+r)>>1; seg[p].mx = max(seg[p*2].mx,seg[p*2+1].mx); seg[p].lmx = seg[p*2].lmx; seg[p].rmx = seg[p*2+1].rmx; if((sub[mid] > 0 && sub[mid+1] != 0) || (sub[mid] < 0 && sub[mid+1] < 0)){ seg[p].mx = max(seg[p].mx,seg[p*2].rmx + seg[p*2+1].lmx); if(mid-l+1 == seg[p*2].lmx) seg[p].lmx = seg[p*2].lmx + seg[p*2+1].lmx; if(r-mid == seg[p*2+1].rmx) seg[p].rmx = seg[p*2+1].rmx + seg[p*2].rmx; }}void build(int l,int r,int p){ seg[p].left = l, seg[p].right = r; if(l == r){ if(sub[l]) seg[p].lmx = 1, seg[p].rmx = 1, seg[p].mx = 1; return; } int mid = (l+r)>>1; build(l,mid,p*2); build(mid+1,r,p*2+1); upmx(l,r,p);}void update(int l,int r,int idx,int p){ if(l == r && l == idx){ if(sub[l]) seg[p].lmx = 1, seg[p].rmx = 1, seg[p].mx = 1; else seg[p].lmx = 0, seg[p].rmx = 0, seg[p].mx = 0; return; } int mid = (l+r)>>1; if(l <= idx && idx <= mid) update(l,mid,idx,p*2); else update(mid+1,r,idx,p*2+1); upmx(l,r,p);}int main(){ int n,m; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); for(int i=1;i<n;i++) sub[i] = a[i+1] - a[i]; if(n!=1) build(1,n-1,1); scanf("%d",&m); for(int i=0,l,r,d;i<m;i++) { scanf("%d %d %d",&l,&r,&d); if(n==1) { printf("1\n"); continue; }; if(l!=1) sub[l-1] += d, update(1,n-1,l-1,1); if(r!=n) sub[r] -= d, update(1,n-1,r,1); printf("%d\n",seg[1].mx+1); }}
- Codeforces Round #381 (Div. 2) E. Alyona and towers
- Codeforces Round #381 (Div. 2) E. Alyona and towers 差分+ 线段树并
- Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)
- Codeforces Round #381 (Div. 2)C - Alyona and mex
- Codeforces Round #381 (Div. 2) A Alyona and copybooks
- Codeforces Round #381 (Div. 2) C. Alyona and mex
- Codeforces Round #381 (Div. 2)D. Alyona and a tree
- Codeforces Round #381 (Div. 2)C. Alyona and mex
- Codeforces Round #381 (Div. 2) Alyona and mex
- Codeforces Round #381 (Div. 2) A.Alyona and copybooks
- Codeforces Round #381 (Div. 2) A. Alyona and copybooks
- Codeforces Round #381 (Div. 2) A. Alyona and flowers
- Codeforces Round #381 (Div. 2) A. Alyona and copybooks
- Codeforces Round #381 (Div. 2) B. Alyona and flowers
- Codeforces Round #381 (Div. 2) D. Alyona and a tree
- Codeforces Round #381 (Div. 2) D. Alyona and a tree
- Codeforces Round #381 (Div. 2)-C. Alyona and mex
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles (找面积最大三角形)
- iOS开发- 友盟SDK开发,社会化分享攻略
- STM32F103/ <1>开发环境IDE的选择
- Java 9,OSGi以及模块化的未来
- 第五届蓝桥杯软件类省赛真题-C-A-3_神奇算式
- 设计模式-策略模式(3)
- Codeforces Round #381 (Div. 2) E. Alyona and towers
- Android 缓存机制(二)
- File常用的方法
- 开发:个人理财_CDUESTC
- dbms_ddl
- 【Android - 框架】之OkHttp的使用
- Dump文件介绍与使用
- |算法讨论|贪心算法 学习笔记
- xgboost特征选择