返回顶部

树状数组优化dp,一维排序,一维离散化

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
#define A first
#define B second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define dd(x) cout << #x << " = " << (x) << ", "
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using namespace std;
#define pll pair<ll,ll>
const int maxn=1e5+7;
ll n,d[maxn<<1];
ll tot;
vector<int>v;
//**********************************
struct Node{
    ll x,y,v;
    bool operator<(const Node& rhs){
        if(x!=rhs.x)return x<rhs.x;
        return y>rhs.y;
    }
}node[maxn];
void update(ll x,ll val)
{
    while(x<=sz(v)){
        d[x]=max(d[x],val);
        x+=lowbit(x);
    }
}
ll query(int x)
{
    ll ans=0;
    while(x){
        ans=max(ans,d[x]);
        x-=lowbit(x);
    }
    return ans;
}
inline int getid(int x){return lower_bound(all(v),x)-v.begin()+1;}
//**********************************
map<pll,ll>Mp;
//**********************************
int main()
{
    fastio;
    cin>>n;
    map<pll,ll>::iterator it;
    rep(i,0,n){
        cin>>node[i].x>>node[i].y>>node[i].v,v.pb(node[i].y);
        pii PII=mp(node[i].x,node[i].y);
        it=Mp.find(PII);
        if(it!=Mp.end())node[i].v+=Mp[PII],Mp.erase(PII);
        Mp.insert(mp(PII,node[i].v));
    }
    sort(all(v));v.erase(unique(all(v)),v.end());
    rep(i,0,n)node[i].y=getid(node[i].y);
    ll tot=0;
    sort(node,node+n);
    ll ans=0;
    rep(i,0,n){
            ll tmp;
            tmp=query(node[i].y-1)+node[i].v;
            ans=max(ans,tmp);
            update(node[i].y,tmp);
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2018-09-04 16:29  牛奶加咖啡~  阅读(203)  评论(0编辑  收藏  举报