P4755 Beautiful Pair 笛卡尔树+主席树

Posted kaka0010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P4755 Beautiful Pair 笛卡尔树+主席树相关的知识,希望对你有一定的参考价值。

原题链接:https://www.luogu.com.cn/problem/P4755

题意

分析

正着考虑比较复杂,不妨逆过来思考。我们先确定每个值为最大值时可以产生的贡献是多少。显然,假设a[i]为最大值,那么在[l,r]区间里的数都必须不大于a[i],然后还要满足的条件就是这两个选中的数对必须在a[i]的两侧。如果a[i]位于x位置,那么两个数一定位于[l,x]和[x,r]中,那么我们可以遍历较小的一个区间,去找满足要求的另一个数可以取多少个。这样就变成主席树的裸题了。至于处理不大于a[i]的区间,我用了笛卡尔树,然后再区间分治解决就可以了。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e9;
const int N = 1e5 + 10;
const int M = 1000010;
const int MOD = 1e9 + 7;
int lc[N], rc[N], stk[N], top, val[N], dep[N], f[N][21];
struct node {
    int ls, rs;
    int sum;
}hjt[N*50];
int cnt, rt[N];
ll ans;
void modify(int &now, int pre, int l, int r, int pos) {
    now = ++cnt;
    hjt[now] = hjt[pre];
    hjt[now].sum ++;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid) modify(hjt[now].ls, hjt[pre].ls, l, mid, pos);
    else modify(hjt[now].rs, hjt[pre].rs, mid+1, r, pos);
}
int query(int now, int pre, int ql, int qr, int l, int r) {
    if (ql <= l && qr >= r) return hjt[now].sum - hjt[pre].sum;
    int mid = (l + r) >> 1;
    int ans = 0;
    if (ql <= mid) ans += query(hjt[now].ls, hjt[pre].ls, ql, qr, l, mid);
    if (qr > mid) ans += query(hjt[now].rs, hjt[pre].rs, ql, qr, mid+1, r);
    return ans;
}

void init(int n) {
    for (int i = 0; i <= n; i++) lc[i] = rc[i] = 0;
}

int build(int n) {
    int Rt;
    for (int i = 1; i <= n; i++) {
        cin >> val[i];
        modify(rt[i], rt[i-1], 1, MOD, val[i]);
        while (top && val[stk[top]] < val[i]) { //大根堆
            lc[i] = stk[top], top--;
        }
        if (top) rc[stk[top]] = i;
        stk[++top] = i;
    }
    while (top) Rt = stk[top--];
    return Rt;
}

void dfs(int x, int l, int r) {
    if (!x) return;
    if (x - l <= r - x) {
        for (int i = l; i <= x; i++) {
            ans += query(rt[r], rt[x-1], 1, val[x] / val[i], 1, MOD);
        }
    } else {
        for (int i = x; i <= r; i++) {
            ans += query(rt[x], rt[l-1], 1, val[x] / val[i], 1, MOD);
        }
    }
    dfs(lc[x], l, x-1);
    dfs(rc[x], x+1, r);
}
void solve() {
    int n; cin >> n;
    init(n);
    int Rt = build(n);
    dfs(Rt, 1, n);
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

以上是关于P4755 Beautiful Pair 笛卡尔树+主席树的主要内容,如果未能解决你的问题,请参考以下文章

P4755 Beautiful Pair (数据结构+分治)

P4755 Beautiful Pair (数据结构+分治)

luoguP4755 Beautiful Pair 笛卡尔树+线段树合并+启发式合并

luoguP4755 Beautiful Pair 笛卡尔树+线段树合并+启发式合并

luoguP4755 Beautiful Pair

次小生成树 克鲁斯卡尔修改