线段树 区间合并
来源:互联网 发布:sja1000 接收数据 编辑:程序博客网 时间:2024/05/17 06:49
sum表示节点的和
max表示节点的最大值
lsum表示节点从左边开始的最大值
rsum表示节点从右边开始的最大值
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows:
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.
Given M queries, your program must output the results of these queries.
Input
- The first line of the input file contains the integer N.
- In the second line, N numbers follow.
- The third line contains the integer M.
- M lines follow, where line i contains 2 numbers xi and yi.
Output
- Your program should output the results of the M queries, one query per line.
Example
Input: 3 -1 2 311 2 Output: 2
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <limits.h>#include <string.h>#include <string>#include <queue>#include <vector>#include <set>#include <map>#include <deque>#include <map>#include <algorithm>#include <iostream>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define MAXN 500000 + 100int sum[MAXN<<2],lsum[MAXN<<2],rsum[MAXN<<2],maxx[MAXN<<2];int num[MAXN];int Max(int a,int b){ return a>b?a:b;}void PushUp(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1]; lsum[rt] = Max(lsum[rt<<1],sum[rt<<1]+lsum[rt<<1|1]); rsum[rt] = Max(rsum[rt<<1|1],sum[rt<<1|1]+rsum[rt<<1]); maxx[rt] = Max(maxx[rt<<1],maxx[rt<<1|1]); maxx[rt] = Max(maxx[rt],lsum[rt]); maxx[rt] = Max(maxx[rt],rsum[rt]); maxx[rt] = Max(maxx[rt],rsum[rt<<1]+lsum[rt<<1|1]);}void build(int l,int r,int rt){ if(l == r){ sum[rt] = lsum[rt] = rsum[rt] = maxx[rt] = num[l]; return ; } int m = (l+r)>>1; build(lson); build(rson); PushUp(rt);}int query_l(int L,int R,int l,int r,int rt){ if(L==l && r==R){ return rsum[rt]; } int m = (l+r)>>1; if(L > m){ return query_l(L,R,rson); } else{ int s = rsum[rt<<1|1]; int ls = query_l(L,m,lson)+sum[rt<<1|1];//sum[rt<<1+1]+rsum[rt<<1]; return Max(s,ls); }}int query_r(int L,int R,int l,int r,int rt){ if(L==l && r==R){ return lsum[rt]; } int m = (l+r)>>1; if(R <= m){ return query_r(L,R,lson); } else{ int s = lsum[rt<<1]; int ls = sum[rt<<1]+query_r(m+1,R,rson);//sum[rt<<1]+lsum[rt<<1|1] return Max(s,ls); }}int query(int L,int R,int l,int r,int rt){ if(L==l && r==R){ return maxx[rt]; } int m = (l+r)>>1; if(m >= R){//等于号位置变了下都超时。。。。。。(这样查询的时间可能是最快的把) return query(L,R,lson); } else if(L > m){ return query(L,R,rson); } else{ int ls = query(L,m,lson); int rs = query(m+1,R,rson); int ms = query_l(L,m,lson)+query_r(m+1,R,rson);//rsum[rt<<1]+lsum[rt<<1|1]; return Max(Max(ls,rs),ms); }}int main(){ int n,m,a,b; int i; while(~scanf("%d",&n)){ for(i=1;i<=n;i++){ scanf("%d",&num[i]); } build(1,n,1); scanf("%d",&m); while(m--){ scanf("%d%d",&a,&b); printf("%d\n",query(a,b,1,n,1)); } } return 0;}
0 0
- 线段树 区间合并
- 线段树 区间合并
- 线段树 区间合并
- 线段树 区间合并
- 线段树-区间合并
- 谈区间合并线段树
- hdu3308 线段树区间合并
- poj3667线段树区间合并
- poj3667 线段树+区间合并
- hdu3308 线段树 区间合并
- 线段树练习 区间合并
- poj3667(线段树区间合并)
- 线段树 (区间合并)
- 线段树区间合并poj3667
- hdu4351 线段树 区间合并
- hfutoj1300 线段树 区间合并
- 线段树之区间合并
- LCIS --线段树区间合并
- Rabin-Miller素性测试算法
- 批处理--从1累加100的和
- ps-help.exe
- String two pointer swapping (合集)
- IOS网络编程之——socket。
- 线段树 区间合并
- apache 去除index.php
- java web中的编解码问题
- github,git 入门配置和基本使用
- Uva-1440-Inspection (只有下界的网络最小流)
- C++设计模式-Builder建造者模式
- A machine learning approach for non-blind image deconvolution(泛读)
- 只有朝着目标进发,才是在进步 一个应届硕士的反思
- 2015去哪儿网南京站面试编程题分析和解答(多个字符的来回震荡)