萌新求助fhq TLE

P6136 【模板】普通平衡树(数据加强版)

IntrepidStrayer @ 2020-05-10 19:43:31

rt,70pts

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <cstdio>
#include <ctime>
#include <cstdlib>

using namespace std;

#define in __inline__
#define rei register int
char inputbuf[1 << 23], *p1 = inputbuf, *p2 = inputbuf;
#define getchar() (p1 == p2 && (p2 = (p1 = inputbuf) + fread(inputbuf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

in int read() {
    int res = 0;
    char ch = getchar();
    bool f = true;
    for(; ch < '0' || ch > '9'; ch = getchar())
        if(ch == '-') f = false;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        res = res * 10 + (ch ^ 48);
    return f ? res : -res;
}
const int N = 1100015;

int ch[N][2], s[N], r[N], v[N], siz, rt;

in int New(int val) {
    s[++siz] = 1;
    r[siz] = rand();
    v[siz] = val;
    return siz;
}
in void upd(int p) {
    s[p] = s[ch[p][0]] + s[ch[p][1]] + 1;
}

int merge(int x, int y) {
    if(!x || !y) return x + y;
    if(r[x] < r[y]) {
        ch[x][1] = merge(ch[x][1], y);
        upd(x);
        return x;
    } else {
        ch[y][0] = merge(x, ch[y][0]);
        upd(y);
        return y;
    }
}
void split(int p, int val, int &x, int &y) {
    if(!p) {
        x = y = 0;
        return;
    }
    v[p] <= val ? (x = p, split(ch[p][1], val, ch[p][1], y)) :
    (y = p, split(ch[p][0], val, x, ch[p][0]));
    upd(p);
}
in void insert(int val) {
    int x, y;
    split(rt, val - 1, x, y);
    rt = merge(merge(x, New(val)), y);
}
in void erase(int val) {
    int x, y, z;
    split(rt, val, x, z);
    split(x, val - 1, x, y);
    rt = merge(merge(x, merge(ch[y][0], ch[y][1])), z);
}
in int rank(int val) {
    int x, y, ans;
    split(rt, val - 1, x, y);
    ans = s[x] + 1;
    rt = merge(x, y);
    return ans;
}
in int kth(int rk) {
    int p = rt;
    while(p) {
        if(s[ch[p][0]] + 1 == rk) return v[p];
        else if(rk <= s[ch[p][0]]) p = ch[p][0];
        else rk = rk - s[ch[p][0]] - 1, p = ch[p][1];
    }
}
in int prev(int val) {
    int x, y, tmp;
    split(rt, val - 1, x, y);
    tmp = x;
    while(ch[tmp][1]) tmp = ch[tmp][1];
    rt = merge(x, y);
    return v[tmp];
}
in int next(int val) {
    int x, y, tmp;
    split(rt, val, x, y);
    tmp = y;
    while(ch[tmp][0]) tmp = ch[tmp][0];
    rt = merge(x, y);
    return v[tmp];
}

int main() {
    int n = read(), q = read(), opt, x, lst = 0, ans = 0;
    srand(time(0));
    for(; n; --n) insert(read());
    for(; q; --q) {
        opt = read(), x = read() ^ lst;
        if(opt == 1) insert(x);
        else if(opt == 2) erase(x);
        else if(opt == 3) ans ^= lst = rank(x);
        else if(opt == 4) ans ^= lst = kth(x);
        else if(opt == 5) ans ^= lst = prev(x);
        else ans ^= lst = next(x);
    }
    printf("%d\n", ans);
}

by IntrepidStrayer @ 2020-05-10 20:52:34

@for_ccf_j_s_1 还是T了一个点>_<


by serene_analysis @ 2020-05-10 20:56:24

@fhh_orz 这个数据似乎是经过了特殊构造,#1我下了发现全是插入删除,只在最后有一个询问,#3你的代码似乎也是不开O2过不去;而#4则是开O2会莫名T,不开就没事


by serene_analysis @ 2020-05-10 20:56:47

@fhh_orz 我把我代码给您吧,可能我的常数小一些?


by serene_analysis @ 2020-05-10 20:58:40

4号跑了2.1s,#1#3都在1.3以内


by IntrepidStrayer @ 2020-05-10 21:03:56

@for_ccf_j_s_1 人傻常数大石锤, 非常感谢巨佬!


by serene_analysis @ 2020-05-10 21:04:24

@fhh_orz 您客气了


上一页 |