Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

非常无聊的用线段树写了一个……0MS

Posted by yyii1111 at 2013-10-10 16:24:14 on Problem 2533
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 10010;

struct node {
    int a, id;

    bool operator < (const node & b) const {
        return a != b.a ? a < b.a : id < b.id;
    }
} a[MAXN];

int dp[MAXN<<2];
int lch[MAXN<<2], rch[MAXN<<2];
int sz;

void build(int l, int r, int rt) {
    if(l == r) {
        dp[rt] = 0;
        return;
    }
    dp[rt] = 0;
    int m = (l + r) >> 1;
    lch[rt] = ++ sz;
    rch[rt] = ++ sz;
    build(l, m, lch[rt]); build(m+1, r, rch[rt]);
}

void pushup(int rt) {
    dp[rt] = max(dp[lch[rt]], dp[rch[rt]]);
}

void update(int p, int c, int l, int r, int rt) {
    if(l == r) {
        dp[rt] = c;
        return;
    }
    int m = (l + r) >> 1;
    if(p <= m) update(p, c, l, m, lch[rt]);
    else update(p, c, m+1, r, rch[rt]);
    pushup(rt);
}

int query(int L, int R, int l, int r, int rt) {
    if(L <= l && r <= R) {
        return dp[rt];
    }
    int m = (l + r) >> 1;
    int ret = -0x3f3f3f3f;
    if(L <= m) ret = max(ret, query(L, R, l, m, lch[rt]));
    if(R  > m) ret = max(ret, query(L, R, m+1, r, rch[rt]));
    return ret;
}

int n;
pair<int,int> sta[MAXN];
int top;

int main() {
    int n;
    while(~scanf("%d", &n)) {
        for(int i=1; i<=n; i++) scanf("%d", &a[i].a), a[i].id = i;
        sort(a+1, a+n+1);
        sz = 1;
        build(0, n, 1);
        int ans = 0, top = 0;
        for(int i=1; i<=n; i++) {
            //update(a[i].id, tmp, 0, n, 1);
            if(a[i].a > a[i-1].a) {
                while(top) {
                    -- top;
                    update(sta[top].first, sta[top].second, 0, n, 1);
                }
            }
            int tmp = query(0, a[i].id-1, 0, n, 1) + 1;
            ans = max(ans, tmp);
            sta[top++] = make_pair(a[i].id, tmp);
        }
        printf("%d\n", ans);
    }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator