Codeforces 620E New Year tree (Dfs sequence + segment tree interval operation + binary)

Source: Internet
Author: User

Test instructions: Given a tree, each node initially has a color value, with m operations, 1 operation: Input id,c. The first ID node and its subtree are dyed c.

2 operation: ID, output ID number of different colors in subtree.

Solution: First run DFS, the first ID node to control the DFS sequence is l,r interval. Then the interval operation is converted to the segment tree.

How many kinds of colors are recorded. Test instructions said the number of colors not more than 60. Then the C-color corresponds to the second-digit C-bit, and the color of the two-segment bands is the "or" of their two-segment color values. Finally, see how many 1 are in this color value binary.

#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include < vector> #include <math.h> #include <queue> #include <map> #include <set> #include <
Algorithm> using namespace std;
#define MAXN 4*100005 #define LL Long Long #define INF 0x3f7f7f7f const double EPS = 1e-10;
    struct node {int l,r,c;
LL Val;
}tr[maxn<<2]; struct point {int l,r,c;}
POINT[MAXN]; struct Edge {int en,next;}
E[MAXN*2];
int p[maxn],num;
int n,m,step;
int VIS[MAXN],DFN[MAXN];
    The Dfn[i],dfs sequence is the node number that corresponds to I, void add (int st,int en) {e[num].en=en;
    E[NUM].NEXT=P[ST];
p[st]=num++;
    } void Dfs (int id) {point[id].l=++step;
    Dfn[step]=id;
    Vis[id]=1;
    int i;
        for (i=p[id];i!=-1;i=e[i].next) {int en=e[i].en;
    if (!vis[en]) dfs (EN);
    } Point[id].r=step;
cout<< "ID:" <<id<< "" <<point[id].l<< "" <<point[id].r<<endl;} void Pushup (int id) {tr[id].val=tr[id*2].val|tr[id*2+1].val;//Two period interval merged color value} void pushdown (int id) {tr[id].c=0;
    Tr[id*2].c=tr[id*2+1].c=1;
Tr[id*2].val=tr[id*2+1].val=tr[id].val;
    } void Build (int id,int l,int r) {tr[id].l=l;
    Tr[id].r=r;
    tr[id].c=0;
    if (l==r) {tr[id].val= (LL) (LL) 1<< (LL) point[dfn[l]].c);
        } else {int mid= (L+R)/2;
        Build (Id*2,l,mid);
        Build (Id*2+1,mid+1,r);
    Pushup (ID); }} void Update (int id,int l,int r,ll val) {if (l<=tr[id].l&&tr[id].r<=r) {tr[id].val= (LL) (
        (LL) 1<<val);
    Tr[id].c=1;
        } else {int mid= (TR[ID].L+TR[ID].R) >>1;
        if (TR[ID].C) pushdown (ID);
        if (r<=mid) update (id*2,l,r,val);
        else if (l>mid) update (id*2+1,l,r,val);
            else {update (id*2,l,r,val);
        Update (ID*2+1,L,R,VAL);
    } pushup (ID);
}} LL query (int id,int l,int R){if (l<=tr[id].l&&tr[id].r<=r) {return tr[id].val;
        } else {int mid= (TR[ID].L+TR[ID].R) >>1;
        if (TR[ID].C) pushdown (ID);
        LL ans;
        if (r<=mid) return query (ID*2,L,R);
        else if (l>mid) return query (ID*2+1,L,R);
            else {ans=query (id*2,l,r);
            Ans=ans|query (ID*2+1,L,R);
        return ans;
    }}} int fun (LL x) {int ans=0;
        for (ll i=0;i<=64;i++) {ll temp= (LL) (LL) 1<< (ll) i);
    if (temp&x) ans++;
} return ans;
    } int main () {int i;
        while (scanf ("%d%d", &n,&m)!=eof) {num=0;
        memset (P,-1,sizeof (p));
        for (i=1;i<=n;i++) scanf ("%d", &point[i].c);
            for (i=1;i<n;i++) {int u,v;
            scanf ("%d%d", &u,&v);
           Add (U,V);
        Add (V,u);
        } step=0; memset (vis,0,sizeof (VIS));
        DFS (1);
        Build (1,1,n);
            for (i=1;i<=m;i++) {int opt;
            int Id,co;
            scanf ("%d", &opt);
                if (opt==1) {scanf ("%d%d", &id,&co);
            Update (1,POINT[ID].L,POINT[ID].R,CO);
                } else {scanf ("%d", &id);
                LL x=query (1,POINT[ID].L,POINT[ID].R);
                cout<<i<< ":::" <<x<<endl;
            printf ("%d\n", Fun (x));
}}} return 0;
 }



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.