zoukankan      html  css  js  c++  java
  • 树标号

    Apple Tree http://poj.org/problem?id=3321

     1 #include<cstdio>
     2 #include<cstring>
     3 #define mt(a,b) memset(a,b,sizeof(a))
     4 #define lrrt int L,int R,int rt
     5 #define iall 1,n,1
     6 #define imid int mid=(L+R)>>1
     7 #define lson L,mid,rt<<1
     8 #define rson mid+1,R,rt<<1|1
     9 using namespace std;
    10 const int M=100010;
    11 int tree[M<<2];
    12 void pushup(int rt){
    13     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
    14 }
    15 void build(lrrt){
    16     if(L==R){
    17         tree[rt]=1;
    18         return ;
    19     }
    20     imid;
    21     build(lson);
    22     build(rson);
    23     pushup(rt);
    24 }
    25 void update(int x,lrrt){
    26     if(L==R){
    27         tree[rt]^=1;
    28         return ;
    29     }
    30     imid;
    31     if(mid>=x) update(x,lson);
    32     else       update(x,rson);
    33     pushup(rt);
    34 }
    35 int query(int x,int y,lrrt){
    36     if(x<=L&&R<=y) return tree[rt];
    37     imid;
    38     int ans=0;
    39     if(mid>=x) ans+=query(x,y,lson);
    40     if(mid<y)  ans+=query(x,y,rson);
    41     return ans;
    42 }
    43 struct G{
    44     struct E{
    45         int u,v,next;
    46     }e[M<<1];
    47     int le,head[M];
    48     void init(){
    49         le=0;
    50         mt(head,-1);
    51     }
    52     void add(int u,int v){
    53         e[le].u=u;
    54         e[le].v=v;
    55         e[le].next=head[u];
    56         head[u]=le++;
    57     }
    58 }g;
    59 struct Node{
    60     int l,r;
    61 }node[M];
    62 int Index;
    63 void dfs(int u,int fa){
    64     node[u].l=++Index;
    65     for(int i=g.head[u];~i;i=g.e[i].next){
    66         int v=g.e[i].v;
    67         if(v!=fa){
    68             dfs(v,u);
    69         }
    70     }
    71     node[u].r=Index;
    72 }
    73 int main(){
    74     int n,m;
    75     while(~scanf("%d",&n)){
    76         g.init();
    77         for(int i=0,u,v;i<n-1;i++){
    78             scanf("%d%d",&u,&v);
    79             g.add(u,v);
    80             g.add(v,u);
    81         }
    82         build(iall);
    83         Index=0;
    84         dfs(1,-1);
    85         scanf("%d",&m);
    86         while(m--){
    87             char op[4];
    88             int x;
    89             scanf("%s%d",op,&x);
    90             if(op[0]=='C'){
    91                 update(node[x].l,iall);
    92             }
    93             else{
    94                 printf("%d
    ",query(node[x].l,node[x].r,iall));
    95             }
    96         }
    97     }
    98     return 0;
    99 }
    View Code

     A Simple Tree Problem http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4969

     1 #include<cstdio>
     2 #include<vector>
     3 #define lrrt int L,int R,int rt
     4 #define iall 1,n,1
     5 #define imid int mid=(L+R)>>1
     6 #define lson L,mid,rt<<1
     7 #define rson mid+1,R,rt<<1|1
     8 using namespace std;
     9 const int M=100010;
    10 vector<int> g[M];
    11 struct N{
    12     int l,r;
    13 }node[M];
    14 int Index;
    15 void dfs(int u,int fa){
    16     node[u].l=++Index;
    17     int len=g[u].size();
    18     for(int i=0;i<len;i++){
    19         int v=g[u][i];
    20         if(v!=fa){
    21             dfs(v,u);
    22         }
    23     }
    24     node[u].r=Index;
    25 }
    26 struct T{
    27     int lazy,sum;
    28 }tree[M<<2];
    29 void pushup(int rt){
    30     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    31 }
    32 void build(lrrt){
    33     tree[rt].sum=0;
    34     tree[rt].lazy=0;
    35     if(L==R) return ;
    36     imid;
    37     build(lson);
    38     build(rson);
    39 }
    40 void pushdown(int mid,lrrt){
    41     if(tree[rt].lazy){
    42         tree[rt<<1].lazy^=1;
    43         tree[rt<<1|1].lazy^=1;
    44         tree[rt<<1].sum=(mid-L+1)-tree[rt<<1].sum;
    45         tree[rt<<1|1].sum=(R-mid)-tree[rt<<1|1].sum;
    46         tree[rt].lazy=0;
    47     }
    48 }
    49 void update(int x,int y,lrrt){
    50     if(x<=L&&R<=y){
    51         tree[rt].sum=(R-L+1)-tree[rt].sum;
    52         tree[rt].lazy^=1;
    53         return ;
    54     }
    55     imid;
    56     pushdown(mid,L,R,rt);
    57     if(mid>=x) update(x,y,lson);
    58     if(mid<y)  update(x,y,rson);
    59     pushup(rt);
    60 }
    61 int query(int x,int y,lrrt){
    62     if(x<=L&&R<=y) return tree[rt].sum;
    63     imid;
    64     pushdown(mid,L,R,rt);
    65     int ans=0;
    66     if(mid>=x) ans+=query(x,y,lson);
    67     if(mid<y)  ans+=query(x,y,rson);
    68     return ans;
    69 }
    70 int main(){
    71     int n,m,x;
    72     char op[4];
    73     while(~scanf("%d%d",&n,&m)){
    74         for(int i=1;i<=n;i++){
    75             g[i].clear();
    76         }
    77         for(int i=2;i<=n;i++){
    78             scanf("%d",&x);
    79             g[i].push_back(x);
    80             g[x].push_back(i);
    81         }
    82         Index=0;
    83         dfs(1,-1);
    84         build(iall);
    85         while(m--){
    86             scanf("%s%d",op,&x);
    87             if(op[0]=='o'){
    88                 update(node[x].l,node[x].r,iall);
    89             }
    90             else{
    91                 printf("%d
    ",query(node[x].l,node[x].r,iall));
    92             }
    93         }
    94         puts("");
    95     }
    96     return 0;
    97 }
    View Code
      1 #include<cstdio>
      2 #include<cstring>
      3 #define mt(a,b) memset(a,b,sizeof(a))
      4 #define lrrt int L,int R,int rt
      5 #define iall 1,n,1
      6 #define imid int mid=(L+R)>>1
      7 #define lson L,mid,rt<<1
      8 #define rson mid+1,R,rt<<1|1
      9 const int M=100010;
     10 struct G{
     11     struct E{
     12         int v,next;
     13     }e[M<<1];
     14     int le,head[M];
     15     void init(){
     16         le=0;
     17         mt(head,-1);
     18     }
     19     void add(int u,int v){
     20         e[le].v=v;
     21         e[le].next=head[u];
     22         head[u]=le++;
     23     }
     24 }g;
     25 struct N{
     26     int l,r;
     27 }node[M];
     28 int Index;
     29 void dfs(int u,int fa){
     30     node[u].l=++Index;
     31     for(int i=g.head[u];~i;i=g.e[i].next){
     32         int v=g.e[i].v;
     33         if(v!=fa){
     34             dfs(v,u);
     35         }
     36     }
     37     node[u].r=Index;
     38 }
     39 struct T{
     40     int lazy,sum;
     41 }tree[M<<2];
     42 void pushup(int rt){
     43     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
     44 }
     45 void build(lrrt){
     46     tree[rt].sum=0;
     47     tree[rt].lazy=0;
     48     if(L==R) return ;
     49     imid;
     50     build(lson);
     51     build(rson);
     52 }
     53 void pushdown(int mid,lrrt){
     54     if(tree[rt].lazy){
     55         tree[rt<<1].lazy^=1;
     56         tree[rt<<1|1].lazy^=1;
     57         tree[rt<<1].sum=(mid-L+1)-tree[rt<<1].sum;
     58         tree[rt<<1|1].sum=(R-mid)-tree[rt<<1|1].sum;
     59         tree[rt].lazy=0;
     60     }
     61 }
     62 void update(int x,int y,lrrt){
     63     if(x<=L&&R<=y){
     64         tree[rt].sum=(R-L+1)-tree[rt].sum;
     65         tree[rt].lazy^=1;
     66         return ;
     67     }
     68     imid;
     69     pushdown(mid,L,R,rt);
     70     if(mid>=x) update(x,y,lson);
     71     if(mid<y)  update(x,y,rson);
     72     pushup(rt);
     73 }
     74 int query(int x,int y,lrrt){
     75     if(x<=L&&R<=y) return tree[rt].sum;
     76     imid;
     77     pushdown(mid,L,R,rt);
     78     int ans=0;
     79     if(mid>=x) ans+=query(x,y,lson);
     80     if(mid<y)  ans+=query(x,y,rson);
     81     return ans;
     82 }
     83 int main(){
     84     int n,m,x;
     85     char op[4];
     86     while(~scanf("%d%d",&n,&m)){
     87         g.init();
     88         for(int i=2;i<=n;i++){
     89             scanf("%d",&x);
     90             g.add(x,i);
     91             g.add(i,x);
     92         }
     93         Index=0;
     94         dfs(1,-1);
     95         build(iall);
     96         while(m--){
     97             scanf("%s%d",op,&x);
     98             if(op[0]=='o'){
     99                 update(node[x].l,node[x].r,iall);
    100             }
    101             else{
    102                 printf("%d
    ",query(node[x].l,node[x].r,iall));
    103             }
    104         }
    105         puts("");
    106     }
    107     return 0;
    108 }
    View Code

    end

  • 相关阅读:
    vue中使用 canvas给页面添加水印
    c++ get keyboard event
    sublime text c++ makefile
    dddd
    songwenxin
    wechat
    ddd
    log
    v3
    xiaoxiaole
  • 原文地址:https://www.cnblogs.com/gaolzzxin/p/3880246.html
Copyright © 2011-2022 走看看