HDU 6047 Maximum Sequence(线段树)






http://acm.split.hdu.edu.cn/showproblem.php?pid=6047











题目大意:

这个题意就比较难表达了     给你 a 和 b 两个数组每组n个数     让你补充a数组中第n+1~2*n项     求a数组中n+1~2*n 的和      补充的规则是       当你补充 a     aj=max(ai-i)    bk=< i <j       b是b数组中的任意一个值    每个 b只能用一次   







分析:

先贪心     b中元素从小到大选   使新增元素尽可能大     然后线段树维护区间的最大值











AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define FIN	freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const int MAX=510000;
const double PI=acos(-1.0);
using namespace std;
LL a[MAX];
struct node{
    LL num;
}numa[MAX];
LL numb[MAX];
struct Tree{
	int l,r;
	LL maxn;
}T[2500000];
void build(int rt,int l,int r){//     建树
	T[rt].l=l;T[rt].r=r;
	if (l==r) {
		T[rt].maxn=numa[l].num;
		return ;
	}
	int mid=(l+r)/2;
	build(rt*2,l,mid);
	build(rt*2+1,mid+1,r);
	T[rt].maxn=max(T[rt*2].maxn,T[rt*2+1].maxn);
}
void update(int rt,int index,LL e){//     单值修改
    if (T[rt].l==T[rt].r){
        if (T[rt].l==index)
            T[rt].maxn+=e;
        return ;
    }
    int mid=(T[rt].l+T[rt].r)/2;
    if (index<=mid) update(rt*2,index,e);
    else update(rt*2+1,index,e);
    T[rt].maxn=max(T[rt*2].maxn,T[rt*2+1].maxn);
}
LL Query(int rt,int Ql,int Qr){//    查询
    if (T[rt].l>=Ql&&T[rt].r<=Qr) {
        return T[rt].maxn;
    }
    int mid=(T[rt].l+T[rt].r)/2;
    LL maxn=-INF;
    if (Ql<=mid) maxn=max(Query(rt*2,Ql,Qr),maxn);
    if (Qr>mid) maxn=max(Query(rt*2+1,Ql,Qr),maxn);
    return maxn;
}
int main (){
    int n;
    while (scanf ("%d",&n)!=EOF){
        memset(numa,0,sizeof(numa));memset(numb,0,sizeof(numa));
        for (int i=1;i<=n;i++){
            scanf ("%lld",&numa[i].num);
            numa[i].num-=i;
        }
        build(1,1,2*n);
        for (int i=1;i<=n;i++) scanf ("%lld",&numb[i]);
        sort(numb+1,numb+n+1);
        LL temp;
        LL sum=0;
        for (int i=1;i<=n;i++){
            temp=Query(1,numb[i],n+i-1);
            sum=(sum+temp)%mod;
            update(1,n+i,temp-(n+i));
        }
        printf ("%lld\n",sum);
    }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值