bzoj1537

来源:互联网 发布:vscode js代码高亮 编辑:程序博客网 时间:2024/06/05 15:35

离散化 + 树状数组优化
学到了

#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)#define lb(x) (x&-x)inline int read(void){    int x = 0, c, f = 1;    do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');    do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');    return x * f;}const int N = 101000, INF = 0x3f3f3f3f;struct note{    int y, d;    friend bool operator < (const note &i, const note &j) {        return i.y != j.y ? i.y < j.y : i.d < j.d;    }}Y[N];struct node{    int x, y, z;    friend bool operator < (const node &i,const node &j) {        return i.x != j.x ? i.x < j.x : i.y < j.y;    }}p[N];int cnt[N], n, A, B, Index, f[N];inline int Query(int x) {    int re = -INF;    for ( ; x; x ^= lb(x)) re = max(re, cnt[x]);    return re;}inline void Updata(int pos,int x) {    for (; pos <= Index; pos += lb(pos)) cnt[pos] = max(cnt[pos], x);}inline void init(void) {    sort(Y + 1, Y + n + 1);    for (int i = 1; i <= n; i++) {        Index += Y[i].y != Y[i-1].y;        p[Y[i].d].y = Index;    }     sort(p + 1, p + n + 1);}int main() {    A = read(), B = read(), n = read();    for (int i = 1; i <= n; i++) {        p[i].x = read(), Y[i].y = read();        Y[i].d = i, p[i].z = read();    } init();    int ans = 0;    for (int i = 1; i <= n; i++) {        f[i] = p[i].z + Query(p[i].y);        Updata(p[i].y, f[i]);        ans = max(ans, f[i]);    }    cout << ans << endl;}