bzoj 4553 [Tjoi2016&Heoi2016]序列 dp+树套树/CDQ分治

题面

题目传送门

解法

显然,本题可以说是变形LIS

\(f_i\)为以第\(i\)个位置结尾的最长不下降子序列长度

那么,\(f_i=max(f_j+1)\ \ (a_j≤l_i,r_j≤a_i)\)

对于后面的限制条件,显然就是一个二维偏序问题,直接CDQ分治或树套树

然而不知为什么树套树RE了……

代码(树套树50pts)

#include <bits/stdc++.h>
#define N 100010
using namespace std;
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int lc, rc, mx;
} t[N * 100];
int n, m, tot, siz, l[N], r[N], a[N], f[N], rt[N * 60];
int lowbit(int x) {return x & -x;}
void ins(int &k, int l, int r, int x, int v) {
    if (!k) k = ++tot; t[k].mx = max(t[k].mx, v);
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (x <= mid) ins(t[k].lc, l, mid, x, v);
        else ins(t[k].rc, mid + 1, r, x, v);
}
void modify(int x, int y, int v) {
    for (int i = x; i <= siz; i++)
        ins(rt[i], 1, siz, y, v);
}
int query(int k, int l, int r, int x) {
    if (!k) return 0;
    if (l == r) return t[k].mx;
    int mid = (l + r) >> 1;
    if (x <= mid) return query(t[k].lc, l, mid, x);
    return max(t[t[k].lc].mx, query(t[k].rc, mid + 1, r, x));
}
int ask(int x, int y) {
    int ret = 0;
    for (int i = x; i; i -= lowbit(i))
        ret = max(ret, query(rt[i], 1, siz, y));
    return ret;
}
int main() {
    read(n), read(m); siz = n;
    for (int i = 1; i <= n; i++)
        read(a[i]), l[i] = r[i] = a[i], siz = max(siz, a[i]);
    for (int i = 1; i <= m; i++) {
        int x, y; read(x), read(y);
        l[x] = min(l[x], y), r[x] = max(r[x], y);
        siz = max(siz, y);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        f[i] = ask(l[i], a[i]) + 1;
        modify(a[i], r[i], f[i]);
        ans = max(ans, f[i]);
    }
    cout << ans << "\n";
    return 0;
}

cdq分治

表示对于这种问题树套树来解决真的辣鸡

cdq分治比较好

时间复杂度:\(O(n\ log^2\ n)\)

代码

#include <bits/stdc++.h>
#define N 200010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int op, tim, x, y, id;
    bool operator < (const Node &a) const {
        return x < a.x;
    }
} a[N], t[N];
int n, m, mx, v[N], l[N], r[N], ans[N], f[N];
int lowbit(int x) {return x & -x;}
void modify(int x, int v) {
    for (int i = x; i <= mx; i += lowbit(i))
        if (v == 0) f[i] = v; else chkmax(f[i], v);
}
int query(int x) {
    int ret = 0;
    for (int i = x; i; i -= lowbit(i))
        chkmax(ret, f[i]);
    return ret;
}
void cdq(int l, int r) {
    if (l == r) return;
    int mid = (l + r) >> 1, tx = l, ty = mid + 1;
    for (int i = l; i <= r; i++)
        if (a[i].tim <= mid) t[tx++] = a[i];
            else t[ty++] = a[i];
    for (int i = l; i <= r; i++) a[i] = t[i]; cdq(l, mid);
    sort(a + l, a + mid + 1);
    tx = l, ty = mid + 1;
    while (ty <= r) {
        while (tx <= mid && a[tx].x <= a[ty].x) {
            if (a[tx].op == 1) modify(a[tx].y, ans[a[tx].id]);
            tx++;
        }
        if (a[ty].op == 2) chkmax(ans[a[ty].id], query(a[ty].y) + 1);
        ty++;
    }
    for (int i = l; i < tx; i++) modify(a[i].y, 0);
    cdq(mid + 1, r);
}
int main() {
    read(n), read(m);
    for (int i = 1; i <= n; i++)
        read(v[i]), l[i] = r[i] = v[i];
    for (int i = 1; i <= m; i++) {
        int x, y; read(x), read(y);
        chkmin(l[x], y), chkmax(r[x], y);
    }
    int len = 0; mx = 0;
    a[++len] = (Node) {1, len, 1, 1, 0};
    for (int i = 1; i <= n; i++) {
        a[++len] = (Node) {2, len, v[i], l[i], i};
        a[++len] = (Node) {1, len, r[i], v[i], i};
    }
    for (int i = 1; i <= n; i++) chkmax(mx, r[i]);
    sort(a + 1, a + len + 1);
    cdq(1, len); int ret = 0;
    for (int i = 1; i <= n; i++) chkmax(ret, ans[i]);
    cout << ret << "\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/copperoxide/p/9478713.html
今日推荐