"Ray, Pass me the dishes!" UVALive - 3938 (线段树,动态区间连续和最大值,分治)

题意

长度为n的整数序列,m个询问,问区间[L, R]中区间连续和最大的左右端点,如果多个答案,取左端点最小,如果还有多个答案,则取右端点最小。

思路

求区间连续和最大有一个分治的思路,就比如[L, R]这个序列,一分两开,最大和就有三个可能,1. 在[L, mid] 2.在[mid + 1, R] 3.左端点在[L, mid], 右端点在[mid+1, R]。

线段树同样具有分治的思想,每次一分两开,维护左右两个区间,然后合并。
那么思路也就有了,那就是用线段树维护max_pre, max_suf, max_sub, 最大前缀和,最大后缀和,最大区间和,因为要求左右端点,可以用前缀和,这样只要记录端点,就可以求出区间和,从而合并。

code

#include <bits/stdc++.h>
using namespace std;
#define N 500000 + 10

typedef long long ll;
struct Ran {
    int l,r;
    Ran(int _l = 0, int _r = 0) : l(_l), r(_r){};
    
};
ll s[N];
Ran cmp(const Ran& a, const Ran& b) {    // 返回最优的区间
    ll suma = s[a.r] - s[a.l - 1];
    ll sumb = s[b.r] - s[b.l - 1];
    return (suma > sumb || (suma == sumb && a.l < b.l) || (suma == sumb && a.l == b.l && a.r < b.r)) ? a : b;
}

struct Node {
    int x, y, pre, suf;
    Ran range;
    Node operator + (const Node & b) const {  // 合并
        Node res;
        res.x = x;
        res.y = b.y;
        res.range = cmp(range, b.range);                // 端点都在左或都在右
        res.range = cmp(res.range, Ran(suf, b.pre));    // 端点在分别左右区间情况
        res.pre = cmp(Ran(x, pre), Ran(x, b.pre)).r;    // 最大前缀和有两个情况,一个是左区间最大前缀和,一个是左区间的和加上右区间的最大前缀和
        res.suf = cmp(Ran(suf, b.y), Ran(b.suf, b.y)).l;// 后缀同理
        return res;
    }
} no[N << 2];


void update(int o) {
    no[o] = no[o<<1] + no[o<<1|1];
}

void build(int L, int R, int o) {
    if(L==R) {
        no[o].x = no[o].y = no[o].pre = no[o].suf = no[o].range.l = no[o].range.r = L;
        return;
    }
    int mid = (L+R) / 2;
    build(L, mid, o << 1);
    build(mid+1, R, o<<1|1);
    update(o);
}
Node query(int L, int R, int o) {
    if(no[o].y <= R && no[o].x >= L) return no[o];
    int mid = (no[o].x + no[o].y) / 2;
    Node res;
    res.x = -1;          
    if(L <= mid) res = query(L, R, o<<1);
    if(R > mid) {
        if(res.x != -1) res = res + query(L,R,o<<1|1);
        else res = query(L,R,o<<1|1);
    }
    return res;
}

int main()
{
    // freopen("/Users/maoxiangsun/MyRepertory/acm/i.txt", "r", stdin);
    int n,m;
    int cas = 0;
    while(~scanf("%d%d", &n, &m)) {
        s[0]=0;
        for(int i = 1; i <= n; i++) {
            ll t;
            scanf("%lld", &t);
            s[i] = s[i-1] + t;
        }
        build(1,n,1);
        printf("Case %d:\n",++cas);
        while(m--) {
            int L, R;
            scanf("%d%d", &L, &R);
            Node ans = query(L, R, 1);
            printf("%d %d\n", ans.range.l, ans.range.r);
        }
    }
    return 0;
}
解释一下列语句的语法,纯文本输出.<!-- sy1\src\views\dashboard\index.vue --> <template> <div class="dashboard-container home"> <!-- 营业数据 --> <Overview :overview-data="overviewData" /> <!-- end --> <!-- 订单管理 --> <Orderview :orderview-data="orderviewData" /> <!-- end --> <div class="homeMain"> <!-- 菜品总览 --> <CuisineStatistics :dishes-data="dishesData" /> <!-- end --> <!-- 套餐总览 --> <SetMealStatistics :set-meal-data="setMealData" /> <!-- end --> </div> <!-- 订单信息 --> <OrderList :order-statics="orderStatics" @getOrderListBy3Status="getOrderListBy3Status" /> <!-- end --> </div> </template> <script lang="ts"> import { Component, Vue } from 'vue-property-decorator' import { getBusinessData, getDataOverView, //营业数据 getOrderData, //订单管理今日订单 getOverviewDishes, //菜品总览 getSetMealStatistics, //套餐总览 } from '@/api/index' import { getOrderListBy } from '@/api/order' // 组件 // 营业数据 import Overview from './components/overview.vue' // 订单管理 import Orderview from './components/orderview.vue' // 菜品总览 import CuisineStatistics from './components/cuisineStatistics.vue' // 套餐总览 import SetMealStatistics from './components/setMealStatistics.vue' // 订单列表 import OrderList from './components/orderList.vue' @Component({ name: 'Dashboard', components: { Overview, Orderview, CuisineStatistics, SetMealStatistics, OrderList, }, }) export default class extends Vue { private todayData = {} as any private overviewData = {} private orderviewData = {} as any private flag = 2 private tateData = [] private dishesData = {} as any private setMealData = {} private orderListData = [] private counts = 0 private page: number = 1 private pageSize: number = 10 private status = 2 private orderStatics = {} as any created() { this.init() } init() { this.$nextTick(() => { this.getBusinessData() this.getOrderStatisticsData() this.getOverStatisticsData() this.getSetMealStatisticsData() }) } // 获取营业数据 async getBusinessData() { const data = await getBusinessData() this.overviewData = data.data.data } // 获取今日订单 async getOrderStatisticsData() { const data = await getOrderData() this.orderviewData = data.data.data } // 获取菜品总览数据 async getOverStatisticsData() { const data = await getOverviewDishes() this.dishesData = data.data.data } // 获取套餐总览数据 async getSetMealStatisticsData() { const data = await getSetMealStatistics() this.setMealData = data.data.data } //获取待处理,待派送,派送中数量 getOrderListBy3Status() { getOrderListBy({}) .then((res) => { if (res.data.code === 1) { this.orderStatics = res.data.data } else { this.$message.error(res.data.msg) } }) .catch((err) => { this.$message.error('请求出错了:' + err.message) }) } } </script> <style lang="scss"> </style>
最新发布
10-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值