hdu6447(dp+树状数组求最值)

学到了新方法用树状数组求最值,但是具有局限性,只能求区间1~x的最值。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define tp(x) printf("x = %d\n",x)
#define ansp(x) printf("%d\n",x)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;

//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const ll inf = 0x3f3f3f3f;
const int mx = 1e5+10;
int f[mx],dp[mx];
int n;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}

inline int getmax(int x){int res=0;while(x)res=max(f[x],res),x-=x&(-x);return res;}
inline void update(int x,int d){while(x<=n)f[x]=max(f[x],d),x+=x&(-x);}
 
struct no{
	int x,y,va;
	/*bool operator < (const no&a)const{
		if(y == a.y)
			return x>a.x;
		return y<a.y;
	}*/ 
}p[mx],te;
bool cmpx(no &a, no &b){ 
	return a.x<b.x;
	}
	
bool cmpy(no &a, no &b){ 
	if(a.y == b.y)
		return a.x>b.x;
	return a.y<b.y;
}
 
int main(){
	//	freopen("F:\\in.txt","r",stdin);
	int T=10;	scanf("%d",&T);
	int N;
	while(T--){
		s1(N);
		//co = 0;
		for(int i = 0; i < N; i++){
			s3(p[i].x,p[i].y,p[i].va);
		}		
		sort(p,p+N,cmpx);
		int pre=-1,num=0;
		for(int i = 0; i < N ;i++){
			if(p[i].x == pre)
				p[i].x=pre;
			else{
				p[i].x= ++num;
				pre = num;
			}
		} 
		n = num;                   //树状数组的边界 
		sort(p,p+N,cmpy);
		mem(f,0);
		mem(dp,0);
		int te,x;
		for(int i = 0; i < N ;i++){
			x = p[i].x;
			te = getmax(x-1);
			if(p[i].va+te > dp[x]){
				dp[x] = p[i].va+te;
				update(x,dp[x]);
			}
		}
		printf("%d\n",getmax(n)); 
	}
	


	return 0;
}