用一下刚学的树状数组优化,以后可以有东西装逼了,这道题一开始没有意识到这种dp,很少做这种排序后dp的题,不过,第一遍把k打成了n,RE了一遍。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#define maxn 100010
#define inf 1000000000000000
using namespace std;
struct yts
{
int x,y,num;
}a[maxn];
map<int,int> p;
int n,m,num;
long long c[maxn];
long long f[maxn];
int b[maxn];
int k;
bool cmp(yts x,yts y)
{
return x.x<y.x || x.x==y.x && x.y<y.y;
}
long long query(int i)
{
long long ans=0;
while (i)
{
ans=max(ans,c[i]);
i-=i&-i;
}
return ans;
}
void modify(int i,long long x)
{
while (i<=k)
{
c[i]=max(c[i],x);
i+=i&-i;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=k;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].num);
for (int i=1;i<=k;i++) b[i]=a[i].x;
sort(b+1,b+k+1);
num=1;p[b[1]]=num;
for (int i=2;i<=k;i++) if (b[i]!=b[i-1]) p[b[i]]=++num;
for (int i=1;i<=k;i++) a[i].x=p[a[i].x];
p.clear();
for (int i=1;i<=k;i++) b[i]=a[i].y;
sort(b+1,b+k+1);
num=1;p[b[1]]=num;
for (int i=2;i<=k;i++) if (b[i]!=b[i-1]) p[b[i]]=++num;
for (int i=1;i<=k;i++) a[i].y=p[a[i].y];
sort(a+1,a+k+1,cmp);
for (int i=1;i<=k;i++) c[i]=0;
for (int i=1;i<=k;i++)
{
f[i]=query(a[i].y)+a[i].num;
modify(a[i].y,f[i]);
}
long long ans=0;
for (int i=1;i<=k;i++) ans=max(ans,f[i]);
printf("%lld\n",ans);
return 0;
}