Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)
来源:互联网 发布:淘宝精品推荐优惠券 编辑:程序博客网 时间:2024/06/05 21:49
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 li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
55 5 5 5 531 3 22 2 14 4 1
245
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to[7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
题意:给你一个数列,m个操作每次修改一段区间,问每次修改后数列的最长山峰(al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.)、
分析:差分后线段树,讨论有点恶心。。
#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<queue>#define INF 0x3f3f3f3f#define eps 1e-9#define MOD 1000000007#define N 300005 #define jud(a,b) (((a > 0) && (b > 0)) || ((a < 0) && (b < 0)))using namespace std;int n,m,l,r,val,a[N];long long b[N];struct Tree{int l,r,lmax,rmax,rud,lud,max;}tr[4*N];void update(int i){int mid = (tr[i].l + tr[i].r) >> 1,l = tr[i].l,r = tr[i].r;if(tr[2*i].lmax == mid - l + 1 && jud(b[l],b[mid+1]) > 0) tr[i].lmax = tr[2*i].lmax + tr[2*i+1].lmax;else tr[i].lmax = tr[2*i].lmax;if(tr[2*i+1].rmax == r - mid && jud(b[mid],b[r]) > 0) tr[i].rmax = tr[2*i+1].rmax + tr[2*i].rmax; else tr[i].rmax = tr[2*i+1].rmax;tr[i].max = max(tr[2*i].max,tr[2*i+1].max);if(b[mid] > 0) tr[i].max = max(tr[i].max,tr[2*i].rmax + max(tr[2*i+1].lud,tr[2*i+1].lmax));else if(b[mid] < 0) tr[i].max = max(tr[i].max,tr[2*i].rud + (b[mid+1] < 0 ? tr[2*i+1].lmax : 0));if(b[l] > 0){tr[i].lud = tr[2*i].lud;if(tr[2*i].lud == mid - l + 1 && b[mid+1] < 0) tr[i].lud = tr[2*i].lud + tr[2*i+1].lmax;if(tr[2*i].lmax == mid - l + 1) tr[i].lud = max(tr[i].lud,tr[2*i].lmax + tr[2*i+1].lud); }else tr[i].lud = 0;if(b[r] < 0){tr[i].rud = tr[2*i+1].rud;if(tr[2*i+1].rud == r - mid && b[mid] > 0) tr[i].rud = tr[2*i+1].rud + tr[2*i].rmax;if(tr[2*i+1].rmax == r - mid) tr[i].rud = max(tr[i].rud,tr[2*i+1].rmax + tr[2*i].rud);}else tr[i].rud = 0;}void Build(int i,int l,int r){tr[i].l = l;tr[i].r = r;if(l == r){if(b[l] > 0) {tr[i].lud = tr[i].lmax = tr[i].rmax = tr[i].max= 1;tr[i].rud = 0;}else if(b[l] < 0) { tr[i].rud = tr[i].lmax = tr[i].rmax = tr[i].max = 1; tr[i].lud = 0; } else tr[i].lmax = tr[i].rmax = tr[i].rud = tr[i].lud = tr[i].max = 0;return;} int mid = (tr[i].l + tr[i].r) >> 1;Build(2*i,l,mid);Build(2*i+1,mid+1,r); update(i);}void deal(int i,int x,int val){int l = tr[i].l,r = tr[i].r;if(l == r){b[l] += val;if(b[l] > 0) {tr[i].lud = tr[i].lmax = tr[i].rmax = tr[i].max= 1;tr[i].rud = 0;}else if(b[l] < 0) { tr[i].rud = tr[i].lmax = tr[i].rmax = tr[i].max = 1; tr[i].lud = 0; } else tr[i].lmax = tr[i].rmax = tr[i].rud = tr[i].lud = tr[i].max = 0;return; } int mid = (l + r) >> 1; if(x <= mid) deal(2*i,x,val); else deal(2*i+1,x,val); update(i);}int main(){scanf("%d",&n);for(int i = 1;i <= n;i++) scanf("%d",&a[i]);for(int i = 1;i <= n;i++) b[i-1] = a[i] - a[i-1];n--;if(n) Build(1,1,n);scanf("%d",&m);for(int i = 1;i <= m;i++){scanf("%d%d%d",&l,&r,&val);if(n == 1){printf("1\n");continue;}if(l-1) deal(1,l-1,val);if(r <= n) deal(1,r,-val);printf("%d\n",tr[1].max+1);}}
- Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)
- Codeforces Round #381 (Div. 2) E. Alyona and towers 差分+ 线段树并
- Codeforces Round #381 (Div. 2) E. Alyona and towers
- [Codeforces div1] Round 739C. Alyona and towers 线段树+差分数组
- 线段树 codeforces Alyona and towers 739C
- Codeforces 739C.Alyona and towers (线段树/非递归线段树)
- Codeforces Round #381 (Div. 2)C - Alyona and mex
- Codeforces Round #381 (Div. 2) C. Alyona and mex
- Codeforces Round #381 (Div. 2)C. Alyona and mex
- Codeforces Round #381 (Div. 2)-C. Alyona and mex
- Codeforces Round #381 (Div. 1) A. Alyona and mex
- codeforces 739c Alyona and towers
- CodeForces 739C Alyona and towers
- 【CodeForces】- 739C Alyona and towers
- Codeforces Round #381C. Alyona and mex
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs+二分+线段树延迟操作、树形化线性
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)C. Alyona and the Tree
- 无状态的 业务逻辑层 如何设计
- QT之HTTP下载文件大小为0
- Android时间轴(Timeline)效果的实现(使用RecyclerView)
- 精通 CSS+DIV 网页样式与布局 75
- 趣味算法之趣味分数
- Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)
- VC编码转换
- 精通 CSS+DIV 网页样式与布局 76
- Ubuntu16.04上安装gradle3.2.1环境详解
- 算法学习--7744问题
- 精通 CSS+DIV 网页样式与布局 77
- 201512-2 试题名称:消除类游戏(100分)ccf认证
- 主目录不小被清空了,桌面目录与主目录变成同一个目录怎么办
- poj2528-Mayor's posters -暴力线性