lg3431

链接:lg3431

题解

nm很小的话可以O(nm)dp
离散化后转移方程f[i]=max(f[j])+val   (a[i].y>=a[j].y)
这里显然是个二位偏序,树状数组维护一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define lowbit(x) x&(-x)
#define ll long long
using namespace std;
const int N=1e5+7;
struct node{
int l,r;
ll x;
}a[N];
int n,m,k,nx,ny;
ll c[N],f[N],ans=-1;
int X[N],Y[N];
map<int,int>hsh_x,hsh_y;
inline int cmp(node x,node y){return x.l==y.l?x.r<=y.r:x.l<y.l;}
void update(int x,ll k){
for(int i=x;i<=ny;i+=lowbit(i))c[i]=max(c[i],k);
}
ll query(int x){
ll res=-1;
for(int i=x;i;i-=lowbit(i))res=max(c[i],res);
return res;
}
int main(){
scanf("%d%d%d",&n,&m,&k);
rep(i,1,k){
scanf("%d%d%lld",&a[i].l,&a[i].r,&a[i].x);
X[i]=a[i].l;
Y[i]=a[i].r;
}
sort(X+1,X+k+1);
sort(Y+1,Y+k+1);
rep(i,1,k){
if(X[i]!=X[i-1])hsh_x[X[i]]=++nx;
if(Y[i]!=Y[i-1])hsh_y[Y[i]]=++ny;
}
rep(i,1,k)a[i].l=hsh_x[a[i].l],a[i].r=hsh_y[a[i].r];
sort(a+1,a+k+1,cmp);
rep(i,1,k){
f[i]=query(a[i].r)+a[i].x;
update(a[i].r,f[i]);
}
rep(i,1,k)ans=max(ans,f[i]);
printf("%lld\n",ans);
return 0;
}