替罪羊树调 4h 无果求助,52pts 1WA 5TLE
查看原帖
替罪羊树调 4h 无果求助,52pts 1WA 5TLE
923947
_sunkuangzheng_楼主2023/7/28 20:08

rt,一下午没调出来 /kk

#include <bits/stdc++.h>
using namespace std;
const double p = 0.75;
const int maxn = 2e5+5;
int siz[maxn],ls[maxn],op,y,c[maxn],ch[maxn][2],t[maxn],fa[maxn],num[maxn],a[maxn],tot = 1,cnt,ms,n,x,lp,cp;
vector <pair<int,int>> acc;
void init(int s,int x){t[s] = x,siz[s] = 1,ls[s] = 0,c[s] = num[s] = 1;}
void pushup(int s){siz[s] = siz[ch[s][0]] + siz[ch[s][1]] + 1,num[s] = num[ch[s][0]] + num[ch[s][1]] + c[s],ls[s] = max(siz[ch[s][0]],siz[ch[s][1]]);}
void build(vector<pair<int,int>> acc,int s,int tp){
    int mid = (acc.size() - 1) / 2;init(acc[mid].first,acc[mid].second);
    ch[s][tp] = acc[mid].first;
    vector<pair<int,int>> rwl,cjr;
    for(int i = 0;i < mid;i ++) rwl.push_back(make_pair(acc[i].first,acc[i].second));
    for(int i = mid+1;i < (int)acc.size();i ++) cjr.push_back(make_pair(acc[i].first,acc[i].second));
    if(mid - 1 >= 0) build(rwl,acc[mid].first,0); if(mid + 1 < (int)acc.size()) build(cjr,acc[mid].first,1); 
    pushup(acc[mid].first);
}
void insert(int s,int x){
    if(x == t[s]) return c[s] ++,num[s] ++,void();
    int k = (x > t[s]);
    if(!ch[s][k]) return ch[s][k] = ++tot,fa[tot] = s,init(ch[s][k],x),pushup(s),void();
    fa[ch[s][k]] = s,insert(ch[s][k],x),pushup(s);
    if((int)((double)siz[s] * p) < ls[s] && s != 1) ms = s;
}
void dfs(int s){
    if(ch[s][0]) dfs(ch[s][0]); 
    if(c[s]) acc.push_back(make_pair(s,t[s]));
    if(ch[s][1]) dfs(ch[s][1]);
    ch[s][0] = ch[s][1] = 0;
}
void rebuild(){
    if(!ms) return ;acc.clear(),dfs(ms);
    build(acc,fa[ms],(ms == ch[fa[ms]][1]));
    while(ms) pushup(ms),ms = fa[ms];
}
void del(int s,int x){
    if(t[s] == x) return c[s] --,num[s] --,void();
    int k = (x > t[s]);del(ch[s][k],x),pushup(s);
}
int gr(int s,int x){
    if(t[s] == x) return num[ch[s][0]] + 1;
    if(x < t[s]) return gr(ch[s][0],x);
    return num[ch[s][0]] + c[s] + gr(ch[s][1],x);
}
int gi(int s,int x){
    if(num[ch[s][0]] >= x) return gi(ch[s][0],x);
    if(num[ch[s][0]] + c[s] >= x) return t[s];
    return gi(ch[s][1],x-num[ch[s][0]]-c[s]);
}
int gp(int s,int x){
    if(!s) return -1e9;
    if(t[s] < x) return max(t[s],gp(ch[s][1],x));
    return gp(ch[s][0],x);
}
int gn(int s,int x){
    if(!s) return 1e9;
    if(t[s] > x) return min(t[s],gn(ch[s][0],x));
    return gn(ch[s][1],x);
}
int main(){
    cin >> n;t[1] = -1e9-7;
    while(n --){
        cin >> op >> x;
        if(op == 1) ms = 0,insert(1,x),rebuild();
        if(op == 2) del(1,x);
        if(op == 3) cout << gr(1,x) << "\n";
        if(op == 4) cout << gi(1,x) << "\n";
        if(op == 5) cout << gp(1,x) << "\n";
        if(op == 6) cout << gn(1,x) << "\n";
    }
    return 0;
}
2023/7/28 20:08
加载中...