zoukankan      html  css  js  c++  java
  • 树状数组优化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) << "
    "
    #define endl "
    "
    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;
    }
  • 相关阅读:
    Hihocoder 1275 扫地机器人 计算几何
    CodeForces 771C Bear and Tree Jumps 树形DP
    CodeForces 778D Parquet Re-laying 构造
    CodeForces 785E Anton and Permutation 分块
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale 二分
    Hexo Next 接入 google AdSense 广告
    如何统计 Hexo 网站的访问地区和IP
    Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
    通过ODBC接口访问人大金仓数据库
  • 原文地址:https://www.cnblogs.com/klaycf/p/9585239.html
Copyright © 2011-2022 走看看