BZOJ 4538: [Hnoi2016]网络

这个题目就是特别裸啊,很明显就是先树链剖分,然后在线段树每个节点上维护两个堆,来维护插入和删除,查询的时候就暴力查就好了。

似乎很简单啊,我竟然在luogu上1A了,结果交到bzoj上MLE,看过讨论后把找重儿子改成大于等于就A了。。

不过我感觉这样是假的啊,一条链在线段树上有\(\log^2n\)段,每个暴力插的话就是\(O(n\log^3n)\)啊,但是还跑的挺快的,这卡的也太不满了吧。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (y=max(x,y))
using namespace std;

typedef long long ll;
const int Maxn=410000;

int to[Maxn],nxt[Maxn],first[Maxn],tot=1;
int tl[Maxn],tr[Maxn],u[Maxn],v[Maxn],w[Maxn];
int dep[Maxn],fa[Maxn],ran[Maxn],top[Maxn],son[Maxn];
int n,m,x,y,opt,cnt;
priority_queue<int> t[Maxn][2];

struct node{
    int l,r;
}s[Maxn];

inline void add(int u,int v) {
    to[tot]=v;
    nxt[tot]=first[u];
    first[u]=tot++;
    to[tot]=u;
    nxt[tot]=first[v];
    first[v]=tot++;
}

int dfs1(int root) {
    int siz=1,sizm=0;
    for(int i=first[root];i;i=nxt[i])
        if(!dep[to[i]]) {
            dep[to[i]]=dep[root]+1;
            fa[to[i]]=root;
            int temp=dfs1(to[i]);
            siz+=temp;
            if(temp>=sizm) {
                sizm=temp;
                son[root]=to[i];
            }
        }
    return siz;
}

void dfs2(int root,int topp) {
    top[root]=topp;ran[root]=++cnt;
    if(son[root]) dfs2(son[root],topp);
    for(int i=first[root];i;i=nxt[i])
        if(!ran[to[i]]) dfs2(to[i],to[i]);
}

void build(int root,int l,int r) {
    tl[root]=l,tr[root]=r;
    int mid=l+r>>1;
    if(l==r) return;
    build(root<<1,l,mid);
    build((root<<1)|1,mid+1,r);
}

int gettop(int root) {
    while(!t[root][1].empty()&&t[root][0].top()==t[root][1].top()) t[root][1].pop(),t[root][0].pop();
    return t[root][0].empty()?-1:t[root][0].top();
}

int query(int root,int x) {
    int l=tl[root],r=tr[root];
    int mid=l+r>>1;
    int ans=gettop(root);
    if(l==r) return ans;
    if(x<=mid) return max(ans,query(root<<1,x));
    else return max(ans,query((root<<1)|1,x));
}

void add(int root,int l,int r,int opt,int x) {
    int lc=tl[root],rc=tr[root];
    int mid=lc+rc>>1;
    if(l<=lc&&r>=rc) {
        t[root][opt].push(x);
        return ;
    }
    if(l<=mid) add(root<<1,l,r,opt,x);
    if(r>mid) add((root<<1)|1,l,r,opt,x);
}

int cmp(node a,node b) {
    return a.l<b.l;
}

void change(int x,int y,int w,int opt) {
    int topp=0;
    while(top[x]!=top[y]) {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        s[++topp]=(node){ran[top[x]],ran[x]};
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    s[++topp]=(node){ran[x],ran[y]};
    sort(s+1,s+topp+1,cmp);
    int zhy=0;
    for(int i=1;i<=topp;zhy=s[i++].r)
        if(zhy+1<s[i].l) add(1,zhy+1,s[i].l-1,opt,w);
    if(zhy<n) add(1,zhy+1,n,opt,w);
}

int main() {
//  freopen("test.in","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        add(x,y);
    }dep[1]=1;
    dfs1(1),dfs2(1,1);
    build(1,1,n);
    for(int i=1;i<=m;i++) {
        scanf("%d",&opt);
        if(opt==2) {
            scanf("%d",&x);
            printf("%d\n",query(1,ran[x]));
        }
        else if(opt==1) {
            scanf("%d",&x);
            change(u[x],v[x],w[x],opt);
        }
        else {
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
            change(u[i],v[i],w[i],opt);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shanxieng/p/10155379.html