样例不过求调qwq
查看原帖
样例不过求调qwq
742017
zhangxiao666楼主2024/3/15 20:15
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int INF = 1e9;
int n, m;
int a[N], f[N], pre[N];

struct tree1
{
    int tr[N];

    int lowbit(int x) {return x & -x;}

    void build()
    {
        for (int i = 1; i <= m; i++) tr[i] = -INF;
    }

    void modify(int pos, int x)
    {
        while (pos <= m)
        {
            tr[pos] = max(tr[pos], x);
            pos += lowbit(pos);
        }
    } 

    int calc(int x)
    {
        int ans = 0;
        while (x)
        {
            ans = max(ans, tr[x]);
            x -= lowbit(x);
        }
        return ans;
    }

    int query(int l, int r)
    {
        return calc(r);
    }
}tr1;

struct tree2
{
    int tr[N];

    int lowbit(int x) {return x & -x;}

    void build()
    {
        for (int i = 1; i <= m; i++) tr[i] = -INF;
    }

    void modify(int pos, int x)
    {
		tr[pos] = max(tr[pos], x);
    } 

    int calc(int x)
    {
        return tr[x];
    }

    int query(int l, int r)
    {
        return calc(r);
    }
}tr2;


struct tree3
{
    int tr[N];

    int lowbit(int x) {return x & -x;}

    void build()
    {
        for (int i = 1; i <= m; i++) tr[i] = -INF;
    }

    void modify(int pos, int x)
    {
        while (pos)
        {
            tr[pos] = max(tr[pos], x);
            pos -= lowbit(pos);
        }
    } 

    int calc(int x)
    {
        int ans = 0;
        while (x <= m)
        {
            ans = max(ans, tr[x]);
            x += lowbit(x);
        }
        return ans;
    }

    int query(int l, int r)
    {
        return calc(l);
    }
}tr3;

void lisanhua()
{
    sort(a + 1, a + n + 1); int m  = unique(a + 1, a + n + 1) - a - 1;
    for (int i = 1; i <= n; i++) 
    {
    	pre[i] = lower_bound(a + 1, a + m + 1, pre[i]) - a; 
    }
}

void work()
{
    cin >> n;
    for (int i = 1; i <= n; i++) f[i] = 0;
    for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1], pre[i] = a[i];
    lisanhua();
    tr1.build(), tr2.build(), tr3.build();
    for (int i = 1; i <= n; i++)
    {
        f[i] =  max({tr1.query(0, pre[i] - 1) + i, tr2.query(pre[i], pre[i]), tr3.query(pre[i] + 1, n) - i});
        tr1.modify(pre[i], f[i] - i), tr2.modify(pre[i], f[i]), tr3.modify(pre[i], f[i] + i);
    }
    cout << f[n] << "\n";
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1; cin >> T;
    while (T--) work();
    return 0;
}
2024/3/15 20:15
加载中...