书接上文,关于前向星和 vector
查看原帖
书接上文,关于前向星和 vector
310818
蒟酱厂妹楼主2022/4/26 00:44

经过检查发现我的树剖有问题,然后我取了第一页的最后一片题解对拍发现还是不对:

#include <bits/stdc++.h>
using namespace std;

template <class T>
inline bool read(T &ret)
{
    char c;
    int sgn;
    if (c = getchar(), c == EOF)
    {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9'))
    {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}

template <class T>
inline void write(T x)
{
    if (x > 9)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

const int MAXN=1e6+10;

// struct edge
// {
//     int u,v,nex;
// };

struct node
{
    int l,r,v;
    node(int L,int R=-1,int V=0):l(L),r(R),v(V) {}
    bool operator <(const node &o) const
    {
        return l<o.l;
    }
};

// edge e[MAXN<<1];
set<node> s;
// int head[MAXN],cnt=0;
std::vector<int>ed[MAXN];
int dep[MAXN],fa[MAXN],top[MAXN],sz[MAXN],id[MAXN],val[MAXN],rk[MAXN],son[MAXN];
int n,m,tot=0,ans1,ans2;

inline set<node>::iterator split(int pos)
{
    set<node>::iterator it=s.lower_bound(node(pos));
    if(it!=s.end()&&it->l==pos)
        return it;
    it--;
    int L=it->l,R=it->r,V=it->v;
    s.erase(it);
    s.insert(node(L,pos-1,V));
    return s.insert(node(pos,R,V)).first;
}

inline void assignval(int l,int r,int v=0)
{
    set<node>::iterator itr=split(r+1),itl=split(l);
    s.erase(itl,itr);
    s.insert(node(l,r,v));
}

inline int query(int l,int r)
{
    set<node>::iterator itr=split(r+1),itl=split(l);
    int cc1=ans1,cc2=ans1;
    --itr,--itl;
    for(;itl!=itr;--itr)
    {
        cc2+=(itr->r-itr->l+1)*itr->v;
        if(cc2>cc1)
            cc1=cc2;
        if(cc2<0)
            cc2=0;
    }
    ans1=cc2;
    return cc1;
}

// inline void add(int u,int v)
// {
//     e[++cnt].u=u;
//     e[cnt].v=v;
//     e[cnt].nex=head[u];
//     head[u]=cnt;
// }

void dfs1(int u)
{
    sz[u]=1;
   //  for(int i=head[u];i;i=e[i].nex)
   //  {
   //      int v=e[i].v;
	for(int v:ed[u]){
        if(v==fa[u]) continue;
        dep[v]=dep[u]+1;
        fa[v]=u;
        dfs1(v);
        sz[u]+=sz[v];
        if(sz[v]>sz[son[u]])
            son[u]=v;
    }
}

void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=++tot;
    rk[tot]=u;
    if(!son[u])
        return;
    dfs2(son[u],tp);
   //  for(int i=head[u];i;i=e[i].nex)
   //  {
   //      int v=e[i].v;
	for(int v:ed[u]){
        if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
    }
}

inline void update(int x,int y,int v)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        assignval(id[top[x]],id[x],v);
        x=fa[top[x]];
    }
    if(id[x]>id[y]) swap(x,y);
    assignval(id[x],id[y],v);
}

inline int sum(int x,int y)
{
    int ret=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y),swap(ans1,ans2);
        ret=max(ret,query(id[top[x]],id[x]));
        x=fa[top[x]];
    }
    if(id[x]>id[y]) swap(x,y),swap(ans1,ans2);
    swap(ans1,ans2);
    ret=max(ret,query(id[x],id[y]));
    ret=max(ret,ans1+ans2);
    return ret;
}

int main()
{
    read(n);
    for(int i=1;i<=n;i++)
        read(val[i]);
    for(int i=1;i<n;i++)
    {
        int x,y;
        read(x),read(y);
		  ed[x].emplace_back(y);
		  ed[y].emplace_back(x);
      //   add(x,y);
      //   add(y,x);
    }
    dfs1(1);
    dfs2(1,1);
   //  for(int i=1;i<=n;i++)
   //      s.insert(node(i,i,val[rk[i]]));
	for(int i=1;i<=n;i++)std::cerr<<rk[i]<<' ';
    read(m);
    while(m--)
    {
        int op;
        read(op);
        if(op==1)
        {
            int x,y;
            read(x),read(y);
            ans1=0,ans2=0;
            write(sum(x,y));
            putchar('\n');
        }
        else
        {
            int x,y,z;
            read(x),read(y),read(z);
            update(x,y,z);
        }
    }
    return 0;
}

如果我把这篇题解的前向星改成 vector 顺序就会有问题这是为什么QAQ MnZn 写这题已经快 12h 了QAQ

2022/4/26 00:44
加载中...