萌新求助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 Kari5307_yu @ 2020-05-10 20:03:12

fhqTreap ×

fhhTreap √


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

@fhh_orz 您能具体到哪个地方占用较多时间吗?


by serene_analysis @ 2020-05-10 20:27:22

@fhh_orz 您的代码在我的对比下似乎显得常数巨大。


by serene_analysis @ 2020-05-10 20:30:42

@fhh_orz 抱歉恕我无能,我的fhq也T了


by IntrepidStrayer @ 2020-05-10 20:33:31

@for_ccf_j_s_1 我和您T在同三个点上(


by IntrepidStrayer @ 2020-05-10 20:40:44

现在80了


by serene_analysis @ 2020-05-10 20:41:57

@fhh_orz 好像快读很必要?


by serene_analysis @ 2020-05-10 20:42:41

@fhh_orz 好的我过了,这题应该是必需快读


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

@fhh_orz 您试试不使用rand函数


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

@fhh_orz 就是通过自然溢出达到随机,像这样

unsigned long long seed=1;
...
fhq[cnt].key=int(seed*=20070509);

| 下一页