Tree chain split Template Title summary

Source: Internet
Author: User
Tags bitset printf split stdin strcmp cmath

Records a few tree-linked template questions, including point manipulation, edge manipulation, and direction-oriented operations. Point Operation

HDU 3966

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <queue> #include <tr1/unordered_set>//#include <tr1/unordered_map> #include <

bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo

R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 50000+200; struct sad {int to,next;}
g[maxn<<2];

int h[maxn],si; void Add (int u, int v) {g[si].to=v;
    G[si].next=h[u];
h[u]=si++;
} int SIZ[MAXN],DEP[MAXN];
int FA[MAXN],SON[MAXN],TOP[MAXN];
    void dfs1 (int u,int f,int d) {fa[u] = f;
    Dep[u] = D;
    Siz[u] = 1;
    Son[u] =-1;
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
            if (v! = f) {DFS1 (v,u,d+1);
            Siz[u] + = Siz[v]; if (son[u]==-1| |
        SIZ[SON[U]]&LT;SIZ[V]) Son[u]=v;
}}} int p[maxn],fp[maxn],pos;
    void dfs2 (int u,int sf) {top[u]=sf;
    p[u]=pos++;
    Fp[p[u]]=u;
    if (son[u] = =-1) return;
    DFS2 (SON[U],SF);
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
    if (son[u]!=v&&fa[u]!=v) DFS2 (V,V);
}} int RS[MAXN];
        void upp (int x,int p) {while (x<=pos) {rs[x]+=p;
    x+=x&-x;
    }} int get (int x) {int ret=0;
        while (x) {ret + = rs[x];
    x-=x&-x;
} return ret; } void Change (int u,inT v,int d) {int f1=top[u], f2=top[v];
            while (f1! = F2) {if (Dep[f1] < DEP[F2]) {swap (F1,F2);
        Swap (U,V);
        } upp (P[F1],D);
        UPP (p[u]+1,-d);
        U=FA[F1];
    F1=top[u];
    } if (Dep[u] > Dep[v]) swap (U,V);
    UPP (P[U],D);
UPP (p[v]+1,-d);

} int A[MAXN];
    int main () {//freopen ("Input.txt", "R", stdin);
    int n,m,p,u,v,d;
    char c;
        while (~SCANF ("%d%d%d", &n,&m,&p)) {clr (h,-1);
        si=0;
        CLR (rs,0);
        Pos=1;
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
            for (int i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);
            Add (U,V);
        Add (V,u);
        } DFS1 (1,-1,1);
        DFS2 (a);
        for (int. i=1;i<=n;i++) Change (i,i,a[i]);
            for (int i=0;i<p;i++) {scanf ("%c", &c);
                if (c = = ' Q ') {scanf ("%d", &u);
            printf ("%d\n", Get (P[u]));
                }else{scanf ("%d%d%d", &u,&v,&d);
                if (c = = ' D ') d*=-1;
            Change (U,V,D);
}}} return 0;
 }

Hysbz 1036

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <queue>/#include <tr1/unordered_set>//#include <tr1/unordered_map> #include <

bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo

R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 30000 + 30; struct edge{int to,next;}
g[maxn<<2];
int h[maxn],si; void Add (int U,int v) {g[si].to=v;
    G[si].next=h[u];
h[u]=si++;
} int SIZ[MAXN],DEP[MAXN];
int FA[MAXN],SON[MAXN],TOP[MAXN];
    void dfs1 (int u,int f,int d) {fa[u]=f;
    Dep[u]=d;
    Siz[u]=1;
    Son[u]=-1;
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
            if (v^f) {DFS1 (v,u,d+1);
            SIZ[U]+=SIZ[V]; if (son[u]==-1| |
        SIZ[SON[U]]&LT;SIZ[V]) Son[u]=v;
}}} int p[maxn],fp[maxn],pos;
    void dfs2 (int u,int sf) {top[u]=sf;
    p[u]=pos++;
    Fp[p[u]]=u;
    if (son[u]==-1) return;
    DFS2 (SON[U],SF);
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
    if (son[u]!=v&&fa[u]!=v) DFS2 (V,V);
}} int W[MAXN];
int rs[maxn<<2],rm[maxn<<2];
    void pushup (int rt) {Rs[rt] = rs[rt<<1] + rs[rt<<1|1];
RM[RT] = max (rm[rt<<1],rm[rt<<1|1]);
        } void Build (int l,int R,int RT) {if (l==r) {Rs[rt] = Rm[rt] = w[fp[l]];
    return;
   } int m=l+r>>1;
    Build (Lson);
    Build (Rson);
Pushup (RT);
        } void Update (int pos,int v,int l,int r,int RT) {if (l==r) {Rs[rt] = rm[rt] = v;
    return;
    } int m=l+r>>1;
    if (POS <= m) update (Pos,v,lson);
    else update (Pos,v,rson);
Pushup (RT);
} int qs,qm;
        void query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) {qs + = Rs[rt];
        QM = MAX (Qm,rm[rt]);
    return;
    } int m=l+r>>1;
    if (L <= m) query (L,r,lson);
if (M < R) query (L,r,rson);
    } void Query (int u,int v) {int f1=top[u],f2=top[v];
            while (F1!=F2) {if (Dep[f1]<dep[f2]) {swap (F1,F2);
        Swap (U,V);
        } query (p[f1],p[u],1,pos-1,1);
        U=FA[F1];
    F1=top[u];
    } if (Dep[u] > Dep[v]) swap (U,V);
Query (p[u],p[v],1,pos-1,1);
    } void Init () {clr (h,-1);
    si=0;
Pos=1;

} Char s[200];
   int main () {//freopen ("Input.txt", "R", stdin); int n;
        while (~SCANF ("%d", &n)) {init ();
        int u,v;
            for (int i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);
            Add (U,V);
        Add (V,u);
        } for (int i=1;i<=n;i++) scanf ("%d", &w[i]);
        DFS1 (1,-1,1);
        DFS2 (a);
        Build (1,pos-1,1);
        int q;
        scanf ("%d", &q);
            for (int i=0;i<q;i++) {scanf ("%s%d%d", s,&u,&v);
            if (!strcmp (S, "Change")) update (p[u],v,1,pos-1,1);
                else if (!strcmp (S, "QMAX")) {QM =-inf;
                Query (U,V);
            printf ("%d\n", QM);
                }else{qs=0;
                Query (U,V);
            printf ("%d\n", QS);
}}} return 0; }

Lightoj 1348

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <queue>/#include <tr1/unordered_set>//#include <tr1/unordered_map> #include <

bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo
R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 30000+300; struct edge{int to,next;}
g[maxn<<2];
int h[maxn],si; void Add (int u, int v) {g[si].to=v;
    G[si].next=h[u];
h[u]=si++;
} int SIZ[MAXN],DEP[MAXN];
int FA[MAXN],SON[MAXN],TOP[MAXN];
    void dfs1 (int u,int f,int d) {fa[u]=f;
    Dep[u]=d;
    Siz[u]=1;
    Son[u]=-1;
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
            if (v^f) {DFS1 (v,u,d+1);
            SIZ[U]+=SIZ[V]; if (son[u]==-1| |
        SIZ[SON[U]]&LT;SIZ[V]) Son[u]=v;
}}} int p[maxn],fp[maxn],pos;
    void dfs2 (int u,int sf) {top[u]=sf;
    p[u]=pos++;
    Fp[p[u]]=u;
    if (son[u]==-1) return;
    DFS2 (SON[U],SF);
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
    if (son[u]!=v&&fa[u]!=v) DFS2 (V,V);
}} int RS[MAXN];
        void upp (int x,int p) {while (x<=pos) {rs[x]+=p;
    x+=x&-x;
    }} int get (int x) {int ret=0;
        while (x) {ret + = rs[x];
    x-=x&-x;
} return ret;
    } int Query (int u,int v) {int tmp=0;
  int F1=TOP[U],F2=TOP[V];  while (F1^F2) {if (Dep[f1]<dep[f2]) {swap (F1,F2);
        Swap (U,V);
        } tmp + = Get (P[u])-Get (P[F1]-1);
        U=FA[F1];
    F1=top[u];
    } if (Dep[u] > Dep[v]) swap (U,V);
    TMP + = Get (P[v])-Get (P[U]-1);
return TMP;
    } void Init () {clr (h,-1);
    si=0;
Pos=1;

} int W[MAXN];
    int main () {//Freopen ("Input.txt", "R", stdin);
    int t,case=0;
    scanf ("%d", &t);
        while (t--) {init ();
        CLR (rs,0);
        int n;
        scanf ("%d", &n);
        for (int i=0;i<n;i++) scanf ("%d", &w[i]);
        int A, B;
            for (int i=0;i<n-1;i++) {scanf ("%d%d", &a,&b);
            Add (A, b);
        Add (B,a);
        } DFS1 (0,-1,1);
        DFS2 (0,0);
        for (int i=0;i<n;i++) upp (P[i],w[i]);
        int q,c;
        scanf ("%d", &q);
        printf ("Case%d:\n", ++case); for (int i=0;i<q;i++) {scanf ("%d%d%D ", &c,&a,&b);
            if (c = = 0) printf ("%d\n", Query (b));
                else {upp (p[a],b-w[a]);
            W[a]=b;
}}} return 0;
 }
Edge Operation

Spoj Qtree

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <queue>/#include <tr1/unordered_set>//#include <tr1/unordered_map> #include <

bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo

R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 10000+20; struct sad {int to,next,id;}
g[maxn<<2];
int h[maxn],si; void Add (inT u,int v,int id) {g[si].to=v;
    G[si].id=id;
    G[si].next=h[u];
h[u]=si++;
} int SIZ[MAXN],DEP[MAXN];
int FA[MAXN],SON[MAXN],TOP[MAXN];
int FID[MAXN];
    void dfs1 (int u,int f,int d) {fa[u]=f;
    Dep[u]=d;
    Siz[u]=1;
    Son[u]=-1;
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
            if (v^f) {fid[g[i].id]=v;
            DFS1 (v,u,d+1);
            SIZ[U]+=SIZ[V]; if (son[u]==-1| |
        SIZ[SON[U]]&LT;SIZ[V]) Son[u]=v;
}}} int p[maxn],fp[maxn],pos;
    void dfs2 (int u,int sf) {top[u]=sf;
    p[u]=pos++;
    Fp[p[u]]=u;
    if (son[u]==-1) return;
    DFS2 (SON[U],SF);
        for (int i=h[u];~i;i=g[i].next) {int v=g[i].to;
    if (son[u]!=v&&fa[u]!=v) DFS2 (V,V);
}} int col[maxn<<2],ma[maxn<<2],mi[maxn<<2];
        void pushdown (int rt) {if (Col[rt]) {col[rt<<1] ^= 1;

        COL[RT&LT;&LT;1|1] ^= 1; Swap (ma[rt<<1],mi[rt<<1]);
        ma[rt<<1]*=-1;

        mi[rt<<1]*=-1;
        Swap (ma[rt<<1|1],mi[rt<<1|1]);
        ma[rt<<1|1]*=-1;

        mi[rt<<1|1]*=-1; col[rt]=

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.