BZOJ 3531: [Sdoi2014]旅行

这题就是树链剖分,对于多种宗教就开多个线段树,动态开点就好了。

记得卡内存

然而一个城市的宗教变了之后还能在变回来,导致我调了一下午。。。

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

typedef long long ll;
const int Maxn=410000;
const int Maxm=4100000;

int to[Maxn],nxt[Maxn],first[Maxn],tot=1;
int dep[Maxn],fa[Maxn],son[Maxn],top[Maxn],ran[Maxn];
int n,q,u,v,cnt,rot[Maxn],a[Maxn],b[Maxn],topp;
int tl[Maxm],tr[Maxm],tn[Maxm],tm[Maxm],ls[Maxm],rs[Maxm];

char getch() {
    char ch=getchar();
    while(ch!='Q'&&ch!='C'&&ch!='S'&&ch!='M'&&ch!='W') ch=getchar();
    return ch;
}

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(sizm<=temp) son[root]=to[i],sizm=temp;
        }
    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]);
}

int newnode(int l,int r,int x=0) {
    int root=++topp;
    tl[root]=l;tr[root]=r;
    tn[root]=tm[root]=x;
    return root;
}

void update(int root) {
    tn[root]=tn[ls[root]]+tn[rs[root]];
    tm[root]=max(tm[ls[root]],tm[rs[root]]);
}

void add(int root,int x,int y) {
    int l=tl[root],r=tr[root],mid=(l+r)>>1;
    if(l==r) {
        tn[root]=tm[root]=y;
        return;
    }
    if(x<=mid) {
        if(!ls[root]) ls[root]=newnode(l,mid,y);
        add(ls[root],x,y);
    }
    else {
        if(!rs[root]) rs[root]=newnode(mid+1,r,y);
        add(rs[root],x,y);
    }
    update(root);
}

int tsum(int root,int l,int r) {
    int lc=tl[root],rc=tr[root],mid=(lc+rc)>>1;
    if(l<=lc&&r>=rc) return tn[root];
    int ans=0;
    if(l<=mid&&ls[root]) ans+=tsum(ls[root],l,r);
    if(r>mid&&rs[root]) ans+=tsum(rs[root],l,r);
    return ans;
}

int tmax(int root,int l,int r) {
    int lc=tl[root],rc=tr[root],mid=(lc+rc)>>1;
    if(l<=lc&&r>=rc) return tm[root];
    int ans=0;
    if(l<=mid&&ls[root]) qmax(ans,tmax(ls[root],l,r));
    if(r>mid&&rs[root]) qmax(ans,tmax(rs[root],l,r));
    return ans;
}

int qsum(int u,int v,int x) {
    int ans=0;
    while(top[u]!=top[v]) {
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        ans+=tsum(rot[x],ran[top[u]],ran[u]);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    return ans+tsum(rot[x],ran[u],ran[v]);
}

int qmx(int u,int v,int x) {
    int ans=0;
    while(top[u]!=top[v]) {
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        qmax(ans,tmax(rot[x],ran[top[u]],ran[u]));
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    return max(ans,tmax(rot[x],ran[u],ran[v]));
}

void change(int root,int x,int y) {
    int l=tl[root],r=tr[root],mid=(l+r)>>1;
    if(l==r) {
        tn[root]=tm[root]=y;
        return;
    }
    if(x<=mid) change(ls[root],x,y);
    else change(rs[root],x,y);
    update(root);
}

int main() {
//  freopen("test.in","r",stdin);
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
    for(int i=1;i<n;i++) {
        scanf("%d%d",&u,&v);
        add(u,v);
    }dep[1]=1;
    dfs1(1);dfs2(1,1);
    for(int i=0;i<=110000;i++) rot[i]=newnode(1,n+1);
    for(int i=1;i<=n;i++) add(rot[b[i]],ran[i],a[i]);
    while(q--) {
        if(getch()=='C')
            if(getch()=='C') {
                scanf("%d%d",&u,&v);
                change(rot[b[u]],ran[u],0);
                b[u]=v;
                add(rot[b[u]],ran[u],a[u]);
            }
            else {
                scanf("%d%d",&u,&v);
                change(rot[b[u]],ran[u],a[u]=v);
            }
        else
            if(getch()=='S') {
                scanf("%d%d",&u,&v);
                printf("%d\n",qsum(u,v,b[u]));
            }
            else {
                scanf("%d%d",&u,&v);
                printf("%d\n",qmx(u,v,b[u]));
            }
    }
    return 0;
}

转载于:https://www.cnblogs.com/shanxieng/p/10158406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值