Basic data Structure _ template __ Basic data Structure _ template

Source: Internet
Author: User

1. Tree-like array


int tree[100005];//Tree
int lowbit (int x)//lowbit
{return
    x& (-X);
}
int sum (int x) is the sum of numbers smaller than the current number, and how it is achieved here is simple: int sum=sum (a[i]);
{
    int sum=0;
    while (x>0)
    {
        sum+=tree[x];
        X-=lowbit (x);
    }
    return sum;
}
void Add (int x,int c)//Add data.
{while
    (x<=n)
    {
        tree[x]+=c;
        X+=lowbit (x);
    }
}

2. Two-D tree-like array


int lowbit (int x)
{return
    x& (-X);
}
void update (int x,int y,int d)
{
    int temp=y;
    while (x<=m)
    {
        y=temp;
        while (y<=m)
        {
            a[x][y]+=d;
            Y=y+lowbit (y);
        }
        X=x+lowbit (x);

    }

}
int getsum (int x,int y)
{
    int sum=0;
    int temp=y;
    while (x>0)
    {
        y=temp;
        while (y>0)
        {
            sum=sum+a[x][y];
            Y=y-lowbit (y);
        }
        X=x-lowbit (x);
    }
    return sum;

}
Update (X+1,Y+1,D);

3. Line Tree

#define Lson l,m,rt*2 #define Rson m+1,r,rt*2+1 void pushup (int rt) {tree[rt]=tree[rt<<1]+tree[rt<<1|1];
    int Query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) {return TREE[RT];
        else {int m= (L+R) >>1;
        int ans=0;
        if (l<=m) {ans+=query (L,r,lson);
        } if (M<r) {ans+=query (L,r,rson);
    return ans;
		} void Build (int l, int r, int rt) {if (L = = r) {scanf ("%d", &tree[rt]);
	return;
		else {int m = (l+r) >>1;
		Build (Lson);
		Build (Rson);
	Pushup (RT);
} void Update (int p,int c,int l,int r,int RT)//p Camp C data.
    {//printf ("%d%d%d%d\n", p,c,l,r,rt);
    if (l==r) {tree[rt]+=c;
        else {int m= (L+R) >>1;
        if (p<=m) update (P,c,lson);
        else update (P,c,rson);
		Pushup (RT);
    printf ("sum[%d]:%d\n", Rt,tree[rt]); } int query (int l, int r, int l, int r, int rt, int *pos)//return seat {if (L <= l && R <= r) {*pos = Posn
        [RT];
    return TREE[RT];
        else {int m = (L + r) >> 1;
        int ret1 = int_min;
        int ret2 = int_min;
        int PA, PB;
        int *POS1 = &pa;
        int *pos2 = &pb;
        if (l <= m) {ret1 = query (L, R, Lson, POS1);
        } if (R > m) {ret2 = query (L, R, Rson, Pos2);
        } if (Ret1 > Ret2) {*pos = PA;
            else {*pos = PB;
        Ret1 = Ret2;
    return ret1;
int POS; printf ("%d%d\n", POS, query (1, n, 1, N, 1, &pos));

4. Segment Tree interval update

#include <stdio.h> #include <string.h> using namespace std;
#define Lson l,m,rt*2 #define Rson m+1,r,rt*2+1 #define LL Long int ll tree[1212112];
ll flag[1212112]; 
        void pushdown (int l,int r,int RT)//Downward maintenance data in the tree {if (Flag[rt])//If the greedy tag is not 0 (indicating the value that needs to be overridden by the coverage interval (or point) {int m= (L+R)/2;
        FLAG[RT*2]+=FLAG[RT];
        FLAG[RT*2+1]+=FLAG[RT];
        tree[rt*2]+= (m-l+1) *flag[rt];//must understand how to overwrite the interval value (the corresponding segment tree's picture Understanding (M-L) +1) is what consciousness.
        tree[rt*2+1]+= (R (m+1) +1) *flag[rt];
    flag[rt]=0;      
    } void Pushup (int rt) {tree[rt]=tree[rt<<1]+tree[rt<<1|1];} void build (int l, int r, int rt) {
	flag[rt]=0;
		if (L = = = r) {scanf ("%lld", &tree[rt]);
	return;
		else {int m = (l+r) >>1;
		Build (Lson);
		Build (Rson);
		Pushup (RT);
	return;
    } ll Query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) {return TREE[RT];
        else {pushdown (L,R,RT);
      int m= (L+R) >>1;  ll Ans=0;
        if (l<=m) {ans+=query (L,r,lson);
        } if (M<r) {ans+=query (L,r,rson);
        } pushup (RT);
    return ans; } void Update (int l,int r,int c,int l,int r,int RT) {if (l<=l&&r<=r)//covers interval ~ {tree[rt]+=
        c* ((r-l) +1)//Overwrite the value of the current point flag[rt]+=c;//at the same time lazy tag ~.
    return;
    } pushdown (L,R,RT);
    int m= (L+R)/2;
    if (l<=m) {update (L,r,c,lson);
    } if (m<r) {update (L,r,c,rson);
} pushup (RT);
     int main () {int n,m;
         while (~SCANF ("%d%d", &n,&m)) {memset (flag,0,sizeof (flag));
         memset (tree,0,sizeof (tree));
         Build (1,n,1);
             for (int i=0;i<m;i++) {char s[5];
             int x,y;
             scanf ("%s%d%d", s,&x,&y);
             if (s[0]== ' Q ') {printf ("%lld\n", Query (x,y,1,n,1));
          }   if (s[0]== ' C ') {ll C;
                 scanf ("%lld", &c);
             Update (x,y,c,1,n,1); }
         }
     }
}


Segment Tree Query maximum seat


#include <cstdio> #include <climits> #include <algorithm> using namespace std;
#define Lson L, M, rt<<1 #define Rson m+1, R, (rt<<1) |1 int tree[111111<<2];
int posn[111111<<2];
        void pushup (int rt) {if (tree[rt<<1] > Tree[rt<<1|1]) {TREE[RT] = tree[rt<<1];
    POSN[RT] = posn[rt<<1];
        else {Tree[rt] = tree[rt<<1|1];
    POSN[RT] = posn[rt<<1|1];
        } void Build (int l, int r, int rt) {if (L = = r) {scanf ("%d", &tree[rt]);
    POSN[RT] = l;
        else {int m = (L + r) >> 1;
        Build (Lson);
        Build (Rson);
    Pushup (RT);
    } void Update (int p, int val, int l, int r, int rt) {if (L = = r) {Tree[rt] = val;
        else {int m = (L + r) >> 1;
        if (P <= m) {update (P, Val, Lson); else {update (P, Val, Rson);
    } pushup (RT);  int query (int l, int r, int l, int r, int rt, int *pos) {if (L <= l && R <= r) {*pos =
        POSN[RT];
    return TREE[RT];
        else {int m = (L + r) >> 1;
        int ret1 = int_min;
        int ret2 = int_min;
        int PA, PB;
        int *POS1 = &pa;
        int *pos2 = &pb;
        if (l <= m) {ret1 = query (L, R, Lson, POS1);
        } if (R > m) {ret2 = query (L, R, Rson, Pos2);
        } if (Ret1 > Ret2) {*pos = PA;
            else {*pos = PB;
        Ret1 = Ret2;
    return ret1;

    {int main (void) {int n, m;

        while (scanf ("%d", &n)!= EOF) {build (1, N, 1);
        scanf ("%d", &m);
        Char op[2];
        int A, B;
            while (m--) {scanf ("%s", op); if (op[0] = = = ' Q ') {int pos;
            printf ("%d%d\n", POS, query (1, n, 1, N, 1, &pos));
                else {scanf ("%d%d", &a, &b);
            Update (A, B, 1, n, 1);
    printf ("\ n");
return 0; }



5. Dictionary Tree

void Creattrie (char *str) {int len=strlen (str);
    Tire *p=root,*q;
        for (int i=0;i<len;i++) {int id=str[i]-' 0 ';
            if (p->next[id]==null) {q= (Trie *) malloc (sizeof (trie));
            q->v=1;
            for (int j=0;j<maxn;j++) {q->next[j]=null;
            } p->next[id]=q;
        p=p->next[id];
            else {p->next[id]->v++;
        p=p->next[id];
}} p->v=-1;
    int Findtrie (char *str) {int len=strlen (str);
    Trie *p=root;
        for (int i=0;i<maxn;i++) {int id=str[i]-' 0 ';
        p=p->next;
        if (P==null) return 0;
        if (p->v==-1) {return-1;
}} return-1;
    int Dealtrie (trie* T) {int i;
    if (T==null) return 0;
    for (i=0;i<max;i++) {if (t->next[i]!=null) deal (t->next[i));
 }   Free (T);
return 0; }


6. Dictionary Tree

#include <stdio.h> #include <string.h> #include <iostream> #include <stdlib.h> using namespace
Std
    #define MAXN 26//contains only lowercase letters, typedef struct TREE {int flag;
    Tree *NEXT[MAXN];
Char la[30];
}tree;

Tree root;
    void creat (char *str) {int len=strlen (str);
    Tree *p=&root,*q;
        for (int i=0;i<len;i++) {int id=str[i]-' a ';
            if (p->next[id]==null) {q= (tree *) malloc (sizeof (root));
            q->v=1;
            for (int j=0;j<26;j++) {q->next[j]=null;
        } p->next[id]=q;
        else {p->next[id]->v++;
    } p=p->next[id];
    {int find (char *str) {int len=strlen (str);
    Tree *p=&root;
        for (int i=0;i<len;i++) {int id=str[i]-' a ';
        p=p->next[id];
    if (P==null) return 0;
Return p->v;  
    } Char str[15];  
    int i; For (int i=0;i<26;i++) {root.next[i]=null;
    } void Freedom (tree* p) {int i;
    for (i=0;i<52;i++) {if (p->next[i]!=null) Freedom (p->next[i));
Free (p); } Freedom (&root);


01 Dictionary Tree Greedy Query, build, delete

#define MAXN 2 typedef struct Tree {*NEX[MAXN];
    int V;
int Val;
}tree;
Tree root;
    void Init () {for (int i=0;i<maxn;i++) {root.nex[i]=null;
    } void creat (char *str,int va) {int len=strlen (str);
    Tree *p=&root,*q;
        for (int i=0;i<len;i++) {int id=str[i]-' 0 ';
            if (p->nex[id]==null) {q= (tree *) malloc (sizeof (root));
            q->v=1;
            for (int j=0;j<2;j++) {q->nex[j]=null;
        } p->nex[id]=q;
        else {p->nex[id]->v++;
        } p=p->nex[id];
        if (i==len-1) {p->val=va;
    }} void del (char *str) {int len=strlen (str);
    Tree *p=&root;
        for (int i=0;i<len;i++) {int id=str[i]-' 0 ';
        p->nex[id]->v--;
        Tree *tmp=p->nex[id]; if (p->nex[id]->v==0) {p->nex[id]=null;
    } p=tmp;
} return;
    } void Find (char *str,int query) {int Len=strlen (str);
    Tree *p=&root;
        for (int i=0;i<len;i++) {int id=str[i]-' 0 ';
        if (p->nex[1-id]!=0) {p=p->nex[1-id];
        else p=p->nex[id];
        if (p==null) return;
    if (i==len-1) printf ("%d\n", p->val^query); }
}




7. And check the collection


int find (int a)
{
    int r=a;
    while (f[r]!=r)
    r=f[r];
    int i=a;
    Int J;
    while (i!=r)
    {
        j=f[i];
        F[i]=r;
        i=j;
    }
    return r;
}
int find (int x)
{return
    f[x] = = x: (F[x] = find (f[x));
}
void merge (int a,int b)
{
    int a,b;
    A=find (a);
    B=find (b);
    if (a!=b)
    f[b]=a;
}

8. With the right and check the collection


#include <stdio.h> #include <string.h> using namespace std;
int f[1000010];
int sum[1000010];
        int find (int x) {if (x!=f[x]) {int pre=f[x];//pre is a parent node of X.
        F[x]=find (f[x]);//recursive search for ancestors.
    Sum[x]+=sum[pre];
return f[x];
    int main () {int n;
            while (~SCANF ("%d", &n)) {for (int i=0;i<=n;i++) {f[i]=i;
        sum[i]=0;
        int op;
            while (n--) {scanf ("%d", &op);
                if (op==1) {int x,y,val;
                scanf ("%d%d%d", &x,&y,&val);
                int X=find (X);
                int Y=find (Y);
                    if (x!=y) {sum[y]=val-sum[y]+sum[x];
                F[y]=x;
                } else {int x,y;
                scanf ("%d%d", &x,&y);
                int X=find (X);
      int Y=find (Y);          if (x!=y) {printf ("? \ n");
                else {printf ("%d\n", sum[y]-sum[x]); }
            }
        }
    }
}

9. Tree chain Split base operation:

void Dfs (int u,int from,int d) {depth[u]=d;size[u]=1;fa[u]=from;  
    if (fa[u]==-1) fa[u]=1;  
    int maxn=0;  
        for (int i=0;i<mp[u].size (); i++) {int v=mp[u][i];  
        if (v==from) continue;  
        Dfs (v,u,d+1);  
        SIZE[U]+=SIZE[V]; if (son[u]==-1| |  
            SIZE[V]&GT;MAXN) {Maxn=size[v];  
        Son[u]=v;  
    }} void Dfs2 (int u,int from,int top) {top[u]=top;dfn[u]=++cnt;  
    if (son[u]!=-1) {DFS2 (son[u],u,top);  
        for (int i=0;i<mp[u].size (); i++) {int v=mp[u][i]; if (v==from| |  
        V==son[u]) continue;  
    DFS2 (V,U,V);  
    } void Slove (int x,int y,int change) {int fx=top[x];  
    int fy=top[y];  
            while (Fx!=fy) {if (Depth[fx]<depth[fy]) {swap (FX,FY);  
        Swap (x,y);  
        Update (dfn[fx],dfn[x],change,1,n,1); X=FA[FX];FX=TOP[X];  
    } if (Depth[x]>depth[y]) {swap (x,y);  
Update (dfn[x],dfn[y],change,1,n,1);   }

void Slove (int x,int y)  
{  
    int sum=0;  
    int fx=top[x],fy=top[y];  
    while (Fx!=fy)  
    {  
        if (Depth[fx]<depth[fy])  
        {  
            swap (fx,fy);  
            Swap (x,y);  
        }  
        Sum+=query (dfn[fx],dfn[x],1,n,1);  
        X=FA[FX];FX=TOP[X];  
    }  
    if (x==y)  
    {  
        printf ("%d\n", sum);  
        return;  
    }  
    if (Depth[x]<depth[y]) swap (x,y);  
    Sum+=query (dfn[son[y]],dfn[x],1,n,1);  
    printf ("%d\n", sum);  
}  








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.