SPOJ 1043 Can you answer these queries I
来源:互联网 发布:uv淘宝论坛 编辑:程序博客网 时间:2024/06/11 23:00
需要维护的是最大和只能是 max(max(Nodes[now<<1],Nodes[now<<1|1],Nodes[now].rmax+Nodes[now<<1|1].lmax)
所以我们要维护的是lmax,rmax,max,sum四个信息
#include <iostream>#include <cstring>#include <cstdlib>#include <algorithm>#include <cmath>using namespace std;#define LL long long#define Now int now,int l,int r#define Mid int mid = (l+r)>>1#define lson now<<1,l,mid#define rson now<<1|1,mid+1,r#define root 1,1,Nconst int INF = 0x3f3f3f3f;const int maxn = 50000+10;struct Info{int lmax,rmax,sum,value;Info(){lmax = rmax = value = -INF;sum = 0;}void maintain(int x){lmax = rmax = sum = value = x;}};Info operator + (const Info & a,const Info & b){Info ret;ret.lmax = max(a.lmax,a.sum+b.lmax);ret.rmax = max(b.rmax,a.rmax+b.sum);ret.sum = a.sum+b.sum;ret.value = max(max(a.value,b.value),a.rmax+b.lmax);return ret;}Info Nodes[maxn<<2];void update(Now,int pos,int val){if(l == r){Nodes[now].maintain(val);return;}Mid;if(pos <= mid)update(lson,pos,val);elseupdate(rson,pos,val);Nodes[now] = Nodes[now<<1]+Nodes[now<<1|1];}Info query(Now,int ql,int qr){Info ret;if(ql <= l && r <= qr){return Nodes[now];}Mid;if(ql <= mid)ret = ret + query(lson,ql,qr);if(qr > mid)ret = ret + query(rson,ql,qr);return ret;}int main(){ios::sync_with_stdio(false);int N,M,x,ql,qr;while(cin>>N){for(int i = 1; i <= N; i++){cin>>x;update(root,i,x);}cin>>M;while(M--){cin>>ql>>qr;cout<<query(root,ql,qr).value<<endl;}}return 0;}
阅读全文
0 0
- spoj SPOJ 1043 Can you answer these queries I
- spoj 1043 Can you answer these queries I
- SPOJ 1043 1043. Can you answer these queries I
- 【SPOJ】 1043 Can you answer these queries I 线段树
- spoj 1043——Can you answer these queries I
- SPOJ 1043 Can you answer these queries I
- SPOJ GSS 1. Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- spoj 1043. Can you answer these queries I (线段树)
- 【SPOJ】Can you answer these queries I【线段树】
- [SPOJ GSS1] Can you answer these queries I [线段树]
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ Can you answer these queries V
- Java多态——向上转型与向下转型
- java中封装 ,继承与多态
- LeetCode 98 Validate Binary Search Tree(判断二叉搜索树)
- <Linux>AM3358内核移植与根文件系统的制作
- web.xml url-pattern 中/ 和/*的区别
- SPOJ 1043 Can you answer these queries I
- Tensorflow --VGG网络
- PAT (Advanced Level) Practise 1083 List Grades (25)
- Masonry介绍与使用实践:快速上手Autolayout(纯代码IOSer)
- 如果再记不住该挨打了
- 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
- RabbitMQ-安装
- 93. Restore IP Addresses
- Boosting和Bagging