QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#705#456132#8546. Min or Max 2HuTaoHuTaoFailed.2024-06-27 11:40:202024-06-27 11:40:21

詳細信息

Extra Test:

Invalid Input

input:

1335
15
11 6 15 2 3 13 4 14 9 12 1 5 10 8 7
14 6 7 15 8 9 2 5 4 12 1 10 13 3 11
50
40 21 20 30 36 8 17 47 46 7 18 29 1 6 37 45 39 26 49 22 48 41 25 35 31 44 43 27 5 13 32 50 4 42 34 38 12 16 3 14 10 2 15 9 24 11 23 19 33 28
8 25 11 36 38 23 21 47 4 32 49 50 41 46 30 6 12 13 27 16 48 35 33 1 37 24 45...

output:


result:

FAIL Integer parameter [name=n] equals to 1, violates the range [2, 500000] (test case 327, stdin, line 980)

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456132#8546. Min or Max 2HuTaoAC ✓270ms34820kbC++142.6kb2024-06-27 11:20:582024-06-27 11:20:59

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;

int n, a[N], b[N], pa[N], pb[N], X, Y;
int t[N * 4][3], pos[N];
int x[3][3] = {
    {0, 1, 1}, 
    {1, 1, 2}, 
    {1, 2, 2}
};
int l[N], r[N], s[N];

#define ls (u << 1)
#define rs (u << 1 | 1)
inline void Init(int u, int d)
{
    t[u][0] = x[d][0];
    t[u][1] = x[d][1];
    t[u][2] = x[d][2];
}
inline void PushUp(int u)
{
    t[u][0] = t[rs][t[ls][0]];
    t[u][1] = t[rs][t[ls][1]];
    t[u][2] = t[rs][t[ls][2]];
}
inline void Build(int u, int a, int b)
{
    if(a == b)
    {
        pos[a] = u;
        Init(u, 1);
        return ;
    }
    int mid = (a + b) >> 1;
    Build(ls, a, mid);
    Build(rs, mid + 1, b);
    PushUp(u);
}
inline void Modify(int i)
{
    int u = pos[i];
    Init(u, (a[i] <= X) + (b[i] > Y));
    for(u >>= 1; u; u >>= 1) PushUp(u);
}
inline bool Query()
{
    return t[1][(a[1] <= X) + (b[1] > Y)] == 2;
}

inline void Work(int res[])
{
    for(int i = 1; i <= n; i ++ )
        pa[a[i]] = pb[b[i]] = i;
    Build(1, 2, n);
    X = 0, Y = 0;
    for(int i = 1; i <= n; i ++ )
    {
        X = i;
        if(pa[i] > 1) Modify(pa[i]);
        while(Y < n && Query())
        {
            Y ++ ;
            if(pb[Y] > 1) Modify(pb[Y]);
        }
        res[i] = Y;
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T -- )
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i ++ ) scanf("%d", &b[i]);

        Work(r);
        for(int i = 1; i <= n; i ++ )
        {
            a[i] = n - a[i] + 1;
            b[i] = n - b[i] + 1;
        }
        Work(l);
        reverse(l + 1, l + n + 1);
        for(int i = 1; i <= n; i ++ )
            l[i] = n - l[i] + 1;
        
        for(int i = 1; i <= n; i ++ )
        {
            // printf("#%d %d %d\n", i, l[i], r[i]);
            if(l[i] > r[i]) continue ;
            if(i >= r[i])
            {
                s[i - r[i]] ++ ;
                s[i - l[i] + 1] -- ;
            }
            else if(i <= l[i])
            {
                s[l[i] - i] ++ ;
                s[r[i] - i + 1] -- ;
            }
            else
            {
                s[0] ++;
                s[1] ++ ;
                s[i - l[i] + 1] -- ;
                s[r[i] - i + 1] -- ;
            }
        }
        for(int i = 1; i < n; i ++ ) s[i] += s[i - 1];

        for(int i = 0; i < n; i ++ ) printf("%d ", s[i]);
        puts("");

        for(int i = 0; i <= n; i ++ ) l[i] = r[i] = s[i] = 0;
    }
    return 0;
}