【codeforces 115E】Linear Kingdom Races (datestructs(seg.tree)+dp)

E. Linear Kingdom Races
time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are a car race organizer and would like to arrange some races in Linear Kingdom.

Linear Kingdom has n consecutive roads spanning from left to right. The roads are numbered from 1 to n from left to right, thus the roads follow in the order of their numbers’ increasing. There will be several races that may be held on these roads. Each race will use a consecutive subset of these roads. Also, each race will pay some amount of money to you if this race is held. No races overlap in time, so some roads can be used in several races.

Unfortunately, some of the roads are in a bad condition and they need repair. Each road has repair costs associated with it, you are required to pay this cost to repair the road. A race can only take place if all the roads used in the race are renovated. Your task is to repair such roads (possibly all or none) that will maximize your profit. Your profit is defined as the total money you get from the races that are held minus the total money you spent to repair the roads. Note that you may decide not to repair any road and gain zero profit.

Print the maximum profit you can gain.

Input
The first line contains two single-space separated integers, n and m (1 ≤ n, m ≤ 2·105), denoting the number of roads and the number of races, respectively.

Then n lines follow, each line will contain a single non-negative integer not exceeding 109 denoting the cost to repair a road. The costs are given in order from road 1 to road n.

Finally, m lines follow. Each line is single-space-separated triplets of integers. Each triplet will be given as lb, ub, and p (1 ≤ lb ≤ ub ≤ n, 1 ≤ p ≤ 109), which means that the race these three integers describe will use all the roads from lb to ub, inclusive, and if it’s held you get p.

Output
Print a single integer denoting the maximum possible profit you can gain.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout stream (also you may use %I64d specificator).

Sample test(s)
input
7 4
3
2
3
2
1
2
3
1 2 5
2 3 5
3 5 3
7 7 5
output
4
input
2 1
0
3
1 2 5
output
2
input
3 1
10
10
10
1 3 10
output
0

题意:一维上 -cost&val 的选择区间,使得收益最大;

思路:
1.收益最大——》dp;
2.pre的贪心—》按.r-sort;
3.dp的方程—–》dp[i]=max{dp[j]+dp[j]-cost[j.l+now.r]+val[now]};
【dp的区间减保证条件】
4.优化—–》segment tree;

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
int n,m;
long long  lazy[200005*8];
long long  ma[200005*8];
long long  cost[300005];
long long  dp[300005];
struct node{
    int l,r,val;
}ss[300005];
int cmp(node x,node y)
{
    return x.r<y.r;
}
void push_up(int id)
{
    ma[id]=max(ma[lson],ma[rson]);
}
void push_down(int id,int l,int mid,int r)
{
    lazy[lson]+=lazy[id];lazy[rson]+=lazy[id];  //+=!!!
    ma[lson]+=lazy[id];
    ma[rson]+=lazy[id];
    lazy[id]=0;
}


void add(int id,int l,int r,int L,int R,long long  V)
{
    if(l>R||r<L)  return ;
    if(l>=L&&r<=R)
    {
        lazy[id]+=V;
        ma[id]+=V;  
        return ;
    }
    int mid=(r+l)>>1;   
    push_down(id,l,mid,r);
    add(lson,l,mid,L,R,V); 
    add(rson,mid+1,r,L,R,V);
    push_up(id);
}
void solve()         //0是必要的,存完整-cost的点!!! 
{
    int j=1;
    for(int i=1;i<=n;i++)
    {
        add(1,0,n,0,i-1,-cost[i]);
        while(ss[j].r==i&&j<=m)             //能有收益当然要拿!! 
        {   
            add(1,0,n,0,ss[j].l-1,ss[j].val);       //0——》L-1 只有这段-cost【.l--.r】; 
            j++;    
        }
        dp[i]=max(dp[i-1],ma[1]); 
        add(1,0,n,i,i,dp[i]);                //子节点存dp!!!!单点更新!!! 
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(ma,0,sizeof(ma));
        memset(dp,0,sizeof(dp));
        memset(lazy,0,sizeof(lazy));
        for(int i=1;i<=n;i++)
        scanf("%d",&cost[i]);
        for(int i=1;i<=m;i++)
        scanf("%d%d%d",&ss[i].l,&ss[i].r,&ss[i].val);
        dp[0]=0;
        sort(ss+1,ss+1+m,cmp);            //sort处理 
        solve();
        printf("%I64d\n",dp[n]);
    }
}

你可能感兴趣的:(dp,codeforces,segment-tr)