CF272C Dima and Staircase(线段树/贪心)
来源:互联网 发布:淘宝广场舞服装春装 编辑:程序博客网 时间:2024/05/01 00:04
线段树直接维护区间最大值即可。或者可以发现,最大值只可能出现在最左端或最右端,贪心解决。
线段树
#include <bits/stdc++.h>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define N 100010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,m,a[N];struct node{ ll mx,lazy;}tree[N<<2];inline void pushup(int p){ tree[p].mx=max(tree[p<<1].mx,tree[p<<1|1].mx);}inline void pushdown(int p){ if(!tree[p].lazy) return; tree[p<<1].lazy=tree[p].lazy;tree[p<<1|1].lazy=tree[p].lazy; tree[p<<1].mx=tree[p].lazy;tree[p<<1|1].mx=tree[p].lazy; tree[p].lazy=0;}inline void build(int p,int l,int r){ if(l==r){tree[p].mx=a[l];return;} int mid=l+r>>1; build(p<<1,l,mid);build(p<<1|1,mid+1,r); pushup(p);}ll qmax(int p,int l,int r,int x,int y){ if(x<=l&&r<=y) return tree[p].mx; int mid=l+r>>1;pushdown(p);ll res=0; if(x<=mid) res=max(res,qmax(p<<1,l,mid,x,y)); if(y>mid) res=max(res,qmax(p<<1|1,mid+1,r,x,y)); return res;}void update(int p,int l,int r,int x,int y,ll val){ if(x<=l&&r<=y){tree[p].lazy=val;tree[p].mx=val;return;} int mid=l+r>>1;pushdown(p); if(x<=mid) update(p<<1,l,mid,x,y,val); if(y>mid) update(p<<1|1,mid+1,r,x,y,val); pushup(p);}int main(){// freopen("a.in","r",stdin); n=read(); for(int i=1;i<=n;++i) a[i]=read(); build(1,1,n);m=read(); while(m--){ int x=read(),val=read(); ll res=qmax(1,1,n,1,x); printf("%I64d\n",res); update(1,1,n,1,x,res+val); } return 0;}
阅读全文
0 0
- CF272C Dima and Staircase(线段树/贪心)
- codeforces 272C. Dima and Staircase(线段树)
- Codeforces 272C Dima and Staircase 思维 or 线段树
- Codeforces 272C Dima and Staircase【线段树】
- Codeforces 272C Dima and Staircase 线段树区间覆盖,最值查询
- Codeforces 272C Dima and Staircase (线段树区间更新 或 线性扫)
- CodeForces 272C-Dima and Staircase-线段树区间更新-RMQ
- CodeForces - 272C Dima and Staircase (线段树区间更新)
- CF 272 C. Dima and Staircase
- CF 272C Dima and Staircase(水题)
- CODEFORCES 272C Dima and Staircase <细节理解题+简单技巧>
- Codeforces 390B Inna, Dima and Song(贪心)
- Codefoeces 387E - George and Cards 贪心+线段树
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
- HDU 5338(ZZX and Permutations-用线段树贪心)
- Hdu 5338 ZZX and Permutations(线段树+贪心)
- codeforces round#353 trians and statistic dp+贪心+线段树
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
- POJ3678 Katu Puzzle(2-sat tanjar判矛盾)
- Tomcat未启动成功,显示No Spring WebApplicationInitializer types detected on classpath
- 349_LeetCode_349 Intersection of Two Arrays 题解
- web页面渲染
- java程序员面试宝典1
- CF272C Dima and Staircase(线段树/贪心)
- 学习资源推荐(不定期更新)
- 3494. 【NOIP2013模拟联考13】线段(segment) (2017.9B组)
- MySQL基础教程之存储过程
- Jzoj4614 字符串(待填)
- c\c++中的数据输入
- Android Studio+Appium+Java+Windows环境安装搭建
- JavaScript代码实现选水果
- Unity_版本切换_预编译手段