P1505 [国家集训队]旅游-树链剖分

P1505 [国家集训队]旅游-树链剖分

码量惊人
在这里插入图片描述
题意 如上
思路
1.板子
2.边权转移到点权上,修改时不同链正常修改,相同链上if(x!=y) update(dfn[x]+1,dfn[y])。
3.其余就是恶心的线段树了。十分恶心
pushup

void pushup(int o){
    t[o].sum=t[ls].sum+t[rs].sum;
    t[o].max=max(t[ls].max,t[rs].max);
    t[o].min=min(t[ls].min,t[rs].min);
}

pushdown

void pushdown(int o){
    if(t[o].lazy){
        t[ls].sum=-t[ls].sum;
        t[rs].sum=-t[rs].sum;
        t[ls].max=-t[ls].max;
        t[ls].min=-t[ls].min;
        t[rs].max=-t[rs].max;
        t[rs].min=-t[rs].min;
        t[ls].lazy^=1;
        t[rs].lazy^=1;
        swap(t[ls].max,t[ls].min);
        swap(t[rs].max,t[rs].min);
        t[o].lazy=0;
    }
}

4.update时一定要pushup

完整代码

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
#define ls o<<1
#define rs o<<1|1
using namespace std;
typedef long long ll;
//======================================
namespace FastIO{
char print_f[105];void read() {}void print() {putchar('\n');}
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth){x = 0;char ch = getchar();ll f = 1;while (!isdigit(ch)){if (ch == '-')f *= -1;ch = getchar();}while (isdigit(ch)){x = x * 10 + ch - 48;ch = getchar();}x *= f;read(oth...);}
template <typename T, typename... T2>
inline void print(T x, T2... oth){ll p3=-1;if(x<0) putchar('-'),x=-x;do{print_f[++p3] = x%10 + 48;}while(x/=10);while(p3>=0) putchar(print_f[p3--]);putchar(' ');print(oth...);}} // namespace FastIO
using FastIO::print;
using FastIO::read;
//======================================
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn = 1e6+5;

struct node{
    int l,r,sum,max,min,lazy;
}t[maxn<<2];
vector<pii>edge[maxn];
void pushup(int o){
    t[o].sum=t[ls].sum+t[rs].sum;
    t[o].max=max(t[ls].max,t[rs].max);
    t[o].min=min(t[ls].min,t[rs].min);
}
void pushdown(int o){
    if(t[o].lazy){
        t[ls].sum=-t[ls].sum;
        t[rs].sum=-t[rs].sum;
        t[ls].max=-t[ls].max;
        t[ls].min=-t[ls].min;
        t[rs].max=-t[rs].max;
        t[rs].min=-t[rs].min;
        t[ls].lazy^=1;
        t[rs].lazy^=1;
        swap(t[ls].max,t[ls].min);
        swap(t[rs].max,t[rs].min);
        t[o].lazy=0;
    }
}
void add(int x,int y,int val){
    edge[x].push_back({y,val});
    edge[y].push_back({x,val});
}
int fa[maxn],deep[maxn],siz[maxn],son[maxn],sonw[maxn];
void dfs1(int u,int fr){
    fa[u]=fr,deep[u]=deep[fr]+1,siz[u]=1;
    for(auto it:edge[u]){
        int v=it.first,val=it.second;
        if(v==fr) continue;
        dfs1(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]) son[u]=v,sonw[u]=val;
    }
}
int top[maxn],dfn[maxn],a[maxn];
void dfs2(int u,int fr,int w){
    top[u]=fr;
    static int cnt=0;
    dfn[u]=++cnt;
    a[cnt]=w;
    if(son[u]) dfs2(son[u],fr,sonw[u]);
    for(auto it:edge[u]){
        int v=it.first,val=it.second;
        if(v==son[u]||v==fa[u]) continue;
        dfs2(v,v,val);
    }
}
void build(int l,int r,int o=1){
    t[o].l=l,t[o].r=r,t[o].sum=0,t[o].max=-inf,t[o].min=inf,t[o].lazy=0;
    if(l==r)  {t[o].sum=a[l],t[o].max=a[l],t[o].min=a[l];return ;}
    int mid=(l+r)>>1;
    build(l,mid,ls);
    build(mid+1,r,rs);
    pushup(o);
}
void update(int x,int y,int o=1){
    if(x==t[o].l&&t[o].l==t[o].r){
        a[t[o].l]=t[o].sum=t[o].max=t[o].min=y;
        t[o].lazy=0;
        return ;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(x<=mid) update(x,y,ls);
    else update(x,y,rs);
    pushup(o);
}
void update2(int l,int r,int o=1){
    if(l==t[o].l&&r==t[o].r){
        t[o].sum=-t[o].sum,t[o].max=-t[o].max,t[o].min=-t[o].min;
        swap(t[o].max,t[o].min);
        t[o].lazy^=1;
        return ;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(r<=mid) update2(l,r,ls);
    else if(l>mid) update2(l,r,rs);
    else{
        update2(l,mid,ls);
        update2(mid+1,r,rs);
    }
    pushup(o);
}
int qurey(int l,int r,int op,int o=1){
    if(l==t[o].l&&r==t[o].r){
        if(op==1) return t[o].sum;
        else if(op==2) return t[o].max;
        else return t[o].min;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(r<=mid) return qurey(l,r,op,ls);
    else if(l>mid) return qurey(l,r,op,rs);
    else{
        if(op==1) return qurey(l,mid,op,ls)+qurey(mid+1,r,op,rs);
        else if(op==2) return max(qurey(l,mid,op,ls),qurey(mid+1,r,op,rs));
        else return min(qurey(l,mid,op,ls),qurey(mid+1,r,op,rs));
    }
}
int get(int x,int y,int op){
    int sum=0,maxe=-inf,mine=inf;
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        if(op==1) sum+=qurey(dfn[top[x]],dfn[x],op);
        else if(op==2) maxe=max(maxe,qurey(dfn[top[x]],dfn[x],op));
        else mine=min(mine,qurey(dfn[top[x]],dfn[x],op));
        x=fa[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    if(x!=y){
        if(op==1) sum+=qurey(dfn[x]+1,dfn[y],op);
        else if(op==2) maxe=max(maxe,qurey(dfn[x]+1,dfn[y],op));
        else mine=min(mine,qurey(dfn[x]+1,dfn[y],op));
    }
    if(op==1) return sum;
    else if(op==2) return maxe;
    return mine;
}
void change(int x,int y){
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        update2(dfn[top[x]],dfn[x]);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    if(x!=y) update2(dfn[x]+1,dfn[y]);
}
char s[105];
int main() {
#ifndef ONLINE_JUDGE
    freopen("H:\\code\\in.in", "r", stdin);
    freopen("H:\\code\\out.out", "w", stdout);
    clock_t c1 = clock();
#endif
//**************************************
    vector<pii>v;v.push_back({0,0});
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int x,y,z;
        cin>>x>>y>>z;
        x++,y++;
        v.push_back({x,y});
        add(x,y,z);
    }
    dfs1(1,0);
    dfs2(1,1,0);
    build(1,n);
    int _;
    cin>>_;
    while(_--){
        cin>>(s+1);
        int x,y;
        cin>>x>>y;
        x++,y++;
        // cout<<(s+1)<<":";
        if(s[1]=='C'){
            x--,y--;
            if(deep[v[x].first]>deep[v[x].second]) x=v[x].first;
            else x=v[x].second;
            update(dfn[x],y);
        }
        else if(s[1]=='N'){
            change(x,y);
        }
        else if(s[1]=='S'){
            cout<<get(x,y,1)<<"\n";
        }
        else if(s[2]=='A'){
            cout<<get(x,y,2)<<"\n";
        }
        else{
            cout<<get(x,y,3)<<"\n";
        }
    }
//**************************************
    
#ifndef ONLINE_JUDGE
    cerr << "Time:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值