30分SBT求调
查看原帖
30分SBT求调
523541
wdy1028楼主2021/10/12 15:05

自从Treap打炸之后,孩子换了个思路,结果又炸了,来个大佬救救孩子吧

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const int INF = 2147483647;
const int S = 3*1e6+5;
struct SBT {int lc,rc,val,size;}tr[S];
int cnt,ans,add,root;
inline void zag(int& x)
{
    int y = tr[x].lc;
    tr[x].lc = tr[y].rc;
    tr[y].rc = x;
    tr[y].size = tr[x].size;
    tr[x].size = tr[tr[x].lc].size+tr[tr[x].rc].size+1;
    x = y;
}
inline void zig(int& x)
{
    int y = tr[x].rc;
    tr[x].rc = tr[y].lc;
    tr[y].lc = x;
    tr[y].size = tr[x].size;
    tr[x].size = tr[tr[x].lc].size+tr[tr[x].rc].size+1;
    x = y;
}
inline void maintain(int& p,bool flag)
{   
    if(!p) return;
    if(!flag)
    {
        if(tr[tr[tr[p].lc].lc].size > tr[tr[p].rc].size) zag(p);
        else if(tr[tr[tr[p].lc].rc].size > tr[tr[p].rc].size) zig(tr[p].lc),zag(p);
        else return;
    }
    else
    {
        if(tr[tr[tr[p].rc].rc].size > tr[tr[p].lc].size) zig(p); 
        else if(tr[tr[tr[p].rc].lc].size > tr[tr[p].lc].size) zag(tr[p].rc),zig(p); 
        else return;       
    }
    maintain(tr[p].lc,false);
    maintain(tr[p].rc,true);
    maintain(p,false);
    maintain(p,true);
}
inline void insert(int& p,int val)
{
    if(!p)
    {
        p = ++cnt;
        tr[p].lc = tr[p].rc = 0;
        tr[p].size = 1;
        tr[p].val = val;
    }
    else
    {
        ++tr[p].size;
        if(val < tr[p].val) insert(tr[p].lc,val);
        else insert(tr[p].rc,val);
        maintain(p,(val >= tr[p].val));
    }
}
inline int remove(int& p,int val)
{
    --tr[p].size;
    if(tr[p].val == val || (tr[p].val > val && !tr[p].lc) || (tr[p].val < val && !tr[p].rc))
    {
        int tmp = tr[p].val;
        if(!tr[p].lc || tr[p].rc) p = tr[p].lc+tr[p].rc;
        else tr[p].val = remove(tr[p].lc,tr[p].val+1);
        return tmp;
    }
    else if(val < tr[p].val) return remove(tr[p].lc,val);
    else return remove(tr[p].rc,val);
}
inline int queryrank(int& p,int k)
{
    int s = tr[tr[p].lc].size+1;
    if(k == s) return tr[p].val;
    else if(s < k) return queryrank(tr[p].rc,k-s);
    else return queryrank(tr[p].lc,k);
}
inline LL read()
{
    LL x=0,f=1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') f = -1;ch = getchar();}
    while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
    return f*x;
}
int main(int argc,char *argv[])
{ 
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);   
    int n = read(),minn = read(),j,k;
    char ch;
    for(int i = 1;i <= n;++i)
    {
        cin>>ch>>k;        
        if(ch == 'I' && k >= minn) insert(root,k-add);
        else if(ch == 'A') add += k;
        else if(ch == 'S')
        {
            add -= k;
            while(tr[root].size && (j = queryrank(root,1))+add < minn)
                remove(root,j),++ans;
        }
        else if(ch == 'F')
        {
            if(k > tr[root].size) puts("-1");
            else printf("%d\n",queryrank(root,tr[root].size-k+1)+add);
        }
    }
    printf("%d\n",ans);
    //printf("Tide used = %.0lf.ds\n",((double)clock()/(double)CLOCKS_PER_SEC) * 1000.0);
    return 0;
}  
2021/10/12 15:05
加载中...