50分求助

P3793 由乃救爷爷

Jairon314 @ 2021-05-08 20:29:21

rt,刚学笛卡尔树,这个题被卡的RE+MLE,求查错/kel,码风不好,请见谅......

#include <algorithm>
// #include <iostream>
// #include <cstring>
#include <cstdio>
// #include <cmath>
#include <stack>
// using namespace std;

// #define int long long

/***************快读***************/

namespace IO {
char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
inline char gc () {return p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;}

#ifndef ONLINE_JUDGE
#endif

#define gc getchar

template<class I>
inline void read(I &x) {
    x = 0; I f = 1; char c = gc();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc(); }
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = gc(); }
    x *= f;
}

template<class I>
inline void write(I x) {
    if(x == 0) {putchar('0'); return;}
    I tmp = x > 0 ? x : -x;
    if(x < 0) putchar('-');
    register int cnt = 0;
    while(tmp > 0) {
        buf1[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while(cnt > 0) putchar(buf1[--cnt]);
}

#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')

} using namespace IO;

/***************快读***************/

#define maxn 5000010

int n,m,seed;
int root;

struct Descartes_Fold{
    struct Des_Tree{
        int son[2];
        int val;
    }tree[maxn<<1];

    std::stack<int> Stack;
    int now,pos;

    inline void insert(register int ID){
        now=pos;
        register int rest;
        while(now&&tree[Stack.top()].val<tree[ID].val){
            rest=Stack.top();
            Stack.pop();
            --now;
        }
        if(now){tree[Stack.top()].son[1]=ID;}
        else{root=ID;}
        if(now<pos){tree[ID].son[0]=rest;}
        pos=++now;
        Stack.push(ID);
    }

    inline void work(){
        register int ans=0,res=0;
        for(register int i=1;i<=n;++i){
            ans^=i*(tree[i].son[0]+1);
            res^=i*(tree[i].son[1]+1);
        }
        out(ans),outn(res);
    }

    inline void dfs(register int now){
        if(now){
            out(now);
            dfs(tree[now].son[0]);
            dfs(tree[now].son[1]);
        }
    }
}sc;

struct LCA_Fold{
    int top[maxn],tot[maxn],depth[maxn];
    int id[maxn],son[maxn],fa[maxn];
    int dfs_cnt;

    inline int dfs1(register int now,register int f,register int dep){
        depth[now]=dep;
        fa[now]=f;
        tot[now]=1;
        register int maxson=-1;
        for(register int i=0;i<=1;++i){
            if(!sc.tree[now].son[i]){continue;}
            tot[now]+=dfs1(sc.tree[now].son[i],now,dep+1);
            if(maxson<tot[sc.tree[now].son[i]]){
                maxson=tot[sc.tree[now].son[i]];
                son[now]=sc.tree[now].son[i];
            }
        }
        return tot[now];
    }

    inline void dfs2(register int now,register int topf){
        id[now]=++dfs_cnt;
        top[now]=topf;
        if(!son[now]){return;}
        dfs2(son[now],topf);
        for(register int i=0;i<=1;i++){
            if(!sc.tree[now].son[i]){continue;}
            if(id[sc.tree[now].son[i]]){continue;}
            dfs2(sc.tree[now].son[i],sc.tree[now].son[i]);
        }
    }

    inline int LCA(register int x,register int y){
        while(top[x]!=top[y]){
            if(depth[top[x]]<depth[top[y]]){std::swap(x,y);}
            x=fa[top[x]];
        }
        if(depth[x]>depth[y]){std::swap(x,y);}
        return sc.tree[x].val;
    }
}lca;

/**data**/

namespace GenHelper{
    unsigned z1,z2,z3,z4,b;
    unsigned rand_()
    {
    b=((z1<<6)^z1)>>13;
    z1=((z1&4294967294U)<<18)^b;
    b=((z2<<2)^z2)>>27;
    z2=((z2&4294967288U)<<2)^b;
    b=((z3<<13)^z3)>>21;
    z3=((z3&4294967280U)<<7)^b;
    b=((z4<<3)^z4)>>12;
    z4=((z4&4294967168U)<<13)^b;
    return (z1^z2^z3^z4);
    }
}

void srand(unsigned x){
    using namespace GenHelper;
    z1=x; z2=(~x)^0x233333333U; z3=x^0x1234598766U; z4=(~x)+51;
}

inline int read_(){
    using namespace GenHelper;
    register int a=rand_()&32767;
    register int b=rand_()&32767;
    return a*32768+b;
}

/**data**/

int /*signed*/ main(){
    read(n),read(m),read(seed);
    srand(seed);
    for(register int i=1,u;i<=n;++i){
        sc.tree[i].val=read_();
        sc.insert(i);
    }
    lca.dfs1(root,0,1);
    lca.dfs2(root,root);
    register int u,v;
    unsigned long long ans=0;
    while(m--){
        u=read_(),v=read_();
        u=u%n+1,v=v%n+1;
        if(u>v){std::swap(u,v);}
        ans+=lca.LCA(u,v);
    }
    outn(ans);
    return 0;
}

by Prean @ 2021-05-08 20:36:13

快读和快输很明显没用(


|