uva1400 区间最大连续和 线段树

本文介绍了一种解决区间查询最大子段和问题的方法。通过构建区间树来快速响应区间内的最大子段和查询,包括最大前缀和最大后缀的查询。此方法适用于大量查询场景,能够高效地处理区间内连续元素的最大和问题。

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ..., the dishes are represented by 1, 2, 3 ...). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.

Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.

For each question Neal asks, he will first write down an interval [a, b] (inclusive) to represent all the dishes a, a + 1,..., b, where a and b are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.

Input 

The input file contains multiple test cases. For each test case, there are two integers n and m in the first line (n, m < 500000). n is the number of dishes and m is the number of questions Neal asks.

Then n numbers come in the second line, which are the values of the dishes from left to right. Next m lines are the questions and each line contains two numbers a, b as described above. Proceed to the end of the input file.

Output 

For each test case, output m lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.

Sample Input 

3 1 
1 2 3 
1 1

Sample Output 

Case 1: 
1 1

  给出长度为N的整数序列,对于询问(a,b),找到两个下标x,y,a<=x<=y<=b,使x到y区间连续和最大。

  把区间[a,b]分成两半,设mid=(a+b)/2,答案区间可能在[a,mid],[mid+1,b],也可能开始在前一半,结束在后一半。设max_prefix[x]为点x包含区间的最大前缀的右边坐标,max_suffix[x]为点x包含区间的最大后缀的左边坐标,pair类型的max_sub[x]为点x包含区间的最大区间左右坐标。建树的时候算出每个节点的这三个值。询问的时候重要的是处理开始在前一半,结束在后一半的情况,同时要查找到[L,mid]的后缀和[mid+1,R]的前缀。因此还需要两个询问函数询问前缀和后缀,这两个询问比较特殊,不用同时给出ql和qr,如果是前缀,询问区间就是[L,qr],如果是后缀,询问区间是[ql,R],查询函数也有点不一样。

  代码很长,但思路比较清楚,sum函数用到了多态性。

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
#define MAXN 500010
#define MAXNODE 1000010
using namespace std;

typedef pair<int,int> Interval;

int N,Q;
long long prefix_sum[MAXN];

long long sum(int L,int R){
    return prefix_sum[R]-prefix_sum[L-1];
}

long long sum(Interval a){
    return sum(a.first,a.second);
}

Interval better(Interval a,Interval b){
    if(sum(a)==sum(b)) return a<b?a:b;
    return sum(a)>sum(b)?a:b;
}

struct IntervalTree{
    int max_prefix[MAXNODE],max_suffix[MAXNODE];
    Interval max_sub[MAXNODE];

    void build(int o,int L,int R){
        if(L==R){
            max_prefix[o]=max_suffix[o]=L;
            max_sub[o]=make_pair(L,L);
            return;
        }

        //创建子树
        int mid=(L+R)/2,l=o<<1,r=(o<<1)+1;
        build(l,L,mid);
        build(r,mid+1,R);

        long long s1,s2;
        s1=sum(L,max_prefix[l]);
        s2=sum(L,max_prefix[r]);
        if(s1==s2) max_prefix[o]=max_prefix[l];
        else max_prefix[o]=s1>s2?max_prefix[l]:max_prefix[r];

        s1=sum(max_suffix[l],R);
        s2=sum(max_suffix[r],R);
        if(s1==s2) max_suffix[o]=max_suffix[l];
        else max_suffix[o]=s1>s2?max_suffix[l]:max_suffix[r];

        max_sub[o]=better(max_sub[l],max_sub[r]);
        max_sub[o]=better(max_sub[o],make_pair(max_suffix[l],max_prefix[r]));
    }

    //[L,qr]的最大前缀区间
    Interval query_prefix(int o,int L,int R,int qr){
        if(max_prefix[o]<=qr) return make_pair(L,max_prefix[o]);
        int mid=(L+R)/2;
        if(qr<=mid) return query_prefix(o<<1,L,mid,qr);
        Interval i=query_prefix((o<<1)+1,mid+1,R,qr);
        i.first=L;
        return better(i,make_pair(L,max_prefix[o<<1]));
    }

    //[ql,R]的最大前缀区间
    Interval query_suffix(int o,int L,int R,int ql){
        if(max_suffix[o]>=ql) return make_pair(max_suffix[o],R);
        int mid=(L+R)/2;
        if(ql>mid) return query_suffix((o<<1)+1,mid+1,R,ql);
        Interval i=query_suffix(o<<1,L,mid,ql);
        i.second=R;
        return better(i,make_pair(max_suffix[(o<<1)+1],R));
    }

    //[ql,qr]的最大区间
    Interval query_sub(int o,int L,int R,int ql,int qr){
        if(ql<=L&&qr>=R) return max_sub[o];
        int mid=(L+R)/2;
        if(qr<=mid) return query_sub(o<<1,L,mid,ql,qr);
        if(ql>mid) return query_sub((o<<1)+1,mid+1,R,ql,qr);
        Interval i1=query_prefix((o<<1)+1,mid+1,R,qr);
        Interval i2=query_suffix(o<<1,L,mid,ql);
        Interval i3=better(query_sub(o<<1,L,mid,ql,qr),query_sub((o<<1)+1,mid+1,R,ql,qr));
        return better(i3,make_pair(i2.first,i1.second));
    }
}tree;

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d%d",&N,&Q)!=EOF){
        prefix_sum[0]=0;
        for(int i=1;i<=N;i++){
            int t;
            scanf("%d",&t);
            prefix_sum[i]=prefix_sum[i-1]+t;
        }
        tree.build(1,1,N);
        printf("Case %d:\n",++cas);
        while(Q--){
            int ql,qr;
            scanf("%d%d",&ql,&qr);
            Interval ans=tree.query_sub(1,1,N,ql,qr);
            printf("%d %d\n",ans.first,ans.second);
        }
    }
    return 0;
}



#include <bits/stdc++.h> #define ll long long #define lb(x) x&(-x) using namespace std; const ll N=2e5; struct node{ ll l,r,suml,sumr,ans; }tr[4*N+5]; ll n,m,t; ll yy[N+5],pp[N+5],r[N+5],rr[N+5]; ll get(ll x,ll y){ ll s=0,ss=0; // cout<<x<<" "<<y<<" "; for(ll i=y-1;i;i-=lb(i)){ s+=rr[i]; } for(ll i=x-1;i;i-=lb(i)){ ss+=rr[i]; } // cout<<s-ss<<endl; return s-ss; } void updata(ll p,ll l,ll r){ ll mid=(l+r)>>1; tr[p].suml=min(tr[2*p].suml+get(mid,r),tr[2*p+1].suml); tr[p].sumr=min(tr[2*p+1].sumr+get(l,mid+1),tr[2*p].sumr); tr[p].ans=tr[2*p].suml+tr[2*p+1].sumr+get(mid,mid+1); tr[p].ans=min(tr[p].ans,min(tr[2*p].ans,tr[2*p+1].ans)); return; } void build(ll p,ll l,ll r){ if(l==r){ tr[p].l=l; tr[p].r=r; tr[p].suml=yy[l]; tr[p].sumr=pp[l]; tr[p].ans=yy[l]+pp[l]; return; } ll mid=(l+r)>>1; build(2*p,l,mid); build(2*p+1,mid+1,r); tr[p].l=tr[2*p].l; tr[p].r=tr[2*p+1].r; updata(p,tr[p].l,tr[p].r); return; } void change(ll p,ll x){ if(tr[p].l==tr[p].r){ tr[p].suml=yy[x]; tr[p].sumr=pp[x]; tr[p].ans=yy[x]+pp[x]; return; } ll mid=(tr[p].l+tr[p].r)>>1; if(x<=mid) change(2*p,x); else change(2*p+1,x); updata(p,tr[p].l,tr[p].r); return; } int main(){ scanf("%lld%lld%lld",&n,&m,&t); for(ll i=1;i<=n;i++) scanf("%lld",&yy[i]); for(ll i=1;i<=n;i++) scanf("%lld",&pp[i]); for(ll i=1;i<n;i++){ scanf("%lld",&r[i]); for(ll j=i;j<n;j+=lb(j)) rr[j]+=r[i]; } build(1,1,n); // for(ll i=1;i<=40;i++){ // cout<<tr[i].l<<" "<<tr[i].r<<" "; // cout<<tr[i].suml<<" "<<tr[i].sumr<<" "<<tr[i].ans<<endl; // } // return 0; printf("%lld\n",tr[1].ans); while(m--){ char op; ll x,y; cin>>op; scanf("%lld%lld",&x,&y); if(op=='r'){ for(ll i=x;i<n;i+=lb(i)){ rr[i]-=r[x]; rr[i]+=y; } r[x]=y; change(1,x),change(1,x+1); } else if(op=='p'){ pp[x]=y; change(1,x); } else{ yy[x]=y; change(1,x); } printf("%lld\n",tr[1].ans); } return 0; } ## **题目描述** 阿杰在游戏中管理着 $n$ 座编号为 $1$ 至 $n$ 的城市。每座城市有一个矿场一个发电厂。他需要完成以下过程: 1. **采矿**:在任意城市 $i$ 的矿场开采矿物,花费 $y_i$(可为负,表示收益)。 2. **运输**:通过道路将矿物运送到任意城市 $j$ 的发电厂。相邻城市 $i$ 与 $i+1$ 间的道路费用为 $r_i$(双向且相同,可为负但不允许重复经过同一条道路)。 3. **发电**:在城市 $j$ 的发电厂消耗矿物发电,花费 $p_j$(可为负)。 **总花费**为三部分之:$y_i + p_j + \text{运输路径费用}$。目标是选择起点 $i$ 终点 $j$,使总花费最小(负花费表示净收益)。 游戏会动态更新费用:共 $m$ 个事件,每个事件将某个 $y_i$、$p_i$ 或 $r_i$ 修改为 $c$。需要实时输出每次更新后的最小总花费。 ## **输入格式** - **第一行**:$n, m, T$(城市数、事件数、测试点编号)。 - **第二行**:$n$ 个整数 $y_1, y_2, \dots, y_n$(采矿费用)。 - **第三行**:$n$ 个整数 $p_1, p_2, \dots, p_n$(发电费用)。 - **第四行**:$n-1$ 个整数 $r_1, r_2, \dots, r_{n-1}$(道路费用,$r_i$ 连接城市 $i$ $i+1$)。 - **接下来 $m$ 行**:每行格式为 `t i c`: - $t \in \{ \texttt{y}, \texttt{p}, \texttt{r} \}$ 表示修改的费用类型。 - $i$ 表示被修改的城市编号($t=\texttt{r}$ 时 $i$ 表示道路编号,范围 $1 \le i \le n-1$)。 - $c$ 为新费用值(绝对值 $\le 10^{12}$)。 ## **输出格式** - **共 $m+1$ 行**: - 第 $1$ 行:初始状态的最小花费。 - 第 $2$ 至 $m+1$ 行:每次事件后的最小花费。 ## **样例** #### 输入样例 ```plaintext 10 10 0 12 12 17 28 42 55 60 73 73 91 89 89 79 77 54 38 34 24 24 0 2 4 2 1 4 3 7 10 10 p 2 92 p 3 87 r 4 3 r 1 9 r 2 4 r 5 3 y 10 72 y 8 67 r 2 0 y 4 91 ``` #### 输出样例 ```plaintext 53 53 53 55 55 55 54 54 54 50 50 ``` ## **数据范围** | 测试点 | $n$ | $m$ | 费用可为负 | $y_i=0$ | $p_i=0$ | |:------:|:-----:|:-------:|:------:|:----:|:-------:| | 1 | $500$ | $500$ | 否 | 否 | 否 | | 2 | $500$ | $500$ | 是 | 否 | 否 | | 3 | $3000$ | $3000$ | 否 | 否 | 否 | | 4 | $3000$ | $3000$ | 是 | 否 | 否 | | 5 | $2\times10^5$ | $2\times10^5$ | 是 | 是 | 是 | | 6–10 | $2\times10^5$ | $2\times10^5$ | 混合 | 混合 | 混合 | **特殊约束**: - 若“费用可为负”为**否**,则所有费用(含修改后)$\ge 0$。 - 若 $y_i=0$ 为**是**,则所有 $y_i=0$ 且无 `y` 类型事件;$p_i=0$ 同理。 --- ### **题目补充说明** 1. **道路特性**: - 道路双向通行且费用相同,但**不允许重复使用**(即使 $r_i<0$ 也不能多次经过同一条路获利)。 - 运输路径是简单路径(无环路)。 2. **发电过程**: - 采矿发电必须在**不同或相同城市**完成(即 $i$ $j$ 可相同,此时运输费用为 $0$)。 - 总花费计算式:$\text{min}_{1 \le i,j \le n} \left\{ y_i + p_j + \text{dis}(i,j) \right\}$,其中 $\text{dis}(i,j)$ 是 $i$ 到 $j$ 的路径费用。 3. **事件限制**: - `r i c` 事件中 $i$ 为道路编号($1 \le i \le n-1$),修改连接 $i$ $i+1$ 的道路费用。 代码答案错误,找出问题
最新发布
08-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值