求助,不知道为啥写挂了
查看原帖
求助,不知道为啥写挂了
1036693
carp_oier楼主2023/11/7 22:33

初步判定是 calc 函数写挂了。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rl register ll
#define fom(i, a) for(rl i=a; i; -- i)
#define foa(i, a, b) for(rl i=a; i < b; ++ i)
#define fos(i, a, b) for(rl i=a; i <= b; ++ i)
#define fop(i, a, b) for(rl i=a; i >= b; -- i)

namespace IO
{
    int pp1=-1; const int pp2=(1<<21)-1; char buf[1<<21],*p1=buf,*p2=buf,buffer[1<<21];
    inline void flush() {fwrite(buffer,1,pp1+1,stdout); pp1=-1;}
    inline void pc(const char ch) {if(pp1==pp2) flush(); buffer[++pp1]=ch;}
    inline char gc(){ return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
    template <class T> inline void r(T &res){char ch=getchar();bool f=0; res=0;for(; !isdigit(ch);ch=getchar()) f|=ch=='-'; for(;isdigit(ch); ch=getchar()) res=(res<<1)+(res<<3)+(ch^48); res=f?~res+1:res;}
    template <class T> inline void read(T &res){char ch=gc();bool f=0;res=0; for(;!isdigit(ch);ch=gc()) f|=ch=='-'; for(;isdigit(ch);ch=gc()) res=(res<<1)+(res<<3)+(ch^48); res=f?~res+1:res;}
    template <class T> inline void ww(T x) { if(!x) pc('0'); else { static int stk[21]; int top = 0; if(x<0) pc('-'),x=~x+1; while(x) stk[top++]=x%10, x/=10; while(top--) pc(stk[top]^48);}}
}

#define ws IO::pc(' ')
#define wl IO::pc('\n')
#define r(x) IO::r(x)
#define ww(x) IO::ww(x)
#define read(x) IO::read(x)
#define flush() IO::flush()

const ll N = 1e5 + 10, M = N << 1, K = 5e6 + 10;

ll n, m, root[N], dep[N], fa[N][22], idx;

ll tot, h[N], res[K], sum[K], lson[K], rson[K], ans[N];

struct edge { ll v, ne; } e[M];

#define add(a, b) ( e[ ++ tot] = { b, h[a]}, h[a] = tot )

inline void dfs(ll u, ll p)
{
    dep[u] = dep[p] + 1, fa[u][0] = p;
    fos(i, 1, 20) fa[u][i] = fa[fa[u][i - 1]][i - 1];
    for(rl i=h[u]; i; i = e[i].ne) { ll v = e[i].v; if(v == p) continue; dfs(v, u); }
}

inline ll lca(ll a, ll b)
{
    if(dep[a] < dep[b]) swap(a, b);
    fom(i, 20) if(dep[fa[a][i]] >= dep[b]) a = fa[a][i];
    if(a == b) return a; fom(i, 20) if(dep[fa[a][i]] != dep[fa[b][i]]) a = fa[a][i], b = fa[b][i];
    return fa[a][0]; 
}

inline void pushup(ll u)
{
    if(sum[lson[u]] < sum[rson[u]])
        res[u] = res[rson[u]], sum[u] = sum[rson[u]];
    else res[u] = res[lson[u]], sum[u] = sum[lson[u]];
}

inline ll merge(ll a, ll b, ll l, ll r)
{
    if(!a) return b; if(!b) return a;
    if(l == r) { sum[a] += sum[b]; return a; }
    ll mid = l + r >> 1;
    lson[a] = merge(lson[a], lson[b], l, mid);
    rson[a] = merge(rson[a], rson[b], mid + 1, r);
    return pushup(a), a;
}

inline ll update(ll u, ll l, ll r, ll co, ll val)
{
    if(!u) u = ++ idx;
    if(l == r) { sum[u] += val; res[u] = co; return u; }
    ll mid = l + r >> 1;
    if(co <= mid) lson[u] = update(lson[u], l, mid, co, val);
    if(co > mid) rson[u] = update(rson[u], mid + 1, r, co, val);
    return pushup(u), u;
}

inline void calc(ll u, ll p)
{
    for(rl i=h[u]; i; i = e[i].ne)
    {
        ll v = e[i].v; if(v == p) continue;
        calc(v, u); root[u] = merge(root[u], root[v], 1, 100000);
    }
    ans[u] = res[root[u]];
    if(!sum[root[u]]) ans[u] = 0;
}

int main()
{
    freopen("1.in", "r", stdin);

    read(n), read(m);
    
    tot = 1;
    foa(i, 1, n) { ll a, b; read(a), read(b); add(a, b), add(b, a); }
    dfs(1, 0);

    while(m -- )
    {
        ll a, b, c; read(a), read(b), read(c);
        root[a] = update(root[a], 1, 100000, c, 1), root[b] = update(root[b], 1, 100000, c, 1);
        ll t = lca(a, b);
        root[t] = update(root[t], 1, 100000, c, -1), root[fa[t][0]] = update(root[fa[t][0]], 1, 100000, c, -1); 
    }
    calc(1, 0);
    fos(i, 1, n) ww(ans[i]), wl;
    return flush(), 0;
}
2023/11/7 22:33
加载中...