萌新求助fhq Treap写法只能过样例
查看原帖
萌新求助fhq Treap写法只能过样例
891245
R_aier楼主2023/8/4 00:28
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
#define LOCAL
namespace FastIO
{
    char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh1 = '\n', hh2 = ' ';
    int p, p3 = -1;
    void read() {}
    void print() { buf2[p3] = hh1; }
    inline int getc()
    {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }
    inline void flush()
    {
        fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
    }
    template <typename T, typename... T2>
    inline void read(T &x, T2 &...oth)
    {
        int f = 0;
        x = 0;
        char ch = getc();
        while (!isdigit(ch))
        {
            if (ch == '-')
                f = 1;
            ch = getc();
        }
        while (isdigit(ch))
        {
            x = x * 10 + ch - 48;
            ch = getc();
        }
        x = f ? -x : x;
        read(oth...);
    }
} // namespace FastIO
using FastIO::read;
struct fhq
{
    int pri,val,ls,rs,siz;
    bool tag;
}t[maxn];

#define ls(x) t[x].ls
#define rs(x) t[x].rs
int cnt,rt,n,a[maxn];
int newnode(int val,int pri)
{
    ++cnt;
    t[cnt].ls=t[cnt].rs=0;
    t[cnt].pri=pri;t[cnt].val=val;
    t[cnt].siz=1;
    return cnt;
}
void push_up(int p)
{
    t[p].siz=t[ls(p)].siz+t[rs(p)].siz+1;
}
void push_down(int p)
{
    if(t[p].tag)
    {
        t[p].tag=0;
        swap(ls(p),rs(p));
        if(ls(p))
        t[ls(p)].tag^=1;
        if(rs(p))
        t[rs(p)].tag^=1;
    }
}
int merge(int x,int y)
{
    if(!x||!y)return x+y;
    if(t[x].pri<t[y].pri)
    {
        push_down(x);
        rs(x)=merge(rs(x),y);
        push_up(x);
        return x;
    }
    else
    {
        push_down(y);
        ls(y)=merge(ls(y),x);
        push_up(y);
        return y;
    }
}

void insert(int val,int pri)
{
    rt=merge(rt,newnode(val,pri));
}
int main()
{
    clock_t clockf = clock();
#ifdef LOCAL
    freopen("2.in", "r", stdin);
    freopen("2.out", "w", stdout);
#endif
//=======================================================================================================================
    read(n);
    for(int i=1;i<=n;i++)
    {
        read(a[i]);
    }
    for(int i=1;i<=n;++i)
    {
        insert(i,a[i]);
    }
    int l,r;
    for(int i=1;i<=n;++i)
    {
        push_down(rt);
        printf("%d ", t[ls(rt)].siz + i);
        l = ls(rt), r = rs(rt);
        ls(rt) = rs(rt) = 0;
        t[ls(rt)].tag ^= 1;
        rt = merge(l, r);
    }
//=======================================================================================================================
    cerr << "Time Used: " << clock() - clockf << " ms" << endl;
    return 0;
}
2023/8/4 00:28
加载中...