【BZOJ】【P1537】【POI2005】【Aut- The Bus】【题解】【树状数组+dp】

传送门 :http://www.lydsy.com/JudgeOnline/problem.php?id=1537

x离散化

y排序

树状数组维护前缀max

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
vector<int>G[maxn];
struct point{
	int x,y,w;
	bool operator<(point oth)const{return y<oth.y||(y==oth.y&&x<oth.x);}
}a[maxn];
inline int lowbit(int x){return x&-x;}
int d[maxn],n;
void updata(int x,int f){while(x<=n)d[x]=max(d[x],f),x+=lowbit(x);}
int Qmax(int x){int ans=0;while(x)ans=max(ans,d[x]),x-=lowbit(x);return ans;}
int dp[maxn];
map<int,int>M;
int main(){
	scanf("%*d%*d%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
		M[a[i].x]=1;
	}sort(a+1,a+1+n);int tot=0;
	for(map<int,int>::iterator it=M.begin();it!=M.end();it++)it->second=++tot;
	for(int i=1;i<=n;i++)a[i].x=M[a[i].x];
	for(int i=1;i<=n;i++){
		dp[i]=Qmax(a[i].x)+a[i].w;
		updata(a[i].x,dp[i]);
	}cout<<*max_element(dp+1,dp+1+n)<<endl;	
	return 0;
}


你可能感兴趣的:(bzoj)