萌新刚学树剖求教

P4114 Qtree1

fzj2007 @ 2020-08-25 14:17:24

RT,不知道为啥,最后四个点T了...没调出来,求教....由于本人刚学树剖,所以请大佬明示错误..(前面是快读,建议跳过..)

//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std; 
using std::cin;
using std::cout;
using std::endl;
namespace IN{
    const int MAX_INPUT = 1000000;
    #define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
    char buf[MAX_INPUT],*p1,*p2;
    template<typename T>inline bool read(T &x) {
        static std::streambuf *inbuf=cin.rdbuf();
        x=0;
        register int f=0,flag=false;
        register char ch=getc();
        while(!isdigit(ch)){
            if (ch=='-') f=1;
            ch=getc();
        }
        if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
        while(isdigit(ch)) {
            x=x*10+ch-48;
            ch=getc();
        }
        x=f?-x:x;
        return flag;
    }
    inline char getch(){
        static std::streambuf *inbuf=cin.rdbuf();
        char ch=getc();
        while(ch!='D'&&ch!='Q'&&ch!='C') ch=getc();
        return ch; 
    }
    template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
       return read(a)&&read(args...);
    }
    #undef getc
}

namespace OUT{
    template<typename T>inline void put(T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top=0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc('\n');
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc('\n');
    }
    inline void putc(const char ch){
        static std::streambuf *outbuf=cout.rdbuf();
        outbuf->sputc(ch);
    }
    inline void putstr(string s){
        for(register int i=0;i<s.length();i++) putc(s[i]);
    }
    template<typename T>inline void put(const char ch,T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top = 0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc(ch);
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc(ch);
    }
    template<typename T,typename ...Args> inline void put(T a,Args ...args){
        put(a);put(args...);
    }
    template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
        put(ch,a);put(ch,args...);
    }
}
using namespace IN;
using namespace OUT;
#define N 100005
#define int long long
#define lc(x) (x<<1)
#define rc(x) (x<<1|1)
#define mid (l+r>>1)
#define max(a,b) (a>b?a:b)
int n,a,b,head[N],dfn[N],rk[N],f[N],num,size[N],son[N],dep[N],top[N],w[N],cnt,t[N<<2]; 
char ch;
struct edge{
    int u,v,nxt,z;
}e[N<<1];
inline void add(int u,int v,int z){
    e[++cnt]=(edge){u,v,head[u],z},head[u]=cnt;
}
namespace ST{//线段树部分
    inline void debug(int x,int l,int r){
        if(l==r) return cout<<x<<" "<<l<<" "<<r<<" "<<t[x]<<endl,void();
        cout<<x<<" "<<l<<" "<<r<<" "<<t[x]<<endl;
        debug(lc(x),l,mid),debug(rc(x),mid+1,r); 
    }
    inline void push_up(int x){
        t[x]=max(t[lc(x)],t[rc(x)]);
    }
    inline void build(int x,int l,int r){
        if(l==r) return t[x]=w[rk[l]],void();
        build(lc(x),l,mid),build(rc(x),mid+1,r);
        push_up(x);
    }
    inline void update(int x,int l,int r,int k,int val){
        if(l==r) return t[x]=val,void();
        if(k<=mid) update(lc(x),l,mid,k,val);
        else update(rc(x),mid+1,r,k,val);
        push_up(x);
    }
    inline int query(int x,int l,int r,int ql,int qr){
        if(l>r) return 0;
        if(ql<=l&&r<=qr) return t[x];
        int ret=0;
        if(ql<=mid) ret=max(ret,query(lc(x),l,mid,ql,qr));
        if(qr>mid) ret=max(ret,query(rc(x),mid+1,r,ql,qr));
        return ret;
    }
}
using namespace ST;
namespace TCP{//树剖部分
    void dfs1(int x,int fath){
        size[x]=1,f[x]=fath,dep[x]=dep[fath]+1;
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].v;
            if(v==fath) continue;
            w[v]=e[i].z;
            dfs1(v,x);
            size[x]+=size[v];
            if(!son[x]||size[v]>size[son[x]]) son[x]=v;
        }
    }
    void dfs2(int x,int ct){
        dfn[x]=++num,rk[num]=x,top[x]=ct;
        if(son[x]) dfs2(son[x],ct);
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].v;
            if(v!=son[x]&&v!=f[x]) dfs2(v,v);
        }
    }
    inline void upd(int x,int val){
        int u=e[x<<1].u,v=e[x<<1].v;
        if(f[v]==u) swap(u,v);
        update(1,1,n,dfn[u],val);
    }
    inline int que(int x,int y){
        if(x==y) return 0;
        int res=-1;
        while(top[x]!=top[y]){
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            res=max(res,query(1,1,n,dfn[top[x]],dfn[x]));
            x=f[top[x]];
        }
        if(dep[x]>dep[y]) swap(x,y);
        return max(res,query(1,1,n,dfn[x]+1,dfn[y]));
    }
}
using namespace TCP;
signed main(signed argc, char const *argv[]){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read(n);
    for(int i=1,u,v,z;i<n;i++) read(u,v,z),add(u,v,z),add(v,u,z);
    dfs1(1,0),dfs2(1,1),build(1,1,n);
    while(1){
        ch=getch();//这里可以保证正确性
        if(ch=='D') break;
        read(a,b);
        if(ch=='C') upd(a,b);
        else put('\n',que(a,b));
    }
    return 0;
}

by cq_loves_Capoo @ 2020-08-25 14:19:34

你不退谷了吗


by fzj2007 @ 2020-08-25 14:31:19

没有大佬看看吗..


by hahazhou @ 2020-08-25 18:38:38

无意义回答,%%%


by andyli @ 2020-08-25 18:45:19

max 定义为宏会导致参数重复计算

@fzj2007


by JK_LOVER @ 2020-08-25 18:47:13

楼上正解

#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define lc(x) (x<<1)
#define rc(x) (x<<1|1)
#define mid (l+r>>1)
int n,head[N],dfn[N],rk[N],f[N],num,size[N],son[N],dep[N],top[N],w[N],cnt,t[N<<2];
struct edge{
    int u,v,nxt,z;
}e[N<<1];
inline void add(int u,int v,int z){
    e[++cnt]=(edge){u,v,head[u],z},head[u]=cnt;
}

    inline void push_up(int x){
        t[x]=max(t[lc(x)],t[rc(x)]);
    }
    inline void build(int x,int l,int r){
        if(l==r) return t[x]=w[rk[l]],void();
        build(lc(x),l,mid),build(rc(x),mid+1,r);
        push_up(x);
    }
    inline void update(int x,int l,int r,int k,int val){
        if(l==r) return t[x]=val,void();
        if(k<=mid) update(lc(x),l,mid,k,val);
        else update(rc(x),mid+1,r,k,val);
        push_up(x);
    }
    inline int query(int x,int l,int r,int ql,int qr){
        if(l>r) return 0;
        if(ql<=l&&r<=qr) return t[x];
        int ret=0;
        if(ql<=mid) ret=max(ret,query(lc(x),l,mid,ql,qr));
        if(qr>mid) ret=max(ret,query(rc(x),mid+1,r,ql,qr));
        return ret;
    }

    void dfs1(int x,int fath){
        size[x]=1,f[x]=fath,dep[x]=dep[fath]+1;
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].v;
            if(v==fath) continue;
            w[v]=e[i].z;
            dfs1(v,x);
            size[x]+=size[v];
            if(!son[x]||size[v]>size[son[x]]) son[x]=v;
        }
    }
    void dfs2(int x,int ct){
        dfn[x]=++num,rk[num]=x,top[x]=ct;
        if(son[x]) dfs2(son[x],ct);
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].v;
            if(v!=son[x]&&v!=f[x]) dfs2(v,v);
        }
    }
    inline void upd(int x,int val){
        int u=e[x<<1].u,v=e[x<<1].v;
        if(f[v]==u) swap(u,v);
        update(1,1,n,dfn[u],val);
    }
    inline int que(int x,int y){
        if(x==y) return 0;
        int res=0;
        while(top[x]!=top[y]){
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            res=max(res,query(1,1,n,dfn[top[x]],dfn[x]));
            x=f[top[x]];
        }
        if(dep[x]>dep[y]) swap(x,y);
        if(dfn[x] == dfn[y]) return res;
        return max(res,query(1,1,n,dfn[x]+1,dfn[y]));
    }
int main(){
    cin>>n;
    for(int i=1,u,v,z;i<n;i++)
    {
        cin>>u>>v>>z;add(u,v,z),add(v,u,z);
     } 
    dfs1(1,0),dfs2(1,1),build(1,1,n);
    while(1){
        char ch[10];
        cin>>ch;
        if(ch[0]=='D') break;
        int a,b;
        cin>>a>>b;
        if(ch[0]=='C') upd(a,b);
        else cout<<que(a,b)<<endl;
    }
    return 0;
}

by fzj2007 @ 2020-08-25 19:11:41

@andyli 哦感谢%%


|