Codeforces Round #381 (Div. 1) C. Alyona and towers(线段树)
2016-12-07 22:40
477 查看
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.
Input
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.
Output
Print m lines. In i-th
line print the maximum width of the hills after the i-th addition.
Example
input
output
Note
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
;
long long b
;
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);
}
}
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.
Input
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.
Output
Print m lines. In i-th
line print the maximum width of the hills after the i-th addition.
Example
input
5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1
output
2 4 5
Note
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
;
long long b
;
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);
}
}
相关文章推荐
- 矩阵乘法
- mysqlC语言接口
- netty客户端同步请求实现
- xml
- matlab画正态分布图简单算法
- RN学习1——前奏,app插件化和热更新的探索
- Linux 文件编程
- Oracle用户管理模式的备份恢复_超越OCP精通Oracle视频教程培训16
- 堆空间与栈空间的区别
- 0014算法笔记——【动态规划】凸多边形最优三角剖分
- Java 3:对象的通用行为
- 创业公司做数据分析(二)运营数据系统
- 【Hibernate框架学习】:Hibernate进阶之Hibernate核心接口和类(二)
- kmp模版
- 国内maven镜像源
- Spring AOP详解
- 形参和实参
- [jQuery知识]jQuery之知识七-事件初级
- Apache
- 在图片相册中复用性程序