zoukankan      html  css  js  c++  java
  • 线段树 SP1043 GSS1

    SP1043 GSS1 - Can you answer these queries I

    题目描述

    给出了序列A[1],A[2],…,A[N]。 (a[i]≤15007,1≤N≤50000)。查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j];x≤i≤j≤y}。 给定M个查询,程序必须输出这些查询的结果。

    输入输出格式

    输入格式:

    • 输入文件的第一行包含整数N。
    • 在第二行,N个数字跟随。
    • 第三行包含整数M。
    • M行跟在后面,其中第1行包含两个数字xi和yi。

    输出格式:

    您的程序应该输出M查询的结果,每一行一个查询。

    不带修改的维护最大子段和,挺裸的,维护四个量就行了。具体参考小白逛公园https://www.cnblogs.com/wangxiaodai/p/9744081.html

    code:

    #include<iostream>
    #include<cstdio>
    #define ls(o) o<<1
    #define rs(o) o<<1|1
    using namespace std;
    const int wx=100017;
    inline int read(){
    	int sum=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
    	return sum*f;
    }
    struct val_tree{
    	int l,r,sum,lsum,rsum,tot;
    	#define sum(o) t[o].sum
    	#define lsum(o) t[o].lsum
    	#define rsum(o) t[o].rsum
    	#define tot(o) t[o].tot
    }t[wx*4];
    int n,m;
    int a[wx];
    void up(int o){
    	tot(o)=tot(ls(o))+tot(rs(o));
    	lsum(o)=max(lsum(ls(o)),tot(ls(o))+lsum(rs(o)));
    	rsum(o)=max(rsum(rs(o)),tot(rs(o))+rsum(ls(o)));
    	sum(o)=max(sum(ls(o)),max(sum(rs(o)),rsum(ls(o))+lsum(rs(o))));
    }
    void build(int o,int l,int r){
    	t[o].l=l;t[o].r=r;
    	if(l==r){sum(o)=tot(o)=lsum(o)=rsum(o)=a[l];return;}
    	int mid=t[o].l+t[o].r>>1;
    	if(l<=mid)build(ls(o),l,mid);
    	if(r>mid)build(rs(o),mid+1,r);
    	up(o);
    }
    val_tree query(int o,int l,int r){
    	if(l<=t[o].l&&t[o].r<=r){
    		return t[o];
    	}
    	int mid=t[o].l+t[o].r>>1;
    	val_tree tmp,tmp1,tmp2;
    	if(r<=mid)return query(ls(o),l,r);
    	if(l>mid)return query(rs(o),l,r);
    	tmp1=query(ls(o),l,r);
    	tmp2=query(rs(o),l,r);
    	tmp.tot=tmp1.tot+tmp2.tot;
    	tmp.lsum=max(tmp1.lsum,tmp1.tot+tmp2.lsum);
    	tmp.rsum=max(tmp2.rsum,tmp2.tot+tmp1.rsum);
    	tmp.sum=max(max(tmp1.sum,tmp2.sum),tmp1.rsum+tmp2.lsum);
    	return tmp;
    }
    int main(){
    	n=read();
    	for(int i=1;i<=n;i++)a[i]=read();
    	build(1,1,n);
    	m=read();
    	for(int i=1;i<=m;i++){
    		int x,y;
    		x=read();y=read();
    		printf("%d
    ",query(1,x,y).sum);
    	}
    	return 0;
    }
    
  • 相关阅读:
    实现 ajax 多项选择框
    Ajax 文件上传之PHP心得
    php tree生成树分享
    基于JQuery框架的AJAX
    jQuery Ajax 全解析
    asp 下载函数
    10元奥运纪念钞明发行 与人民币等值流通
    jQuery(Ajax)/PHP/MySQL实现搜索自动提示功能
    隔行扫描和逐行扫描(interlace and progressive )
    VGA 输出调试
  • 原文地址:https://www.cnblogs.com/wangxiaodai/p/9786411.html
Copyright © 2011-2022 走看看