P3431 [POI2005]AUT-The Bus

题面:https://www.luogu.org/problemnew/show/P3431

Code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;
const int N=100005;
int c[N],dc[N];
int n,m,k,ans;
struct Node{
    int x,y,p;
}p[N];
bool cmp(Node a,Node b){
    if(a.x!=b.x){
        return a.x<b.x;
    }
    return a.y<b.y;
}
int lowbit(int x) {
    return x&-x;
}
void update(int x,int val){
    while(x<=m){
        c[x]=max(c[x], val);
        x+=lowbit(x);
    }
} 
int query(int x){
    int ans=0;
    while(x>0){
        ans=max(ans,c[x]);
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;i++){
        scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].p);
        dc[i]=p[i].y; 
    }
    sort(dc+1,dc+1+k);
    m=unique(dc+1,dc+k+1)-dc-1;
    for(int i=1;i<=k;i++){
        p[i].y=lower_bound(dc+1,dc+m+1,p[i].y)-dc;
    }
    sort(p+1,p+k+1,cmp);
    for(int i=1;i<=k;i++){
        int res=query(p[i].y)+p[i].p;
        ans=max(ans,res);
        update(p[i].y,res);
    }
    printf("%d\n", ans);
    return 0;
}
posted @ 2019-07-18 13:45  prestige  阅读(84)  评论(0编辑  收藏  举报