关于线段树合并空间复杂度的疑问
查看原帖
关于线段树合并空间复杂度的疑问
434015
Calanosay楼主2021/8/18 16:58

难道不是nlogn吗,为什么我开32倍显示mle,开50倍就过了?(同时问一下为什么少开数组会显示mle而不是re啊 对了还有一个点过不了,95pts,有没有大佬顺便debug了(我快饿死了,先跑路了

#include <iostream>
#include <algorithm>

using namespace std;
#define endl '\n'
const int maxn = 1e5+5;
const int maxm = maxn << 1;
const int maxx = maxn * 50;
int ls[maxx],rs[maxx],rt[maxx],ans[maxn],n,m,fa[maxn][25],idx,val[maxx];
int head[maxn],nex[maxm],v[maxm],w[maxm],cnt,dep[maxn];
int cntt[maxx],id[maxx];
void add(int x,int y){
    nex[++cnt] = head[x];
    head[x] = cnt;
    v[cnt] = y;
}
void pushup(int node){
    if (cntt[ls[node]] >= cntt[rs[node]]){
        id[node] = id[ls[node]];
        cntt[node]  = cntt[ls[node]];
        return;
    }
    id[node] = id[rs[node]];
    cntt[node] = cntt[rs[node]];
}
void dfs(int node,int f){
    dep[node] = dep[f] + 1;
    fa[node][0] = f;
    for (int i = 1; i <= 24; ++i) {
        fa[node][i] = fa[fa[node][i - 1]][i - 1];
    }
    for (int i = head[node]; i ; i = nex[i]) {
        int to = v[i];
        if (to == f)    continue;
        dfs(to,node);
    }
}
int lca(int x,int y){
    if (dep[x] < dep[y]) swap(x,y);
    for (int i = 24; i >= 0; --i) {
        if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
    }
    if (x == y) return x;
    for (int i = 24; i >= 0; --i) {
        if (fa[x][i] != fa[y][i]) x = fa[x][i],y = fa[y][i];
    }
    return fa[x][0];
}
void update(int &node, int l, int r, int pos,int v){
    if (!node) node = ++idx;
    if (l == r){
        val[node] += v;
        id[node] = l;
        cntt[node] = val[node];
        return;
    }
    int mid =  l + r >> 1;
    if (pos <= mid) update(ls[node],l,mid,pos,v);
    else update(rs[node],mid+1,r,pos,v);
    pushup(node);
}
int merge(int x,int y,int l,int r){
    if (!x) return y;
    if (!y) return x;
    val[x] += val[y];
    if (l == r){
        cntt[x] = val[x];
        id[x] = l;
        return x;
    }
    int mid = l + r >> 1;
    ls[x]  = merge(ls[x],ls[y],l,mid);
    rs[x] =  merge(rs[x],rs[y],mid+1,r);
    pushup(x);
    return x;
}
void dfs2(int node,int f){
    for (int i = head[node]; i ; i = nex[i]) {
        int to = v[i];
        if (to == f)   continue;
        dfs2(to,node);
        rt[node] = merge(rt[node],rt[to],1,maxn);
    }
    ans[node] = id[rt[node]];
}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n - 1; ++i) {
        int x,y;
        cin >> x >> y;
        add(x,y);
        add(y,x);
    }
    dfs(1,0);
    for (int i = 1; i <= m; ++i) {
        int x,y,z;
        cin >> x >> y >> z;
        update(rt[x],1,maxn,z,1);
        update(rt[y],1,maxn,z,1);
        int f = lca(x,y);
        update(rt[f],1,maxn,z,-1);
        update(rt[fa[f][0]],1,maxn,z,-1);
    }
    dfs2(1,0);
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << endl;
    }
}
2021/8/18 16:58
加载中...